-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
32 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,52 +6,48 @@ | |
|
||
|
||
class TestOAuthClient(TestCase): | ||
def setUp(self): | ||
self.client = OAuthClient(client_id='some_client_id', client_secret='some_client_secret', | ||
host_url='https://host.example.com', oauth_url='https://oauth.example.com', | ||
user_info_url='https://userinfo.example.com') | ||
|
||
def test_auth_url(self): | ||
client = OAuthClient('some_client_id', 'some_client_secret', 'https://example.com') | ||
auth_url = client.auth_url('some_state') | ||
auth_url = self.client.auth_url('some_state') | ||
self.assertEqual( | ||
'https://accounts.google.com/o/oauth2/auth?client_id=some_client_id&redirect_uri=https%3A%2F%2Fexample.com%2Foauth%2Fcallback&response_type=code&scope=email&state=some_state', | ||
'https://oauth.example.com/auth?client_id=some_client_id' | ||
'&redirect_uri=https%3A%2F%2Fhost.example.com%2Foauth%2Fcallback&response_type=code&scope=email' | ||
'&state=some_state', | ||
auth_url | ||
) | ||
|
||
@responses.activate | ||
def test_fetch_access_token(self): | ||
client = OAuthClient('some_client_id', 'some_client_secret', 'https://example.com') | ||
token_endpoint = responses.post( | ||
'https://accounts.google.com/o/oauth2/token', | ||
json={'access_token': 'some_access_token'} | ||
) | ||
token_endpoint = responses.post('https://oauth.example.com/token', json={'access_token': 'some_access_token'}) | ||
|
||
access_token = client.fetch_access_token('some_code') | ||
access_token = self.client.fetch_access_token('some_code') | ||
|
||
self.assertEqual('some_access_token', access_token) | ||
self.assertEqual(1, token_endpoint.call_count) | ||
recorded_request = responses.calls[0].request | ||
self.assertEqual( | ||
"client_id=some_client_id&client_secret=some_client_secret&code=some_code&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fexample.com%2Foauth%2Fcallback", | ||
"client_id=some_client_id&client_secret=some_client_secret&code=some_code&grant_type=authorization_code" | ||
"&redirect_uri=https%3A%2F%2Fhost.example.com%2Foauth%2Fcallback", | ||
recorded_request.body | ||
) | ||
|
||
@responses.activate | ||
def test_fetch_access_token_bad_request(self): | ||
client = OAuthClient('some_client_id', 'some_client_secret', 'https://example.com') | ||
responses.post( | ||
'https://accounts.google.com/o/oauth2/token', | ||
status=400 | ||
) | ||
responses.post('https://oauth.example.com/token', status=400) | ||
|
||
access_token = client.fetch_access_token('some_code') | ||
access_token = self.client.fetch_access_token('some_code') | ||
|
||
self.assertIsNone(access_token) | ||
|
||
@responses.activate | ||
def test_read_email_from_token(self): | ||
user_info_endpoint = responses.get( | ||
'https://www.googleapis.com/oauth2/v3/userinfo', | ||
json={'email': '[email protected]'} | ||
) | ||
user_info_endpoint = responses.get('https://userinfo.example.com', json={'email': '[email protected]'}) | ||
|
||
email = OAuthClient.read_email_from_token('some_token') | ||
email = self.client.read_email_from_token('some_token') | ||
|
||
self.assertEqual('[email protected]', email) | ||
self.assertEqual(1, user_info_endpoint.call_count) | ||
|
@@ -63,11 +59,8 @@ def test_read_email_from_token(self): | |
|
||
@responses.activate | ||
def test_read_email_from_token_bad_request(self): | ||
responses.get( | ||
'https://www.googleapis.com/oauth2/v3/userinfo', | ||
status=400 | ||
) | ||
responses.get('https://userinfo.example.com', status=400) | ||
|
||
email = OAuthClient.read_email_from_token('some_token') | ||
email = self.client.read_email_from_token('some_token') | ||
|
||
self.assertIsNone(email) |