def test_server_error_raises_http_error(self): c = Credentials('id', 'secret') send = MagicMock(return_value=mock_response(500)) with patch('tekore.auth.Credentials._send', send): with self.assertRaises(HTTPError): c.request_client_token()
def __init__(self, client_id: str, client_secret: str, redirect_uri: str = None, sender: Sender = None): self._client = Credentials(client_id, client_secret, redirect_uri, sender)
def test_refresh_valid_refresh_interpreted_as_user_token(self): c = Credentials('id', 'secret', 'uri') token = MagicMock() token.refresh_token = 'refresh' mock = MagicMock() with patch('tekore.auth.Credentials.refresh_user_token', mock): c.refresh(token) mock.assert_called_once()
def test_refresh_none_refresh_interpreted_as_client_token(self): c = Credentials('id', 'secret', 'uri') token = MagicMock() token.refresh_token = None mock = MagicMock() with patch('tekore.auth.Credentials.request_client_token', mock): c.refresh(token) mock.assert_called_once()
def test_refresh_user_token_refresh_replaced_if_returned(self): c = Credentials('id', 'secret', 'uri') token = make_token_dict() response = mock_response(content=token) send = MagicMock(return_value=response) with patch('tekore.auth.Credentials._send', send): refreshed = c.refresh_user_token('refresh') self.assertEqual(refreshed.refresh_token, token['refresh_token'])
def __init__(self, client_id: str, client_secret: str, redirect_uri: str = None, sender: SyncSender = None): self._client = Credentials(client_id, client_secret, redirect_uri, sender, asynchronous=False)
def test_credentials_init_redirect_uri_optional(self): Credentials('id', 'secret')
def test_credentials_initialisation(self): Credentials(client_id='id', client_secret='secret', redirect_uri='uri')
def test_bad_arguments_raises_oauth_error(self): c = Credentials('id', 'secret') with self.assertRaises(OAuthError): c.request_client_token()
def test_refresh_user_token(self): c = Credentials(self.client_id, self.client_secret) c.refresh_user_token(self.user_refresh)
def test_async_refresh_user_token(self): c = Credentials(self.client_id, self.client_secret, asynchronous=True) run(c.refresh_user_token(self.user_refresh))
def test_user_authorisation_url_equal_to_credentials(self): auth = Credentials(self.client_id, self.client_secret, self.redirect_uri) util = self._initialise() self.assertEqual(auth.user_authorisation_url(), util.user_authorisation_url())
def test_request_user_token(self): c = Credentials('id', 'secret', 'uri') send = MagicMock(return_value=mock_response()) with patch('tekore.auth.Credentials._send', send): c.request_user_token('code') send.assert_called_once()
def test_user_authorisation_url(self): c = Credentials('id', 'secret', 'uri') url = c.user_authorisation_url('scope', 'state', True) self.assertIn('scope=scope', url) self.assertIn('state=state', url) self.assertIn('show_dialog=true', url)
class RefreshingCredentials: """ Client for retrieving automatically refreshing access tokens. Delegates to an underlying :class:`Credentials <tekore.auth.expiring.Credentials>` manager and parses tokens it returns into :class:`RefreshingToken`. Parameters ---------- client_id client id client_secret client secret redirect_uri whitelisted redirect URI sender request sender """ def __init__(self, client_id: str, client_secret: str, redirect_uri: str = None, sender: Sender = None): self._client = Credentials(client_id, client_secret, redirect_uri, sender) def request_client_token(self) -> RefreshingToken: """ Request a refreshing client token. Returns ------- RefreshingToken automatically refreshing client token """ token = self._client.request_client_token() return RefreshingToken(token, self._client) def user_authorisation_url(self, scope=None, state: str = None, show_dialog: bool = False) -> str: """ Construct an authorisation URL. Step 1/2 in authorisation code flow. User should be redirected to the resulting URL for authorisation. Parameters ---------- scope access rights as a space-separated list state additional state show_dialog force login dialog even if previously authorised Returns ------- str login URL """ return self._client.user_authorisation_url(scope, state, show_dialog) def request_user_token(self, code: str) -> RefreshingToken: """ Request a new refreshing user token. Step 2/2 in authorisation code flow. Code is provided as a URL parameter in the redirect URI after login in step 1. Parameters ---------- code code from redirect parameters Returns ------- RefreshingToken automatically refreshing user token """ token = self._client.request_user_token(code) return RefreshingToken(token, self._client) def refresh_user_token(self, refresh_token: str) -> RefreshingToken: """ Request an automatically refreshing user token with a refresh token. Parameters ---------- refresh_token refresh token Returns ------- RefreshingToken automatically refreshing user token """ token = self._client.refresh_user_token(refresh_token) return RefreshingToken(token, self._client)
def test_request_client_token(self): c = Credentials(self.client_id, self.client_secret) c.request_client_token()
def test_async_request_client_token(self): c = Credentials(self.client_id, self.client_secret, asynchronous=True) run(c.request_client_token())