2015-10-13 22:31:20 +02:00
|
|
|
"""
|
|
|
|
Tests for :py:mod:`contact_form.forms`.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
from django.contrib.sites.models import Site
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.urls import reverse
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
from contact_form.forms import ContactForm
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_DATA = {"name": "Test User", "email": "test@example.org", "body": "Test message"}
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ContactFormTest(TestCase):
|
|
|
|
def test_constructor_needs_request(self):
|
|
|
|
with self.assertRaises(KeyError):
|
2015-12-05 14:47:41 +01:00
|
|
|
ContactForm()
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
request = MagicMock()
|
|
|
|
form = ContactForm(request=request)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "request"))
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(form.request, request)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(form.helper.form_action, reverse("contact_form"))
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(len(form.helper.inputs), 1)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_constructor_fields(self):
|
|
|
|
request = MagicMock()
|
|
|
|
form = ContactForm(request=request)
|
|
|
|
self.assertEqual(len(form.fields), 3)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("email", form.fields)
|
|
|
|
self.assertIn("name", form.fields)
|
|
|
|
self.assertIn("body", form.fields)
|
2015-10-13 22:31:20 +02:00
|
|
|
self.assertEqual(len(form.data), 0)
|
|
|
|
|
|
|
|
def test_get_context_invalid(self):
|
|
|
|
request = MagicMock()
|
|
|
|
form = ContactForm(request=request)
|
|
|
|
with self.assertRaisesMessage(
|
2019-01-30 21:27:25 +01:00
|
|
|
ValueError, "Cannot generate context from invalid contact form"
|
|
|
|
):
|
2015-10-13 22:31:20 +02:00
|
|
|
form.get_context()
|
|
|
|
|
|
|
|
def test_get_context_valid_site_installed(self):
|
|
|
|
request = MagicMock()
|
|
|
|
form = ContactForm(request=request, data=TEST_DATA)
|
|
|
|
context = form.get_context()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("site", context)
|
|
|
|
self.assertIn("name", context)
|
|
|
|
self.assertIn("email", context)
|
|
|
|
self.assertIn("body", context)
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_get_context_valid_site_not_installed(self):
|
|
|
|
request = MagicMock()
|
|
|
|
form = ContactForm(request=request, data=TEST_DATA)
|
2019-01-30 21:27:25 +01:00
|
|
|
with patch("contact_form.forms.Site") as sitemock:
|
2015-10-13 22:31:20 +02:00
|
|
|
sitemock._meta.installed = False
|
|
|
|
context = form.get_context()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("site", context)
|
|
|
|
self.assertIn("name", context)
|
|
|
|
self.assertIn("email", context)
|
|
|
|
self.assertIn("body", context)
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_message(self):
|
|
|
|
request = Mock()
|
2019-01-30 21:27:25 +01:00
|
|
|
request.META = {"REMOTE_ADDR": "127.0.0.1"}
|
2015-10-13 22:31:20 +02:00
|
|
|
form = ContactForm(request=request, data=TEST_DATA)
|
|
|
|
message = form.message()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn(TEST_DATA["name"], message)
|
|
|
|
self.assertIn(TEST_DATA["email"], message)
|
|
|
|
self.assertIn(TEST_DATA["body"], message)
|
|
|
|
self.assertIn("127.0.0.1", message)
|
2015-10-13 22:31:20 +02:00
|
|
|
|
|
|
|
def test_subject(self):
|
|
|
|
request = Mock()
|
|
|
|
form = ContactForm(request=request, data=TEST_DATA)
|
|
|
|
subject = form.subject()
|
|
|
|
self.assertIn(Site.objects.get_current().name, subject)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn(TEST_DATA["name"], subject)
|