2015-10-13 00:07:25 +02:00
|
|
|
"""
|
|
|
|
Tests for :py:mod:`contact_form.views`.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2015-10-13 22:31:20 +02:00
|
|
|
from django.core import mail
|
2015-10-13 00:07:25 +02:00
|
|
|
from django.test import TestCase
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.urls import reverse
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER = "test"
|
|
|
|
TEST_PASSWORD = "secret"
|
|
|
|
TEST_EMAIL = "test@example.org"
|
|
|
|
TEST_NAME = "Example Tester".split()
|
|
|
|
TEST_MESSAGE = """
|
2015-10-13 22:31:20 +02:00
|
|
|
This is a really unimportant test message.
|
2019-01-30 21:27:25 +01:00
|
|
|
"""
|
2015-10-13 22:31:20 +02:00
|
|
|
|
2015-10-13 00:07:25 +02:00
|
|
|
|
|
|
|
class ContactFormViewTest(TestCase):
|
2015-10-13 22:31:20 +02:00
|
|
|
def _setup_user(self, **kwargs):
|
|
|
|
return User.objects.create_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD, **kwargs
|
|
|
|
)
|
2015-10-13 22:31:20 +02:00
|
|
|
|
2015-10-13 00:07:25 +02:00
|
|
|
def test_get_contact_form_template(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_form"))
|
|
|
|
self.assertTemplateUsed(response, "contact_form/contact_form.html")
|
2015-10-13 00:07:25 +02:00
|
|
|
|
|
|
|
def test_get_contact_form_anonymous_status(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_form"))
|
2015-10-13 00:07:25 +02:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_contact_form_anonymous_has_empty_form(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_form"))
|
|
|
|
self.assertIn("form", response.context)
|
|
|
|
form = response.context["form"]
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(len(form.initial), 0)
|
2015-10-13 00:07:25 +02:00
|
|
|
|
|
|
|
def test_get_contact_form_fields_anonymous(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_form"))
|
|
|
|
for name in ("name", "email", "body"):
|
|
|
|
self.assertIn(name, response.context["form"].fields)
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_post_empty_form_template(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.post(reverse("contact_form"), {})
|
|
|
|
self.assertTemplateUsed(response, "contact_form/contact_form.html")
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_post_empty_form_status(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.post(reverse("contact_form"), {})
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_post_empty_form_validation_errors(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.post(reverse("contact_form"), {})
|
|
|
|
self.assertIn("form", response.context)
|
|
|
|
form = response.context["form"]
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertFalse(form.is_valid())
|
|
|
|
self.assertEqual(len(form.errors), 3)
|
|
|
|
|
|
|
|
def test_post_empty_form_no_mail(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self.client.post(reverse("contact_form"), {})
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
|
|
|
def test_get_contact_form_logged_in_no_fullname_initial(self):
|
2015-12-05 14:47:41 +01:00
|
|
|
self._setup_user()
|
2015-10-13 22:31:20 +02:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_form"))
|
|
|
|
self.assertIn("form", response.context)
|
|
|
|
form = response.context["form"]
|
|
|
|
self.assertEqual(form.initial, {"name": TEST_USER, "email": TEST_EMAIL})
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_get_contact_form_logged_in_fullname_initial(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self._setup_user(first_name=TEST_NAME[0], last_name=TEST_NAME[1])
|
2015-10-13 22:31:20 +02:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_form"))
|
|
|
|
self.assertIn("form", response.context)
|
|
|
|
form = response.context["form"]
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(
|
2019-01-30 21:27:25 +01:00
|
|
|
form.initial, {"name": " ".join(TEST_NAME), "email": TEST_EMAIL}
|
|
|
|
)
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_post_filled_form_anonymous_redirects(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("contact_form"),
|
|
|
|
{"name": TEST_USER, "email": TEST_EMAIL, "body": TEST_MESSAGE},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, reverse("contact_success"))
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_post_filled_form_anonymous_mail(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self.client.post(
|
|
|
|
reverse("contact_form"),
|
|
|
|
{"name": TEST_USER, "email": TEST_EMAIL, "body": TEST_MESSAGE},
|
|
|
|
)
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
|
|
|
|
def test_post_filled_form_logged_in_redirects(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self._setup_user(first_name=TEST_NAME[0], last_name=TEST_NAME[1])
|
2015-10-13 22:31:20 +02:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("contact_form"),
|
|
|
|
{"name": " ".join(TEST_NAME), "email": TEST_EMAIL, "body": TEST_MESSAGE},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, reverse("contact_success"))
|
2015-10-13 22:31:20 +02:00
|
|
|
|
2015-12-05 14:47:41 +01:00
|
|
|
def test_post_filled_form_logged_in_mail(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self._setup_user(first_name=TEST_NAME[0], last_name=TEST_NAME[1])
|
2015-10-13 22:31:20 +02:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.client.post(
|
|
|
|
reverse("contact_form"),
|
|
|
|
{"name": " ".join(TEST_NAME), "email": TEST_EMAIL, "body": TEST_MESSAGE},
|
|
|
|
)
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
|
|
|
|
|
|
|
|
class ContactSuccessViewTest(TestCase):
|
|
|
|
def test_get_template(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_success"))
|
|
|
|
self.assertTemplateUsed(response, "contact_form/contact_success.html")
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_get_status(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.get(reverse("contact_success"))
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(response.status_code, 200)
|