Skip to content

Commit

Permalink
Add tests of email template context variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
ubernostrum committed Jul 11, 2023
1 parent d0ee9e6 commit 0a51a0b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
67 changes: 55 additions & 12 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
Base classes for other test cases to inherit from.
"""
import json
from contextlib import contextmanager

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.http import HttpRequest
from django.test import TestCase, override_settings
from django.test import TestCase, modify_settings, override_settings
from django.urls import reverse

from django_registration import signals
Expand Down Expand Up @@ -241,9 +243,27 @@ def test_registration(self):
# New user must not be active.
assert not new_user.is_active

@override_settings(ACCOUNT_ACTIVATION_DAYS=7)
def test_registration_email(self):
"""
The activation email is sent and contains the expected template variables.
"""
super().test_registration()
user_model = get_user_model()
new_user = user_model.objects.get(**self.user_lookup_kwargs)

# An activation email was sent.
assert len(mail.outbox) == 1
assert "http" in mail.outbox[0].subject
message = mail.outbox[0]

subject_json, body_json = json.loads(message.subject), json.loads(message.body)
assert subject_json == body_json

assert len(body_json["activation_key"]) > 1
assert body_json["expiration_days"] == settings.ACCOUNT_ACTIVATION_DAYS
assert len(body_json["site"]) > 1
assert body_json["user"] == new_user.username

def test_registration_failure(self):
"""
Expand All @@ -256,23 +276,46 @@ def test_registration_failure(self):
# Activation email was not sent.
assert 0 == len(mail.outbox)

@modify_settings(INSTALLED_APPS={"remove": ["django.contrib.sites"]})
def test_registration_no_sites(self):
"""
Registration still functions properly when
``django.contrib.sites`` is not installed; the fallback will
be a ``RequestSite`` instance.
"""
with self.modify_settings(INSTALLED_APPS={"remove": ["django.contrib.sites"]}):
with self.assertSignalSent(signals.user_registered):
resp = self.client.post(
reverse("django_registration_register"), data=self.valid_data
)
with self.assertSignalSent(signals.user_registered):
resp = self.client.post(
reverse("django_registration_register"), data=self.valid_data
)

assert 302 == resp.status_code

assert 302 == resp.status_code
user_model = get_user_model()
new_user = user_model.objects.get(**self.user_lookup_kwargs)

assert new_user.check_password(self.valid_data["password1"])
assert new_user.email == self.valid_data["email"]

@override_settings(ACCOUNT_ACTIVATION_DAYS=7)
@modify_settings(INSTALLED_APPS={"remove": ["django.contrib.sites"]})
def test_registration_email_no_sites(self):
"""
The activation email is sent and contains the expected template variables.
"""
super().test_registration()
user_model = get_user_model()
new_user = user_model.objects.get(**self.user_lookup_kwargs)

# An activation email was sent.
assert len(mail.outbox) == 1
message = mail.outbox[0]

user_model = get_user_model()
new_user = user_model.objects.get(**self.user_lookup_kwargs)
subject_json, body_json = json.loads(message.subject), json.loads(message.body)
assert subject_json == body_json

assert new_user.check_password(self.valid_data["password1"])
assert new_user.email == self.valid_data["email"]
assert len(body_json["activation_key"]) > 1
assert body_json["expiration_days"] == settings.ACCOUNT_ACTIVATION_DAYS
assert len(body_json["site"]) > 1
assert body_json["user"] == new_user.username
7 changes: 7 additions & 0 deletions tests/templates/django_registration/activation_email_body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"activation_key": "{{ activation_key }}",
"expiration_days": {{ expiration_days }},
"scheme": "{{ scheme }}",
"site": "{{ site }}",
"user": "{{ user.username }}"
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ scheme }}
{"activation_key": "{{ activation_key }}", "expiration_days": {{ expiration_days }}, "scheme": "{{ scheme }}", "site": "{{ site }}", "user": "{{ user.username }}"}

0 comments on commit 0a51a0b

Please sign in to comment.