Skip to content

Commit

Permalink
[fix] Fixed - Exception raised on opening non existing user ID #228
Browse files Browse the repository at this point in the history
- Added a test to check the response status on navigating to a non-existing user change page
- Handled exceptions for an invalid UUID and a non-existing user

Fixes #228
  • Loading branch information
Saurav-Shrivastav committed Mar 13, 2021
1 parent f2cdb92 commit a215ab5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions openwisp_users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ def get_inline_instances(self, request, obj=None):

def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = extra_context or {}
obj = self.model.objects.get(pk=object_id)
if user_not_allowed_to_change_owner(request.user, obj):
obj = self.get_object(request, object_id)
if (obj is not None) and (user_not_allowed_to_change_owner(request.user, obj)):
show_owner_warning = True
extra_context.update({'show_owner_warning': show_owner_warning})
return super().change_view(request, object_id, form_url, extra_context)
Expand Down
27 changes: 27 additions & 0 deletions openwisp_users/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re
import smtplib
import uuid
from unittest.mock import patch

from django.contrib import admin
Expand Down Expand Up @@ -169,6 +170,32 @@ def test_admin_change_user_email_empty(self):
self.assertEqual(len(mail.outbox), 0)
self.assertContains(response, 'errors field-email')

def test_admin_change_user_page_get_invalid_UUID(self):
admin = self._create_admin()
self.client.force_login(admin)
with self.subTest('Test for wrong identifier'):
response = self.client.get(
reverse(f'admin:{self.app_label}_user_change', args=['WRONG']),
follow=True,
)
html = (
'<li class="warning">User with ID “WRONG” '
+ 'doesn’t exist. Perhaps it was deleted?</li>'
)
self.assertEquals(response.status_code, 200)
self.assertContains(response, html)
with self.subTest('Test for non-existing user'):
id = uuid.uuid4()
response = self.client.get(
reverse(f'admin:{self.app_label}_user_change', args=[id],), follow=True
)
html = (
f'<li class="warning">User with ID “{id}” '
+ 'doesn’t exist. Perhaps it was deleted?</li>'
)
self.assertEquals(response.status_code, 200)
self.assertContains(response, html)

def test_organization_view_on_site(self):
admin = self._create_admin()
self.client.force_login(admin)
Expand Down

0 comments on commit a215ab5

Please sign in to comment.