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
=== 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 @@
17# Postorius. If not, see <http://www.gnu.org/licenses/>.17# Postorius. If not, see <http://www.gnu.org/licenses/>.
1818
19from django import forms19from django import forms
20from django.core.validators import validate_email, URLValidator20from django.core.validators import validate_email, URLValidator, validate_slug
21from django.utils.translation import gettext_lazy as _21from django.utils.translation import gettext_lazy as _
22from fieldset_forms import FieldsetForm22from fieldset_forms import FieldsetForm
23from django.forms.models import modelformset_factory23from django.forms.models import modelformset_factory
@@ -102,6 +102,7 @@
102 Form fields to add a new list. Languages are hard coded which should102 Form fields to add a new list. Languages are hard coded which should
103 be replaced by a REST lookup of available languages.103 be replaced by a REST lookup of available languages.
104 """104 """
105
105 listname = forms.CharField(106 listname = forms.CharField(
106 label=_('List Name'),107 label=_('List Name'),
107 required=True,108 required=True,
@@ -126,6 +127,7 @@
126 label=_('Description'),127 label=_('Description'),
127 required=True)128 required=True)
128129
130
129 def __init__(self, domain_choices, *args, **kwargs):131 def __init__(self, domain_choices, *args, **kwargs):
130 super(ListNew, self).__init__(*args, **kwargs)132 super(ListNew, self).__init__(*args, **kwargs)
131 self.fields["mail_host"] = forms.ChoiceField(133 self.fields["mail_host"] = forms.ChoiceField(
@@ -138,17 +140,20 @@
138 if len(domain_choices) < 2:140 if len(domain_choices) < 2:
139 self.fields["mail_host"].help_text = _(141 self.fields["mail_host"].help_text = _(
140 "Site admin has not created any domains")142 "Site admin has not created any domains")
141 # if len(choices) < 2:143 #if len(choices) < 2:
142 # help_text=_("No domains available: " +144 # help_text=_("No domains available: " +
143 # "The site admin must create new domains " +145 # "The site admin must create new domains " +
144 # "before you will be able to create a list")146 # "before you will be able to create a list")
145147
146 def clean_listname(self):148 def clean_listname(self):
149
150 cleaned_data = super(ListNew, self).clean()
151 listname = cleaned_data.get('listname')
147 try:152 try:
148 validate_email(self.cleaned_data['listname'] + '@example.net')153 validate_slug(listname)
149 except:154 except:
150 raise forms.ValidationError(_("Please enter a valid listname"))155 raise forms.ValidationError(_("Please enter a valid listname"))
151 return self.cleaned_data['listname']156 return listname
152157
153 class Meta:158 class Meta:
154159
@@ -827,14 +832,19 @@
827 widget=forms.PasswordInput(render_value=False))832 widget=forms.PasswordInput(render_value=False))
828833
829 def clean(self):834 def clean(self):
830 cleaned_data = self.cleaned_data835 password = self.cleaned_data.get('password')
831 password = cleaned_data.get("password")836 password_repeat = self.cleaned_data.get('password_repeat')
832 password_repeat = cleaned_data.get("password_repeat")
833 if password != password_repeat:837 if password != password_repeat:
834 raise forms.ValidationError("Passwords must be identical.")838 raise forms.ValidationError("Passwords must be identical.")
835839 return password_repeat
836 return cleaned_data840
837841 def clean_email(self):
842 email = self.cleaned_data['email']
843 try:
844 validate_email(email)
845 except:
846 raise forms.ValidationError(_("Please enter a valid email ID"))
847 return email
838848
839class UserSettings(FieldsetForm):849class UserSettings(FieldsetForm):
840850
841851
=== modified file 'src/postorius/tests/__init__.py'
--- src/postorius/tests/__init__.py 2015-02-10 13:56:47 +0000
+++ src/postorius/tests/__init__.py 2015-03-04 13:56:28 +0000
@@ -20,7 +20,6 @@
2020
21from django.conf import settings21from django.conf import settings
2222
23
24TEST_ROOT = os.path.abspath(os.path.dirname(__file__))23TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
2524
26FIXTURES_DIR = getattr(25FIXTURES_DIR = getattr(
2726
=== modified file 'src/postorius/tests/test_forms.py'
--- src/postorius/tests/test_forms.py 2015-02-09 14:35:44 +0000
+++ src/postorius/tests/test_forms.py 2015-03-04 13:56:28 +0000
@@ -15,9 +15,7 @@
15# You should have received a copy of the GNU General Public License along with15# You should have received a copy of the GNU General Public License along with
16# Postorius. If not, see <http://www.gnu.org/licenses/>.16# Postorius. If not, see <http://www.gnu.org/licenses/>.
17from django.utils import unittest17from django.utils import unittest
1818from postorius.forms import UserPreferences, DomainNew, ListNew, UserNew
19from postorius.forms import UserPreferences, DomainNew
20
2119
22class UserPreferencesTest(unittest.TestCase):20class UserPreferencesTest(unittest.TestCase):
2321
@@ -32,20 +30,78 @@
3230
33class DomainNewTest(unittest.TestCase):31class DomainNewTest(unittest.TestCase):
3432
33 def setUp(self):
34 self.form_data = {
35 'mail_host': 'mailman.most-desirable.org',
36 'web_host': 'http://mailman.most-desirable.org',
37 'description': 'The Most Desirable organization',
38 'contact_address': 'contact@mailman.most-desirable.org',
39 }
40
35 def test_form_fields_webhost(self):41 def test_form_fields_webhost(self):
36 form = DomainNew({42 form = DomainNew(self.form_data)
37 'mail_host': 'mailman.most-desirable.org',
38 'web_host': 'http://mailman.most-desirable.org',
39 'description': 'The Most Desirable organization',
40 'contact_address': 'contact@mailman.most-desirable.org',
41 })
42 self.assertTrue(form.is_valid())43 self.assertTrue(form.is_valid())
4344
44 def test_form_fields_webhost_invalid(self):45 def test_form_fields_webhost_invalid(self):
45 form = DomainNew({46 self.form_data['web_host'] = 'mailman.most-desirable.org'
47 form = DomainNew(self.form_data)
48 self.assertFalse(form.is_valid())
49
50 def test_form_fields_mailhost(self):
51 form = DomainNew(self.form_data)
52 self.assertTrue(form.is_valid())
53
54 def test_form_fields_mailhost_invalid(self):
55 self.form_data['mail_host'] = 'mailman.most-desirable..org'
56 form = DomainNew(self.form_data)
57 self.assertFalse(form.is_valid())
58
59class ListNewTest(unittest.TestCase):
60
61 def setUp(self):
62 self.choosable_domains = [('mailman.most-desirable.org', 'mailman.most-desirable.org')]
63
64 self.form_data = {
65 'listname' : 'test_list1',
46 'mail_host': 'mailman.most-desirable.org',66 'mail_host': 'mailman.most-desirable.org',
47 'web_host': 'mailman.most-desirable.org',67 'list_owner': 'james@example.com',
68 'advertised': 'True',
48 'description': 'The Most Desirable organization',69 'description': 'The Most Desirable organization',
49 'contact_address': 'contact@mailman.most-desirable.org',70 }
50 })71
51 self.assertFalse(form.is_valid())72 def test_form_fields_listname(self):
73 form = ListNew(self.choosable_domains,self.form_data)
74 self.assertTrue(form.is_valid())
75
76 def test_form_fields_listname_invalid(self):
77 self.form_data['listname'] = 'test$list1'
78 form = ListNew(self.choosable_domains,self.form_data)
79 self.assertFalse(form.is_valid())
80
81class UserNewTest(unittest.TestCase):
82
83 def setUp(self):
84 self.form_data = {
85 'display_name' : 'Pranjal Yadav',
86 'email' : 'pranjal@example.com',
87 'password' : 'password123',
88 'password_repeat' : 'password123',
89 }
90
91 def test_form_fields_password(self):
92 form = UserNew(self.form_data)
93 self.assertTrue(form.is_valid())
94
95 def test_form_fields_password_invalid(self):
96 self.form_data['password_repeat'] = 'random'
97 form = UserNew(self.form_data)
98 self.assertFalse(form.is_valid())
99
100 def test_form_fields_email(self):
101 form = UserNew(self.form_data)
102 self.assertTrue(form.is_valid())
103
104 def test_form_fields_email_invalid(self):
105 self.form_data['email'] = 'pranjal@example..com'
106 form = UserNew(self.form_data)
107 self.assertFalse(form.is_valid())
52\ No newline at end of file108\ No newline at end of file

Subscribers

People subscribed via source and target branches