don't notify when editing existing organizations
[redakcja.git] / apps / forms_builder / forms / tests.py
1 from __future__ import unicode_literals
2
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
10
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
17
18
19 class Tests(TestCase):
20
21     def setUp(self):
22         self._site = Site.objects.get_current()
23
24     def test_form_fields(self):
25         """
26         Simple 200 status check against rendering and posting to forms with
27         both optional and required fields.
28         """
29         for required in (True, False):
30             form = Form.objects.create(title="Test", status=STATUS_PUBLISHED)
31             if USE_SITES:
32                 form.sites.add(self._site)
33                 form.save()
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)
43
44     def test_draft_form(self):
45         """
46         Test that a form with draft status is only visible to staff.
47         """
48         settings.DEBUG = True # Don't depend on having a 404 template.
49         username = "test"
50         password = "test"
51         User.objects.create_superuser(username, "", password)
52         self.client.logout()
53         draft = Form.objects.create(title="Draft", status=STATUS_DRAFT)
54         if USE_SITES:
55             draft.sites.add(self._site)
56             draft.save()
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)
62
63     def test_form_signals(self):
64         """
65         Test that each of the signals are sent.
66         """
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)
73         if USE_SITES:
74             form.sites.add(self._site)
75             form.save()
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)
82
83     def test_tag(self):
84         """
85         Test that the different formats for the ``render_built_form``
86         tag all work.
87         """
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)
96
97     def test_optional_filefield(self):
98         form = Form.objects.create(title="Test", status=STATUS_PUBLISHED)
99         if USE_SITES:
100             form.sites.add(self._site)
101         form.save()
102         form.fields.create(label="file field",
103                 field_type=FILE,
104                 required=False,
105                 visible=True)
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
111         # may not be NULL
112         form_for_form.save()
113
114
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])
119         field.save()
120         self.assertEqual(field.slug, 'first_name')
121
122         field_2 = Field(form=form,
123                 label="First name", field_type=NAMES[0][0])
124         try:
125             field_2.save()
126         except IntegrityError:
127             self.fail("Slugs were not auto-unique")
128
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)
136
137     def test_form_errors(self):
138         from future.builtins import str
139         form = Form.objects.create(title="Test")
140         if USE_SITES:
141             form.sites.add(self._site)
142             form.save()
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))
147
148     def test_form_redirect(self):
149         redirect_url = 'http://example.com/foo'
150         form = Form.objects.create(title='Test', redirect_url=redirect_url)
151         if USE_SITES:
152             form.sites.add(self._site)
153             form.save()
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))