|
| 1 | +from django.core import mail |
| 2 | +import random |
| 3 | +import re |
| 4 | +import string |
| 5 | + |
| 6 | +from django.core.cache import cache |
| 7 | +from django.urls import reverse |
| 8 | +from allauth.account.models import EmailAddress |
| 9 | +from rest_framework import status |
| 10 | + |
| 11 | + |
| 12 | +def test_register_verification(client, db, django_user_model): |
| 13 | + creds = { |
| 14 | + 'username': 'ThomasCromwell', |
| 15 | + 'password1': 'annaregina', |
| 16 | + 'password2': 'annaregina', |
| 17 | + 'email': 'thomas@wolfhall.com' |
| 18 | + } |
| 19 | + response = client.post('/rest-auth/registration/', creds) |
| 20 | + assert response |
| 21 | + |
| 22 | + # Check if user was registered |
| 23 | + db_user = django_user_model.objects.get( |
| 24 | + username=creds['username']) |
| 25 | + assert db_user |
| 26 | + |
| 27 | + # Check if registration mail was sent |
| 28 | + box = mail.outbox |
| 29 | + assert len(box) == 1 |
| 30 | + verify_mail = box[0] |
| 31 | + assert verify_mail.to[0] == creds['email'] |
| 32 | + |
| 33 | + key = re.search(r'confirm-email\/(.+)\/', |
| 34 | + verify_mail.body).group(1) |
| 35 | + assert key |
| 36 | + |
| 37 | + # Check key information |
| 38 | + key_info = client.get(f'/rest-auth/infofromkey/{key}/') |
| 39 | + assert key_info.status_code == 200 |
| 40 | + assert key_info.json().get('username') == creds.get('username') |
| 41 | + |
| 42 | + # Check verification |
| 43 | + verify_response = client.post( |
| 44 | + '/rest-auth/registration/verify-email/', {'key': key}) |
| 45 | + assert verify_response.status_code == 200 |
| 46 | + |
| 47 | + # Check if email address was verified |
| 48 | + allauth_email = EmailAddress.objects.get(user=db_user) |
| 49 | + assert allauth_email.email == creds.get('email') |
| 50 | + assert allauth_email.verified is True |
| 51 | + assert allauth_email.primary is True |
0 commit comments