Commit 77d9b01c authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Merge branch 'fix/registrations-confirm-email-doesnotexist' into 'master'

Crash on confirming non-existing registration entry

Closes #665

See merge request !867
parents ccd627fe f5865c99
from unittest import mock
from unittest.mock import MagicMock, Mock
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.admin.utils import model_ngettext from django.contrib.admin.utils import model_ngettext
...@@ -14,6 +11,8 @@ from django.test import Client, TestCase ...@@ -14,6 +11,8 @@ from django.test import Client, TestCase
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from unittest import mock
from unittest.mock import MagicMock, Mock
from members.models import Membership from members.models import Membership
from registrations import views from registrations import views
...@@ -270,6 +269,7 @@ class ConfirmEmailViewTest(TestCase): ...@@ -270,6 +269,7 @@ class ConfirmEmailViewTest(TestCase):
) )
def setUp(self): def setUp(self):
self.client = Client()
self.view = views.ConfirmEmailView() self.view = views.ConfirmEmailView()
@mock.patch('registrations.services.confirm_entry') @mock.patch('registrations.services.confirm_entry')
...@@ -281,39 +281,60 @@ class ConfirmEmailViewTest(TestCase): ...@@ -281,39 +281,60 @@ class ConfirmEmailViewTest(TestCase):
qs_mock.return_value = entry_qs qs_mock.return_value = entry_qs
qs_mock.get = Mock(return_value=entry_qs.get()) qs_mock.get = Mock(return_value=entry_qs.get())
request = _get_mock_request() with self.subTest('Successful email confirmation'):
self.view.request = request response = self.client.get(reverse(
'registrations:confirm-email', args=(self.entry.pk,)))
response = self.view.get(request, pk=self.entry.pk) self.assertEqual(response.status_code, 200)
self.assertEqual(
response.template_name,
['registrations/confirm_email.html']
)
self.assertEqual(response.status_code, 200) confirm_entry.assert_called_once_with(entry_qs)
self.assertEqual( board_mail.assert_called_once_with(entry_qs.get())
response.template_name,
['registrations/confirm_email.html']
)
confirm_entry.assert_called_once_with(entry_qs) with self.subTest('Redirect when nothing was processed'):
board_mail.assert_called_once_with(entry_qs.get()) confirm_entry.return_value = 0
confirm_entry.return_value = None response = self.client.get(reverse(
'registrations:confirm-email', args=(self.entry.pk,)))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/registration/register/')
response = self.view.get(request, pk=self.entry.pk) with self.subTest('Redirect when registration does not exist'):
self.assertEqual(response.status_code, 302) confirm_entry.side_effect = ValidationError(message='Error')
self.assertEqual(response.url, '/registration/register/') board_mail.side_effect = Registration.DoesNotExist
confirm_entry.side_effect = ValidationError(message='Error') response = self.client.get(reverse(
board_mail.side_effect = Registration.DoesNotExist 'registrations:confirm-email', args=(self.entry.pk,)))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/registration/register/')
response = self.view.get(request, pk=self.entry.pk) with self.subTest('Redirect when entry does not exist'):
self.assertEqual(response.status_code, 302) confirm_entry.return_value = 0
self.assertEqual(response.url, '/registration/register/') qs_mock.get.side_effect = Entry.DoesNotExist
def test_get_no_mocks(self): response = self.client.get(reverse(
request = _get_mock_request() 'registrations:confirm-email',
self.view.request = request args=('00000000-0000-0000-0000-000000000000',)
))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/registration/register/')
with self.subTest('Redirect when no entries were processed'):
confirm_entry.return_value = 0
response = self.view.get(request, pk=self.entry.pk) response = self.client.get(reverse(
'registrations:confirm-email',
args=('00000000-0000-0000-0000-000000000000',)
))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/registration/register/')
def test_get_no_mocks(self):
response = self.client.get(reverse(
'registrations:confirm-email', args=(self.entry.pk,)))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
......
...@@ -90,20 +90,20 @@ class ConfirmEmailView(View, TemplateResponseMixin): ...@@ -90,20 +90,20 @@ class ConfirmEmailView(View, TemplateResponseMixin):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
entry = Entry.objects.filter(pk=kwargs['pk']) entry = Entry.objects.filter(pk=kwargs['pk'])
processed = None processed = 0
try: try:
processed = services.confirm_entry(entry) processed = services.confirm_entry(entry)
except ValidationError: except ValidationError:
pass pass
if processed == 0:
return redirect('registrations:register')
try: try:
emails.send_new_registration_board_message(entry.get()) emails.send_new_registration_board_message(entry.get())
except Registration.DoesNotExist: except Entry.DoesNotExist:
pass pass
if processed is None:
return redirect('registrations:register')
return self.render_to_response({}) return self.render_to_response({})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment