Changeset 1d92bf2 in OpenWorkouts-current for ow/tests/views
- Timestamp:
- Dec 16, 2018, 1:07:04 AM (5 years ago)
- Branches:
- current, feature/docs, master
- Children:
- 6560b8f
- Parents:
- 929097a
- Location:
- ow/tests/views
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/tests/views/test_root.py
r929097a r1d92bf2 20 20 21 21 @pytest.fixture 22 def root(self): 22 def john(self): 23 user = User(firstname='John', lastname='Doe', 24 email='john.doe@example.net') 25 user.password = 's3cr3t' 26 return user 27 28 @pytest.fixture 29 def root(self, john): 23 30 root = OpenWorkouts() 24 root['john'] = User(firstname='John', lastname='Doe', 25 email='john.doe@example.net') 26 root['john'].password = 's3cr3t' 31 root.add_user(john) 27 32 workout = Workout( 28 33 start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), … … 30 35 distance=30, sport='cycling' 31 36 ) 32 root['john'].add_workout(workout)37 john.add_workout(workout) 33 38 return root 34 39 … … 50 55 return request 51 56 52 def test_user_list(self, get_request ):57 def test_user_list(self, get_request, john): 53 58 request = get_request 54 59 response = user_list(request.root, request) 55 assert list(response['users']) == [ request.root['john']]60 assert list(response['users']) == [john] 56 61 57 62 def test_add_user_get(self, get_request): … … 66 71 response = add_user(request.root, request) 67 72 assert 'form' in response 68 # All required fields ( 4) are marked in the form errors73 # All required fields (3) are marked in the form errors 69 74 # You can see which fields are required in the schema 70 75 # ow.schemas.user.UserAddSchema 71 assert len(response['form'].form.errors) == 4 76 errors = response['form'].form.errors 77 assert len(errors) == 3 78 assert 'email' in errors 79 assert 'firstname' in errors 80 assert 'lastname' in errors 72 81 73 82 def test_add_user_post_valid(self, post_request): 74 83 request = post_request 75 request.POST[' uid'] = 'addeduser'84 request.POST['nickname'] = 'addeduser' 76 85 request.POST['email'] = 'addeduser@example.net' 77 86 request.POST['firstname'] = 'added' … … 80 89 assert isinstance(response, HTTPFound) 81 90 assert response.location.endswith('/userlist') 82 assert len(request.root.all_usernames()) == 2 83 assert 'addeduser' in request.root.all_usernames() 91 # 1 nick name, as the default user has no nickname 92 assert len(request.root.all_nicknames) == 1 93 assert 'addeduser' in request.root.all_nicknames -
ow/tests/views/test_user.py
r929097a r1d92bf2 24 24 25 25 @pytest.fixture 26 def root(self): 26 def john(self): 27 user = User(firstname='John', lastname='Doe', 28 email='john.doe@example.net') 29 user.password = 's3cr3t' 30 return user 31 32 @pytest.fixture 33 def root(self, john): 27 34 root = OpenWorkouts() 28 root['john'] = User(firstname='John', lastname='Doe', 29 email='john.doe@example.net') 30 root['john'].password = 's3cr3t' 35 root.add_user(john) 31 36 workout = Workout( 32 37 start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), … … 34 39 distance=30 35 40 ) 36 root['john'].add_workout(workout)41 john.add_workout(workout) 37 42 return root 38 43 … … 44 49 45 50 @pytest.fixture 46 def profile_post_request(self, root ):51 def profile_post_request(self, root, john): 47 52 """ 48 53 This is a valid POST request to update an user profile. 49 54 Form will validate, but nothing will be really updated/changed. 50 55 """ 51 user = root['john']56 user = john 52 57 request = DummyRequest() 53 58 request.root = root … … 94 99 request.POST = MultiDict({ 95 100 'submit': True, 96 ' username': 'JackBlack',101 'nickname': 'JackBlack', 97 102 'email': 'jack.black@example.net', 98 103 'firstname': 'Jack', … … 134 139 assert response.location == '/dashboard' 135 140 136 def test_dashboard(self, dummy_request ):141 def test_dashboard(self, dummy_request, john): 137 142 """ 138 143 Renders the user dashboard 139 144 """ 140 145 request = dummy_request 141 user = request.root['john'] 142 response = user_views.dashboard(user, request) 146 response = user_views.dashboard(john, request) 143 147 assert response == {} 144 148 145 def test_profile(self, dummy_request ):149 def test_profile(self, dummy_request, john): 146 150 """ 147 151 Renders the user profile page 148 152 """ 149 153 request = dummy_request 150 user = request.root['john'] 151 response = user_views.profile(user, request) 154 response = user_views.profile(john, request) 152 155 assert response == {} 153 156 … … 159 162 response = user_views.login(request.root, request) 160 163 assert response['message'] == '' 161 assert response[' username'] == ''164 assert response['email'] == '' 162 165 assert response['password'] == '' 163 166 assert response['redirect_url'] == request.resource_url(request.root) 164 167 165 def test_login_get_return_to(self, dummy_request ):168 def test_login_get_return_to(self, dummy_request, john): 166 169 """ 167 170 GET request to access the login page, if there is a page set to where … … 170 173 """ 171 174 request = dummy_request 172 workout = request.root['john'].workouts()[0]175 workout = john.workouts()[0] 173 176 workout_url = request.resource_url(workout) 174 177 request.params['return_to'] = workout_url … … 176 179 assert response['redirect_url'] == workout_url 177 180 178 def test_login_post_ bad_username(self, dummy_request):181 def test_login_post_wrong_email(self, dummy_request): 179 182 request = dummy_request 180 183 request.method = 'POST' 181 184 request.POST['submit'] = True 182 request.POST[' username'] = 'jack'185 request.POST['email'] = 'jack@example.net' 183 186 response = user_views.login(request.root, request) 184 assert response['message'] == u' Bad username'185 186 def test_login_post_ bad_password(self, dummy_request):187 assert response['message'] == u'Wrong email address' 188 189 def test_login_post_wrong_password(self, dummy_request): 187 190 request = dummy_request 188 191 request.method = 'POST' 189 192 request.POST['submit'] = True 190 request.POST[' username'] = 'john'193 request.POST['email'] = 'john.doe@example.net' 191 194 request.POST['password'] = 'badpassword' 192 195 response = user_views.login(request.root, request) 193 assert response['message'] == u' Badpassword'196 assert response['message'] == u'Wrong password' 194 197 195 198 @patch('ow.views.user.remember') … … 198 201 request.method = 'POST' 199 202 request.POST['submit'] = True 200 request.POST[' username'] = 'john'203 request.POST['email'] = 'john.doe@example.net' 201 204 request.POST['password'] = 's3cr3t' 202 205 response = user_views.login(request.root, request) … … 216 219 217 220 @pytest.mark.parametrize('extension', extensions) 218 def test_profile_picture(self, extension, dummy_request ):221 def test_profile_picture(self, extension, dummy_request, john): 219 222 """ 220 223 GET request to get the profile picture of an user. … … 222 225 request = dummy_request 223 226 # Get the user 224 user = request.root['john']227 user = john 225 228 # Get the path to the image, then open it and copy it to a new Blob 226 229 # object … … 242 245 assert response.content_type == 'image' 243 246 244 def test_edit_profile_get(self, dummy_request ):247 def test_edit_profile_get(self, dummy_request, john): 245 248 """ 246 249 GET request to the edit profile page, returns the form ready to … … 248 251 """ 249 252 request = dummy_request 250 user = request.root['john']253 user = john 251 254 response = user_views.edit_profile(user, request) 252 255 assert isinstance(response['form'], OWFormRenderer) … … 261 264 assert response['form'].data['email'] == 'john.doe@example.net' 262 265 263 def test_edit_profile_post_ok(self, profile_post_request ):266 def test_edit_profile_post_ok(self, profile_post_request, john): 264 267 request = profile_post_request 265 user = request.root['john']268 user = john 266 269 # Update the bio field 267 270 bio = 'Some text about this user' … … 272 275 assert user.bio == bio 273 276 274 def test_edit_profile_post_missing_required(self, profile_post_request): 277 def test_edit_profile_post_missing_required( 278 self, profile_post_request, john): 275 279 request = profile_post_request 276 280 request.POST['email'] = '' 277 user = request.root['john']281 user = john 278 282 response = user_views.edit_profile(user, request) 279 283 assert isinstance(response['form'], OWFormRenderer) … … 284 288 assert response['form'].errors_for('email') == [error] 285 289 286 def test_change_password_get(self, dummy_request ):287 request = dummy_request 288 user = request.root['john']290 def test_change_password_get(self, dummy_request, john): 291 request = dummy_request 292 user = john 289 293 response = user_views.change_password(user, request) 290 294 assert isinstance(response['form'], OWFormRenderer) … … 292 296 assert response['form'].errorlist() == '' 293 297 294 def test_change_password_post_ok(self, passwd_post_request ):298 def test_change_password_post_ok(self, passwd_post_request, john): 295 299 request = passwd_post_request 296 user = request.root['john']300 user = john 297 301 request.POST['old_password'] = 's3cr3t' 298 302 request.POST['password'] = 'h1dd3n s3cr3t' … … 305 309 assert user.check_password('h1dd3n s3cr3t') 306 310 307 def test_change_password_post_no_values(self, passwd_post_request ):311 def test_change_password_post_no_values(self, passwd_post_request, john): 308 312 request = passwd_post_request 309 user = request.root['john']313 user = john 310 314 response = user_views.change_password(user, request) 311 315 assert isinstance(response['form'], OWFormRenderer) … … 322 326 assert user.check_password('s3cr3t') 323 327 324 def test_change_password_post_bad_old_password(self, passwd_post_request): 328 def test_change_password_post_bad_old_password( 329 self, passwd_post_request, john): 325 330 request = passwd_post_request 326 user = request.root['john']331 user = john 327 332 request.POST['old_password'] = 'FAIL PASSWORD' 328 333 request.POST['password'] = 'h1dd3n s3cr3t' … … 338 343 assert not user.check_password('h1dd3n s3cr3t') 339 344 340 def test_change_password_post_password_mismatch(self, passwd_post_request): 345 def test_change_password_post_password_mismatch( 346 self, passwd_post_request, john): 341 347 request = passwd_post_request 342 user = request.root['john']348 user = john 343 349 request.POST['old_password'] = 's3cr3t' 344 350 request.POST['password'] = 'h1dd3n s3cr3ts' … … 363 369 def test_signup_post_ok(self, signup_post_request): 364 370 request = signup_post_request 365 assert 'JackBlack' not in request.root.all_usernames() 371 assert 'jack.black@example.net' not in request.root.emails 372 assert 'JackBlack' not in request.root.all_nicknames 366 373 response = user_views.signup(request.root, request) 367 374 assert isinstance(response, HTTPFound) 368 375 assert response.location == request.resource_url(request.root) 369 assert 'JackBlack' in request.root.all_usernames() 376 assert 'jack.black@example.net' in request.root.emails 377 assert 'JackBlack' in request.root.all_nicknames 370 378 371 379 def test_signup_missing_required(self, signup_post_request): 372 380 request = signup_post_request 373 381 request.POST['email'] = '' 374 assert 'JackBlack' not in request.root.all_usernames() 382 assert 'jack.black@example.net' not in request.root.emails 383 assert 'JackBlack' not in request.root.all_nicknames 375 384 response = user_views.signup(request.root, request) 376 385 assert isinstance(response['form'], OWFormRenderer) … … 382 391 assert errorlist == html_error 383 392 assert response['form'].errors_for('email') == [error] 384 assert 'JackBlack' not in request.root.all_usernames() 385 386 def test_signup_existing_username(self, signup_post_request): 393 assert 'jack.black@example.net' not in request.root.emails 394 assert 'JackBlack' not in request.root.all_nicknames 395 396 def test_signup_existing_nickname(self, signup_post_request, john): 387 397 request = signup_post_request 388 request.POST['username'] = 'john' 389 assert 'JackBlack' not in request.root.all_usernames() 398 # assign john a nickname first 399 john.nickname = 'john' 400 # now set it for the POST request 401 request.POST['nickname'] = 'john' 402 # check jack is not there yet 403 assert 'jack.black@example.net' not in request.root.emails 404 assert 'JackBlack' not in request.root.all_nicknames 405 # now signup as jack, but trying to set the nickname 'john' 390 406 response = user_views.signup(request.root, request) 391 407 assert isinstance(response['form'], OWFormRenderer) 392 error = u'Another user is already registered with the username john'408 error = u'Another user is already using the nickname john' 393 409 html_error = '<ul class="error">' 394 410 html_error += '<li>' + error + '</li>' … … 396 412 errorlist = response['form'].errorlist().replace('\n', '') 397 413 assert errorlist == html_error 398 assert response['form'].errors_for('username') == [error] 399 assert 'JackBlack' not in request.root.all_usernames() 414 assert response['form'].errors_for('nickname') == [error] 415 # all the errors, and jack is not there 416 assert 'jack.black@example.net' not in request.root.emails 417 assert 'JackBlack' not in request.root.all_nicknames 400 418 401 419 def test_signup_existing_email(self, signup_post_request): 402 420 request = signup_post_request 403 421 request.POST['email'] = 'john.doe@example.net' 404 assert 'JackBlack' not in request.root.all_usernames() 422 assert 'jack.black@example.net' not in request.root.emails 423 assert 'JackBlack' not in request.root.all_nicknames 405 424 response = user_views.signup(request.root, request) 406 425 assert isinstance(response['form'], OWFormRenderer) … … 413 432 assert errorlist == html_error 414 433 assert response['form'].errors_for('email') == [error] 415 assert 'JackBlack' not in request.root.all_usernames() 434 assert 'jack.black@example.net' not in request.root.emails 435 assert 'JackBlack' not in request.root.all_nicknames
Note: See TracChangeset
for help on using the changeset viewer.