##// END OF EJS Templates
fixed registration test
marcink -
r3377:7d3d0a96 beta
parent child Browse files
Show More
@@ -1,291 +1,291 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 from rhodecode.tests import *
2 from rhodecode.tests import *
3 from rhodecode.model.db import User, Notification
3 from rhodecode.model.db import User, Notification
4 from rhodecode.lib.utils2 import generate_api_key
4 from rhodecode.lib.utils2 import generate_api_key
5 from rhodecode.lib.auth import check_password
5 from rhodecode.lib.auth import check_password
6 from rhodecode.lib import helpers as h
6 from rhodecode.lib import helpers as h
7 from rhodecode.model import validators
7 from rhodecode.model import validators
8
8
9
9
10 class TestLoginController(TestController):
10 class TestLoginController(TestController):
11
11
12 def tearDown(self):
12 def tearDown(self):
13 for n in Notification.query().all():
13 for n in Notification.query().all():
14 self.Session().delete(n)
14 self.Session().delete(n)
15
15
16 self.Session().commit()
16 self.Session().commit()
17 self.assertEqual(Notification.query().all(), [])
17 self.assertEqual(Notification.query().all(), [])
18
18
19 def test_index(self):
19 def test_index(self):
20 response = self.app.get(url(controller='login', action='index'))
20 response = self.app.get(url(controller='login', action='index'))
21 self.assertEqual(response.status, '200 OK')
21 self.assertEqual(response.status, '200 OK')
22 # Test response...
22 # Test response...
23
23
24 def test_login_admin_ok(self):
24 def test_login_admin_ok(self):
25 response = self.app.post(url(controller='login', action='index'),
25 response = self.app.post(url(controller='login', action='index'),
26 {'username': 'test_admin',
26 {'username': 'test_admin',
27 'password': 'test12'})
27 'password': 'test12'})
28 self.assertEqual(response.status, '302 Found')
28 self.assertEqual(response.status, '302 Found')
29 self.assertEqual(response.session['rhodecode_user'].get('username'),
29 self.assertEqual(response.session['rhodecode_user'].get('username'),
30 'test_admin')
30 'test_admin')
31 response = response.follow()
31 response = response.follow()
32 self.assertTrue('%s repository' % HG_REPO in response.body)
32 self.assertTrue('%s repository' % HG_REPO in response.body)
33
33
34 def test_login_regular_ok(self):
34 def test_login_regular_ok(self):
35 response = self.app.post(url(controller='login', action='index'),
35 response = self.app.post(url(controller='login', action='index'),
36 {'username': 'test_regular',
36 {'username': 'test_regular',
37 'password': 'test12'})
37 'password': 'test12'})
38
38
39 self.assertEqual(response.status, '302 Found')
39 self.assertEqual(response.status, '302 Found')
40 self.assertEqual(response.session['rhodecode_user'].get('username'),
40 self.assertEqual(response.session['rhodecode_user'].get('username'),
41 'test_regular')
41 'test_regular')
42 response = response.follow()
42 response = response.follow()
43 self.assertTrue('%s repository' % HG_REPO in response.body)
43 self.assertTrue('%s repository' % HG_REPO in response.body)
44 self.assertTrue('<a title="Admin" href="/_admin">' not in response.body)
44 self.assertTrue('<a title="Admin" href="/_admin">' not in response.body)
45
45
46 def test_login_ok_came_from(self):
46 def test_login_ok_came_from(self):
47 test_came_from = '/_admin/users'
47 test_came_from = '/_admin/users'
48 response = self.app.post(url(controller='login', action='index',
48 response = self.app.post(url(controller='login', action='index',
49 came_from=test_came_from),
49 came_from=test_came_from),
50 {'username': 'test_admin',
50 {'username': 'test_admin',
51 'password': 'test12'})
51 'password': 'test12'})
52 self.assertEqual(response.status, '302 Found')
52 self.assertEqual(response.status, '302 Found')
53 response = response.follow()
53 response = response.follow()
54
54
55 self.assertEqual(response.status, '200 OK')
55 self.assertEqual(response.status, '200 OK')
56 self.assertTrue('Users administration' in response.body)
56 self.assertTrue('Users administration' in response.body)
57
57
58 @parameterized.expand([
58 @parameterized.expand([
59 ('data:text/html,<script>window.alert("xss")</script>',),
59 ('data:text/html,<script>window.alert("xss")</script>',),
60 ('mailto:test@rhodecode.org',),
60 ('mailto:test@rhodecode.org',),
61 ('file:///etc/passwd',),
61 ('file:///etc/passwd',),
62 ('ftp://some.ftp.server',),
62 ('ftp://some.ftp.server',),
63 ('http://other.domain',),
63 ('http://other.domain',),
64 ])
64 ])
65 def test_login_bad_came_froms(self, url_came_from):
65 def test_login_bad_came_froms(self, url_came_from):
66 response = self.app.post(url(controller='login', action='index',
66 response = self.app.post(url(controller='login', action='index',
67 came_from=url_came_from),
67 came_from=url_came_from),
68 {'username': 'test_admin',
68 {'username': 'test_admin',
69 'password': 'test12'})
69 'password': 'test12'})
70 self.assertEqual(response.status, '302 Found')
70 self.assertEqual(response.status, '302 Found')
71 self.assertEqual(response._environ['paste.testing_variables']
71 self.assertEqual(response._environ['paste.testing_variables']
72 ['tmpl_context'].came_from, '/')
72 ['tmpl_context'].came_from, '/')
73 response = response.follow()
73 response = response.follow()
74
74
75 self.assertEqual(response.status, '200 OK')
75 self.assertEqual(response.status, '200 OK')
76
76
77 def test_login_short_password(self):
77 def test_login_short_password(self):
78 response = self.app.post(url(controller='login', action='index'),
78 response = self.app.post(url(controller='login', action='index'),
79 {'username': 'test_admin',
79 {'username': 'test_admin',
80 'password': 'as'})
80 'password': 'as'})
81 self.assertEqual(response.status, '200 OK')
81 self.assertEqual(response.status, '200 OK')
82
82
83 self.assertTrue('Enter 3 characters or more' in response.body)
83 self.assertTrue('Enter 3 characters or more' in response.body)
84
84
85 def test_login_wrong_username_password(self):
85 def test_login_wrong_username_password(self):
86 response = self.app.post(url(controller='login', action='index'),
86 response = self.app.post(url(controller='login', action='index'),
87 {'username': 'error',
87 {'username': 'error',
88 'password': 'test12'})
88 'password': 'test12'})
89
89
90 self.assertTrue('invalid user name' in response.body)
90 self.assertTrue('invalid user name' in response.body)
91 self.assertTrue('invalid password' in response.body)
91 self.assertTrue('invalid password' in response.body)
92
92
93 #==========================================================================
93 #==========================================================================
94 # REGISTRATIONS
94 # REGISTRATIONS
95 #==========================================================================
95 #==========================================================================
96 def test_register(self):
96 def test_register(self):
97 response = self.app.get(url(controller='login', action='register'))
97 response = self.app.get(url(controller='login', action='register'))
98 self.assertTrue('Sign Up to RhodeCode' in response.body)
98 self.assertTrue('Sign Up to RhodeCode' in response.body)
99
99
100 def test_register_err_same_username(self):
100 def test_register_err_same_username(self):
101 uname = 'test_admin'
101 uname = 'test_admin'
102 response = self.app.post(url(controller='login', action='register'),
102 response = self.app.post(url(controller='login', action='register'),
103 {'username': uname,
103 {'username': uname,
104 'password': 'test12',
104 'password': 'test12',
105 'password_confirmation': 'test12',
105 'password_confirmation': 'test12',
106 'email': 'goodmail@domain.com',
106 'email': 'goodmail@domain.com',
107 'firstname': 'test',
107 'firstname': 'test',
108 'lastname': 'test'})
108 'lastname': 'test'})
109
109
110 msg = validators.ValidUsername()._messages['username_exists']
110 msg = validators.ValidUsername()._messages['username_exists']
111 msg = h.html_escape(msg % {'username': uname})
111 msg = h.html_escape(msg % {'username': uname})
112 response.mustcontain(msg)
112 response.mustcontain(msg)
113
113
114 def test_register_err_same_email(self):
114 def test_register_err_same_email(self):
115 response = self.app.post(url(controller='login', action='register'),
115 response = self.app.post(url(controller='login', action='register'),
116 {'username': 'test_admin_0',
116 {'username': 'test_admin_0',
117 'password': 'test12',
117 'password': 'test12',
118 'password_confirmation': 'test12',
118 'password_confirmation': 'test12',
119 'email': 'test_admin@mail.com',
119 'email': 'test_admin@mail.com',
120 'firstname': 'test',
120 'firstname': 'test',
121 'lastname': 'test'})
121 'lastname': 'test'})
122
122
123 msg = validators.UniqSystemEmail()()._messages['email_taken']
123 msg = validators.UniqSystemEmail()()._messages['email_taken']
124 response.mustcontain(msg)
124 response.mustcontain(msg)
125
125
126 def test_register_err_same_email_case_sensitive(self):
126 def test_register_err_same_email_case_sensitive(self):
127 response = self.app.post(url(controller='login', action='register'),
127 response = self.app.post(url(controller='login', action='register'),
128 {'username': 'test_admin_1',
128 {'username': 'test_admin_1',
129 'password': 'test12',
129 'password': 'test12',
130 'password_confirmation': 'test12',
130 'password_confirmation': 'test12',
131 'email': 'TesT_Admin@mail.COM',
131 'email': 'TesT_Admin@mail.COM',
132 'firstname': 'test',
132 'firstname': 'test',
133 'lastname': 'test'})
133 'lastname': 'test'})
134 msg = validators.UniqSystemEmail()()._messages['email_taken']
134 msg = validators.UniqSystemEmail()()._messages['email_taken']
135 response.mustcontain(msg)
135 response.mustcontain(msg)
136
136
137 def test_register_err_wrong_data(self):
137 def test_register_err_wrong_data(self):
138 response = self.app.post(url(controller='login', action='register'),
138 response = self.app.post(url(controller='login', action='register'),
139 {'username': 'xs',
139 {'username': 'xs',
140 'password': 'test',
140 'password': 'test',
141 'password_confirmation': 'test',
141 'password_confirmation': 'test',
142 'email': 'goodmailm',
142 'email': 'goodmailm',
143 'firstname': 'test',
143 'firstname': 'test',
144 'lastname': 'test'})
144 'lastname': 'test'})
145 self.assertEqual(response.status, '200 OK')
145 self.assertEqual(response.status, '200 OK')
146 response.mustcontain('An email address must contain a single @')
146 response.mustcontain('An email address must contain a single @')
147 response.mustcontain('Enter a value 6 characters long or more')
147 response.mustcontain('Enter a value 6 characters long or more')
148
148
149 def test_register_err_username(self):
149 def test_register_err_username(self):
150 response = self.app.post(url(controller='login', action='register'),
150 response = self.app.post(url(controller='login', action='register'),
151 {'username': 'error user',
151 {'username': 'error user',
152 'password': 'test12',
152 'password': 'test12',
153 'password_confirmation': 'test12',
153 'password_confirmation': 'test12',
154 'email': 'goodmailm',
154 'email': 'goodmailm',
155 'firstname': 'test',
155 'firstname': 'test',
156 'lastname': 'test'})
156 'lastname': 'test'})
157
157
158 response.mustcontain('An email address must contain a single @')
158 response.mustcontain('An email address must contain a single @')
159 response.mustcontain('Username may only contain '
159 response.mustcontain('Username may only contain '
160 'alphanumeric characters underscores, '
160 'alphanumeric characters underscores, '
161 'periods or dashes and must begin with '
161 'periods or dashes and must begin with '
162 'alphanumeric character')
162 'alphanumeric character')
163
163
164 def test_register_err_case_sensitive(self):
164 def test_register_err_case_sensitive(self):
165 usr = 'Test_Admin'
165 usr = 'Test_Admin'
166 response = self.app.post(url(controller='login', action='register'),
166 response = self.app.post(url(controller='login', action='register'),
167 {'username': usr,
167 {'username': usr,
168 'password': 'test12',
168 'password': 'test12',
169 'password_confirmation': 'test12',
169 'password_confirmation': 'test12',
170 'email': 'goodmailm',
170 'email': 'goodmailm',
171 'firstname': 'test',
171 'firstname': 'test',
172 'lastname': 'test'})
172 'lastname': 'test'})
173
173
174 response.mustcontain('An email address must contain a single @')
174 response.mustcontain('An email address must contain a single @')
175 msg = validators.ValidUsername()._messages['username_exists']
175 msg = validators.ValidUsername()._messages['username_exists']
176 msg = h.html_escape(msg % {'username': usr})
176 msg = h.html_escape(msg % {'username': usr})
177 response.mustcontain(msg)
177 response.mustcontain(msg)
178
178
179 def test_register_special_chars(self):
179 def test_register_special_chars(self):
180 response = self.app.post(url(controller='login', action='register'),
180 response = self.app.post(url(controller='login', action='register'),
181 {'username': 'xxxaxn',
181 {'username': 'xxxaxn',
182 'password': 'Δ…Δ‡ΕΊΕΌΔ…Ε›Ε›Ε›Ε›',
182 'password': 'Δ…Δ‡ΕΊΕΌΔ…Ε›Ε›Ε›Ε›',
183 'password_confirmation': 'Δ…Δ‡ΕΊΕΌΔ…Ε›Ε›Ε›Ε›',
183 'password_confirmation': 'Δ…Δ‡ΕΊΕΌΔ…Ε›Ε›Ε›Ε›',
184 'email': 'goodmailm@test.plx',
184 'email': 'goodmailm@test.plx',
185 'firstname': 'test',
185 'firstname': 'test',
186 'lastname': 'test'})
186 'lastname': 'test'})
187
187
188 msg = validators.ValidPassword()._messages['invalid_password']
188 msg = validators.ValidPassword()._messages['invalid_password']
189 response.mustcontain(msg)
189 response.mustcontain(msg)
190
190
191 def test_register_password_mismatch(self):
191 def test_register_password_mismatch(self):
192 response = self.app.post(url(controller='login', action='register'),
192 response = self.app.post(url(controller='login', action='register'),
193 {'username': 'xs',
193 {'username': 'xs',
194 'password': '123qwe',
194 'password': '123qwe',
195 'password_confirmation': 'qwe123',
195 'password_confirmation': 'qwe123',
196 'email': 'goodmailm@test.plxa',
196 'email': 'goodmailm@test.plxa',
197 'firstname': 'test',
197 'firstname': 'test',
198 'lastname': 'test'})
198 'lastname': 'test'})
199 msg = validators.ValidPasswordsMatch()._messages['password_mismatch']
199 msg = validators.ValidPasswordsMatch()._messages['password_mismatch']
200 response.mustcontain(msg)
200 response.mustcontain(msg)
201
201
202 def test_register_ok(self):
202 def test_register_ok(self):
203 username = 'test_regular4'
203 username = 'test_regular4'
204 password = 'qweqwe'
204 password = 'qweqwe'
205 email = 'marcin@test.com'
205 email = 'marcin@test.com'
206 name = 'testname'
206 name = 'testname'
207 lastname = 'testlastname'
207 lastname = 'testlastname'
208
208
209 response = self.app.post(url(controller='login', action='register'),
209 response = self.app.post(url(controller='login', action='register'),
210 {'username': username,
210 {'username': username,
211 'password': password,
211 'password': password,
212 'password_confirmation': password,
212 'password_confirmation': password,
213 'email': email,
213 'email': email,
214 'firstname': name,
214 'firstname': name,
215 'lastname': lastname,
215 'lastname': lastname,
216 'admin': True}) # This should be overriden
216 'admin': True}) # This should be overriden
217 self.assertEqual(response.status, '302 Found')
217 self.assertEqual(response.status, '302 Found')
218 self.checkSessionFlash(response, 'You have successfully registered into rhodecode')
218 self.checkSessionFlash(response, 'You have successfully registered into RhodeCode')
219
219
220 ret = self.Session().query(User).filter(User.username == 'test_regular4').one()
220 ret = self.Session().query(User).filter(User.username == 'test_regular4').one()
221 self.assertEqual(ret.username, username)
221 self.assertEqual(ret.username, username)
222 self.assertEqual(check_password(password, ret.password), True)
222 self.assertEqual(check_password(password, ret.password), True)
223 self.assertEqual(ret.email, email)
223 self.assertEqual(ret.email, email)
224 self.assertEqual(ret.name, name)
224 self.assertEqual(ret.name, name)
225 self.assertEqual(ret.lastname, lastname)
225 self.assertEqual(ret.lastname, lastname)
226 self.assertNotEqual(ret.api_key, None)
226 self.assertNotEqual(ret.api_key, None)
227 self.assertEqual(ret.admin, False)
227 self.assertEqual(ret.admin, False)
228
228
229 def test_forgot_password_wrong_mail(self):
229 def test_forgot_password_wrong_mail(self):
230 bad_email = 'marcin@wrongmail.org'
230 bad_email = 'marcin@wrongmail.org'
231 response = self.app.post(
231 response = self.app.post(
232 url(controller='login', action='password_reset'),
232 url(controller='login', action='password_reset'),
233 {'email': bad_email, }
233 {'email': bad_email, }
234 )
234 )
235
235
236 msg = validators.ValidSystemEmail()._messages['non_existing_email']
236 msg = validators.ValidSystemEmail()._messages['non_existing_email']
237 msg = h.html_escape(msg % {'email': bad_email})
237 msg = h.html_escape(msg % {'email': bad_email})
238 response.mustcontain()
238 response.mustcontain()
239
239
240 def test_forgot_password(self):
240 def test_forgot_password(self):
241 response = self.app.get(url(controller='login',
241 response = self.app.get(url(controller='login',
242 action='password_reset'))
242 action='password_reset'))
243 self.assertEqual(response.status, '200 OK')
243 self.assertEqual(response.status, '200 OK')
244
244
245 username = 'test_password_reset_1'
245 username = 'test_password_reset_1'
246 password = 'qweqwe'
246 password = 'qweqwe'
247 email = 'marcin@python-works.com'
247 email = 'marcin@python-works.com'
248 name = 'passwd'
248 name = 'passwd'
249 lastname = 'reset'
249 lastname = 'reset'
250
250
251 new = User()
251 new = User()
252 new.username = username
252 new.username = username
253 new.password = password
253 new.password = password
254 new.email = email
254 new.email = email
255 new.name = name
255 new.name = name
256 new.lastname = lastname
256 new.lastname = lastname
257 new.api_key = generate_api_key(username)
257 new.api_key = generate_api_key(username)
258 self.Session().add(new)
258 self.Session().add(new)
259 self.Session().commit()
259 self.Session().commit()
260
260
261 response = self.app.post(url(controller='login',
261 response = self.app.post(url(controller='login',
262 action='password_reset'),
262 action='password_reset'),
263 {'email': email, })
263 {'email': email, })
264
264
265 self.checkSessionFlash(response, 'Your password reset link was sent')
265 self.checkSessionFlash(response, 'Your password reset link was sent')
266
266
267 response = response.follow()
267 response = response.follow()
268
268
269 # BAD KEY
269 # BAD KEY
270
270
271 key = "bad"
271 key = "bad"
272 response = self.app.get(url(controller='login',
272 response = self.app.get(url(controller='login',
273 action='password_reset_confirmation',
273 action='password_reset_confirmation',
274 key=key))
274 key=key))
275 self.assertEqual(response.status, '302 Found')
275 self.assertEqual(response.status, '302 Found')
276 self.assertTrue(response.location.endswith(url('reset_password')))
276 self.assertTrue(response.location.endswith(url('reset_password')))
277
277
278 # GOOD KEY
278 # GOOD KEY
279
279
280 key = User.get_by_username(username).api_key
280 key = User.get_by_username(username).api_key
281 response = self.app.get(url(controller='login',
281 response = self.app.get(url(controller='login',
282 action='password_reset_confirmation',
282 action='password_reset_confirmation',
283 key=key))
283 key=key))
284 self.assertEqual(response.status, '302 Found')
284 self.assertEqual(response.status, '302 Found')
285 self.assertTrue(response.location.endswith(url('login_home')))
285 self.assertTrue(response.location.endswith(url('login_home')))
286
286
287 self.checkSessionFlash(response,
287 self.checkSessionFlash(response,
288 ('Your password reset was successful, '
288 ('Your password reset was successful, '
289 'new password has been sent to your email'))
289 'new password has been sent to your email'))
290
290
291 response = response.follow()
291 response = response.follow()
General Comments 0
You need to be logged in to leave comments. Login now