test_forms.py
6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
from wtforms import ValidationError
from nose.tools import raises
from flask import current_app
from default import Test, db, with_context
from pybossa.forms.forms import (RegisterForm, LoginForm, EMAIL_MAX_LENGTH,
USER_NAME_MAX_LENGTH, USER_FULLNAME_MAX_LENGTH)
from pybossa.forms import validator
from pybossa.repositories import UserRepository
from factories import UserFactory
user_repo = UserRepository(db)
class TestValidator(Test):
def setUp(self):
super(TestValidator, self).setUp()
with self.flask_app.app_context():
self.create()
@raises(ValidationError)
def test_unique(self):
"""Test VALIDATOR Unique works."""
with self.flask_app.test_request_context('/'):
f = LoginForm()
f.email.data = self.email_addr
u = validator.Unique(user_repo.get_by, 'email_addr')
u.__call__(f, f.email)
@raises(ValidationError)
def test_not_allowed_chars(self):
"""Test VALIDATOR NotAllowedChars works."""
with self.flask_app.test_request_context('/'):
f = LoginForm()
f.email.data = self.email_addr + "$"
u = validator.NotAllowedChars()
u.__call__(f, f.email)
@raises(ValidationError)
def test_comma_separated_integers(self):
"""Test VALIDATOR CommaSeparatedIntegers works."""
with self.flask_app.test_request_context('/'):
f = LoginForm()
f.email.data = '1 2 3'
u = validator.CommaSeparatedIntegers()
u.__call__(f, f.email)
@with_context
@raises(ValidationError)
def test_reserved_names_account_signin(self):
"""Test VALIDATOR ReservedName for account URLs"""
form = RegisterForm()
form.name.data = 'signin'
val = validator.ReservedName('account', current_app)
val(form, form.name)
@with_context
@raises(ValidationError)
def test_reserved_names_project_published(self):
"""Test VALIDATOR ReservedName for project URLs"""
form = RegisterForm()
form.name.data = 'category'
val = validator.ReservedName('project', current_app)
val(form, form.name)
class TestRegisterForm(Test):
def setUp(self):
super(TestRegisterForm, self).setUp()
self.fill_in_data = {'fullname': 'Tyrion Lannister', 'name': 'mylion',
'email_addr': 'tyrion@casterly.rock',
'password':'secret', 'confirm':'secret'}
fields = ['fullname', 'name', 'email_addr', 'password', 'confirm']
@with_context
def test_register_form_contains_fields(self):
form = RegisterForm()
for field in self.fields:
assert form.__contains__(field), 'Field %s is not in form' %field
@with_context
def test_register_form_validates_with_valid_fields(self):
form = RegisterForm(**self.fill_in_data)
assert form.validate()
@with_context
def test_register_form_unique_name(self):
form = RegisterForm(**self.fill_in_data)
user = UserFactory.create(name='mylion')
assert not form.validate()
assert "The user name is already taken" in form.errors['name'], form.errors
@with_context
def test_register_name_length(self):
self.fill_in_data['name'] = 'a'
form = RegisterForm(**self.fill_in_data)
error_message = "User name must be between 3 and %s characters long" % USER_NAME_MAX_LENGTH
assert not form.validate()
assert error_message in form.errors['name'], form.errors
@with_context
def test_register_name_allowed_chars(self):
self.fill_in_data['name'] = '$#&\/|'
form = RegisterForm(**self.fill_in_data)
assert not form.validate()
assert "$#&\\/| and space symbols are forbidden" in form.errors['name'], form.errors
@with_context
def test_register_name_reserved_name(self):
self.fill_in_data['name'] = 'signin'
form = RegisterForm(**self.fill_in_data)
assert not form.validate()
assert u'This name is used by the system.' in form.errors['name'], form.errors
@with_context
def test_register_form_unique_email(self):
form = RegisterForm(**self.fill_in_data)
user = UserFactory.create(email_addr='tyrion@casterly.rock')
assert not form.validate()
assert "Email is already taken" in form.errors['email_addr'], form.errors
@with_context
def test_register_email_length(self):
self.fill_in_data['email_addr'] = ''
form = RegisterForm(**self.fill_in_data)
error_message = "Email must be between 3 and %s characters long" % EMAIL_MAX_LENGTH
assert not form.validate()
assert error_message in form.errors['email_addr'], form.errors
@with_context
def test_register_email_valid_format(self):
self.fill_in_data['email_addr'] = 'notanemail'
form = RegisterForm(**self.fill_in_data)
assert not form.validate()
assert "Invalid email address." in form.errors['email_addr'], form.errors
@with_context
def test_register_fullname_length(self):
self.fill_in_data['fullname'] = 'a'
form = RegisterForm(**self.fill_in_data)
error_message = "Full name must be between 3 and %s characters long" % USER_FULLNAME_MAX_LENGTH
assert not form.validate()
assert error_message in form.errors['fullname'], form.errors
@with_context
def test_register_password_required(self):
self.fill_in_data['password'] = ''
form = RegisterForm(**self.fill_in_data)
assert not form.validate()
assert "Password cannot be empty" in form.errors['password'], form.errors
@with_context
def test_register_password_missmatch(self):
self.fill_in_data['confirm'] = 'badpasswd'
form = RegisterForm(**self.fill_in_data)
assert not form.validate()
assert "Passwords must match" in form.errors['password'], form.errors