Changeset 1d92bf2 in OpenWorkouts-current for ow/tests/models/test_root.py


Ignore:
Timestamp:
Dec 16, 2018, 1:07:04 AM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
6560b8f
Parents:
929097a
Message:

(#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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/models/test_root.py

    r929097a r1d92bf2  
    11import json
     2from unittest.mock import Mock
    23from datetime import datetime, timedelta, timezone
    34
    45import pytest
     6from repoze.catalog.catalog import Catalog
    57
    68from ow.models.workout import Workout
     
    1214
    1315    @pytest.fixture
    14     def root(self):
     16    def john(self):
     17        user = User(firstname='John', lastname='Doe',
     18                    email='john.doe@example.net')
     19        user.password = 's3cr3t'
     20        return user
     21
     22    @pytest.fixture
     23    def root(self, john):
    1524        root = OpenWorkouts()
    16         root['john'] = User(firstname='John', lastname='Doe',
    17                             email='john.doe@example.net')
    18         root['john'].password = 's3cr3t'
     25        root.add_user(john)
    1926        workout = Workout(
    2027            start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc),
     
    2229            distance=30, sport='cycling'
    2330        )
    24         root['john'].add_workout(workout)
     31        john.add_workout(workout)
    2532        return root
    2633
    27     def test_all_usernames(self, root):
    28         # the method returns an OOBtree object
    29         assert [u for u in root.all_usernames()] == ['john']
     34    def test__init__(self, root):
     35        # a new OpenWorkouts instance has a catalog created automatically
     36        assert isinstance(root.catalog, Catalog)
     37        assert len(root.catalog) == 2
     38        assert 'email' in root.catalog
     39        assert 'sport' in root.catalog
    3040
    31     def test_lowercase_usernames(self, root):
    32         root.add_user(uid='Jack', firstname='Jack', lastname='Dumb',
    33                       email='jack.dumb@example.net')
    34         assert root.lowercase_usernames() == ['jack', 'john']
     41    def test_add_user_ok(self, root):
     42        assert len(root.users) == 1
     43        user = User(firstname='New', lastname='For Testing',
     44                    email='new.for.testing@example.net')
     45        root.add_user(user)
     46        assert len(root.users) == 2
     47        assert user in root.users
     48
     49    def test_add_user_invalid(self, root):
     50        assert len(root.users) == 1
     51        with pytest.raises(AttributeError):
     52            root.add_user('faked-user-object')
     53
     54    def test_del_user_ok(self, root, john):
     55        assert len(root.users) == 1
     56        root.del_user(john)
     57        assert len(root.users) == 0
     58
     59    def test_del_user_failure(self, root):
     60        assert len(root.users) == 1
     61        with pytest.raises(AttributeError):
     62            root.add_user('faked-user-object')
     63
     64    def test_get_user_by_uid(self, root, john):
     65        # first, get an user that does exist
     66        user = root.get_user_by_uid(str(john.uid))
     67        assert user == john
     68        # now, without converting first to str, works too
     69        user = root.get_user_by_uid(john.uid)
     70        assert user == john
     71        # now, something that is not there
     72        new_user = User(firstname='someone', lastname='else',
     73                        email='someone.else@example.net')
     74        user = root.get_user_by_uid(new_user.uid)
     75        assert user is None
     76        # now, something that is not an uid
     77        user = root.get_user_by_uid('faked-user-uid')
     78        assert user is None
     79
     80    def test_get_user_by_email(self, root, john):
     81        # first, get an user that does exist
     82        user = root.get_user_by_email(str(john.email))
     83        assert user == john
     84        # now, something that is not there
     85        new_user = User(firstname='someone', lastname='else',
     86                        email='someone.else@example.net')
     87        user = root.get_user_by_email(new_user.email)
     88        assert user is None
     89        # now, something that is not an email
     90        user = root.get_user_by_email('faked-user-email')
     91        assert user is None
     92        # passing in None
     93        user = root.get_user_by_email(None)
     94        assert user is None
     95        # passing in something that is not None or a string will break
     96        # the query code
     97        with pytest.raises(TypeError):
     98            user = root.get_user_by_email(False)
     99        with pytest.raises(TypeError):
     100            user = root.get_user_by_email(Mock())
     101
     102    def test_users(self, root, john):
     103        assert root.users == [john]
     104
     105    def test_all_nicknames(self, root, john):
     106        # the existing user has not a nickname, and empty nicknames are not
     107        # added to the list of nicknames
     108        assert root.all_nicknames == []
     109        # now set one
     110        john.nickname = 'MrJohn'
     111        assert root.all_nicknames == ['MrJohn']
     112
     113    def test_lowercase_nicknames(self, root, john):
     114        # the existing user has not a nickname
     115        assert root.lowercase_nicknames == []
     116        # now set one
     117        john.nickname = 'MrJohn'
     118        assert root.lowercase_nicknames == ['mrjohn']
    35119
    36120    def test_emails(self, root):
    37         assert root.emails() == ['john.doe@example.net']
     121        assert root.emails == ['john.doe@example.net']
    38122
    39123    def test_lowercase_emails(self, root):
    40         root.add_user(uid='Jack', firstname='Jack', lastname='Dumb',
    41                       email='Jack.Dumb@example.net')
    42         assert root.lowercase_emails() == ['jack.dumb@example.net',
    43                                            'john.doe@example.net']
     124        user = User(firstname='Jack', lastname='Dumb',
     125                    email='Jack.Dumb@example.net')
     126        root.add_user(user)
     127        assert root.lowercase_emails == ['john.doe@example.net',
     128                                         'jack.dumb@example.net']
    44129
    45     def test_users(self, root):
    46         # the method returns an OOBtree object
    47         assert [u for u in root.users()] == [root['john']]
    48 
    49     def test_get_user(self, root):
    50         assert root.get_user('john') == root['john']
    51         assert root.get_user('jack') is None
    52         with pytest.raises(TypeError):
    53             root.get_user()
    54 
    55     def test_add_user(self, root):
    56         assert len(root.users()) == 1
    57         root.add_user(uid='jack', firstname='Jack', lastname='Dumb',
    58                       email='jack.dumb@example.net')
    59         assert len(root.users()) == 2
    60         assert 'jack' in root.all_usernames()
    61 
    62     def test_sports(self, root):
     130    def test_sports(self, root, john):
    63131        assert root.sports == ['cycling']
    64132        workout = Workout(
     
    66134            duration=timedelta(minutes=60),
    67135            distance=10, sport='running')
    68         root['john'].add_workout(workout)
     136        john.add_workout(workout)
    69137        assert root.sports == ['cycling', 'running']
    70138
    71     def test_sports_json(self, root):
     139    def test_sports_json(self, root, john):
    72140        assert root.sports_json == json.dumps(["cycling"])
    73141        workout = Workout(
     
    75143            duration=timedelta(minutes=60),
    76144            distance=10, sport='running')
    77         root['john'].add_workout(workout)
     145        john.add_workout(workout)
    78146        assert root.sports_json == json.dumps(["cycling", "running"])
Note: See TracChangeset for help on using the changeset viewer.