Пример #1
0
    def test_credentials_retrieve_session(self, mock_retrieve):

        creds = InteractiveCredentials.retrieve_session("client_id", "redirect")
        mock_retrieve.asset_called_with(mock.ANY)

        mock_retrieve.side_effect = ValueError("No stored token")
        with self.assertRaises(ValueError):
            InteractiveCredentials.retrieve_session("client_id", "redirect")

        mock_retrieve.side_effect = TokenExpiredError("Token expired")
        with self.assertRaises(TokenExpiredError):
            InteractiveCredentials.retrieve_session("client_id", "redirect")
Пример #2
0
    def test_credentials_retrieve_session(self, mock_retrieve):

        creds = InteractiveCredentials.retrieve_session("client_id", "redirect")
        mock_retrieve.asset_called_with(mock.ANY)

        mock_retrieve.side_effect = ValueError("No stored token")
        with self.assertRaises(ValueError):
            InteractiveCredentials.retrieve_session("client_id", "redirect")

        mock_retrieve.side_effect = TokenExpiredError("Token expired")
        with self.assertRaises(TokenExpiredError):
            InteractiveCredentials.retrieve_session("client_id", "redirect")
Пример #3
0
    def test_credentials_retrieve_session(self):

        creds = mock.create_autospec(InteractiveCredentials)
        creds._retrieve_stored_token.return_value = {
            'expires_at':'1',
            'expires_in':'2',
            'refresh_token':"test"}

        token = InteractiveCredentials.retrieve_session(creds)
        self.assertEqual(token, creds._retrieve_stored_token.return_value)

        creds._retrieve_stored_token.side_effect=ValueError("No stored token")
        self.assertIsNone(InteractiveCredentials.retrieve_session(creds))

        creds._retrieve_stored_token.side_effect=None
        creds.signed_session.side_effect=TokenExpiredError("Token expired")
        self.assertIsNone(InteractiveCredentials.retrieve_session(creds))
Пример #4
0
    def test_credentials_signed_session(self, mock_requests):

        creds = mock.create_autospec(InteractiveCredentials)
        creds.id = 'client_id'
        creds.token_uri = "token_uri"
        creds.resource = "resource"

        creds.token = {'expires_at':'1',
                       'expires_in':'2',
                       'refresh_token':"test"}

        InteractiveCredentials.signed_session(creds)
        mock_requests.OAuth2Session.assert_called_with(
            'client_id',
            token=creds.token,
            auto_refresh_url='token_uri',
            auto_refresh_kwargs={'client_id':'client_id', 'resource':'resource'},
            token_updater=creds._default_token_cache)
Пример #5
0
    def test_credentials_auth_url(self):

        creds = mock.create_autospec(InteractiveCredentials)
        session = mock.create_autospec(OAuth2Session)
        creds._setup_session.return_value = session
        creds.auth_uri = "auth_uri"
        creds.resource = "auth_resource"
        session.authorization_url.return_value = ("a","b")

        url, state = InteractiveCredentials.get_auth_url(creds)
        self.assertEqual(url, "a")
        self.assertEqual(state, "b")
        session.authorization_url.assert_called_with(
            "auth_uri", resource="auth_resource")

        InteractiveCredentials.get_auth_url(creds, msa=True, test="extra_arg")
        session.authorization_url.assert_called_with(
            "auth_uri", resource="auth_resource",
            domain_hint='live.com', test='extra_arg')
Пример #6
0
    def test_credentials_auth_url(self):

        creds = mock.create_autospec(InteractiveCredentials)
        session = mock.create_autospec(OAuth2Session)
        creds._setup_session.return_value = session
        creds.auth_uri = "auth_uri"
        creds.resource = "auth_resource"
        session.authorization_url.return_value = ("a","b")

        url, state = InteractiveCredentials.get_auth_url(creds)
        self.assertEqual(url, "a")
        self.assertEqual(state, "b")
        session.authorization_url.assert_called_with(
            "auth_uri", resource="auth_resource")

        InteractiveCredentials.get_auth_url(creds, msa=True, test="extra_arg")
        session.authorization_url.assert_called_with(
            "auth_uri", resource="auth_resource",
            domain_hint='live.com', test='extra_arg')
Пример #7
0
    def test_credentials_retrieve_session(self):

        creds = mock.create_autospec(InteractiveCredentials)
        creds._retrieve_stored_token.return_value = {
            'expires_at': '1',
            'expires_in': '2',
            'refresh_token': "test"
        }

        token = InteractiveCredentials.retrieve_session(creds)
        self.assertEqual(token, creds._retrieve_stored_token.return_value)

        creds._retrieve_stored_token.side_effect = ValueError(
            "No stored token")
        self.assertIsNone(InteractiveCredentials.retrieve_session(creds))

        creds._retrieve_stored_token.side_effect = None
        creds.signed_session.side_effect = TokenExpiredError("Token expired")
        self.assertIsNone(InteractiveCredentials.retrieve_session(creds))
Пример #8
0
    def test_credentials_get_token(self):

        creds = mock.create_autospec(InteractiveCredentials)
        session = mock.create_autospec(OAuth2Session)
        creds._setup_session.return_value = session

        session.fetch_token.return_value = {
            'expires_at': '1',
            'expires_in': '2',
            'refresh_token': "test"
        }

        creds.redirect = "//my_service.com"
        creds.token_uri = "token_uri"
        creds._check_state.return_value = True
        creds.verify = True

        InteractiveCredentials.set_token(creds, "response")
        self.assertEqual(creds.token, session.fetch_token.return_value)
        session.fetch_token.assert_called_with(
            "token_uri",
            authorization_response="https://my_service.com/response",
            verify=True)

        creds._check_state.side_effect = ValueError("failed")
        with self.assertRaises(ValueError):
            InteractiveCredentials.set_token(creds, "response")

        creds._check_state.side_effect = None
        session.fetch_token.side_effect = oauthlib.oauth2.OAuth2Error
        with self.assertRaises(AuthenticationError):
            InteractiveCredentials.set_token(creds, "response")
Пример #9
0
    def test_credentials_get_token(self):

        creds = mock.create_autospec(InteractiveCredentials)
        session = mock.create_autospec(OAuth2Session)
        creds._setup_session.return_value = session

        session.fetch_token.return_value = {
            'expires_at':'1',
            'expires_in':'2',
            'refresh_token':"test"}

        creds.redirect = "//my_service.com"
        creds.token_uri = "token_uri"
        creds._check_state.return_value = True
        creds.verify = True

        InteractiveCredentials.set_token(creds, "response")
        self.assertEqual(creds.token, session.fetch_token.return_value)
        session.fetch_token.assert_called_with(
            "token_uri",
            authorization_response="https://my_service.com/response",
            verify=True)

        creds._check_state.side_effect = ValueError("failed")
        with self.assertRaises(ValueError):
            InteractiveCredentials.set_token(creds, "response")

        creds._check_state.side_effect = None
        session.fetch_token.side_effect = oauthlib.oauth2.OAuth2Error
        with self.assertRaises(AuthenticationError):
            InteractiveCredentials.set_token(creds, "response")
Пример #10
0
    def test_credentials_signed_session(self, mock_requests):

        creds = mock.create_autospec(InteractiveCredentials)
        creds.id = 'client_id'
        creds.token_uri = "token_uri"
        creds.resource = "resource"

        creds.token = {
            'expires_at': '1',
            'expires_in': '2',
            'refresh_token': "test"
        }

        InteractiveCredentials.signed_session(creds)
        mock_requests.OAuth2Session.assert_called_with(
            'client_id',
            token=creds.token,
            auto_refresh_url='token_uri',
            auto_refresh_kwargs={
                'client_id': 'client_id',
                'resource': 'resource'
            },
            token_updater=creds._default_token_cache)