Skip to content

Commit 9f693eb

Browse files
committed
Add tests that cover DeviceConfirmView for various values for action
This commit adds test covereage for the two if branches in DeviceConfirmView.form_valid.
1 parent c10a471 commit 9f693eb

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/test_device.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,79 @@ def test_device_flow_authorization_user_code_confirm_and_access_token(self):
290290
)
291291
assert refresh_token.user == device.user
292292

293+
def test_user_denies_access(self):
294+
"""
295+
This test asserts the when the user denies access, the state of the grant is saved
296+
and the user is redirected to the page where they can see the "denied" state.
297+
298+
The /token View returning the appropriate message for the "denied" state is covered
299+
in test_token_view_returns_error_if_device_in_invalid_state.
300+
"""
301+
UserModel.objects.create_user(
302+
username="test_user_device_flow",
303+
304+
password="password123",
305+
)
306+
self.client.login(username="test_user_device_flow", password="password123")
307+
308+
device = DeviceModel(
309+
client_id="client_id",
310+
device_code="device_code",
311+
user_code="user_code",
312+
scope="scope",
313+
expires=datetime.now() + timedelta(days=1),
314+
status=DeviceModel.AUTHORIZATION_PENDING,
315+
)
316+
device.save()
317+
318+
device_confirm_url = reverse(
319+
"oauth2_provider:device-confirm",
320+
kwargs={"user_code": "user_code", "client_id": "client_id"},
321+
)
322+
323+
device_grant_status_url = reverse(
324+
"oauth2_provider:device-grant-status",
325+
kwargs={"user_code": "user_code", "client_id": "client_id"},
326+
)
327+
328+
self.assertRedirects(
329+
response=self.client.post(device_confirm_url, data={"action": "deny"}),
330+
expected_url=device_grant_status_url,
331+
)
332+
333+
device.refresh_from_db()
334+
assert device.status == device.DENIED
335+
336+
def test_device_confirm_view_returns_400_on_incorrect_action(self):
337+
"""
338+
This test asserts that the confirm view returns 400 if action is not
339+
"accept" or "deny".
340+
"""
341+
UserModel.objects.create_user(
342+
username="test_user_device_flow",
343+
344+
password="password123",
345+
)
346+
self.client.login(username="test_user_device_flow", password="password123")
347+
348+
device = DeviceModel(
349+
client_id="client_id",
350+
device_code="device_code",
351+
user_code="user_code",
352+
scope="scope",
353+
expires=datetime.now() + timedelta(days=1),
354+
status=DeviceModel.AUTHORIZATION_PENDING,
355+
)
356+
device.save()
357+
358+
device_confirm_url = reverse(
359+
"oauth2_provider:device-confirm",
360+
kwargs={"user_code": "user_code", "client_id": "client_id"},
361+
)
362+
response = self.client.post(device_confirm_url, data={"action": "inccorect_action"})
363+
364+
assert response.status_code == 400
365+
293366
def test_device_flow_authorization_device_invalid_state_returns_form_error(self):
294367
"""
295368
This test asserts that only devices in the expected state (authorization-pending)

0 commit comments

Comments
 (0)