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')

        self.assertTrue(self.bc_session_cls.called,
            'Botocore session was not created')
        self.assertTrue(bc_session.set_credentials.called,
            'Botocore session set_credentials not called from constructor')
        bc_session.set_credentials.assert_called_with(
            'key', 'secret', 'token')

    def test_custom_session(self):
        bc_session = self.bc_session_cls()
        self.bc_session_cls.reset_mock()

        Session(botocore_session=bc_session)

        # No new session was created
        self.assertFalse(self.bc_session_cls.called)

    def test_user_agent(self):
        bc_session = self.bc_session_cls()
        bc_session.user_agent_name = 'Botocore'
        bc_session.user_agent_version = '0.68.0'
        bc_session.user_agent_extra = ''

        Session()

        self.assertEqual(bc_session.user_agent_name, 'Boto3')
        self.assertEqual(bc_session.user_agent_version, __version__)
        self.assertEqual(bc_session.user_agent_extra, 'Botocore/0.68.0')

    def test_user_agent_extra(self):
        bc_session = self.bc_session_cls()
        bc_session.user_agent_name = 'Botocore'
        bc_session.user_agent_version = '0.68.0'
        bc_session.user_agent_extra = 'foo'

        Session()

        self.assertEqual(bc_session.user_agent_name, 'Boto3')
        self.assertEqual(bc_session.user_agent_version, __version__)
        self.assertEqual(bc_session.user_agent_extra, 'foo Botocore/0.68.0')

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

Review comment (suggestion):
I'm missing something.  How does this test actually work?  Is there some global patching happening that I'm missing.

Perhaps it's worth making this test more straightforward and exposing a user agent interface on the session itself.  That's what we really care about:

```
session = <create boto3 session>
self.assertIn('Boto3/<version>', session.user_agent) # or startswith() or something.
```


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')

        self.assertTrue(self.bc_session_cls.called,
            'Botocore session was not created')
        self.assertTrue(bc_session.set_credentials.called,
            'Botocore session set_credentials not called from constructor')
        bc_session.set_credentials.assert_called_with(
            'key', 'secret', 'token')

    def test_custom_session(self):
        bc_session = self.bc_session_cls()
        self.bc_session_cls.reset_mock()

        Session(botocore_session=bc_session)

        # No new session was created
        self.assertFalse(self.bc_session_cls.called)

    def test_user_agent(self):
        # Here we get the underlying Botocore session, create a Boto 3
        # session, and ensure that the user-agent is modified as expected
        bc_session = self.bc_session_cls.return_value
        bc_session.user_agent_name = 'Botocore'
        bc_session.user_agent_version = '0.68.0'
        bc_session.user_agent_extra = ''

        Session(botocore_session=bc_session)

        self.assertEqual(bc_session.user_agent_name, 'Boto3')
        self.assertEqual(bc_session.user_agent_version, __version__)
        self.assertEqual(bc_session.user_agent_extra, 'Botocore/0.68.0')

    def test_user_agent_extra(self):
        # This test is the same as above, but includes custom extra content
        # which must still be in the final modified user-agent.
        bc_session = self.bc_session_cls.return_value
        bc_session.user_agent_name = 'Botocore'
        bc_session.user_agent_version = '0.68.0'
        bc_session.user_agent_extra = 'foo'

        Session(botocore_session=bc_session)

        self.assertEqual(bc_session.user_agent_extra, 'foo Botocore/0.68.0')