Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3513e31

Browse files
committed
Added tests for edge cases
1 parent 445a396 commit 3513e31

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

oauth2/datatype.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ def expires_in(self):
3434
return time_left
3535
return 0
3636

37+
def is_expired(self):
38+
"""
39+
Determines if the token has expired.
40+
41+
:return: `True` if the token has expired. Otherwise `False`.
42+
"""
43+
if self.expires_at is None:
44+
return False
45+
46+
if self.expires_in > 0:
47+
return False
48+
49+
return True
50+
3751
def to_json(self):
3852
json = {"access_token": self.token, "token_type": "Bearer"}
3953

oauth2/grant.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ def _sanitize_return_value(self, value):
321321
return value, None
322322

323323
class AccessTokenMixin(object):
324+
"""
325+
Used by grants that handle refresh token and unique token.
326+
"""
324327
def __init__(self, access_token_store, token_generator, **kwargs):
325328
self.access_token_store = access_token_store
326329
self.token_generator = token_generator
@@ -339,7 +342,9 @@ def create_token(self, client_id, data, grant_type, scopes, user_id):
339342
grant_type,
340343
user_id)
341344

342-
if access_token is not None:
345+
if (access_token is not None
346+
and access_token.scopes == scopes
347+
and access_token.is_expired() == False):
343348
token_data = {"access_token": access_token.token,
344349
"token_type": "Bearer"}
345350

oauth2/test/test_grant.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,77 @@ def test_process_with_unique_access_token_not_found(self):
872872
self.assertDictEqual(expected_response_body,
873873
json.loads(response_result.body))
874874

875+
def test_process_with_unique_access_token_different_scope(self):
876+
access_token_data = {"client_id": "myclient",
877+
"grant_type": "authorization_code",
878+
"token": "xyz890", "data": {}, "expires_at": 1200,
879+
"refresh_token": "mno789", "scopes": ["foo", "bar"],
880+
"user_id": 123}
881+
access_token = AccessToken(**access_token_data)
882+
token_data = {"access_token": "abc123", "token_type": "Bearer",
883+
"refresh_token": "def456", "expires_in": 1000}
884+
expected_response_body = copy(token_data)
885+
expected_response_body["scope"] = "bar baz"
886+
887+
response = Response()
888+
889+
access_token_store_mock = Mock(spec=AccessTokenStore)
890+
access_token_store_mock.fetch_existing_token_of_user.return_value = access_token
891+
892+
token_generator_mock = Mock(spec=TokenGenerator)
893+
token_generator_mock.create_access_token_data.return_value = token_data
894+
895+
handler = AuthorizationCodeTokenHandler(
896+
access_token_store=access_token_store_mock,
897+
auth_token_store=Mock(spec=AuthCodeStore),
898+
client_store=Mock(spec=ClientStore),
899+
token_generator=token_generator_mock)
900+
handler.client_id = access_token_data["client_id"]
901+
handler.data = {}
902+
handler.unique_token = True
903+
handler.user_id = 123
904+
handler.scopes = ["bar", "baz"]
905+
906+
response_result = handler.process(Mock(), response, {})
907+
self.assertDictEqual(expected_response_body,
908+
json.loads(response_result.body))
909+
910+
@patch("time.time", mock_time)
911+
def test_process_with_unique_access_token_expired_token(self):
912+
access_token_data = {"client_id": "myclient",
913+
"grant_type": "authorization_code",
914+
"token": "xyz890", "data": {}, "expires_at": 300,
915+
"refresh_token": "mno789", "scopes": ["foo", "bar"],
916+
"user_id": 123}
917+
access_token = AccessToken(**access_token_data)
918+
token_data = {"access_token": "abc123", "token_type": "Bearer",
919+
"refresh_token": "def456", "expires_in": 1000}
920+
expected_response_body = copy(token_data)
921+
expected_response_body["scope"] = "foo bar"
922+
923+
response = Response()
924+
925+
access_token_store_mock = Mock(spec=AccessTokenStore)
926+
access_token_store_mock.fetch_existing_token_of_user.return_value = access_token
927+
928+
token_generator_mock = Mock(spec=TokenGenerator)
929+
token_generator_mock.create_access_token_data.return_value = token_data
930+
931+
handler = AuthorizationCodeTokenHandler(
932+
access_token_store=access_token_store_mock,
933+
auth_token_store=Mock(spec=AuthCodeStore),
934+
client_store=Mock(spec=ClientStore),
935+
token_generator=token_generator_mock)
936+
handler.client_id = access_token_data["client_id"]
937+
handler.data = {}
938+
handler.unique_token = True
939+
handler.user_id = 123
940+
handler.scopes = ["foo", "bar"]
941+
942+
response_result = handler.process(Mock(), response, {})
943+
self.assertDictEqual(expected_response_body,
944+
json.loads(response_result.body))
945+
875946
def test_process_with_unique_access_token_no_user_id(self):
876947
handler = AuthorizationCodeTokenHandler(
877948
access_token_store=Mock(spec=AccessTokenStore),

0 commit comments

Comments
 (0)