def test_get_auth_info__parameter_name_exists(self, mocker: MockerFixture, syn, environ_with_param_name): mocker.patch.dict(os.environ, environ_with_param_name) mock_boto3_client, stubber = self.stub_ssm(mocker) token = 'KmhhY2tlciB2b2ljZSogIkknbSBpbiI=' response = { 'Parameter': { 'Name': '/synapse/cred/i-12134312', 'Type': 'SecureString', 'Value': token, 'Version': 502, 'LastModifiedDate': 'Sun, 20 Apr 1969 16:20:00 GMT', 'ARN': 'arn:aws:ssm:us-east-1:123123123:parameter/synapse/cred/i-12134312', 'DataType': 'text' }, } stubber.add_response('get_parameter', response) username = '******' user_login_args = UserLoginArgs(username=username, password=None, api_key=None, skip_cache=False, auth_token=None) with stubber: assert (username, None, None, token) == self.provider._get_auth_info( syn, user_login_args) mock_boto3_client.assert_called_once_with("ssm") stubber.assert_no_pending_responses()
def test_get_auth_info(self): user_login_args = UserLoginArgs("username", "password", base64.b64encode(b"api_key"), False) provider = UserArgsCredentialsProvider() returned_tuple = provider._get_auth_info(self.syn, user_login_args) assert (user_login_args.username, user_login_args.password, user_login_args.api_key) == returned_tuple
def test_get_auth_info__session_token_is_None(self): user_login_args = UserLoginArgs(session_token=None) returned_tuple = self.provider._get_auth_info(syn, user_login_args) assert_equals((None, None, None), returned_tuple) self.mock_get_user_profile.assert_not_called() self.mock_get_api_key.assert_not_called()
def test_get_auth_info__user_arg_username_does_not_match_config(self): user_login_args = UserLoginArgs(username="******", password=None, api_key=None, skip_cache=False) returned_tuple = self.provider._get_auth_info(syn, user_login_args) assert_equals((None, None, None), returned_tuple) self.mock_get_config_authentication.assert_called_once_with()
def test_get_auth_info__skip_cache_is_True(self): user_login_args = UserLoginArgs(username=self.username, password=None, api_key=None, skip_cache=True) returned_tuple = self.provider._get_auth_info(syn, user_login_args) assert_equals((None, None, None), returned_tuple) self.mock_get_most_recent_user.assert_not_called() self.mock_get_api_key.assert_not_called()
def test_get_auth_info__user_arg_username_is_None(self): user_login_args = UserLoginArgs(username=None, password=None, api_key=None, skip_cache=False) returned_tuple = self.provider._get_auth_info(syn, user_login_args) assert_equals(self.expected_return_tuple, returned_tuple) self.mock_get_most_recent_user.assert_called_once_with() self.mock_get_api_key.assert_called_once_with(self.username)
def test_get_auth_info(self): user_login_args = UserLoginArgs('username', 'password', base64.b64encode(b"api_key"), False, None, 'auth_token') provider = UserArgsCredentialsProvider() returned_tuple = provider._get_auth_info(self.syn, user_login_args) assert (user_login_args.username, user_login_args.password, user_login_args.api_key, user_login_args.auth_token) == returned_tuple
def test_get_auth_info__user_arg_username_matches_config(self): user_login_args = UserLoginArgs(username=self.username, password=None, api_key=None, skip_cache=False) returned_tuple = self.provider._get_auth_info(self.syn, user_login_args) assert self.expected_return_tuple == returned_tuple self.mock_get_config_authentication.assert_called_once_with()
def test_get_auth_info__has_environment_variable_user_args_with_username( self, mocker: MockerFixture, syn): token = "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1mQzdvVU9VRUVpNA==" mocker.patch.dict(os.environ, {'SYNAPSE_AUTH_TOKEN': token}) username = "******" user_login_args = UserLoginArgs(username=username, password=None, api_key=None, skip_cache=False, auth_token=None) assert (username, None, None, token) == self.provider._get_auth_info(syn, user_login_args)
def test_get_auth_info__username_is_None(self, mock_cached_sessions): """Verify if there is no username provided and one isn't cached we return nothing (but also don't blow up)""" mock_cached_sessions.get_most_recent_user.return_value = None user_login_args = UserLoginArgs(username=None, password=None, api_key=None, skip_cache=False, auth_token=None) returned_tuple = self.provider._get_auth_info(self.syn, user_login_args) assert (None, None, None, None) == returned_tuple
def test_get_auth_info__no_environment_variable(self, mocker: MockerFixture, syn): mocker.patch.dict(os.environ, {}, clear=True) user_login_args = UserLoginArgs(username=None, password=None, api_key=None, skip_cache=False, auth_token=None) assert (None, ) * 4 == self.provider._get_auth_info( syn, user_login_args)
def test_get_auth_info__skip_cache_is_True(self): user_login_args = UserLoginArgs(username=self.username, password=None, api_key=None, skip_cache=True, session_token=None, auth_token=None) returned_tuple = self.provider._get_auth_info(self.syn, user_login_args) assert (None, None, None, None) == returned_tuple self.mock_get_most_recent_user.assert_not_called() self.mock_api_key_credentials.assert_not_called() self.mock_auth_token_credentials.assert_not_called()
def setup(self): self.username = "******" self.password = "******" self.api_key = base64.b64encode(b"api_key").decode() self.user_login_args = UserLoginArgs(self.username, self.password, self.api_key, False) # SynapseCredentialsProvider has abstractmethod so we can't instantiate it unless we overwrite it class SynapseCredProviderTester(SynapseCredentialsProvider): def _get_auth_info(self, syn, user_login_args): pass self.provider = SynapseCredProviderTester()
def test_get_auth_info__boto3_ImportError(self, mocker, syn, environ_with_param_name): mocker.patch.dict(os.environ, environ_with_param_name) # simulate import error by "removing" boto3 from sys.modules mocker.patch.dict(sys.modules, {'boto3': None}) user_login_args = UserLoginArgs(username=None, password=None, api_key=None, skip_cache=False, auth_token=None) assert (None, ) * 4 == self.provider._get_auth_info( syn, user_login_args)
def test_get_auth_info__user_arg_username_does_not_match_config(self): """Verify that if the username is provided via an arg and it doesn't match what's in the config then we don't read any other values from config""" user_login_args = UserLoginArgs(username="******", password=None, api_key=None, skip_cache=False, auth_token=None) returned_tuple = self.provider._get_auth_info(self.syn, user_login_args) assert (None, None, None, None) == returned_tuple self.mock_get_config_authentication.assert_called_once_with()
def test_get_auth_info__session_token_not_None(self): username = '******' api_key = 'qwerty' session_token = "my session token" user_login_args = UserLoginArgs(session_token=session_token) self.mock_get_user_profile.return_value = {'userName': username} self.mock_get_api_key.return_value = api_key returned_tuple = self.provider._get_auth_info(syn, user_login_args) assert_equals((username, None, api_key), returned_tuple) self.mock_get_user_profile.assert_called_once_with( sessionToken=session_token) self.mock_get_api_key.assert_called_once_with(session_token)
def test_get_auth_info__user_arg_username_is_not_None(self): user_login_args = UserLoginArgs(username=self.username, password=None, api_key=None, skip_cache=False) returned_tuple = self.provider._get_auth_info(self.syn, user_login_args) assert self.expected_return_tuple == returned_tuple self.mock_get_most_recent_user.assert_not_called() self.mock_api_key_credentials.get_from_keyring.assert_called_once_with( self.username) self.mock_auth_token_credentials.get_from_keyring.assert_called_once_with( self.username)
def test_get_auth_info__no_environment_variable(self, mocker: MockerFixture, syn): mocker.patch.dict(os.environ, {}, clear=True) mock_boto3_client, stubber = self.stub_ssm(mocker) user_login_args = UserLoginArgs(username=None, password=None, api_key=None, skip_cache=False, auth_token=None) assert (None, ) * 4 == self.provider._get_auth_info( syn, user_login_args) assert not mock_boto3_client.called stubber.assert_no_pending_responses()
def test_get_auth_info__get_parameter_error(self, mocker: MockerFixture, syn, environ_with_param_name, error_code): mocker.patch.dict(os.environ, environ_with_param_name) mock_boto3_client, stubber = self.stub_ssm(mocker) stubber.add_client_error('get_parameter', error_code) user_login_args = UserLoginArgs(username=None, password=None, api_key=None, skip_cache=False, auth_token=None) with stubber: assert (None, ) * 4 == self.provider._get_auth_info( syn, user_login_args) mock_boto3_client.assert_called_once_with("ssm") stubber.assert_no_pending_responses()
def setup(self): self.cred_provider = create_autospec(SynapseCredentialsProvider) self.user_login_args = UserLoginArgs( *(None, ) * 4) # user login args don't matter for these tests self.credential_provider_chain = SynapseCredentialsProviderChain( [self.cred_provider])