[5ec3a0b] | 1 | from datetime import datetime, timedelta, timezone |
---|
| 2 | |
---|
| 3 | import pytest |
---|
| 4 | |
---|
| 5 | from pyramid.testing import DummyRequest |
---|
| 6 | from pyramid.httpexceptions import HTTPFound |
---|
| 7 | |
---|
| 8 | from webob.multidict import MultiDict |
---|
| 9 | |
---|
| 10 | from ow.models.workout import Workout |
---|
| 11 | from ow.models.user import User |
---|
| 12 | from ow.models.root import OpenWorkouts |
---|
| 13 | |
---|
| 14 | from ow.views.root import user_list, add_user |
---|
| 15 | |
---|
| 16 | from ow.schemas.user import UserAddSchema |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | class TestRootOpenWorkoutsViews(object): |
---|
| 20 | |
---|
| 21 | @pytest.fixture |
---|
[1d92bf2] | 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): |
---|
[5ec3a0b] | 30 | root = OpenWorkouts() |
---|
[1d92bf2] | 31 | root.add_user(john) |
---|
[5ec3a0b] | 32 | workout = Workout( |
---|
| 33 | start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), |
---|
| 34 | duration=timedelta(minutes=60), |
---|
| 35 | distance=30, sport='cycling' |
---|
| 36 | ) |
---|
[1d92bf2] | 37 | john.add_workout(workout) |
---|
[5ec3a0b] | 38 | return root |
---|
| 39 | |
---|
| 40 | @pytest.fixture |
---|
| 41 | def get_request(self, root): |
---|
| 42 | request = DummyRequest() |
---|
| 43 | request.root = root |
---|
| 44 | return request |
---|
| 45 | |
---|
| 46 | @pytest.fixture |
---|
| 47 | def post_request(self, root): |
---|
| 48 | request = DummyRequest() |
---|
| 49 | request.root = root |
---|
| 50 | request.method = 'POST' |
---|
| 51 | # Fill in this with the required field values, depending on the test |
---|
| 52 | request.POST = MultiDict({ |
---|
| 53 | 'submit': True, |
---|
| 54 | }) |
---|
| 55 | return request |
---|
| 56 | |
---|
[1d92bf2] | 57 | def test_user_list(self, get_request, john): |
---|
[5ec3a0b] | 58 | request = get_request |
---|
| 59 | response = user_list(request.root, request) |
---|
[1d92bf2] | 60 | assert list(response['users']) == [john] |
---|
[5ec3a0b] | 61 | |
---|
| 62 | def test_add_user_get(self, get_request): |
---|
| 63 | request = get_request |
---|
| 64 | response = add_user(request.root, request) |
---|
| 65 | assert 'form' in response |
---|
| 66 | assert len(response['form'].form.errors) == 0 |
---|
| 67 | assert isinstance(response['form'].form.schema, UserAddSchema) |
---|
| 68 | |
---|
| 69 | def test_add_user_post_invalid(self, post_request): |
---|
| 70 | request = post_request |
---|
| 71 | response = add_user(request.root, request) |
---|
| 72 | assert 'form' in response |
---|
[1d92bf2] | 73 | # All required fields (3) are marked in the form errors |
---|
[5ec3a0b] | 74 | # You can see which fields are required in the schema |
---|
| 75 | # ow.schemas.user.UserAddSchema |
---|
[1d92bf2] | 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 |
---|
[5ec3a0b] | 81 | |
---|
| 82 | def test_add_user_post_valid(self, post_request): |
---|
| 83 | request = post_request |
---|
[1d92bf2] | 84 | request.POST['nickname'] = 'addeduser' |
---|
[5ec3a0b] | 85 | request.POST['email'] = 'addeduser@example.net' |
---|
| 86 | request.POST['firstname'] = 'added' |
---|
| 87 | request.POST['lastname'] = 'user' |
---|
| 88 | response = add_user(request.root, request) |
---|
| 89 | assert isinstance(response, HTTPFound) |
---|
| 90 | assert response.location.endswith('/userlist') |
---|
[1d92bf2] | 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 |
---|