Merge lp:~godricglow/postorius/tests_forms into lp:postorius

Proposed by Pranjal Yadav
Status: Needs review
Proposed branch: lp:~godricglow/postorius/tests_forms
Merge into: lp:postorius
Diff against target: 196 lines (+90/-25)
3 files modified
src/postorius/forms.py (+20/-10)
src/postorius/tests/__init__.py (+0/-1)
src/postorius/tests/test_forms.py (+70/-14)
To merge this branch: bzr merge lp:~godricglow/postorius/tests_forms
Reviewer Review Type Date Requested Status
Florian Fuchs Pending
Review via email: mp+251740@code.launchpad.net

Description of the change

Added tests for forms.py. Improving overall coverage

To post a comment you must log in.
Revision history for this message
Florian Fuchs (flo-fuchs) wrote :

Hi Pranjal,

thanks for this merge proposal. It looks pretty good so far, I only have some minor comments (see below).

Florian

Revision history for this message
Pranjal Yadav (godricglow) wrote :
Download full text (9.2 KiB)

Hi Florian

Thanks for you reply, I checked all you comments carefully and have replied
below.

On Tue, Mar 10, 2015 at 2:43 PM, Florian Fuchs <email address hidden> wrote:

> Hi Pranjal,
>
> thanks for this merge proposal. It looks pretty good so far, I only have
> some minor comments (see below).
>
> Florian
>
> Diff comments:
>
> > === modified file 'src/postorius/forms.py'
> > --- src/postorius/forms.py 2015-02-09 14:35:44 +0000
> > +++ src/postorius/forms.py 2015-03-04 13:56:28 +0000
> > @@ -17,7 +17,7 @@
> > # Postorius. If not, see <http://www.gnu.org/licenses/>.
> >
> > from django import forms
> > -from django.core.validators import validate_email, URLValidator
> > +from django.core.validators import validate_email, URLValidator,
> validate_slug
> > from django.utils.translation import gettext_lazy as _
> > from fieldset_forms import FieldsetForm
> > from django.forms.models import modelformset_factory
> > @@ -102,6 +102,7 @@
> > Form fields to add a new list. Languages are hard coded which should
> > be replaced by a REST lookup of available languages.
> > """
> > +
> > listname = forms.CharField(
> > label=_('List Name'),
> > required=True,
> > @@ -126,6 +127,7 @@
> > label=_('Description'),
> > required=True)
> >
> > +
> > def __init__(self, domain_choices, *args, **kwargs):
> > super(ListNew, self).__init__(*args, **kwargs)
> > self.fields["mail_host"] = forms.ChoiceField(
> > @@ -138,17 +140,20 @@
> > if len(domain_choices) < 2:
> > self.fields["mail_host"].help_text = _(
> > "Site admin has not created any domains")
> > - # if len(choices) < 2:
> > + #if len(choices) < 2:
> > # help_text=_("No domains available: " +
> > # "The site admin must create new domains " +
> > # "before you will be able to create a list")
> >
> > def clean_listname(self):
> > +
> > + cleaned_data = super(ListNew, self).clean()
> > + listname = cleaned_data.get('listname')
>
> For a single field's clean method, we don't need to call the parent's
> clean method. We can just access `self.cleaned_data.get('listname')`
> directly.
>

Good point but I already tried doing that, it seems __init__ method creates
some problem otherwise when I don't call the parent's clean method. I
talked to abhilash and he too said that cleaning through parent seems to
work. I need your advice on this part.

>
> > try:
> > - validate_email(self.cleaned_data['listname'] + '@
> example.net')
> > + validate_slug(listname)
>
> I think `validate_slug` is too restrictive here, because the local part of
> an email address has more valid characters than validate_slug allows.
>

> I guess there is some confusion here, I'm validating the email with
> 'validate_email' only. I'm using validate_slug for listname only and I
> think listname need to have that restriction which again is not my decision
> but my choice as per sample data from various tests, Please tell me If I
> need to do otherwise.
> > except:
> > ...

Read more...

Unmerged revisions

204. By Pranjal Yadav <email address hidden>

Adding new tests for forms.py

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/postorius/forms.py'
2--- src/postorius/forms.py 2015-02-09 14:35:44 +0000
3+++ src/postorius/forms.py 2015-03-04 13:56:28 +0000
4@@ -17,7 +17,7 @@
5 # Postorius. If not, see <http://www.gnu.org/licenses/>.
6
7 from django import forms
8-from django.core.validators import validate_email, URLValidator
9+from django.core.validators import validate_email, URLValidator, validate_slug
10 from django.utils.translation import gettext_lazy as _
11 from fieldset_forms import FieldsetForm
12 from django.forms.models import modelformset_factory
13@@ -102,6 +102,7 @@
14 Form fields to add a new list. Languages are hard coded which should
15 be replaced by a REST lookup of available languages.
16 """
17+
18 listname = forms.CharField(
19 label=_('List Name'),
20 required=True,
21@@ -126,6 +127,7 @@
22 label=_('Description'),
23 required=True)
24
25+
26 def __init__(self, domain_choices, *args, **kwargs):
27 super(ListNew, self).__init__(*args, **kwargs)
28 self.fields["mail_host"] = forms.ChoiceField(
29@@ -138,17 +140,20 @@
30 if len(domain_choices) < 2:
31 self.fields["mail_host"].help_text = _(
32 "Site admin has not created any domains")
33- # if len(choices) < 2:
34+ #if len(choices) < 2:
35 # help_text=_("No domains available: " +
36 # "The site admin must create new domains " +
37 # "before you will be able to create a list")
38
39 def clean_listname(self):
40+
41+ cleaned_data = super(ListNew, self).clean()
42+ listname = cleaned_data.get('listname')
43 try:
44- validate_email(self.cleaned_data['listname'] + '@example.net')
45+ validate_slug(listname)
46 except:
47 raise forms.ValidationError(_("Please enter a valid listname"))
48- return self.cleaned_data['listname']
49+ return listname
50
51 class Meta:
52
53@@ -827,14 +832,19 @@
54 widget=forms.PasswordInput(render_value=False))
55
56 def clean(self):
57- cleaned_data = self.cleaned_data
58- password = cleaned_data.get("password")
59- password_repeat = cleaned_data.get("password_repeat")
60+ password = self.cleaned_data.get('password')
61+ password_repeat = self.cleaned_data.get('password_repeat')
62 if password != password_repeat:
63 raise forms.ValidationError("Passwords must be identical.")
64-
65- return cleaned_data
66-
67+ return password_repeat
68+
69+ def clean_email(self):
70+ email = self.cleaned_data['email']
71+ try:
72+ validate_email(email)
73+ except:
74+ raise forms.ValidationError(_("Please enter a valid email ID"))
75+ return email
76
77 class UserSettings(FieldsetForm):
78
79
80=== modified file 'src/postorius/tests/__init__.py'
81--- src/postorius/tests/__init__.py 2015-02-10 13:56:47 +0000
82+++ src/postorius/tests/__init__.py 2015-03-04 13:56:28 +0000
83@@ -20,7 +20,6 @@
84
85 from django.conf import settings
86
87-
88 TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
89
90 FIXTURES_DIR = getattr(
91
92=== modified file 'src/postorius/tests/test_forms.py'
93--- src/postorius/tests/test_forms.py 2015-02-09 14:35:44 +0000
94+++ src/postorius/tests/test_forms.py 2015-03-04 13:56:28 +0000
95@@ -15,9 +15,7 @@
96 # You should have received a copy of the GNU General Public License along with
97 # Postorius. If not, see <http://www.gnu.org/licenses/>.
98 from django.utils import unittest
99-
100-from postorius.forms import UserPreferences, DomainNew
101-
102+from postorius.forms import UserPreferences, DomainNew, ListNew, UserNew
103
104 class UserPreferencesTest(unittest.TestCase):
105
106@@ -32,20 +30,78 @@
107
108 class DomainNewTest(unittest.TestCase):
109
110+ def setUp(self):
111+ self.form_data = {
112+ 'mail_host': 'mailman.most-desirable.org',
113+ 'web_host': 'http://mailman.most-desirable.org',
114+ 'description': 'The Most Desirable organization',
115+ 'contact_address': 'contact@mailman.most-desirable.org',
116+ }
117+
118 def test_form_fields_webhost(self):
119- form = DomainNew({
120- 'mail_host': 'mailman.most-desirable.org',
121- 'web_host': 'http://mailman.most-desirable.org',
122- 'description': 'The Most Desirable organization',
123- 'contact_address': 'contact@mailman.most-desirable.org',
124- })
125+ form = DomainNew(self.form_data)
126 self.assertTrue(form.is_valid())
127
128 def test_form_fields_webhost_invalid(self):
129- form = DomainNew({
130+ self.form_data['web_host'] = 'mailman.most-desirable.org'
131+ form = DomainNew(self.form_data)
132+ self.assertFalse(form.is_valid())
133+
134+ def test_form_fields_mailhost(self):
135+ form = DomainNew(self.form_data)
136+ self.assertTrue(form.is_valid())
137+
138+ def test_form_fields_mailhost_invalid(self):
139+ self.form_data['mail_host'] = 'mailman.most-desirable..org'
140+ form = DomainNew(self.form_data)
141+ self.assertFalse(form.is_valid())
142+
143+class ListNewTest(unittest.TestCase):
144+
145+ def setUp(self):
146+ self.choosable_domains = [('mailman.most-desirable.org', 'mailman.most-desirable.org')]
147+
148+ self.form_data = {
149+ 'listname' : 'test_list1',
150 'mail_host': 'mailman.most-desirable.org',
151- 'web_host': 'mailman.most-desirable.org',
152+ 'list_owner': 'james@example.com',
153+ 'advertised': 'True',
154 'description': 'The Most Desirable organization',
155- 'contact_address': 'contact@mailman.most-desirable.org',
156- })
157- self.assertFalse(form.is_valid())
158+ }
159+
160+ def test_form_fields_listname(self):
161+ form = ListNew(self.choosable_domains,self.form_data)
162+ self.assertTrue(form.is_valid())
163+
164+ def test_form_fields_listname_invalid(self):
165+ self.form_data['listname'] = 'test$list1'
166+ form = ListNew(self.choosable_domains,self.form_data)
167+ self.assertFalse(form.is_valid())
168+
169+class UserNewTest(unittest.TestCase):
170+
171+ def setUp(self):
172+ self.form_data = {
173+ 'display_name' : 'Pranjal Yadav',
174+ 'email' : 'pranjal@example.com',
175+ 'password' : 'password123',
176+ 'password_repeat' : 'password123',
177+ }
178+
179+ def test_form_fields_password(self):
180+ form = UserNew(self.form_data)
181+ self.assertTrue(form.is_valid())
182+
183+ def test_form_fields_password_invalid(self):
184+ self.form_data['password_repeat'] = 'random'
185+ form = UserNew(self.form_data)
186+ self.assertFalse(form.is_valid())
187+
188+ def test_form_fields_email(self):
189+ form = UserNew(self.form_data)
190+ self.assertTrue(form.is_valid())
191+
192+ def test_form_fields_email_invalid(self):
193+ self.form_data['email'] = 'pranjal@example..com'
194+ form = UserNew(self.form_data)
195+ self.assertFalse(form.is_valid())
196\ No newline at end of file

Subscribers

People subscribed via source and target branches