def test_obtain_credentials_empty(self): # If the entered credentials are empty or only whitespace, # obtain_credentials returns None. getpass = self.patch(auth, "getpass") getpass.return_value = None self.assertEqual(None, auth.obtain_credentials(None)) getpass.assert_called_once()
def __call__(self, options): # Try and obtain credentials interactively if they're not given, or # read them from stdin if they're specified as "-". credentials = obtain_credentials(options.url, options.credentials) # Check for bogus credentials. Do this early so that the user is not # surprised when next invoking the MAAS CLI. if credentials is not None: try: valid_apikey = check_valid_apikey( options.url, credentials, options.insecure) except UnexpectedResponse as e: raise SystemExit("%s" % e) else: if not valid_apikey: raise SystemExit("The MAAS server rejected your API key.") # Get description of remote API. description = fetch_api_description(options.url, options.insecure) # Save the config. profile_name = options.profile_name with ProfileConfig.open() as config: config[profile_name] = { "credentials": credentials, "description": description, "name": profile_name, "url": options.url, } profile = config[profile_name] self.print_whats_next(profile)
def test_obtain_credentials_empty(self): # If the entered credentials are empty or only whitespace, # obtain_credentials returns None. getpass = self.patch(auth, "getpass") getpass.return_value = None self.assertEqual(None, auth.obtain_credentials(None)) getpass.assert_called_once()
def test_obtain_credentials_empty(self): # If the entered credentials are empty or only whitespace, # obtain_credentials returns None. getpass = self.patch(auth, "getpass") getpass.return_value = None self.assertEqual(None, auth.obtain_credentials(None)) self.assertThat(getpass, MockCalledOnceWith(ANY))
def test_obtain_credentials_via_getpass(self): # When None is passed to obtain_credentials, it attempts to obtain # credentials via getpass, then converts it into a 3-tuple of creds. credentials = self.make_credentials() getpass = self.patch(auth, "getpass") getpass.return_value = convert_tuple_to_string(credentials) self.assertEqual(credentials, auth.obtain_credentials(None)) getpass.assert_called_once()
def test_obtain_credentials_via_getpass(self): # When None is passed to obtain_credentials, it attempts to obtain # credentials via getpass, then converts it into a 3-tuple of creds. credentials = self.make_credentials() getpass = self.patch(auth, "getpass") getpass.return_value = convert_tuple_to_string(credentials) self.assertEqual(credentials, auth.obtain_credentials(None)) getpass.assert_called_once()
def test_obtain_credentials_from_stdin(self): # When "-" is passed to obtain_credentials, it reads credentials from # stdin, trims whitespace, and converts it into a 3-tuple of creds. credentials = self.make_credentials() stdin = self.patch(sys, "stdin") stdin.readline.return_value = ( convert_tuple_to_string(credentials) + "\n") self.assertEqual(credentials, auth.obtain_credentials("-")) stdin.readline.assert_called_once()
def test_obtain_credentials_via_getpass(self): # When None is passed to obtain_credentials, it attempts to obtain # credentials via getpass, then converts it into a 3-tuple of creds. credentials = make_credentials() getpass = self.patch(auth, "getpass") getpass.return_value = convert_tuple_to_string(credentials) self.assertEqual(credentials, auth.obtain_credentials("http://example.com/", None)) self.assertThat(getpass, MockCalledOnceWith(ANY))
def test_obtain_credentials_from_stdin(self): # When "-" is passed to obtain_credentials, it reads credentials from # stdin, trims whitespace, and converts it into a 3-tuple of creds. credentials = self.make_credentials() stdin = self.patch(sys, "stdin") stdin.readline.return_value = (convert_tuple_to_string(credentials) + "\n") self.assertEqual(credentials, auth.obtain_credentials("-")) stdin.readline.assert_called_once()
def test_obtain_credentials_from_stdin(self): # When "-" is passed to obtain_credentials, it reads credentials from # stdin, trims whitespace, and converts it into a 3-tuple of creds. credentials = make_credentials() stdin = self.patch(sys, "stdin") stdin.readline.return_value = (convert_tuple_to_string(credentials) + "\n") self.assertEqual(credentials, auth.obtain_credentials("http://example.com/", "-")) self.assertThat(stdin.readline, MockCalledOnceWith())
def __call__(self, options): # Try and obtain credentials interactively if they're not given, or # read them from stdin if they're specified as "-". credentials = obtain_credentials(options.credentials) # Get description of remote API. description = fetch_api_description(options.url, options.insecure) # Save the config. profile_name = options.profile_name with ProfileConfig.open() as config: config[profile_name] = { "credentials": credentials, "description": description, "name": profile_name, "url": options.url, } profile = config[profile_name] self.print_whats_next(profile)
def __call__(self, options): # Try and obtain credentials interactively if they're not given, or # read them from stdin if they're specified as "-". credentials = obtain_credentials(options.credentials) # Get description of remote API. description = fetch_api_description(options.url, options.insecure) # Save the config. profile_name = options.profile_name with ProfileConfig.open() as config: config[profile_name] = { "credentials": credentials, "description": description, "name": profile_name, "url": options.url, } profile = config[profile_name] self.print_whats_next(profile)
def test_obtain_credentials_macaroon(self): # If no credentials are passed, the client will use httpbakery # to get a macroon and create a new api key. # The .request method will send the user to Candid and get the macaroon # that way. Ideally we should test this better by faking the http # responses that httpbakery expects, but it's not trivial to do so. token = { 'token_key': 'token-key', 'token_secret': 'token-secret', 'consumer_key': 'consumer-key', 'name': 'MAAS consumer' } self.bakery_response.status_code = 200 self.bakery_response._content = json.dumps(token).encode('utf-8') credentials = auth.obtain_credentials('http://example.com/MAAS', None) self.assertEqual(('consumer-key', 'token-key', 'token-secret'), credentials)