[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 |
---|
| 22 | def root(self): |
---|
| 23 | root = OpenWorkouts() |
---|
| 24 | root['john'] = User(firstname='John', lastname='Doe', |
---|
| 25 | email='john.doe@example.net') |
---|
| 26 | root['john'].password = 's3cr3t' |
---|
| 27 | workout = Workout( |
---|
| 28 | start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), |
---|
| 29 | duration=timedelta(minutes=60), |
---|
| 30 | distance=30, sport='cycling' |
---|
| 31 | ) |
---|
| 32 | root['john'].add_workout(workout) |
---|
| 33 | return root |
---|
| 34 | |
---|
| 35 | @pytest.fixture |
---|
| 36 | def get_request(self, root): |
---|
| 37 | request = DummyRequest() |
---|
| 38 | request.root = root |
---|
| 39 | return request |
---|
| 40 | |
---|
| 41 | @pytest.fixture |
---|
| 42 | def post_request(self, root): |
---|
| 43 | request = DummyRequest() |
---|
| 44 | request.root = root |
---|
| 45 | request.method = 'POST' |
---|
| 46 | # Fill in this with the required field values, depending on the test |
---|
| 47 | request.POST = MultiDict({ |
---|
| 48 | 'submit': True, |
---|
| 49 | }) |
---|
| 50 | return request |
---|
| 51 | |
---|
| 52 | def test_user_list(self, get_request): |
---|
| 53 | request = get_request |
---|
| 54 | response = user_list(request.root, request) |
---|
| 55 | assert list(response['users']) == [request.root['john']] |
---|
| 56 | |
---|
| 57 | def test_add_user_get(self, get_request): |
---|
| 58 | request = get_request |
---|
| 59 | response = add_user(request.root, request) |
---|
| 60 | assert 'form' in response |
---|
| 61 | assert len(response['form'].form.errors) == 0 |
---|
| 62 | assert isinstance(response['form'].form.schema, UserAddSchema) |
---|
| 63 | |
---|
| 64 | def test_add_user_post_invalid(self, post_request): |
---|
| 65 | request = post_request |
---|
| 66 | response = add_user(request.root, request) |
---|
| 67 | assert 'form' in response |
---|
| 68 | # All required fields (4) are marked in the form errors |
---|
| 69 | # You can see which fields are required in the schema |
---|
| 70 | # ow.schemas.user.UserAddSchema |
---|
| 71 | assert len(response['form'].form.errors) == 4 |
---|
| 72 | |
---|
| 73 | def test_add_user_post_valid(self, post_request): |
---|
| 74 | request = post_request |
---|
[39b1497] | 75 | request.POST['uid'] = 'addeduser' |
---|
[5ec3a0b] | 76 | request.POST['email'] = 'addeduser@example.net' |
---|
| 77 | request.POST['firstname'] = 'added' |
---|
| 78 | request.POST['lastname'] = 'user' |
---|
| 79 | response = add_user(request.root, request) |
---|
| 80 | assert isinstance(response, HTTPFound) |
---|
| 81 | assert response.location.endswith('/userlist') |
---|
| 82 | assert len(request.root.all_usernames()) == 2 |
---|
| 83 | assert 'addeduser' in request.root.all_usernames() |
---|