From 07cba8bc7fbd1cff9172fe9894ab44428c33653b Mon Sep 17 00:00:00 2001 From: Ryan Westfall Date: Fri, 10 Jul 2026 12:53:17 -0500 Subject: [PATCH] Add unit tests ensuring /preview_email/ requires authentication. Locks in login_required behavior for issue #16 so unauthenticated access stays redirected. --- company_site/public/tests.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/company_site/public/tests.py b/company_site/public/tests.py index 0f8577a..e8368a6 100644 --- a/company_site/public/tests.py +++ b/company_site/public/tests.py @@ -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",