def test_credentials_get_session(self, mock_requests):
        """Test get_session"""

        creds = mock.create_autospec(Credentials)
        creds._id = 'abc'
        creds.auth = {'root':'1/',
                     'unattended_key':'3',
                     'token_uri':'/auth',
                     'resource':'https://test',
                     'tenant':'common',
                     'client_id':'abc'}
        creds.token = {'expires_at':'1',
                       'expires_in':'2',
                       'refresh_token':"test"}
        creds._log = logging.getLogger()

        Credentials.get_session(creds)
        mock_requests.OAuth2Session.assert_called_with(
            'abc',
            token=creds.token,
            auto_refresh_url='https://1/common/auth',
            auto_refresh_kwargs={'client_id':'abc', 'resource':'https://test'},
            token_updater=creds.store_auth)

        creds.token = {'expires_at':'1', 'expires_in':'2'}
        Credentials.get_session(creds)
        mock_requests.OAuth2Session.assert_called_with('abc',
                                                       token=creds.token)
    def test_credentials_clear_auth(self, mock_keyring):
        """Test clear_auth"""

        creds = mock.create_autospec(Credentials)
        creds._log = logging.getLogger()
        creds._id = 'test'
        Credentials.clear_auth(creds)

        mock_keyring.delete_password.assert_called_with("AzureBatchApps",
                                                        "test")
    def test_credentials_store_auth(self, mock_keyring):
        """Test store_auth"""

        creds = mock.create_autospec(Credentials)
        creds._log = logging.getLogger()
        creds._id = 'test'
        Credentials.store_auth(creds, {'token_type':'1',
                                       'access_token':'2'})

        mock_keyring.set_password.assert_called_with("AzureBatchApps",
                                                     "test",
                                                     str({'token_type':'1',
                                                          'access_token':'2'}))
    def test_credentials_refresh_session(self, mock_req, mock_oauth):

        creds = mock.create_autospec(Credentials)
        creds._id = 'abc'
        creds.auth = {'root':'1/',
                     'unattended_key':'3',
                     'token_uri':'/auth',
                     'resource':'https://test',
                     'tenant':'common',
                     'client_id':'abc'}
        creds.token = {'expires_at':'1',
                       'expires_in':'2',
                       'refresh_token':"test"}
        creds._log = logging.getLogger()

        Credentials.refresh_session(creds)
    def test_credentials_get_stored_auth(self, mock_keyring):
        """Test get_stored_auth"""

        creds = mock.create_autospec(Credentials)
        creds._log = logging.getLogger()
        creds._id = 'test'
        mock_keyring.get_password.return_value = None

        with self.assertRaises(AuthenticationException):
            Credentials.get_stored_auth(creds)
        mock_keyring.get_password.assert_called_with("AzureBatchApps",
                                                     "test")

        mock_keyring.get_password.return_value = str({'token_type':'1',
                                                      'access_token':'2'})
        Credentials.get_stored_auth(creds)
        mock_keyring.get_password.assert_called_with("AzureBatchApps", "test")