-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue with creating missing templates through the admin
Also expanded admin tests, added WebTest and fixed some Django settings not being accessed through the settings wrapper
- Loading branch information
Bart van der Schoor
committed
Feb 16, 2024
1 parent
bd067ff
commit f9e9f21
Showing
9 changed files
with
99 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
'pytest-pep8', | ||
'pytest-pylint', | ||
'pytest-pythonpath', | ||
'django-webtest', | ||
], | ||
|
||
# metadata | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
from django.test import TestCase, override_settings | ||
from django.urls import reverse | ||
from django.utils.translation import gettext_lazy as _ | ||
from django_webtest import WebTest | ||
|
||
from mail_editor.helpers import find_template | ||
|
||
from mail_editor.models import MailTemplate | ||
|
||
try: | ||
from unittest.mock import patch | ||
|
@@ -26,7 +27,7 @@ | |
|
||
|
||
@override_settings(MAIL_EDITOR_CONF=CONFIG) | ||
class AdminPreviewTestCase(TestCase): | ||
class AdminTestCase(WebTest): | ||
def setUp(self): | ||
site_patch = patch("mail_editor.helpers.get_current_site") | ||
current_site_mock = site_patch.start() | ||
|
@@ -41,27 +42,34 @@ def tearDown(self): | |
def test_changelist_view(self): | ||
template = find_template("test_template") | ||
|
||
url = reverse('admin:{}_{}_changelist'.format(template._meta.app_label, template._meta.model_name)) | ||
url = reverse('admin:mail_editor_mailtemplate_changelist') | ||
|
||
response = self.app.get(url, user=self.super_user) | ||
|
||
# test search isn't broken | ||
form = response.forms["changelist-search"] | ||
form["q"] = "test" | ||
response = form.submit() | ||
self.assertEqual([template], list(response.context["cl"].queryset.all())) | ||
|
||
self.client.force_login(self.super_user) | ||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, 200) | ||
form = response.forms["changelist-search"] | ||
form["q"] = "not_found" | ||
response = form.submit() | ||
self.assertEqual([], list(response.context["cl"].queryset.all())) | ||
|
||
def test_changelist_view__reset_template_action(self): | ||
template = find_template("test_template") | ||
template.body = "something else" | ||
template.subject = "something else" | ||
template.save() | ||
|
||
url = reverse('admin:{}_{}_changelist'.format(template._meta.app_label, template._meta.model_name)) | ||
url = reverse('admin:mail_editor_mailtemplate_changelist') | ||
|
||
self.client.force_login(self.super_user) | ||
data = { | ||
'action': 'reload_templates', | ||
'_selected_action': [str(template.pk)], | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, 302) | ||
response = self.app.get(url, user=self.super_user) | ||
form = response.forms["changelist-form"] | ||
form['action'] = 'reload_templates' | ||
form['_selected_action'] = [str(template.pk)] | ||
response = form.submit().follow() | ||
|
||
template.refresh_from_db() | ||
self.assertIn(str(_("Test mail sent from testcase with {{ id }}")), template.body, ) | ||
|
@@ -70,34 +78,77 @@ def test_changelist_view__reset_template_action(self): | |
def test_change_view(self): | ||
template = find_template("test_template") | ||
|
||
url = reverse('admin:{}_{}_change'.format(template._meta.app_label, template._meta.model_name), args=[template.id]) | ||
url = reverse('admin:mail_editor_mailtemplate_change', args=[template.id]) | ||
|
||
response = self.app.get(url, user=self.super_user) | ||
form = response.forms["mailtemplate_form"] | ||
form["subject"] = "mail subject" | ||
form.submit() | ||
template.refresh_from_db() | ||
self.assertEqual(template.subject, "mail subject") | ||
|
||
def test_add_view(self): | ||
url = reverse('admin:mail_editor_mailtemplate_add') | ||
|
||
response = self.app.get(url, user=self.super_user) | ||
form = response.forms["mailtemplate_form"] | ||
form["template_type"] = "test_template" | ||
form["subject"] = "mail subject" | ||
form["body"] = "mail body" | ||
response = form.submit().follow() | ||
|
||
template = MailTemplate.objects.get() | ||
self.assertEqual(template.template_type, "test_template") | ||
self.assertEqual(template.subject, "mail subject") | ||
self.assertEqual(template.body, "mail body") | ||
|
||
def test_add_view__handle_duplicates(self): | ||
template = find_template("test_template") | ||
|
||
self.client.force_login(self.super_user) | ||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, 200) | ||
url = reverse('admin:mail_editor_mailtemplate_add') | ||
|
||
with self.subTest("unique language templates"): | ||
with override_settings(MAIL_EDITOR_UNIQUE_LANGUAGE_TEMPLATES=True): | ||
response = self.app.get(url, user=self.super_user) | ||
form = response.forms["mailtemplate_form"] | ||
form["template_type"] = "test_template" | ||
form["subject"] = "mail subject" | ||
form["body"] = "mail body" | ||
response = form.submit(status=200) | ||
self.assertEqual(str(response.context["errors"][0][0]), _('Mail template with this type and language already exists')) | ||
|
||
self.assertEqual(1, MailTemplate.objects.filter(template_type="test_template").count()) | ||
|
||
with self.subTest("not-unique language templates"): | ||
with override_settings(MAIL_EDITOR_UNIQUE_LANGUAGE_TEMPLATES=False): | ||
response = self.app.get(url, user=self.super_user) | ||
form = response.forms["mailtemplate_form"] | ||
form["template_type"] = "test_template" | ||
form["subject"] = "mail subject" | ||
form["body"] = "mail body" | ||
response = form.submit().follow() | ||
self.assertEqual(2, MailTemplate.objects.filter(template_type="test_template").count()) | ||
|
||
def test_variable_view(self): | ||
template = find_template("test_template") | ||
|
||
url = reverse('admin:mailtemplate_variables', args=[template.template_type]) | ||
|
||
self.client.force_login(self.super_user) | ||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, 200) | ||
response = self.app.get(url, user=self.super_user) | ||
|
||
def test_preview_view(self): | ||
template = find_template("test_template") | ||
|
||
url = reverse('admin:mailtemplate_preview', args=[template.id]) | ||
|
||
self.client.force_login(self.super_user) | ||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, 200) | ||
response = self.app.get(url, user=self.super_user) | ||
|
||
self.assertContains(response, _("Important message for --id--")) | ||
|
||
# test sending the email | ||
self.client.post(url, {"recipient": "[email protected]"}) | ||
# send the mail | ||
form = response.forms['mailtemplate_form'] | ||
form["recipient"] = "[email protected]" | ||
response = form.submit() | ||
|
||
self.assertEqual(len(mail.outbox), 1) | ||
message = mail.outbox[0] | ||
|
@@ -109,8 +160,6 @@ def test_render_view(self): | |
|
||
url = reverse('admin:mailtemplate_render', args=[template.id]) | ||
|
||
self.client.force_login(self.super_user) | ||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, 200) | ||
response = self.app.get(url, user=self.super_user) | ||
|
||
self.assertContains(response, _("Test mail sent from testcase with --id--")) | ||
self.assertIn(str(_("Test mail sent from testcase with --id--")), response.text) |