Repository: boto/boto3
File: tests/unit/test_session.py (Python)

Code before:

    def test_credentials_can_be_set(self):
        bc_session = self.bc_session_cls.return_value

        # Set values in constructor
        Session(
            aws_access_key_id='key',
            aws_secret_access_key='secret',
            aws_session_token='token',
            aws_account_id='account',
        )

        assert self.bc_session_cls.called
        assert bc_session.set_credentials.called
        bc_session.set_credentials.assert_called_with(
            'key', 'secret', 'token', 'account'
        )

    def test_can_get_credentials(self):
        access_key = 'foo'
        secret_key = 'bar'
        token = 'baz'
        account_id = 'bin'

        creds = mock.Mock()
        creds.access_key = access_key
        creds.secret_key = secret_key
        creds.token = token
        creds.account_id = account_id

        bc_session = self.bc_session_cls.return_value
        bc_session.get_credentials.return_value = creds

        session = Session(
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_key,
            aws_session_token=token,
            aws_account_id=account_id,
        )

        credentials = session.get_credentials()
        assert credentials.access_key == access_key
        assert credentials.secret_key == secret_key
        assert credentials.token == token
        assert credentials.account_id == account_id

    def test_profile_can_be_set(self):
        bc_session = self.bc_session_cls.return_value

        session = Session(profile_name='foo')


Review comment (security):
I'd like to see this maybe split into two tests, one with and one without account id.  We should still be testing that credentials can be set without account id

Code after:

    def test_credentials_can_be_set(self):
        bc_session = self.bc_session_cls.return_value

        # Set values in constructor
        Session(
            aws_access_key_id='key',
            aws_secret_access_key='secret',
            aws_session_token='token',
        )

        assert self.bc_session_cls.called
        assert bc_session.set_credentials.called
        bc_session.set_credentials.assert_called_with(
            'key', 'secret', 'token', None
        )

    def test_credentials_can_be_set_with_account_id(self):
        bc_session = self.bc_session_cls.return_value

        # Set values in constructor
        Session(
            aws_access_key_id='key',
            aws_secret_access_key='secret',
            aws_session_token='token',
            aws_account_id='account',
        )

        assert self.bc_session_cls.called
        assert bc_session.set_credentials.called
        bc_session.set_credentials.assert_called_with(
            'key', 'secret', 'token', 'account'
        )

    def test_account_id_set_without_credentials(self):
        bc_session = self.bc_session_cls.return_value

        with pytest.raises(NoCredentialsError) as e:
            Session(aws_account_id='account_id')

        assert not bc_session.set_credentials.called
        assert 'Unable to locate credentials' in str(e.value)

    def test_can_get_credentials(self):
        access_key = 'foo'
        secret_key = 'bar'
        token = 'baz'
        account_id = 'bin'

        creds = mock.Mock()
        creds.access_key = access_key