improve contact_form test coverage
This commit is contained in:
parent
e51d202abd
commit
2de53757df
3 changed files with 192 additions and 2 deletions
|
@ -12,8 +12,8 @@ from django.template import RequestContext
|
||||||
from django.template import loader
|
from django.template import loader
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from django.contrib.sites.models import RequestSite
|
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
|
from django.contrib.sites.requests import RequestSite
|
||||||
|
|
||||||
from crispy_forms.helper import FormHelper
|
from crispy_forms.helper import FormHelper
|
||||||
from crispy_forms.layout import Submit
|
from crispy_forms.layout import Submit
|
||||||
|
|
92
gnuviechadmin/contact_form/tests/test_forms.py
Normal file
92
gnuviechadmin/contact_form/tests/test_forms.py
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
"""
|
||||||
|
Tests for :py:mod:`contact_form.forms`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
|
import mock
|
||||||
|
from mock import MagicMock, Mock
|
||||||
|
|
||||||
|
from django.core import mail
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.contrib.sites.models import Site
|
||||||
|
|
||||||
|
from contact_form.forms import ContactForm
|
||||||
|
|
||||||
|
TEST_DATA = {
|
||||||
|
'name': 'Test User',
|
||||||
|
'email': 'test@example.org',
|
||||||
|
'body': 'Test message'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ContactFormTest(TestCase):
|
||||||
|
|
||||||
|
def test_constructor_needs_request(self):
|
||||||
|
with self.assertRaises(KeyError):
|
||||||
|
form = ContactForm()
|
||||||
|
|
||||||
|
def test_constructor(self):
|
||||||
|
request = MagicMock()
|
||||||
|
form = ContactForm(request=request)
|
||||||
|
self.assertTrue(hasattr(form, 'request'))
|
||||||
|
self.assertEqual(form.request, request)
|
||||||
|
self.assertTrue(hasattr(form, 'helper'))
|
||||||
|
self.assertEqual(form.helper.form_action, reverse('contact_form'))
|
||||||
|
self.assertEqual(len(form.helper.inputs), 1)
|
||||||
|
self.assertEqual(form.helper.inputs[0].name, 'submit')
|
||||||
|
|
||||||
|
def test_constructor_fields(self):
|
||||||
|
request = MagicMock()
|
||||||
|
form = ContactForm(request=request)
|
||||||
|
self.assertEqual(len(form.fields), 3)
|
||||||
|
self.assertIn('email', form.fields)
|
||||||
|
self.assertIn('name', form.fields)
|
||||||
|
self.assertIn('body', form.fields)
|
||||||
|
self.assertEqual(len(form.data), 0)
|
||||||
|
|
||||||
|
def test_get_context_invalid(self):
|
||||||
|
request = MagicMock()
|
||||||
|
form = ContactForm(request=request)
|
||||||
|
with self.assertRaisesMessage(
|
||||||
|
ValueError,
|
||||||
|
'Cannot generate context from invalid contact form'):
|
||||||
|
form.get_context()
|
||||||
|
|
||||||
|
def test_get_context_valid_site_installed(self):
|
||||||
|
request = MagicMock()
|
||||||
|
form = ContactForm(request=request, data=TEST_DATA)
|
||||||
|
context = form.get_context()
|
||||||
|
self.assertIn('site', context)
|
||||||
|
self.assertIn('name', context)
|
||||||
|
self.assertIn('email', context)
|
||||||
|
self.assertIn('body', context)
|
||||||
|
|
||||||
|
def test_get_context_valid_site_not_installed(self):
|
||||||
|
request = MagicMock()
|
||||||
|
form = ContactForm(request=request, data=TEST_DATA)
|
||||||
|
with mock.patch('contact_form.forms.Site') as sitemock:
|
||||||
|
sitemock._meta.installed = False
|
||||||
|
context = form.get_context()
|
||||||
|
self.assertIn('site', context)
|
||||||
|
self.assertIn('name', context)
|
||||||
|
self.assertIn('email', context)
|
||||||
|
self.assertIn('body', context)
|
||||||
|
|
||||||
|
def test_message(self):
|
||||||
|
request = Mock()
|
||||||
|
request.META = {'REMOTE_ADDR': '127.0.0.1'}
|
||||||
|
form = ContactForm(request=request, data=TEST_DATA)
|
||||||
|
message = form.message()
|
||||||
|
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)
|
||||||
|
|
||||||
|
def test_subject(self):
|
||||||
|
request = Mock()
|
||||||
|
form = ContactForm(request=request, data=TEST_DATA)
|
||||||
|
subject = form.subject()
|
||||||
|
self.assertIn(Site.objects.get_current().name, subject)
|
||||||
|
self.assertIn(TEST_DATA['name'], subject)
|
|
@ -4,12 +4,31 @@ Tests for :py:mod:`contact_form.views`.
|
||||||
"""
|
"""
|
||||||
from __future__ import absolute_import, unicode_literals
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
|
from django.core import mail
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
TEST_USER = 'test'
|
||||||
|
TEST_PASSWORD = 'secret'
|
||||||
|
TEST_EMAIL = 'test@example.org'
|
||||||
|
TEST_NAME = 'Example Tester'.split()
|
||||||
|
TEST_MESSAGE = '''
|
||||||
|
This is a really unimportant test message.
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
class ContactFormViewTest(TestCase):
|
class ContactFormViewTest(TestCase):
|
||||||
|
|
||||||
|
def _setup_user(self, **kwargs):
|
||||||
|
return User.objects.create_user(
|
||||||
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD,
|
||||||
|
**kwargs)
|
||||||
|
|
||||||
def test_get_contact_form_template(self):
|
def test_get_contact_form_template(self):
|
||||||
response = self.client.get(reverse('contact_form'))
|
response = self.client.get(reverse('contact_form'))
|
||||||
self.assertTemplateUsed(response, 'contact_form/contact_form.html')
|
self.assertTemplateUsed(response, 'contact_form/contact_form.html')
|
||||||
|
@ -21,9 +40,88 @@ class ContactFormViewTest(TestCase):
|
||||||
def test_get_contact_form_anonymous_has_empty_form(self):
|
def test_get_contact_form_anonymous_has_empty_form(self):
|
||||||
response = self.client.get(reverse('contact_form'))
|
response = self.client.get(reverse('contact_form'))
|
||||||
self.assertIn('form', response.context)
|
self.assertIn('form', response.context)
|
||||||
self.assertEqual(len(response.context['form'].initial), 0)
|
form = response.context['form']
|
||||||
|
self.assertEqual(len(form.initial), 0)
|
||||||
|
|
||||||
def test_get_contact_form_fields_anonymous(self):
|
def test_get_contact_form_fields_anonymous(self):
|
||||||
response = self.client.get(reverse('contact_form'))
|
response = self.client.get(reverse('contact_form'))
|
||||||
for name in ('name', 'email', 'body'):
|
for name in ('name', 'email', 'body'):
|
||||||
self.assertIn(name, response.context['form'].fields)
|
self.assertIn(name, response.context['form'].fields)
|
||||||
|
|
||||||
|
def test_post_empty_form_template(self):
|
||||||
|
response = self.client.post(reverse('contact_form'), {})
|
||||||
|
self.assertTemplateUsed(response, 'contact_form/contact_form.html')
|
||||||
|
|
||||||
|
def test_post_empty_form_status(self):
|
||||||
|
response = self.client.post(reverse('contact_form'), {})
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
def test_post_empty_form_validation_errors(self):
|
||||||
|
response = self.client.post(reverse('contact_form'), {})
|
||||||
|
self.assertIn('form', response.context)
|
||||||
|
form = response.context['form']
|
||||||
|
self.assertFalse(form.is_valid())
|
||||||
|
self.assertEqual(len(form.errors), 3)
|
||||||
|
|
||||||
|
def test_post_empty_form_no_mail(self):
|
||||||
|
response = self.client.post(reverse('contact_form'), {})
|
||||||
|
self.assertEqual(len(mail.outbox), 0)
|
||||||
|
|
||||||
|
def test_get_contact_form_logged_in_no_fullname_initial(self):
|
||||||
|
user = self._setup_user()
|
||||||
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
||||||
|
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})
|
||||||
|
|
||||||
|
def test_get_contact_form_logged_in_fullname_initial(self):
|
||||||
|
user = self._setup_user(
|
||||||
|
first_name=TEST_NAME[0], last_name=TEST_NAME[1])
|
||||||
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
||||||
|
response = self.client.get(reverse('contact_form'))
|
||||||
|
self.assertIn('form', response.context)
|
||||||
|
form = response.context['form']
|
||||||
|
self.assertEqual(
|
||||||
|
form.initial,
|
||||||
|
{'name': " ".join(TEST_NAME), 'email': TEST_EMAIL})
|
||||||
|
|
||||||
|
def test_post_filled_form_anonymous_redirects(self):
|
||||||
|
response = self.client.post(reverse('contact_form'), {
|
||||||
|
'name': TEST_USER, 'email': TEST_EMAIL, 'body': TEST_MESSAGE})
|
||||||
|
self.assertRedirects(response, reverse('contact_success'))
|
||||||
|
|
||||||
|
def test_post_filled_form_anonymous_mail(self):
|
||||||
|
response = self.client.post(reverse('contact_form'), {
|
||||||
|
'name': TEST_USER, 'email': TEST_EMAIL, 'body': TEST_MESSAGE})
|
||||||
|
self.assertEqual(len(mail.outbox), 1)
|
||||||
|
|
||||||
|
def test_post_filled_form_logged_in_redirects(self):
|
||||||
|
user = self._setup_user(
|
||||||
|
first_name=TEST_NAME[0], last_name=TEST_NAME[1])
|
||||||
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
||||||
|
response = self.client.post(reverse('contact_form'), {
|
||||||
|
'name': " ".join(TEST_NAME), 'email': TEST_EMAIL,
|
||||||
|
'body': TEST_MESSAGE})
|
||||||
|
self.assertRedirects(response, reverse('contact_success'))
|
||||||
|
|
||||||
|
def test_post_filled_form_logged_in_redirects(self):
|
||||||
|
user = self._setup_user(
|
||||||
|
first_name=TEST_NAME[0], last_name=TEST_NAME[1])
|
||||||
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
||||||
|
response = self.client.post(reverse('contact_form'), {
|
||||||
|
'name': " ".join(TEST_NAME), 'email': TEST_EMAIL,
|
||||||
|
'body': TEST_MESSAGE})
|
||||||
|
self.assertEqual(len(mail.outbox), 1)
|
||||||
|
|
||||||
|
|
||||||
|
class ContactSuccessViewTest(TestCase):
|
||||||
|
|
||||||
|
def test_get_template(self):
|
||||||
|
response = self.client.get(reverse('contact_success'))
|
||||||
|
self.assertTemplateUsed(response, 'contact_form/contact_success.html')
|
||||||
|
|
||||||
|
def test_get_status(self):
|
||||||
|
response = self.client.get(reverse('contact_success'))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
Loading…
Reference in a new issue