source: OpenWorkouts-current/ow/tests/views/test_root.py

current
Last change on this file was 1d92bf2, checked in by borja <borja@…>, 5 years ago

(#37) Allow login using email address instead of username:

  • Use user uids as keys in the root folder for referencing user objects (instead of username)
  • Use uids for referencing users all over the place (auth, permissions, traversal urls, etc)
  • Replaced the username concept with nickname. This nickname will be used as a shortcut to access "public profile" pages for users
  • Reworked lots of basic methods in the OpenWorkouts root object (s/username/nickname, marked as properties some methods like users, emails, etc)
  • Added new add_user() and delete_user() helpers to the OpenWorkouts root object
  • Fixed bug in the dashboard redirect view, causing an endless loop if an authenticated user does not exist anymore when loading a page.
  • Lots of tests fixes, adaptations and catch up.
  • Property mode set to 100644
File size: 3.0 KB
Line 
1from datetime import datetime, timedelta, timezone
2
3import pytest
4
5from pyramid.testing import DummyRequest
6from pyramid.httpexceptions import HTTPFound
7
8from webob.multidict import MultiDict
9
10from ow.models.workout import Workout
11from ow.models.user import User
12from ow.models.root import OpenWorkouts
13
14from ow.views.root import user_list, add_user
15
16from ow.schemas.user import UserAddSchema
17
18
19class TestRootOpenWorkoutsViews(object):
20
21    @pytest.fixture
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):
30        root = OpenWorkouts()
31        root.add_user(john)
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        )
37        john.add_workout(workout)
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
57    def test_user_list(self, get_request, john):
58        request = get_request
59        response = user_list(request.root, request)
60        assert list(response['users']) == [john]
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
73        # All required fields (3) are marked in the form errors
74        # You can see which fields are required in the schema
75        # ow.schemas.user.UserAddSchema
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
81
82    def test_add_user_post_valid(self, post_request):
83        request = post_request
84        request.POST['nickname'] = 'addeduser'
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')
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
Note: See TracBrowser for help on using the repository browser.