-
Notifications
You must be signed in to change notification settings - Fork 7
/
tests.py
45 lines (37 loc) · 1.86 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from django.core import mail
from django.test import tag, TestCase
from django.urls import reverse
from .models import ContactMessage
@tag('contact', 'views')
class TestContactMessageCreateView(TestCase):
@classmethod
def setUpTestData(cls):
"""Set up data for all tests in this class."""
cls.contact_data = {'first_name': 'john',
'last_name': 'smith',
'email_address': '[email protected]',
'subject': 'Message Subject',
'message': 'This is the message.'}
def test_200_status(self):
"""Test that a GET request returns a 200 status."""
response = self.client.get(reverse("contact"))
self.assertEqual(response.status_code, 200)
def test_contact_message_object_creation(self):
"""Test that the form submission results in `ContactMessage` object creation."""
self.client.post(reverse("contact"), self.contact_data)
messages = ContactMessage.objects.all()
self.assertEqual(len(messages), 1)
self.assertEqual(messages[0].message, "This is the message.")
def test_send_email(self):
"""Test that the form submission results in an email being sent via send_email()."""
self.client.post(reverse("contact"), self.contact_data)
self.assertEqual(len(mail.outbox), 1)
expected_body = "Contact message received from John Smith ([email protected]).\n\n" \
"Subject: Message Subject\nMessage:\n\nThis is the message."
self.assertEqual(mail.outbox[0].body, expected_body)
@tag('contact', 'views')
class TestContactSuccessView(TestCase):
def test_200_status(self):
"""Test that a request returns a 200 status."""
response = self.client.get(reverse("contact_success"))
self.assertEqual(response.status_code, 200)