[5ec3a0b] | 1 | import os |
---|
[421f05f] | 2 | import json |
---|
[55470f9] | 3 | from decimal import Decimal |
---|
[5ec3a0b] | 4 | from datetime import datetime, timedelta, timezone |
---|
| 5 | from shutil import copyfileobj |
---|
| 6 | from unittest.mock import Mock, patch |
---|
| 7 | |
---|
| 8 | import pytest |
---|
| 9 | |
---|
| 10 | from ZODB.blob import Blob |
---|
| 11 | |
---|
| 12 | from pyramid.testing import DummyRequest |
---|
| 13 | from pyramid.httpexceptions import HTTPFound |
---|
| 14 | from pyramid.response import Response |
---|
| 15 | |
---|
| 16 | from webob.multidict import MultiDict |
---|
| 17 | |
---|
| 18 | from ow.models.root import OpenWorkouts |
---|
| 19 | from ow.models.user import User |
---|
| 20 | from ow.models.workout import Workout |
---|
| 21 | from ow.views.renderers import OWFormRenderer |
---|
| 22 | import ow.views.user as user_views |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | class TestUserViews(object): |
---|
| 26 | |
---|
| 27 | @pytest.fixture |
---|
[1d92bf2] | 28 | def john(self): |
---|
| 29 | user = User(firstname='John', lastname='Doe', |
---|
| 30 | email='john.doe@example.net') |
---|
| 31 | user.password = 's3cr3t' |
---|
| 32 | return user |
---|
| 33 | |
---|
| 34 | @pytest.fixture |
---|
| 35 | def root(self, john): |
---|
[5ec3a0b] | 36 | root = OpenWorkouts() |
---|
[1d92bf2] | 37 | root.add_user(john) |
---|
[5ec3a0b] | 38 | workout = Workout( |
---|
| 39 | start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), |
---|
| 40 | duration=timedelta(minutes=60), |
---|
| 41 | distance=30 |
---|
| 42 | ) |
---|
[1d92bf2] | 43 | john.add_workout(workout) |
---|
[5ec3a0b] | 44 | return root |
---|
| 45 | |
---|
| 46 | @pytest.fixture |
---|
| 47 | def dummy_request(self, root): |
---|
| 48 | request = DummyRequest() |
---|
| 49 | request.root = root |
---|
| 50 | return request |
---|
| 51 | |
---|
| 52 | @pytest.fixture |
---|
[1d92bf2] | 53 | def profile_post_request(self, root, john): |
---|
[5ec3a0b] | 54 | """ |
---|
| 55 | This is a valid POST request to update an user profile. |
---|
| 56 | Form will validate, but nothing will be really updated/changed. |
---|
| 57 | """ |
---|
[1d92bf2] | 58 | user = john |
---|
[5ec3a0b] | 59 | request = DummyRequest() |
---|
| 60 | request.root = root |
---|
| 61 | request.method = 'POST' |
---|
| 62 | request.POST = MultiDict({ |
---|
| 63 | 'submit': True, |
---|
| 64 | 'firstname': user.firstname, |
---|
| 65 | 'lastname': user.lastname, |
---|
| 66 | 'email': user.email, |
---|
| 67 | 'bio': user.bio, |
---|
| 68 | 'weight': user.weight, |
---|
| 69 | 'height': user.height, |
---|
| 70 | 'gender': user.gender, |
---|
| 71 | 'birth_date': user.birth_date, |
---|
| 72 | 'picture': user.picture, |
---|
| 73 | }) |
---|
| 74 | return request |
---|
| 75 | |
---|
| 76 | @pytest.fixture |
---|
| 77 | def passwd_post_request(self, root): |
---|
| 78 | """ |
---|
| 79 | This is a valid POST request to change the user password, but |
---|
| 80 | the form will not validate (empty fields) |
---|
| 81 | """ |
---|
| 82 | request = DummyRequest() |
---|
| 83 | request.root = root |
---|
| 84 | request.method = 'POST' |
---|
| 85 | request.POST = MultiDict({ |
---|
| 86 | 'submit': True, |
---|
| 87 | 'old_password': '', |
---|
| 88 | 'password': '', |
---|
| 89 | 'password_confirm': '' |
---|
| 90 | }) |
---|
| 91 | return request |
---|
| 92 | |
---|
| 93 | @pytest.fixture |
---|
| 94 | def signup_post_request(self, root): |
---|
| 95 | """ |
---|
| 96 | This is a valid POST request to signup a new user. |
---|
| 97 | """ |
---|
| 98 | request = DummyRequest() |
---|
| 99 | request.root = root |
---|
| 100 | request.method = 'POST' |
---|
| 101 | request.POST = MultiDict({ |
---|
| 102 | 'submit': True, |
---|
[1d92bf2] | 103 | 'nickname': 'JackBlack', |
---|
[5ec3a0b] | 104 | 'email': 'jack.black@example.net', |
---|
| 105 | 'firstname': 'Jack', |
---|
| 106 | 'lastname': 'Black', |
---|
| 107 | 'password': 'j4ck s3cr3t', |
---|
| 108 | 'password_confirm': 'j4ck s3cr3t' |
---|
| 109 | }) |
---|
| 110 | return request |
---|
| 111 | |
---|
| 112 | def test_dashboard_redirect_unauthenticated(self, root): |
---|
| 113 | """ |
---|
| 114 | Anoymous access to the root object, send the user to the login page. |
---|
| 115 | |
---|
| 116 | Instead of reusing the DummyRequest from the request fixture, we do |
---|
| 117 | Mock fully the request here, because we need to use |
---|
| 118 | authenticated_userid, which cannot be easily set in the DummyRequest |
---|
| 119 | """ |
---|
| 120 | request = DummyRequest() |
---|
| 121 | request.root = root |
---|
| 122 | response = user_views.dashboard_redirect(root, request) |
---|
| 123 | assert isinstance(response, HTTPFound) |
---|
| 124 | assert response.location == request.resource_url(root, 'login') |
---|
| 125 | |
---|
[fe6089a] | 126 | def test_dashboard_redirect_authenticated(self, root, john): |
---|
[5ec3a0b] | 127 | """ |
---|
| 128 | Authenticated user accesing the root object, send the user to her |
---|
| 129 | dashboard |
---|
| 130 | |
---|
| 131 | Instead of reusing the DummyRequest from the request fixture, we do |
---|
| 132 | Mock fully the request here, because we need to use |
---|
| 133 | authenticated_userid, which cannot be easily set in the DummyRequest |
---|
| 134 | """ |
---|
[fe6089a] | 135 | alt_request = DummyRequest() |
---|
[5ec3a0b] | 136 | request = Mock() |
---|
| 137 | request.root = root |
---|
[fe6089a] | 138 | request.authenticated_userid = str(john.uid) |
---|
| 139 | request.resource_url = alt_request.resource_url |
---|
[5ec3a0b] | 140 | response = user_views.dashboard_redirect(root, request) |
---|
| 141 | assert isinstance(response, HTTPFound) |
---|
[fe6089a] | 142 | assert response.location == request.resource_url(john) |
---|
| 143 | # if authenticated_userid is the id of an user that does not exist |
---|
| 144 | # anymore, we send the user to the logout page |
---|
| 145 | request.authenticated_userid = 'faked-uid' |
---|
| 146 | response = user_views.dashboard_redirect(root, request) |
---|
| 147 | assert isinstance(response, HTTPFound) |
---|
| 148 | assert response.location == request.resource_url(root, 'logout') |
---|
[5ec3a0b] | 149 | |
---|
[1d92bf2] | 150 | def test_dashboard(self, dummy_request, john): |
---|
[5ec3a0b] | 151 | """ |
---|
| 152 | Renders the user dashboard |
---|
| 153 | """ |
---|
| 154 | request = dummy_request |
---|
[1d92bf2] | 155 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 156 | assert len(response) == 6 |
---|
[2d91474] | 157 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 158 | assert response['current_year'] == datetime.now().year |
---|
| 159 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 160 | # this user has a single workout, in 2015 |
---|
| 161 | assert response['viewing_year'] == 2015 |
---|
| 162 | assert response['viewing_month'] == 6 |
---|
| 163 | assert response['workouts'] == [w for w in john.workouts()] |
---|
| 164 | |
---|
| 165 | def test_dashboard_year(self, dummy_request, john): |
---|
| 166 | """ |
---|
| 167 | Renders the user dashboard for a chosen year. |
---|
| 168 | """ |
---|
| 169 | request = dummy_request |
---|
| 170 | # first test the year for which we know there is a workout |
---|
| 171 | request.GET['year'] = 2015 |
---|
| 172 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 173 | assert len(response) == 6 |
---|
[2d91474] | 174 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 175 | assert response['current_year'] == datetime.now().year |
---|
| 176 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 177 | # this user has a single workout, in 2015 |
---|
| 178 | assert response['viewing_year'] == 2015 |
---|
| 179 | assert response['viewing_month'] == 6 |
---|
| 180 | assert response['workouts'] == [w for w in john.workouts()] |
---|
| 181 | # now, a year we know there is no workout info |
---|
| 182 | request.GET['year'] = 2000 |
---|
| 183 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 184 | assert len(response) == 6 |
---|
[2d91474] | 185 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 186 | assert response['current_year'] == datetime.now().year |
---|
| 187 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 188 | # this user has a single workout, in 2015 |
---|
| 189 | assert response['viewing_year'] == 2000 |
---|
| 190 | # we have no data for that year and we didn't ask for a certain month, |
---|
| 191 | # so the passing value for that is None |
---|
| 192 | assert response['viewing_month'] is None |
---|
| 193 | assert response['workouts'] == [] |
---|
| 194 | |
---|
| 195 | def test_dashboard_year_month(self, dummy_request, john): |
---|
| 196 | """ |
---|
| 197 | Renders the user dashboard for a chosen year and month. |
---|
| 198 | """ |
---|
| 199 | request = dummy_request |
---|
| 200 | # first test the year/month for which we know there is a workout |
---|
| 201 | request.GET['year'] = 2015 |
---|
| 202 | request.GET['month'] = 6 |
---|
| 203 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 204 | assert len(response) == 6 |
---|
[2d91474] | 205 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 206 | assert response['current_year'] == datetime.now().year |
---|
| 207 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 208 | # this user has a single workout, in 2015 |
---|
| 209 | assert response['viewing_year'] == 2015 |
---|
| 210 | assert response['viewing_month'] == 6 |
---|
| 211 | assert response['workouts'] == [w for w in john.workouts()] |
---|
| 212 | # now, change month to one without values |
---|
| 213 | request.GET['month'] = 2 |
---|
| 214 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 215 | assert len(response) == 6 |
---|
[2d91474] | 216 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 217 | assert response['current_year'] == datetime.now().year |
---|
| 218 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 219 | # this user has a single workout, in 2015 |
---|
| 220 | assert response['viewing_year'] == 2015 |
---|
| 221 | assert response['viewing_month'] == 2 |
---|
| 222 | assert response['workouts'] == [] |
---|
| 223 | # now the month with data, but in a different year |
---|
| 224 | request.GET['year'] = 2010 |
---|
| 225 | request.GET['month'] = 6 |
---|
| 226 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 227 | assert len(response) == 6 |
---|
[2d91474] | 228 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 229 | assert response['current_year'] == datetime.now().year |
---|
| 230 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 231 | # this user has a single workout, in 2015 |
---|
| 232 | assert response['viewing_year'] == 2010 |
---|
| 233 | assert response['viewing_month'] == 6 |
---|
| 234 | assert response['workouts'] == [] |
---|
| 235 | |
---|
| 236 | def test_dashboard_month(self, dummy_request, john): |
---|
| 237 | """ |
---|
| 238 | Passing a month without a year when rendering the dashboard. The last |
---|
| 239 | year for which workout data is available is assumed |
---|
| 240 | """ |
---|
| 241 | request = dummy_request |
---|
| 242 | # Set a month without workout data |
---|
| 243 | request.GET['month'] = 5 |
---|
| 244 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 245 | assert len(response) == 6 |
---|
[2d91474] | 246 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 247 | assert response['current_year'] == datetime.now().year |
---|
| 248 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 249 | # this user has a single workout, in 2015 |
---|
| 250 | assert response['viewing_year'] == 2015 |
---|
| 251 | assert response['viewing_month'] == 5 |
---|
| 252 | assert response['workouts'] == [] |
---|
| 253 | # now a month with data |
---|
| 254 | request.GET['month'] = 6 |
---|
| 255 | response = user_views.dashboard(john, request) |
---|
[5bdfbfb] | 256 | assert len(response) == 6 |
---|
[2d91474] | 257 | assert 'month_name' in response.keys() |
---|
[5bdfbfb] | 258 | assert response['current_year'] == datetime.now().year |
---|
| 259 | assert response['current_day_name'] == datetime.now().strftime('%a') |
---|
[2d91474] | 260 | # this user has a single workout, in 2015 |
---|
| 261 | assert response['viewing_year'] == 2015 |
---|
| 262 | assert response['viewing_month'] == 6 |
---|
| 263 | assert response['workouts'] == [w for w in john.workouts()] |
---|
[5ec3a0b] | 264 | |
---|
[1d92bf2] | 265 | def test_profile(self, dummy_request, john): |
---|
[5ec3a0b] | 266 | """ |
---|
| 267 | Renders the user profile page |
---|
| 268 | """ |
---|
| 269 | request = dummy_request |
---|
[dbfab70] | 270 | # profile page for the current day (no workouts avalable) |
---|
[1d92bf2] | 271 | response = user_views.profile(john, request) |
---|
[55470f9] | 272 | assert len(response.keys()) == 4 |
---|
[bed4f06] | 273 | current_month = datetime.now(timezone.utc).strftime('%Y-%m') |
---|
| 274 | assert response['current_month'] == current_month |
---|
[5cf5787] | 275 | assert response['current_week'] is None |
---|
[dbfab70] | 276 | assert response['workouts'] == [] |
---|
[55470f9] | 277 | assert response['totals'] == { |
---|
| 278 | 'distance': Decimal(0), |
---|
| 279 | 'time': timedelta(0), |
---|
| 280 | 'elevation': Decimal(0) |
---|
| 281 | } |
---|
[dbfab70] | 282 | # profile page for a previous date, that has workouts |
---|
| 283 | request.GET['year'] = 2015 |
---|
[55470f9] | 284 | request.GET['month'] = 6 |
---|
[dbfab70] | 285 | response = user_views.profile(john, request) |
---|
[55470f9] | 286 | assert len(response.keys()) == 4 |
---|
| 287 | assert response['current_month'] == '2015-06' |
---|
[5cf5787] | 288 | assert response['current_week'] is None |
---|
[55470f9] | 289 | workouts = john.workouts(2015, 6) |
---|
| 290 | assert response['workouts'] == workouts |
---|
| 291 | assert response['totals'] == { |
---|
| 292 | 'distance': workouts[0].distance, |
---|
| 293 | 'time': workouts[0].duration, |
---|
| 294 | 'elevation': Decimal(0) |
---|
| 295 | } |
---|
[5cf5787] | 296 | # same, passing a week, first on a week without workouts |
---|
| 297 | request.GET['year'] = 2015 |
---|
[55470f9] | 298 | request.GET['month'] = 6 |
---|
[5cf5787] | 299 | request.GET['week'] = 25 |
---|
| 300 | response = user_views.profile(john, request) |
---|
[55470f9] | 301 | assert len(response.keys()) == 4 |
---|
| 302 | assert response['current_month'] == '2015-06' |
---|
| 303 | assert response['current_week'] == 25 |
---|
[5cf5787] | 304 | assert response['workouts'] == [] |
---|
[55470f9] | 305 | assert response['totals'] == { |
---|
| 306 | 'distance': Decimal(0), |
---|
| 307 | 'time': timedelta(0), |
---|
| 308 | 'elevation': Decimal(0) |
---|
| 309 | } |
---|
| 310 | # now in a week with workouts |
---|
[5cf5787] | 311 | request.GET['year'] = 2015 |
---|
[55470f9] | 312 | request.GET['month'] = 6 |
---|
[5cf5787] | 313 | request.GET['week'] = 26 |
---|
| 314 | response = user_views.profile(john, request) |
---|
[55470f9] | 315 | assert len(response.keys()) == 4 |
---|
| 316 | assert response['current_month'] == '2015-06' |
---|
| 317 | assert response['current_week'] == 26 |
---|
| 318 | workouts = john.workouts(2015, 6) |
---|
| 319 | assert response['workouts'] == workouts |
---|
| 320 | assert response['totals'] == { |
---|
| 321 | 'distance': workouts[0].distance, |
---|
| 322 | 'time': workouts[0].duration, |
---|
| 323 | 'elevation': Decimal(0) |
---|
| 324 | } |
---|
[5ec3a0b] | 325 | |
---|
| 326 | def test_login_get(self, dummy_request): |
---|
| 327 | """ |
---|
| 328 | GET request to access the login page |
---|
| 329 | """ |
---|
| 330 | request = dummy_request |
---|
| 331 | response = user_views.login(request.root, request) |
---|
| 332 | assert response['message'] == '' |
---|
[1d92bf2] | 333 | assert response['email'] == '' |
---|
[5ec3a0b] | 334 | assert response['password'] == '' |
---|
| 335 | assert response['redirect_url'] == request.resource_url(request.root) |
---|
| 336 | |
---|
[1d92bf2] | 337 | def test_login_get_return_to(self, dummy_request, john): |
---|
[5ec3a0b] | 338 | """ |
---|
| 339 | GET request to access the login page, if there is a page set to where |
---|
| 340 | the user should be sent to, the response "redirect_url" key will have |
---|
| 341 | such url |
---|
| 342 | """ |
---|
| 343 | request = dummy_request |
---|
[1d92bf2] | 344 | workout = john.workouts()[0] |
---|
[5ec3a0b] | 345 | workout_url = request.resource_url(workout) |
---|
| 346 | request.params['return_to'] = workout_url |
---|
| 347 | response = user_views.login(request.root, request) |
---|
| 348 | assert response['redirect_url'] == workout_url |
---|
| 349 | |
---|
[1d92bf2] | 350 | def test_login_post_wrong_email(self, dummy_request): |
---|
[5ec3a0b] | 351 | request = dummy_request |
---|
| 352 | request.method = 'POST' |
---|
| 353 | request.POST['submit'] = True |
---|
[1d92bf2] | 354 | request.POST['email'] = 'jack@example.net' |
---|
[5ec3a0b] | 355 | response = user_views.login(request.root, request) |
---|
[1d92bf2] | 356 | assert response['message'] == u'Wrong email address' |
---|
[5ec3a0b] | 357 | |
---|
[1d92bf2] | 358 | def test_login_post_wrong_password(self, dummy_request): |
---|
[5ec3a0b] | 359 | request = dummy_request |
---|
| 360 | request.method = 'POST' |
---|
| 361 | request.POST['submit'] = True |
---|
[1d92bf2] | 362 | request.POST['email'] = 'john.doe@example.net' |
---|
[5ec3a0b] | 363 | request.POST['password'] = 'badpassword' |
---|
| 364 | response = user_views.login(request.root, request) |
---|
[1d92bf2] | 365 | assert response['message'] == u'Wrong password' |
---|
[5ec3a0b] | 366 | |
---|
| 367 | @patch('ow.views.user.remember') |
---|
[f24ad73] | 368 | def test_login_post_ok(self, rem, dummy_request, john): |
---|
[5ec3a0b] | 369 | request = dummy_request |
---|
| 370 | request.method = 'POST' |
---|
| 371 | request.POST['submit'] = True |
---|
[1d92bf2] | 372 | request.POST['email'] = 'john.doe@example.net' |
---|
[5ec3a0b] | 373 | request.POST['password'] = 's3cr3t' |
---|
| 374 | response = user_views.login(request.root, request) |
---|
| 375 | assert isinstance(response, HTTPFound) |
---|
| 376 | assert rem.called |
---|
[f24ad73] | 377 | assert response.location == request.resource_url(john) |
---|
[5ec3a0b] | 378 | |
---|
| 379 | @patch('ow.views.user.forget') |
---|
| 380 | def test_logout(self, forg, dummy_request): |
---|
| 381 | request = dummy_request |
---|
| 382 | response = user_views.logout(request.root, request) |
---|
| 383 | assert isinstance(response, HTTPFound) |
---|
| 384 | assert forg.called |
---|
| 385 | assert response.location == request.resource_url(request.root) |
---|
| 386 | |
---|
| 387 | extensions = ('png', 'jpg', 'jpeg', 'gif') |
---|
| 388 | |
---|
| 389 | @pytest.mark.parametrize('extension', extensions) |
---|
[1d92bf2] | 390 | def test_profile_picture(self, extension, dummy_request, john): |
---|
[5ec3a0b] | 391 | """ |
---|
| 392 | GET request to get the profile picture of an user. |
---|
| 393 | """ |
---|
| 394 | request = dummy_request |
---|
| 395 | # Get the user |
---|
[1d92bf2] | 396 | user = john |
---|
[5ec3a0b] | 397 | # Get the path to the image, then open it and copy it to a new Blob |
---|
| 398 | # object |
---|
| 399 | path = 'fixtures/image.' + extension |
---|
| 400 | image_path = os.path.join( |
---|
| 401 | os.path.dirname(os.path.dirname(__file__)), path) |
---|
| 402 | blob = Blob() |
---|
| 403 | with open(image_path, 'rb') as infile, blob.open('w') as out: |
---|
| 404 | infile.seek(0) |
---|
| 405 | copyfileobj(infile, out) |
---|
| 406 | |
---|
| 407 | # Set the blob with the picture |
---|
| 408 | user.picture = blob |
---|
| 409 | |
---|
| 410 | # Call the profile_picture view |
---|
| 411 | response = user_views.profile_picture(user, request) |
---|
| 412 | assert isinstance(response, Response) |
---|
| 413 | assert response.status_int == 200 |
---|
| 414 | assert response.content_type == 'image' |
---|
| 415 | |
---|
[1d92bf2] | 416 | def test_edit_profile_get(self, dummy_request, john): |
---|
[5ec3a0b] | 417 | """ |
---|
| 418 | GET request to the edit profile page, returns the form ready to |
---|
| 419 | be rendered |
---|
| 420 | """ |
---|
| 421 | request = dummy_request |
---|
[1d92bf2] | 422 | user = john |
---|
[5ec3a0b] | 423 | response = user_views.edit_profile(user, request) |
---|
| 424 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 425 | # no errors in the form (first load) |
---|
| 426 | assert response['form'].errorlist() == '' |
---|
| 427 | # the form carries along the proper data keys, taken from the |
---|
| 428 | # loaded user profile |
---|
[722ae18] | 429 | data = ['firstname', 'lastname', 'email', 'nickname', 'bio', |
---|
[31adfa5] | 430 | 'birth_date', 'height', 'weight', 'gender', 'timezone'] |
---|
[5ec3a0b] | 431 | assert list(response['form'].data.keys()) == data |
---|
| 432 | # and check the email to see data is properly loaded |
---|
| 433 | assert response['form'].data['email'] == 'john.doe@example.net' |
---|
| 434 | |
---|
[1d92bf2] | 435 | def test_edit_profile_post_ok(self, profile_post_request, john): |
---|
[5ec3a0b] | 436 | request = profile_post_request |
---|
[1d92bf2] | 437 | user = john |
---|
[5ec3a0b] | 438 | # Update the bio field |
---|
| 439 | bio = 'Some text about this user' |
---|
| 440 | request.POST['bio'] = bio |
---|
| 441 | response = user_views.edit_profile(user, request) |
---|
| 442 | assert isinstance(response, HTTPFound) |
---|
| 443 | assert response.location == request.resource_url(user, 'profile') |
---|
| 444 | assert user.bio == bio |
---|
| 445 | |
---|
[1d92bf2] | 446 | def test_edit_profile_post_missing_required( |
---|
| 447 | self, profile_post_request, john): |
---|
[5ec3a0b] | 448 | request = profile_post_request |
---|
| 449 | request.POST['email'] = '' |
---|
[1d92bf2] | 450 | user = john |
---|
[5ec3a0b] | 451 | response = user_views.edit_profile(user, request) |
---|
| 452 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 453 | # error on the missing email field |
---|
| 454 | error = u'Please enter an email address' |
---|
| 455 | html_error = u'<ul class="error"><li>' + error + '</li></ul>' |
---|
| 456 | assert response['form'].errorlist() == html_error |
---|
| 457 | assert response['form'].errors_for('email') == [error] |
---|
| 458 | |
---|
[fe6089a] | 459 | def test_edit_profile_post_ok_picture_empty_bytes( |
---|
| 460 | self, profile_post_request, john): |
---|
| 461 | """ |
---|
| 462 | POST request with an empty picture, the content of |
---|
| 463 | request['POST'].picture is a empty bytes string (b'') which triggers |
---|
| 464 | a bug in formencode, we put a fix in place, test that |
---|
| 465 | (more in ow.user.views.edit_profile) |
---|
| 466 | """ |
---|
| 467 | # for the purposes of this test, we can mock the picture |
---|
| 468 | picture = Mock() |
---|
| 469 | john.picture = picture |
---|
| 470 | request = profile_post_request |
---|
| 471 | user = john |
---|
| 472 | # Mimic what happens when a picture is not provided by the user |
---|
| 473 | request.POST['picture'] = b'' |
---|
| 474 | response = user_views.edit_profile(user, request) |
---|
| 475 | assert isinstance(response, HTTPFound) |
---|
| 476 | assert response.location == request.resource_url(user, 'profile') |
---|
| 477 | assert user.picture == picture |
---|
| 478 | |
---|
| 479 | def test_edit_profile_post_ok_missing_picture( |
---|
| 480 | self, profile_post_request, john): |
---|
| 481 | """ |
---|
| 482 | POST request without picture |
---|
| 483 | """ |
---|
| 484 | # for the purposes of this test, we can mock the picture |
---|
| 485 | picture = Mock() |
---|
| 486 | john.picture = picture |
---|
| 487 | request = profile_post_request |
---|
| 488 | user = john |
---|
| 489 | # No pic is provided in the request POST values |
---|
| 490 | del request.POST['picture'] |
---|
| 491 | response = user_views.edit_profile(user, request) |
---|
| 492 | assert isinstance(response, HTTPFound) |
---|
| 493 | assert response.location == request.resource_url(user, 'profile') |
---|
| 494 | assert user.picture == picture |
---|
| 495 | |
---|
| 496 | def test_edit_profile_post_ok_nickname(self, profile_post_request, john): |
---|
| 497 | """ |
---|
| 498 | User with a nickname set saves profile without changing the profile, |
---|
| 499 | we have to be sure there are no "nickname already in use" errors |
---|
| 500 | """ |
---|
| 501 | request = profile_post_request |
---|
| 502 | user = john |
---|
| 503 | user.nickname = 'mr_jones' |
---|
| 504 | # add the nickname, the default post request has not a nickname set |
---|
| 505 | request.POST['nickname'] = 'mr_jones' |
---|
| 506 | response = user_views.edit_profile(user, request) |
---|
| 507 | assert isinstance(response, HTTPFound) |
---|
| 508 | assert response.location == request.resource_url(user, 'profile') |
---|
| 509 | |
---|
[1d92bf2] | 510 | def test_change_password_get(self, dummy_request, john): |
---|
[5ec3a0b] | 511 | request = dummy_request |
---|
[1d92bf2] | 512 | user = john |
---|
[5ec3a0b] | 513 | response = user_views.change_password(user, request) |
---|
| 514 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 515 | # no errors in the form (first load) |
---|
| 516 | assert response['form'].errorlist() == '' |
---|
| 517 | |
---|
[1d92bf2] | 518 | def test_change_password_post_ok(self, passwd_post_request, john): |
---|
[5ec3a0b] | 519 | request = passwd_post_request |
---|
[1d92bf2] | 520 | user = john |
---|
[5ec3a0b] | 521 | request.POST['old_password'] = 's3cr3t' |
---|
| 522 | request.POST['password'] = 'h1dd3n s3cr3t' |
---|
| 523 | request.POST['password_confirm'] = 'h1dd3n s3cr3t' |
---|
| 524 | response = user_views.change_password(user, request) |
---|
| 525 | assert isinstance(response, HTTPFound) |
---|
| 526 | assert response.location == request.resource_url(user, 'profile') |
---|
| 527 | # password was changed |
---|
| 528 | assert not user.check_password('s3cr3t') |
---|
| 529 | assert user.check_password('h1dd3n s3cr3t') |
---|
| 530 | |
---|
[1d92bf2] | 531 | def test_change_password_post_no_values(self, passwd_post_request, john): |
---|
[5ec3a0b] | 532 | request = passwd_post_request |
---|
[1d92bf2] | 533 | user = john |
---|
[5ec3a0b] | 534 | response = user_views.change_password(user, request) |
---|
| 535 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 536 | error = u'Please enter a value' |
---|
| 537 | html_error = u'<ul class="error">' |
---|
| 538 | html_error += ('<li>' + error + '</li>') * 3 # 3 fields |
---|
| 539 | html_error += '</ul>' |
---|
| 540 | errorlist = response['form'].errorlist().replace('\n', '') |
---|
| 541 | assert errorlist == html_error |
---|
| 542 | assert response['form'].errors_for('old_password') == [error] |
---|
| 543 | assert response['form'].errors_for('password') == [error] |
---|
| 544 | assert response['form'].errors_for('password_confirm') == [error] |
---|
| 545 | # password was not changed |
---|
| 546 | assert user.check_password('s3cr3t') |
---|
| 547 | |
---|
[1d92bf2] | 548 | def test_change_password_post_bad_old_password( |
---|
| 549 | self, passwd_post_request, john): |
---|
[5ec3a0b] | 550 | request = passwd_post_request |
---|
[1d92bf2] | 551 | user = john |
---|
[5ec3a0b] | 552 | request.POST['old_password'] = 'FAIL PASSWORD' |
---|
| 553 | request.POST['password'] = 'h1dd3n s3cr3t' |
---|
| 554 | request.POST['password_confirm'] = 'h1dd3n s3cr3t' |
---|
| 555 | response = user_views.change_password(user, request) |
---|
| 556 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 557 | error = u'The given password does not match the existing one ' |
---|
| 558 | html_error = u'<ul class="error"><li>' + error + '</li></ul>' |
---|
| 559 | assert response['form'].errorlist() == html_error |
---|
| 560 | assert response['form'].errors_for('old_password') == [error] |
---|
| 561 | # password was not changed |
---|
| 562 | assert user.check_password('s3cr3t') |
---|
| 563 | assert not user.check_password('h1dd3n s3cr3t') |
---|
| 564 | |
---|
[1d92bf2] | 565 | def test_change_password_post_password_mismatch( |
---|
| 566 | self, passwd_post_request, john): |
---|
[5ec3a0b] | 567 | request = passwd_post_request |
---|
[1d92bf2] | 568 | user = john |
---|
[5ec3a0b] | 569 | request.POST['old_password'] = 's3cr3t' |
---|
| 570 | request.POST['password'] = 'h1dd3n s3cr3ts' |
---|
| 571 | request.POST['password_confirm'] = 'h1dd3n s3cr3t' |
---|
| 572 | response = user_views.change_password(user, request) |
---|
| 573 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 574 | error = u'Fields do not match' |
---|
| 575 | html_error = u'<ul class="error"><li>' + error + '</li></ul>' |
---|
| 576 | assert response['form'].errorlist() == html_error |
---|
| 577 | assert response['form'].errors_for('password_confirm') == [error] |
---|
| 578 | # password was not changed |
---|
| 579 | assert user.check_password('s3cr3t') |
---|
| 580 | assert not user.check_password('h1dd3n s3cr3t') |
---|
| 581 | |
---|
| 582 | def test_signup_get(self, dummy_request): |
---|
| 583 | request = dummy_request |
---|
| 584 | response = user_views.signup(request.root, request) |
---|
| 585 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 586 | # no errors in the form (first load) |
---|
| 587 | assert response['form'].errorlist() == '' |
---|
| 588 | |
---|
| 589 | def test_signup_post_ok(self, signup_post_request): |
---|
| 590 | request = signup_post_request |
---|
[1d92bf2] | 591 | assert 'jack.black@example.net' not in request.root.emails |
---|
| 592 | assert 'JackBlack' not in request.root.all_nicknames |
---|
[5ec3a0b] | 593 | response = user_views.signup(request.root, request) |
---|
| 594 | assert isinstance(response, HTTPFound) |
---|
| 595 | assert response.location == request.resource_url(request.root) |
---|
[1d92bf2] | 596 | assert 'jack.black@example.net' in request.root.emails |
---|
| 597 | assert 'JackBlack' in request.root.all_nicknames |
---|
[5ec3a0b] | 598 | |
---|
| 599 | def test_signup_missing_required(self, signup_post_request): |
---|
| 600 | request = signup_post_request |
---|
| 601 | request.POST['email'] = '' |
---|
[1d92bf2] | 602 | assert 'jack.black@example.net' not in request.root.emails |
---|
| 603 | assert 'JackBlack' not in request.root.all_nicknames |
---|
[5ec3a0b] | 604 | response = user_views.signup(request.root, request) |
---|
| 605 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 606 | error = u'Please enter an email address' |
---|
| 607 | html_error = '<ul class="error">' |
---|
| 608 | html_error += '<li>' + error + '</li>' |
---|
| 609 | html_error += '</ul>' |
---|
| 610 | errorlist = response['form'].errorlist().replace('\n', '') |
---|
| 611 | assert errorlist == html_error |
---|
| 612 | assert response['form'].errors_for('email') == [error] |
---|
[1d92bf2] | 613 | assert 'jack.black@example.net' not in request.root.emails |
---|
| 614 | assert 'JackBlack' not in request.root.all_nicknames |
---|
[5ec3a0b] | 615 | |
---|
[1d92bf2] | 616 | def test_signup_existing_nickname(self, signup_post_request, john): |
---|
[5ec3a0b] | 617 | request = signup_post_request |
---|
[1d92bf2] | 618 | # assign john a nickname first |
---|
| 619 | john.nickname = 'john' |
---|
| 620 | # now set it for the POST request |
---|
| 621 | request.POST['nickname'] = 'john' |
---|
| 622 | # check jack is not there yet |
---|
| 623 | assert 'jack.black@example.net' not in request.root.emails |
---|
| 624 | assert 'JackBlack' not in request.root.all_nicknames |
---|
| 625 | # now signup as jack, but trying to set the nickname 'john' |
---|
[5ec3a0b] | 626 | response = user_views.signup(request.root, request) |
---|
| 627 | assert isinstance(response['form'], OWFormRenderer) |
---|
[1d92bf2] | 628 | error = u'Another user is already using the nickname john' |
---|
[5ec3a0b] | 629 | html_error = '<ul class="error">' |
---|
| 630 | html_error += '<li>' + error + '</li>' |
---|
| 631 | html_error += '</ul>' |
---|
| 632 | errorlist = response['form'].errorlist().replace('\n', '') |
---|
| 633 | assert errorlist == html_error |
---|
[1d92bf2] | 634 | assert response['form'].errors_for('nickname') == [error] |
---|
| 635 | # all the errors, and jack is not there |
---|
| 636 | assert 'jack.black@example.net' not in request.root.emails |
---|
| 637 | assert 'JackBlack' not in request.root.all_nicknames |
---|
[5ec3a0b] | 638 | |
---|
| 639 | def test_signup_existing_email(self, signup_post_request): |
---|
| 640 | request = signup_post_request |
---|
| 641 | request.POST['email'] = 'john.doe@example.net' |
---|
[1d92bf2] | 642 | assert 'jack.black@example.net' not in request.root.emails |
---|
| 643 | assert 'JackBlack' not in request.root.all_nicknames |
---|
[5ec3a0b] | 644 | response = user_views.signup(request.root, request) |
---|
| 645 | assert isinstance(response['form'], OWFormRenderer) |
---|
| 646 | error = u'Another user is already registered with the email ' |
---|
| 647 | error += u'john.doe@example.net' |
---|
| 648 | html_error = '<ul class="error">' |
---|
| 649 | html_error += '<li>' + error + '</li>' |
---|
| 650 | html_error += '</ul>' |
---|
| 651 | errorlist = response['form'].errorlist().replace('\n', '') |
---|
| 652 | assert errorlist == html_error |
---|
| 653 | assert response['form'].errors_for('email') == [error] |
---|
[1d92bf2] | 654 | assert 'jack.black@example.net' not in request.root.emails |
---|
| 655 | assert 'JackBlack' not in request.root.all_nicknames |
---|
[421f05f] | 656 | |
---|
| 657 | def test_week_stats_no_stats(self, dummy_request, john): |
---|
| 658 | response = user_views.week_stats(john, dummy_request) |
---|
| 659 | assert isinstance(response, Response) |
---|
| 660 | assert response.content_type == 'application/json' |
---|
| 661 | # the body is a valid json-encoded stream |
---|
| 662 | obj = json.loads(response.body) |
---|
| 663 | assert obj == [ |
---|
| 664 | {'distance': 0, 'elevation': 0, 'name': 'Mon', |
---|
| 665 | 'time': '00', 'workouts': 0}, |
---|
| 666 | {'distance': 0, 'elevation': 0, 'name': 'Tue', |
---|
| 667 | 'time': '00', 'workouts': 0}, |
---|
| 668 | {'distance': 0, 'elevation': 0, 'name': 'Wed', |
---|
| 669 | 'time': '00', 'workouts': 0}, |
---|
| 670 | {'distance': 0, 'elevation': 0, 'name': 'Thu', |
---|
| 671 | 'time': '00', 'workouts': 0}, |
---|
| 672 | {'distance': 0, 'elevation': 0, 'name': 'Fri', |
---|
| 673 | 'time': '00', 'workouts': 0}, |
---|
| 674 | {'distance': 0, 'elevation': 0, 'name': 'Sat', |
---|
| 675 | 'time': '00', 'workouts': 0}, |
---|
| 676 | {'distance': 0, 'elevation': 0, 'name': 'Sun', |
---|
| 677 | 'time': '00', 'workouts': 0} |
---|
| 678 | ] |
---|
| 679 | |
---|
| 680 | def test_week_stats(self, dummy_request, john): |
---|
| 681 | workout = Workout( |
---|
| 682 | start=datetime.now(timezone.utc), |
---|
| 683 | duration=timedelta(minutes=60), |
---|
| 684 | distance=30, |
---|
| 685 | elevation=540 |
---|
| 686 | ) |
---|
| 687 | john.add_workout(workout) |
---|
| 688 | response = user_views.week_stats(john, dummy_request) |
---|
| 689 | assert isinstance(response, Response) |
---|
| 690 | assert response.content_type == 'application/json' |
---|
| 691 | # the body is a valid json-encoded stream |
---|
| 692 | obj = json.loads(response.body) |
---|
| 693 | assert len(obj) == 7 |
---|
| 694 | for day in obj: |
---|
| 695 | if datetime.now(timezone.utc).strftime('%a') == day['name']: |
---|
| 696 | day['distance'] == 30 |
---|
| 697 | day['elevation'] == 540 |
---|
| 698 | day['time'] == '01' |
---|
| 699 | day['workouts'] == 1 |
---|
| 700 | else: |
---|
| 701 | day['distance'] == 0 |
---|
| 702 | day['elevation'] == 0 |
---|
| 703 | day['time'] == '00' |
---|
| 704 | day['workouts'] == 0 |
---|