|
24 | 24 | from unittest import skipIf |
25 | 25 | from urllib.parse import urlsplit |
26 | 26 |
|
| 27 | +import django.core.signing |
27 | 28 | from django.urls import reverse as urlreverse |
28 | 29 | from django.contrib.auth.models import User |
29 | 30 | from django.conf import settings |
30 | | -from django.template.loader import render_to_string |
| 31 | +from django.template.loader import render_to_string |
31 | 32 | from django.utils import timezone |
32 | 33 |
|
33 | 34 | import debug # pyflakes:ignore |
@@ -937,6 +938,72 @@ def test_edit_person_extresources(self): |
937 | 938 | self.assertEqual(person.personextresource_set.get(name__slug='github_repo').display_name, 'Some display text') |
938 | 939 | self.assertIn(person.personextresource_set.first().name.slug, str(person.personextresource_set.first())) |
939 | 940 |
|
| 941 | + def test_confirm_new_email(self): |
| 942 | + person = PersonFactory() |
| 943 | + valid_auth = django.core.signing.dumps( |
| 944 | + [person.user.username, "new_email@example.com"], salt="add_email" |
| 945 | + ) |
| 946 | + invalid_auth = django.core.signing.dumps( |
| 947 | + [person.user.username, "not_this_one@example.com"], salt="pepper" |
| 948 | + ) |
| 949 | + |
| 950 | + # Test that we check the salt |
| 951 | + r = self.client.get( |
| 952 | + urlreverse("ietf.ietfauth.views.confirm_new_email", kwargs={"auth": invalid_auth}) |
| 953 | + ) |
| 954 | + self.assertEqual(r.status_code, 404) |
| 955 | + r = self.client.post( |
| 956 | + urlreverse("ietf.ietfauth.views.confirm_new_email", kwargs={"auth": invalid_auth}) |
| 957 | + ) |
| 958 | + self.assertEqual(r.status_code, 404) |
| 959 | + |
| 960 | + # Now check that the valid auth works |
| 961 | + self.assertFalse( |
| 962 | + person.email_set.filter(address__icontains="new_email@example.com").exists() |
| 963 | + ) |
| 964 | + confirm_url = urlreverse( |
| 965 | + "ietf.ietfauth.views.confirm_new_email", kwargs={"auth": valid_auth} |
| 966 | + ) |
| 967 | + r = self.client.get(confirm_url) |
| 968 | + self.assertContains(r, urllib.parse.quote(confirm_url), status_code=200) |
| 969 | + r = self.client.post(confirm_url, data={"action": "confirm"}) |
| 970 | + self.assertContains(r, "has been updated", status_code=200) |
| 971 | + self.assertTrue( |
| 972 | + person.email_set.filter(address__icontains="new_email@example.com").exists() |
| 973 | + ) |
| 974 | + |
| 975 | + # Authorizing a second time should be handled gracefully |
| 976 | + r = self.client.post(confirm_url, data={"action": "confirm"}) |
| 977 | + self.assertContains(r, "already includes", status_code=200) |
| 978 | + |
| 979 | + # Another person should not be able to add the same address and should be told so, |
| 980 | + # whether they use the same or different letter case |
| 981 | + other_person = PersonFactory() |
| 982 | + other_auth = django.core.signing.dumps( |
| 983 | + [other_person.user.username, "new_email@example.com"], salt="add_email" |
| 984 | + ) |
| 985 | + r = self.client.post( |
| 986 | + urlreverse("ietf.ietfauth.views.confirm_new_email", kwargs={"auth": other_auth}), |
| 987 | + data={"action": "confirm"}, |
| 988 | + ) |
| 989 | + self.assertContains(r, "in use by another user", status_code=200) |
| 990 | + |
| 991 | + other_auth = django.core.signing.dumps( |
| 992 | + [other_person.user.username, "NeW_eMaIl@eXaMpLe.CoM"], salt="add_email" |
| 993 | + ) |
| 994 | + r = self.client.post( |
| 995 | + urlreverse("ietf.ietfauth.views.confirm_new_email", kwargs={"auth": other_auth}), |
| 996 | + data={"action": "confirm"}, |
| 997 | + ) |
| 998 | + |
| 999 | + self.assertContains(r, "in use by another user", status_code=200) |
| 1000 | + self.assertFalse( |
| 1001 | + other_person.email_set.filter(address__icontains="new_email@example.com").exists() |
| 1002 | + ) |
| 1003 | + self.assertTrue( |
| 1004 | + person.email_set.filter(address__icontains="new_email@example.com").exists() |
| 1005 | + ) |
| 1006 | + |
940 | 1007 |
|
941 | 1008 | class OpenIDConnectTests(TestCase): |
942 | 1009 | def request_matcher(self, request): |
|
0 commit comments