Author SHA1 Message Date
westfarn 07cba8bc7f Add unit tests ensuring /preview_email/ requires authentication.
CI / test (pull_request) Successful in 11s
Unit Tests / test (pull_request) Successful in 10s
Locks in login_required behavior for issue #16 so unauthenticated access stays redirected.
2026-07-10 12:53:17 -05:00
+29 -1
View File
@@ -1,12 +1,40 @@
from unittest.mock import patch
from django.contrib.auth.models import User
from django.test import Client, TestCase, override_settings
from django.urls import reverse
from .models import Contact
from .models import Contact, EmailMessage
from .seo import SERVICE_URL_NAMES, get_service_entries
class PreviewEmailAuthTests(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create_user(username="previewer", password="pass")
self.email = EmailMessage.objects.create(
subject="Preview subject",
body="Preview body content",
recipient="recipient@example.com",
)
self.url = reverse("preview_email", kwargs={"pk": self.email.pk})
def test_unauthenticated_user_is_redirected_to_login(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 302)
self.assertIn("/accounts/login/", response.url)
def test_authenticated_user_can_preview_email(self):
self.client.login(username="previewer", password="pass")
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Preview subject")
self.assertContains(response, "Preview body content")
@override_settings(
DEBUG=True,
EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend",