Skip to content

Commit 74ffed5

Browse files
committed
Increase test coverage
This commit adds two tests that cover lines flagged by codecov. One test covers the use case when oauthlib create_token_response method returns a different status than 200. The second test covers an edge case where the second time is_expired is called, the memoised value is returned.
1 parent f32c57d commit 74ffed5

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

oauth2_provider/views/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,11 @@ def device_flow_token_response(
360360
)
361361

362362
url, headers, body, status = self.create_token_response(request)
363+
response = http.JsonResponse(data=json.loads(body), status=status)
364+
363365
if status != 200:
364-
return http.JsonResponse(data=json.loads(body), status=status)
366+
return response
365367

366-
response = http.JsonResponse(data=json.loads(body), status=status)
367368
for k, v in headers.items():
368369
response[k] = v
369370

tests/test_device.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,50 @@ def test_token_view_returns_404_error_if_device_not_found(self):
439439
# consumed by devices.
440440
self.assertEqual(response.__getitem__("content-type"), "application/json")
441441

442+
def test_token_view_status_equals_what_oauthlib_token_response_method_returns(self):
443+
"""
444+
Tests the use case where oauthlib create_token_response returns a status different
445+
than 200.
446+
"""
447+
448+
class MockOauthlibCoreClass:
449+
def create_token_response(self, _):
450+
return "url", {"headers_are_ignored": True}, '{"Key": "Value"}', 299
451+
452+
device = DeviceModel(
453+
client_id="client_id",
454+
device_code="device_code",
455+
user_code="user_code",
456+
scope="scope",
457+
expires=datetime.now() + timedelta(seconds=60),
458+
status="authorized",
459+
)
460+
device.save()
461+
462+
token_payload = {
463+
"device_code": "device_code",
464+
"client_id": "client_id",
465+
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
466+
}
467+
468+
with mock.patch(
469+
"oauth2_provider.views.mixins.OAuthLibMixin.get_oauthlib_core", MockOauthlibCoreClass
470+
):
471+
response = self.client.post(
472+
"/o/token/",
473+
data=urlencode(token_payload),
474+
content_type="application/x-www-form-urlencoded",
475+
)
476+
477+
self.assertEqual(response["content-type"], "application/json")
478+
self.assertContains(
479+
response=response,
480+
status_code=299,
481+
text='{"Key": "Value"}',
482+
count=1,
483+
)
484+
assert not response.has_header("headers_are_ignored")
485+
442486
@mock.patch(
443487
"oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
444488
lambda: "abc",
@@ -644,3 +688,9 @@ def test_device_is_expired_method_sets_status_to_expired_if_deadline_passed(self
644688

645689
assert is_expired
646690
assert device.status == device.EXPIRED
691+
692+
# calling again is_expired() should return true and not change the state
693+
is_expired = device.is_expired()
694+
695+
assert is_expired
696+
assert device.status == device.EXPIRED

0 commit comments

Comments
 (0)