Skip to content

Commit 607c2e6

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 607c2e6

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ def tearDown(self):
5353
return super().tearDown()
5454

5555

56+
class MockOauthlibCoreClass:
57+
"""
58+
Mock oauthlib, used in test_token_view_status_equals_what_oauthlib_token_response_method_returns.
59+
"""
60+
61+
def create_token_response(self, _):
62+
return "url", {"headers_are_ignored": True}, '{"Key": "Value"}', 299
63+
64+
5665
class TestDeviceFlow(DeviceFlowBaseTestCase):
5766
"""
5867
The first 2 tests test the device flow in order
@@ -439,6 +448,39 @@ def test_token_view_returns_404_error_if_device_not_found(self):
439448
# consumed by devices.
440449
self.assertEqual(response.__getitem__("content-type"), "application/json")
441450

451+
@mock.patch("oauth2_provider.views.mixins.OAuthLibMixin.get_oauthlib_core", MockOauthlibCoreClass)
452+
def test_token_view_status_equals_what_oauthlib_token_response_method_returns(self):
453+
device = DeviceModel(
454+
client_id="client_id",
455+
device_code="device_code",
456+
user_code="user_code",
457+
scope="scope",
458+
expires=datetime.now() + timedelta(seconds=60),
459+
status="authorized",
460+
)
461+
device.save()
462+
463+
token_payload = {
464+
"device_code": "device_code",
465+
"client_id": "client_id",
466+
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
467+
}
468+
469+
response = self.client.post(
470+
"/o/token/",
471+
data=urlencode(token_payload),
472+
content_type="application/x-www-form-urlencoded",
473+
)
474+
475+
self.assertEqual(response["content-type"], "application/json")
476+
self.assertContains(
477+
response=response,
478+
status_code=299,
479+
text='{"Key": "Value"}',
480+
count=1,
481+
)
482+
assert not response.has_header("headers_are_ignored")
483+
442484
@mock.patch(
443485
"oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
444486
lambda: "abc",
@@ -644,3 +686,9 @@ def test_device_is_expired_method_sets_status_to_expired_if_deadline_passed(self
644686

645687
assert is_expired
646688
assert device.status == device.EXPIRED
689+
690+
# calling again is_expired() should return true and not change the state
691+
is_expired = device.is_expired()
692+
693+
assert is_expired
694+
assert device.status == device.EXPIRED

0 commit comments

Comments
 (0)