1 from __future__ import unicode_literals
3 from django.conf import settings
4 from django.contrib.auth.models import User, AnonymousUser
5 from django.contrib.sites.models import Site
6 from django.db import IntegrityError
7 from django.http import HttpResponseRedirect
8 from django.template import Context, RequestContext, Template
9 from django.test import TestCase
11 from forms_builder.forms.fields import NAMES, FILE
12 from forms_builder.forms.forms import FormForForm
13 from forms_builder.forms.models import (Form, Field,
14 STATUS_DRAFT, STATUS_PUBLISHED)
15 from forms_builder.forms.settings import USE_SITES
16 from forms_builder.forms.signals import form_invalid, form_valid
19 class Tests(TestCase):
22 self._site = Site.objects.get_current()
24 def test_form_fields(self):
26 Simple 200 status check against rendering and posting to forms with
27 both optional and required fields.
29 for required in (True, False):
30 form = Form.objects.create(title="Test", status=STATUS_PUBLISHED)
32 form.sites.add(self._site)
34 for (field, _) in NAMES:
35 form.fields.create(label=field, field_type=field,
36 required=required, visible=True)
37 response = self.client.get(form.get_absolute_url())
38 self.assertEqual(response.status_code, 200)
39 fields = form.fields.visible()
40 data = dict([(f.slug, "test") for f in fields])
41 response = self.client.post(form.get_absolute_url(), data=data)
42 self.assertEqual(response.status_code, 200)
44 def test_draft_form(self):
46 Test that a form with draft status is only visible to staff.
48 settings.DEBUG = True # Don't depend on having a 404 template.
51 User.objects.create_superuser(username, "", password)
53 draft = Form.objects.create(title="Draft", status=STATUS_DRAFT)
55 draft.sites.add(self._site)
57 response = self.client.get(draft.get_absolute_url())
58 self.assertEqual(response.status_code, 404)
59 self.client.login(username=username, password=password)
60 response = self.client.get(draft.get_absolute_url())
61 self.assertEqual(response.status_code, 200)
63 def test_form_signals(self):
65 Test that each of the signals are sent.
67 events = ["valid", "invalid"]
68 invalid = lambda **kwargs: events.remove("invalid")
69 form_invalid.connect(invalid)
70 valid = lambda **kwargs: events.remove("valid")
71 form_valid.connect(valid)
72 form = Form.objects.create(title="Signals", status=STATUS_PUBLISHED)
74 form.sites.add(self._site)
76 form.fields.create(label="field", field_type=NAMES[0][0],
77 required=True, visible=True)
78 self.client.post(form.get_absolute_url(), data={})
79 data = {form.fields.visible()[0].slug: "test"}
80 self.client.post(form.get_absolute_url(), data=data)
81 self.assertEqual(len(events), 0)
85 Test that the different formats for the ``render_built_form``
88 form = Form.objects.create(title="Tags", status=STATUS_PUBLISHED)
89 request = type(str(""), (), {"META": {}, "user": AnonymousUser()})()
90 context = RequestContext(request, {"form": form})
91 template = "{%% load forms_builder_tags %%}{%% render_built_form %s %%}"
92 formats = ("form", "form=form", "id=form.id", "slug=form.slug")
93 for format in formats:
94 t = Template(template % format).render(context)
95 self.assertTrue(form.get_absolute_url(), t)
97 def test_optional_filefield(self):
98 form = Form.objects.create(title="Test", status=STATUS_PUBLISHED)
100 form.sites.add(self._site)
102 form.fields.create(label="file field",
106 fields = form.fields.visible()
107 data = {'field_%s' % fields[0].id: ''}
108 context = Context({})
109 form_for_form = FormForForm(form, context, data=data)
110 # Should not raise IntegrityError: forms_fieldentry.value
115 def test_field_validate_slug_names(self):
116 form = Form.objects.create(title="Test")
117 field = Field(form=form,
118 label="First name", field_type=NAMES[0][0])
120 self.assertEqual(field.slug, 'first_name')
122 field_2 = Field(form=form,
123 label="First name", field_type=NAMES[0][0])
126 except IntegrityError:
127 self.fail("Slugs were not auto-unique")
129 def test_field_default_ordering(self):
130 form = Form.objects.create(title="Test")
131 form.fields.create(label="second field",
132 field_type=NAMES[0][0], order=2)
133 f1 = form.fields.create(label="first field",
134 field_type=NAMES[0][0], order=1)
135 self.assertEqual(form.fields.all()[0], f1)
137 def test_form_errors(self):
138 from future.builtins import str
139 form = Form.objects.create(title="Test")
141 form.sites.add(self._site)
143 form.fields.create(label="field", field_type=NAMES[0][0],
144 required=True, visible=True)
145 response = self.client.post(form.get_absolute_url(), {"foo": "bar"})
146 self.assertTrue("This field is required" in str(response.content))
148 def test_form_redirect(self):
149 redirect_url = 'http://example.com/foo'
150 form = Form.objects.create(title='Test', redirect_url=redirect_url)
152 form.sites.add(self._site)
154 form.fields.create(label='field', field_type=NAMES[3][0],
155 required=True, visible=True)
156 form_absolute_url = form.get_absolute_url()
157 response = self.client.post(form_absolute_url, {'field': '0'})
158 self.assertEqual(response["location"], redirect_url)
159 response = self.client.post(form_absolute_url, {'field': 'bar'})
160 self.assertFalse(isinstance(response, HttpResponseRedirect))