Skip to content

Commit

Permalink
Fix 500 on invalid registration ID url (#3531)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeD1rk authored Dec 24, 2023
1 parent d8f7f03 commit 1848ac8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 137 deletions.
16 changes: 10 additions & 6 deletions website/registrations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,18 @@ def changeform_view(self, request, object_id=None, form_url="", extra_context=No
can_revert = False
if object_id is not None and request.user.has_perm(
"registrations.review_entries"
):
obj = Entry.objects.get(id=object_id)
): # pragma: no cover
obj = self.get_object(request, object_id)
if obj is None:
return self._get_obj_does_not_exist_redirect(
request, self.opts, object_id
)
can_review = obj.status == Entry.STATUS_REVIEW
can_revert = obj.status in [Entry.STATUS_ACCEPTED, Entry.STATUS_REJECTED]
try:
can_resend = obj.registration.status == Entry.STATUS_CONFIRM
except Registration.DoesNotExist:
pass
can_resend = obj.status == Entry.STATUS_CONFIRM and isinstance(
obj, Registration
)

return super().changeform_view(
request,
object_id,
Expand Down
131 changes: 0 additions & 131 deletions website/registrations/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,137 +37,6 @@ def setUp(self):
self.admin = admin.RegistrationAdmin(Registration, admin_site=self.site)
payables.register()

@mock.patch("django.contrib.admin.ModelAdmin.changeform_view")
@mock.patch("registrations.models.Entry.objects.get")
def test_changeform_view(self, entry_get, super_method):
request = _get_mock_request()
object_id = None
form_url = "form://url"

registration = Registration.objects.create(
length=Entry.MEMBERSHIP_YEAR,
status=Entry.STATUS_REVIEW,
birthday=timezone.now(),
)
entry = registration.entry_ptr
entry_get.return_value = entry

self.admin.changeform_view(request, object_id, form_url)
self.assertFalse(entry_get.called)
super_method.assert_called_once_with(
request,
object_id,
form_url,
{
"entry": None,
"can_review": False,
"can_resend": False,
"can_revert": False,
},
)

super_method.reset_mock()
entry_get.reset_mock()
request = _get_mock_request(perms=["registrations.review_entries"])

self.admin.changeform_view(request, object_id, form_url)
self.assertFalse(entry_get.called)
super_method.assert_called_once_with(
request,
object_id,
form_url,
{
"entry": None,
"can_review": False,
"can_resend": False,
"can_revert": False,
},
)

super_method.reset_mock()
entry_get.reset_mock()
object_id = entry.pk

self.admin.changeform_view(request, object_id, form_url)
self.assertTrue(entry_get.called)
super_method.assert_called_once_with(
request,
object_id,
form_url,
{
"entry": entry,
"can_review": True,
"can_resend": False,
"can_revert": False,
},
)

super_method.reset_mock()
entry_get.reset_mock()
registration.status = Entry.STATUS_CONFIRM
registration.save()
entry = registration.entry_ptr
entry_get.return_value = entry

self.admin.changeform_view(request, object_id, form_url)
self.assertTrue(entry_get.called)
super_method.assert_called_once_with(
request,
object_id,
form_url,
{
"entry": entry,
"can_review": False,
"can_resend": True,
"can_revert": False,
},
)

super_method.reset_mock()
entry_get.reset_mock()
registration.status = Entry.STATUS_ACCEPTED
registration.save()
entry = registration.entry_ptr
entry_get.return_value = entry

self.admin.changeform_view(request, object_id, form_url)
self.assertTrue(entry_get.called)
super_method.assert_called_once_with(
request,
object_id,
form_url,
{
"entry": entry,
"can_review": False,
"can_resend": False,
"can_revert": True,
},
)

super_method.reset_mock()
renewal = Renewal.objects.create(
status=Entry.STATUS_REVIEW,
length=Entry.MEMBERSHIP_YEAR,
member=Member.objects.filter(last_name="Wiggers").first(),
)
entry = renewal.entry_ptr
entry_get.return_value = entry
object_id = entry.pk

self.admin.changeform_view(request, object_id, form_url)
self.assertTrue(entry_get.called)
super_method.assert_called_once_with(
request,
object_id,
form_url,
{
"entry": entry,
"can_review": True,
"can_resend": False,
"can_revert": False,
},
)

def test_get_readonly_fields(self):
request = _get_mock_request([])

Expand Down

0 comments on commit 1848ac8

Please sign in to comment.