async def test_cleans_previous_token_before_fetching_new_one(): now = int(time.time()) new_token = deepcopy(default_token) past = now - 7200 default_token['expires_at'] = past new_token['expires_at'] = now + 3600 url = 'https://example.com/token' dispatch = MockDispatch(new_token) with mock.patch('time.time', lambda: now): async with AsyncOAuth2Client('foo', token=default_token, dispatch=dispatch) as sess: assert await sess.fetch_token(url) == new_token
async def test_add_token_to_body(): async def assert_func(request): content = await request.body() assert default_token['access_token'] in content.decode() mock_response = MockDispatch({'a': 'a'}, assert_func=assert_func) async with AsyncOAuth2Client('foo', token=default_token, token_placement='body', app=mock_response) as client: resp = await client.get('https://i.b') data = resp.json() assert data['a'] == 'a'
def test_add_token_to_header(self): def assert_func(request): token = 'Bearer ' + self.token['access_token'] auth_header = request.headers.get('authorization') self.assertEqual(auth_header, token) mock_response = MockDispatch({'a': 'a'}, assert_func=assert_func) with OAuth2Client(self.client_id, token=self.token, dispatch=mock_response) as client: resp = client.get('https://i.b') data = resp.json() self.assertEqual(data['a'], 'a')
async def test_add_token_to_header(): def assert_func(request): token = 'Bearer ' + default_token['access_token'] auth_header = request.headers.get('authorization') assert auth_header == token mock_response = MockDispatch({'a': 'a'}, assert_func=assert_func) async with AsyncOAuth2Client('foo', token=default_token, dispatch=mock_response) as client: resp = await client.get('https://i.b') data = resp.json() assert data['a'] == 'a'
def test_add_token_to_uri(): def assert_func(request): assert default_token['access_token'] in str(request.url) mock_response = MockDispatch({'a': 'a'}, assert_func=assert_func) with OAuth2Client( 'foo', token=default_token, token_placement='uri', app=mock_response ) as client: resp = client.get('https://i.b') data = resp.json() assert data['a'] == 'a'
def test_client_credentials_type(): url = 'https://example.com/token' def assert_func(request): content = request.form assert content.get('scope') == 'profile' assert content.get('grant_type') == 'client_credentials' app = MockDispatch(default_token, assert_func=assert_func) with OAuth2Client('foo', scope='profile', app=app) as sess: token = sess.fetch_token(url) assert token == default_token token = sess.fetch_token(url, grant_type='client_credentials') assert token == default_token
async def test_access_token_response_hook(): url = 'https://example.com/token' def _access_token_response_hook(resp): assert resp.json() == default_token return resp access_token_response_hook = mock.Mock( side_effect=_access_token_response_hook) app = MockDispatch(default_token) async with AsyncOAuth2Client('foo', token=default_token, app=app) as sess: sess.register_compliance_hook('access_token_response', access_token_response_hook) assert await sess.fetch_token(url) == default_token assert access_token_response_hook.called is True
def test_add_token_to_uri(self): def assert_func(request): self.assertIn(self.token['access_token'], str(request.url)) mock_response = MockDispatch({'a': 'a'}, assert_func=assert_func) with OAuth2Client( self.client_id, token=self.token, token_placement='uri', dispatch=mock_response ) as client: resp = client.get('https://i.b') data = resp.json() self.assertEqual(data['a'], 'a')
def test_client_credentials_type(self): url = 'https://example.com/token' def assert_func(request): body = request.content.decode() self.assertIn('scope=profile', body) self.assertIn('grant_type=client_credentials', body) dispatch = MockDispatch(self.token, assert_func=assert_func) with OAuth2Client(self.client_id, scope='profile', dispatch=dispatch) as sess: token = sess.fetch_token(url) self.assertEqual(token, self.token) token = sess.fetch_token(url, grant_type='client_credentials') self.assertEqual(token, self.token)
def test_fetch_token_post(self): url = 'https://example.com/token' def assert_func(request): body = request.content.decode() self.assertIn('code=v', body) self.assertIn('client_id=', body) self.assertIn('grant_type=authorization_code', body) mock_response = MockDispatch(self.token, assert_func=assert_func) with OAuth2Client(self.client_id, dispatch=mock_response) as client: token = client.fetch_token( url, authorization_response='https://i.b/?code=v') self.assertEqual(token, self.token) with OAuth2Client(self.client_id, token_endpoint_auth_method='none', dispatch=mock_response) as client: token = client.fetch_token(url, code='v') self.assertEqual(token, self.token) mock_response = MockDispatch({'error': 'invalid_request'}) with OAuth2Client(self.client_id, dispatch=mock_response) as client: self.assertRaises(OAuthError, client.fetch_token, url)
def test_access_token_response_hook(self): url = 'https://example.com/token' def _access_token_response_hook(resp): self.assertEqual(resp.json(), self.token) return resp access_token_response_hook = mock.Mock(side_effect=_access_token_response_hook) dispatch = MockDispatch(self.token) with OAuth2Client(self.client_id, token=self.token, dispatch=dispatch) as sess: sess.register_compliance_hook( 'access_token_response', access_token_response_hook ) self.assertEqual(sess.fetch_token(url), self.token) self.assertTrue(access_token_response_hook.called)
async def test_client_credentials_type(): url = 'https://example.com/token' def assert_func(request): body = request.content.decode() assert 'scope=profile' in body assert 'grant_type=client_credentials' in body dispatch = MockDispatch(default_token, assert_func=assert_func) async with AsyncOAuth2Client('foo', scope='profile', dispatch=dispatch) as sess: token = await sess.fetch_token(url) assert token == default_token token = await sess.fetch_token(url, grant_type='client_credentials') assert token == default_token
def test_add_token_to_body(): def assert_func(request): content = request.data content = content.decode() assert content == 'access_token=%s' % default_token['access_token'] mock_response = MockDispatch({'a': 'a'}, assert_func=assert_func) with OAuth2Client( 'foo', token=default_token, token_placement='body', app=mock_response ) as client: resp = client.get('https://i.b') data = resp.json() assert data['a'] == 'a'
def test_password_grant_type(): url = 'https://example.com/token' def assert_func(request): content = request.form assert content.get('username') == 'v' assert content.get('scope') == 'profile' assert content.get('grant_type') == 'password' app = MockDispatch(default_token, assert_func=assert_func) with OAuth2Client('foo', scope='profile', app=app) as sess: token = sess.fetch_token(url, username='******', password='******') assert token == default_token token = sess.fetch_token( url, username='******', password='******', grant_type='password') assert token == default_token
def test_cleans_previous_token_before_fetching_new_one(self): """Makes sure the previous token is cleaned before fetching a new one. The reason behind it is that, if the previous token is expired, this method shouldn't fail with a TokenExpiredError, since it's attempting to get a new one (which shouldn't be expired). """ now = int(time.time()) new_token = deepcopy(self.token) past = now - 7200 self.token['expires_at'] = past new_token['expires_at'] = now + 3600 url = 'https://example.com/token' dispatch = MockDispatch(new_token) with mock.patch('time.time', lambda: now): with OAuth2Client(self.client_id, token=self.token, dispatch=dispatch) as sess: self.assertEqual(sess.fetch_token(url), new_token)
def test_password_grant_type(self): url = 'https://example.com/token' def assert_func(request): body = request.content.decode() self.assertIn('username=v', body) self.assertIn('scope=profile', body) self.assertIn('grant_type=password', body) dispatch = MockDispatch(self.token, assert_func=assert_func) with OAuth2Client(self.client_id, scope='profile', dispatch=dispatch) as sess: token = sess.fetch_token(url, username='******', password='******') self.assertEqual(token, self.token) token = sess.fetch_token( url, username='******', password='******', grant_type='password') self.assertEqual(token, self.token)
def test_get_via_header(self): mock_response = MockDispatch(b'hello') with OAuth1Client( 'id', 'secret', token='foo', token_secret='bar', dispatch=mock_response, ) as client: response = client.get('https://example.com/') self.assertEqual(b'hello', response.content) request = response.request auth_header = request.headers.get('authorization') self.assertIn('oauth_token="foo"', auth_header) self.assertIn('oauth_consumer_key="id"', auth_header) self.assertIn('oauth_signature=', auth_header)
async def test_get_via_header(): mock_response = MockDispatch(b'hello') async with AsyncOAuth1Client( 'id', 'secret', token='foo', token_secret='bar', dispatch=mock_response, ) as client: response = await client.get('https://example.com/') assert response.content == b'hello' request = response.request auth_header = request.headers.get('authorization') assert 'oauth_token="foo"' in auth_header assert 'oauth_consumer_key="id"' in auth_header assert 'oauth_signature=' in auth_header
def test_token_auth_method_client_secret_post(self): url = 'https://example.com/token' def assert_func(request): body = request.content.decode() self.assertIn('code=v', body) self.assertIn('client_id=', body) self.assertIn('client_secret=bar', body) self.assertIn('grant_type=authorization_code', body) mock_response = MockDispatch(self.token, assert_func=assert_func) with OAuth2Client(self.client_id, 'bar', token_endpoint_auth_method='client_secret_post', dispatch=mock_response) as client: token = client.fetch_token(url, code='v') self.assertEqual(token, self.token)
async def test_token_auth_method_client_secret_post(): url = 'https://example.com/token' def assert_func(request): body = request.content.decode() assert 'code=v' in body assert 'client_id=' in body assert 'client_secret=bar' in body assert 'grant_type=authorization_code' in body mock_response = MockDispatch(default_token, assert_func=assert_func) async with AsyncOAuth2Client( 'foo', 'bar', token_endpoint_auth_method='client_secret_post', dispatch=mock_response) as client: token = await client.fetch_token(url, code='v') assert token == default_token
def test_token_auth_method_client_secret_post(): url = 'https://example.com/token' def assert_func(request): content = request.form assert content.get('code') == 'v' assert content.get('client_id') == 'foo' assert content.get('client_secret') == 'bar' assert content.get('grant_type') == 'authorization_code' mock_response = MockDispatch(default_token, assert_func=assert_func) with OAuth2Client( 'foo', 'bar', token_endpoint_auth_method='client_secret_post', app=mock_response ) as client: token = client.fetch_token(url, code='v') assert token == default_token
def test_get_via_body(self): mock_response = MockDispatch(b'hello') with OAuth1Client( 'id', 'secret', token='foo', token_secret='bar', signature_type=SIGNATURE_TYPE_BODY, dispatch=mock_response, ) as client: response = client.post('https://example.com/') self.assertEqual(b'hello', response.content) request = response.request auth_header = request.headers.get('authorization') self.assertIsNone(auth_header) self.assertIn(b'oauth_token=foo', request.content) self.assertIn(b'oauth_consumer_key=id', request.content) self.assertIn(b'oauth_signature=', request.content)
async def test_password_grant_type(): url = 'https://example.com/token' def assert_func(request): body = request.content.decode() assert 'username=v' in body assert 'scope=profile' in body assert 'grant_type=password' in body dispatch = MockDispatch(default_token, assert_func=assert_func) async with AsyncOAuth2Client('foo', scope='profile', dispatch=dispatch) as sess: token = await sess.fetch_token(url, username='******', password='******') assert token == default_token token = await sess.fetch_token(url, username='******', password='******', grant_type='password') assert token == default_token
def test_fetch_request_token_via_body(self): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} def assert_func(request): auth_header = request.headers.get('authorization') self.assertIsNone(auth_header) self.assertIn(b'oauth_consumer_key=id', request.content) self.assertIn(b'&oauth_signature=', request.content) mock_response = MockDispatch(request_token, assert_func=assert_func) with OAuth1Client( 'id', 'secret', signature_type=SIGNATURE_TYPE_BODY, dispatch=mock_response, ) as client: response = client.fetch_request_token(self.oauth_url) self.assertEqual(response, request_token)
async def test_fetch_request_token_via_body(): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} def assert_func(request): auth_header = request.headers.get('authorization') assert auth_header is None assert b'oauth_consumer_key=id' in request.content assert b'&oauth_signature=' in request.content mock_response = MockDispatch(request_token, assert_func=assert_func) async with AsyncOAuth1Client( 'id', 'secret', signature_type=SIGNATURE_TYPE_BODY, dispatch=mock_response, ) as client: response = await client.fetch_request_token(oauth_url) assert response == request_token
async def test_get_via_query(): mock_response = MockDispatch(b'hello') async with AsyncOAuth1Client( 'id', 'secret', token='foo', token_secret='bar', signature_type=SIGNATURE_TYPE_QUERY, dispatch=mock_response, ) as client: response = await client.get('https://example.com/') assert response.content == b'hello' request = response.request auth_header = request.headers.get('authorization') assert auth_header is None url = str(request.url) assert 'oauth_token=foo' in url assert 'oauth_consumer_key=id' in url assert 'oauth_signature=' in url
async def test_auto_refresh_token3(): async def _update_token(token, refresh_token=None, access_token=None): assert access_token == 'a' assert token == default_token update_token = mock.Mock(side_effect=_update_token) old_token = dict(access_token='a', token_type='bearer', expires_at=100) app = MockDispatch(default_token) async with AsyncOAuth2Client( 'foo', token=old_token, token_endpoint='https://i.b/token', update_token=update_token, grant_type='client_credentials', app=app, ) as client: await client.post('https://i.b/user', json={'foo': 'bar'}) assert update_token.called is True
def test_fetch_request_token_via_body(): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} def assert_func(request): auth_header = request.headers.get('authorization') assert auth_header is None content = request.form assert content.get('oauth_consumer_key') == 'id' assert 'oauth_signature' in content mock_response = MockDispatch(request_token, assert_func=assert_func) with OAuth1Client( 'id', 'secret', signature_type=SIGNATURE_TYPE_BODY, app=mock_response, ) as client: response = client.fetch_request_token(oauth_url) assert response == request_token
async def test_auto_refresh_token4(): async def _update_token(token, refresh_token=None, access_token=None): await asyncio.sleep( 0.1) # artificial sleep to force other coroutines to wake update_token = mock.Mock(side_effect=_update_token) old_token = dict(access_token='a', token_type='bearer', expires_at=100) app = MockDispatch(default_token) async with AsyncOAuth2Client( 'foo', token=old_token, token_endpoint='https://i.b/token', update_token=update_token, grant_type='client_credentials', app=app, ) as client: coroutines = [client.get('https://i.b/user') for x in range(10)] await asyncio.gather(*coroutines) update_token.assert_called_once()
def test_fetch_request_token_via_query(): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} def assert_func(request): auth_header = request.headers.get('authorization') assert auth_header is None url = str(request.url) assert 'oauth_consumer_key=id' in url assert '&oauth_signature=' in url mock_response = MockDispatch(request_token, assert_func=assert_func) with OAuth1Client( 'id', 'secret', signature_type=SIGNATURE_TYPE_QUERY, app=mock_response, ) as client: response = client.fetch_request_token(oauth_url) assert response == request_token