Self hosted FLOSS fitness/workout, nutrition and weight tracker
Test that the verification mails are sent and the link does work
Roland Geider4773f47860e3
diff --git a/wger/core/tests/test_registration.py b/wger/core/tests/test_registration.pyindex fe27835d4..a8032ecd8 100644--- a/wger/core/tests/test_registration.py+++ b/wger/core/tests/test_registration.py@@ -18,10 +18,13 @@ from io import StringIO# Djangofrom django.contrib.auth.models import User+from django.core import mailfrom django.core.management import call_command+from django.test import override_settingsfrom django.urls import reverse# Third Party+from allauth.account.models import EmailAddressfrom rest_framework.authtoken.models import Tokenfrom rest_framework.status import (HTTP_200_OK,@@ -179,6 +182,32 @@ class RegistrationTestCase(WgerTestCase):self.assertFalse(response.context['form'].is_valid())self.user_logout()+ @override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')+ def test_registration_sends_verification_email(self):+ """+ Tests that a verification email is sent when a new user registers+ """++ registration_data = {+ 'username': 'newuser',+ 'password1': 'quai8fai7Zae',+ 'password2': 'quai8fai7Zae',+ 'email': '[email protected]',+ 'g-recaptcha-response': 'PASSED',+ }+ response = self.client.post(reverse('core:user:registration'), registration_data)+ self.assertEqual(response.status_code, 302)++ # A verification email was sent+ self.assertEqual(len(mail.outbox), 1)+ self.assertIn('[email protected]', mail.outbox[0].to)++ # An EmailAddress entry was created with verified=False+ user = User.objects.get(username='newuser')+ email_obj = EmailAddress.objects.get(user=user)+ self.assertEqual(email_obj.email, '[email protected]')+ self.assertFalse(email_obj.verified)+def test_registration_deactivated(self):"""Test that with deactivated registration no users can register@@ -231,6 +260,26 @@ class ApiRegistrationTestCase(WgerTestCase):self.assertEqual(response.data['token'], token.key)self.assertEqual(count_after, count_before + 1)+ @override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')+ def test_api_registration_sends_verification_email(self):+ """Tests that a verification email is sent when registering via the API"""++ response = self.client.post(+ reverse('api_register'),+ {'username': 'restapi_email', 'email': '[email protected]', 'password': 'AekaiLe0ga'},+ )+ self.assertEqual(response.status_code, HTTP_201_CREATED)++ # A verification email was sent+ self.assertEqual(len(mail.outbox), 1)+ self.assertIn('[email protected]', mail.outbox[0].to)++ # An EmailAddress entry was created with verified=False+ user = User.objects.get(username='restapi_email')+ email_obj = EmailAddress.objects.get(user=user)+ self.assertEqual(email_obj.email, '[email protected]')+ self.assertFalse(email_obj.verified)+def test_post_valid_api_user_creation_no_email(self):"""Successfully register a user via the REST API without providing an email"""diff --git a/wger/core/tests/test_verification.py b/wger/core/tests/test_verification.pynew file mode 100644index 000000000..bb66423f0--- /dev/null+++ b/wger/core/tests/test_verification.py@@ -0,0 +1,189 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# Standard Library+import logging+import re++# Django+from django.contrib.auth.models import User+from django.core import mail+from django.test import override_settings+from django.urls import reverse++# Third Party+from allauth.account.models import EmailAddress+from rest_framework.status import HTTP_200_OK++# wger+from wger.core.tests.base_testcase import WgerTestCase+++logger = logging.getLogger(__name__)+++@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')+class EmailVerificationFromPreferencesTestCase(WgerTestCase):+ """+ Tests that clicking the verification link in the preferences sidebar+ sends a verification email via allauth.+ """++ def test_confirm_email_sends_mail_for_unverified_user(self):+ """+ An unverified user clicking confirm-email receives a verification email+ """++ # User 1 (admin) has verified=False in the fixtures+ self.user_login('admin')++ mail.outbox = []+ response = self.client.get(reverse('core:user:confirm-email'))++ # Redirects to dashboard+ self.assertEqual(response.status_code, 302)+ self.assertIn(reverse('core:dashboard'), response.url)++ # A verification email was sent+ self.assertEqual(len(mail.outbox), 1)+ self.assertIn('[email protected]', mail.outbox[0].to)++ def test_confirm_email_link_verifies_email(self):+ """+ Following the confirmation link from the email marks the address as verified+ """++ # User 1 (admin) has verified=False in the fixtures+ self.user_login('admin')++ mail.outbox = []+ self.client.get(reverse('core:user:confirm-email'))+ self.assertEqual(len(mail.outbox), 1)++ # Extract the confirmation key from the email body+ email_body = mail.outbox[0].body+ match = re.search(r'/email/confirm-email/([^/\s]+)/', email_body)+ self.assertIsNotNone(match, 'Could not find confirmation link in email body')+ key = match.group(1)++ # Visit the confirmation URL (GET shows the page, POST confirms)+ confirm_url = reverse('account_confirm_email', args=[key])+ self.client.get(confirm_url)++ # The email should now be verified+ user = User.objects.get(username='admin')+ email_obj = EmailAddress.objects.get_for_user(user, user.email)+ self.assertTrue(email_obj.verified)++ def test_confirm_email_no_mail_for_verified_user(self):+ """+ A verified user clicking confirm-email does not receive a new email+ """++ # User 4 (trainer1) has verified=True in the fixtures+ self.user_login('trainer1')++ mail.outbox = []+ response = self.client.get(reverse('core:user:confirm-email'))++ # Redirects to dashboard+ self.assertEqual(response.status_code, 302)++ # No email was sent+ self.assertEqual(len(mail.outbox), 0)++ def test_confirm_email_requires_login(self):+ """+ Anonymous users are redirected to the login page+ """++ self.user_logout()+ response = self.client.get(reverse('core:user:confirm-email'))+ self.assertEqual(response.status_code, 302)+ self.assertIn('/login', response.url)+++@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')+class EmailVerificationFromAPITestCase(WgerTestCase):+ """+ Tests that requesting email verification via the API sends a+ verification email via allauth.+ """++ def test_verify_email_sends_mail_for_unverified_user(self):+ """+ POST to verify-email endpoint sends a verification email for unverified users+ """++ # User 1 (admin) has verified=False in the fixtures+ self.user_login('admin')++ mail.outbox = []+ response = self.client.get(+ reverse('userprofile-verify-email'),+ )++ self.assertEqual(response.status_code, HTTP_200_OK)+ self.assertEqual(response.data['status'], 'sent')++ # A verification email was sent+ self.assertEqual(len(mail.outbox), 1)+ self.assertIn('[email protected]', mail.outbox[0].to)++ def test_verify_email_link_verifies_email(self):+ """+ Following the confirmation link from the API-triggered email marks the address as verified+ """++ # User 1 (admin) has verified=False in the fixtures+ self.user_login('admin')++ mail.outbox = []+ response = self.client.get(reverse('userprofile-verify-email'))+ self.assertEqual(response.data['status'], 'sent')+ self.assertEqual(len(mail.outbox), 1)++ # Extract the confirmation key from the email body+ email_body = mail.outbox[0].body+ match = re.search(r'/email/confirm-email/([^/\s]+)/', email_body)+ self.assertIsNotNone(match, 'Could not find confirmation link in email body')+ key = match.group(1)++ # Visit the confirmation URL (POST confirms)+ confirm_url = reverse('account_confirm_email', args=[key])+ self.client.get(confirm_url)++ # The email should now be verified+ user = User.objects.get(username='admin')+ email_obj = EmailAddress.objects.get_for_user(user, user.email)+ self.assertTrue(email_obj.verified)++ def test_verify_email_no_mail_for_verified_user(self):+ """+ POST to verify-email endpoint does not send email for already verified users+ """++ # User 4 (trainer1) has verified=True in the fixtures+ self.user_login('trainer1')++ mail.outbox = []+ response = self.client.get(+ reverse('userprofile-verify-email'),+ )++ self.assertEqual(response.status_code, HTTP_200_OK)+ self.assertEqual(response.data['status'], 'verified')++ # No email was sent+ self.assertEqual(len(mail.outbox), 0)