[2d91474] | 1 | from datetime import datetime, timedelta, timezone |
---|
| 2 | |
---|
[5ec3a0b] | 3 | import pytest |
---|
| 4 | from pyramid.security import Allow |
---|
| 5 | |
---|
| 6 | from ow.models.root import OpenWorkouts |
---|
| 7 | from ow.models.workout import Workout |
---|
| 8 | from ow.models.user import User |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | class TestUser(object): |
---|
| 12 | @pytest.fixture |
---|
| 13 | def root(self): |
---|
| 14 | root = OpenWorkouts() |
---|
| 15 | root['john'] = User(firstname='John', lastname='Doe', |
---|
| 16 | email='john.doe@example.net') |
---|
| 17 | root['john'].password = 's3cr3t' |
---|
| 18 | return root |
---|
| 19 | |
---|
| 20 | @pytest.fixture |
---|
| 21 | def workouts(self): |
---|
| 22 | workouts = [Workout(sport='running'), |
---|
| 23 | Workout(sport='cycling'), |
---|
| 24 | Workout(sport='swimming')] |
---|
| 25 | return workouts |
---|
| 26 | |
---|
| 27 | def test_user_attrs(self, root): |
---|
| 28 | assert root['john'].firstname == 'John' |
---|
| 29 | assert root['john'].lastname == 'Doe' |
---|
| 30 | assert root['john'].email == 'john.doe@example.net' |
---|
| 31 | |
---|
| 32 | def test__acl__(self, root): |
---|
| 33 | uid = str(root['john'].uid) |
---|
| 34 | permissions = [(Allow, uid, 'edit'), (Allow, uid, 'view')] |
---|
| 35 | assert root['john'].__acl__() == permissions |
---|
| 36 | |
---|
| 37 | def test__str__(self, root): |
---|
| 38 | email = root['john'].email |
---|
| 39 | uid = str(root['john'].uid) |
---|
| 40 | assert root['john'].__str__() == u'User: ' + email + ' (' + uid + ')' |
---|
| 41 | |
---|
| 42 | def test_fullname(self, root): |
---|
| 43 | assert root['john'].fullname == 'John Doe' |
---|
| 44 | |
---|
| 45 | def test_password_is_encrypted(self, root): |
---|
| 46 | assert root['john'].password != 's3cr3t' |
---|
| 47 | |
---|
| 48 | def test_check_wrong_password(self, root): |
---|
| 49 | assert not root['john'].check_password('badpass') |
---|
| 50 | |
---|
| 51 | def test_check_good_password(self, root): |
---|
| 52 | assert root['john'].check_password('s3cr3t') |
---|
| 53 | |
---|
| 54 | def test_add_workout(self, root, workouts): |
---|
| 55 | # First add all workouts at once |
---|
| 56 | for workout in workouts: |
---|
| 57 | root['john'].add_workout(workout) |
---|
| 58 | # Then check they are there, in the correct position/number |
---|
| 59 | for workout in workouts: |
---|
| 60 | index = workouts.index(workout) + 1 |
---|
| 61 | assert root['john'][str(index)] == workout |
---|
| 62 | |
---|
| 63 | def test_workouts(self, root, workouts): |
---|
| 64 | # First add all workouts at once |
---|
| 65 | for workout in workouts: |
---|
| 66 | root['john'].add_workout(workout) |
---|
| 67 | # workouts() will return the workouts sorted from newest to oldest |
---|
| 68 | workouts.reverse() |
---|
| 69 | assert list(root['john'].workouts()) == workouts |
---|
| 70 | assert list(root['john'].workout_ids()) == ['1', '2', '3'] |
---|
| 71 | assert root['john'].num_workouts == len(workouts) |
---|
[2d91474] | 72 | |
---|
| 73 | def test_activity_dates_tree(self, root): |
---|
| 74 | # first an empty test |
---|
| 75 | assert root['john'].activity_dates_tree == {} |
---|
| 76 | # now add a cycling workout in a given date (25/11/2018) |
---|
| 77 | workout = Workout( |
---|
| 78 | start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc), |
---|
| 79 | duration=timedelta(minutes=(60*4)), |
---|
| 80 | distance=115, |
---|
| 81 | sport='cycling') |
---|
| 82 | root['john'].add_workout(workout) |
---|
| 83 | assert root['john'].activity_dates_tree == { |
---|
| 84 | 2018: {11: {'cycling': 1}} |
---|
| 85 | } |
---|
| 86 | # add a running workout on the same date |
---|
| 87 | workout = Workout( |
---|
| 88 | start=datetime(2018, 11, 25, 16, 30, tzinfo=timezone.utc), |
---|
| 89 | duration=timedelta(minutes=60), |
---|
| 90 | distance=12, |
---|
| 91 | sport='running') |
---|
| 92 | root['john'].add_workout(workout) |
---|
| 93 | assert root['john'].activity_dates_tree == { |
---|
| 94 | 2018: {11: {'cycling': 1, 'running': 1}} |
---|
| 95 | } |
---|
| 96 | # add a swimming workout on a different date, same year |
---|
| 97 | workout = Workout( |
---|
| 98 | start=datetime(2018, 8, 15, 11, 30, tzinfo=timezone.utc), |
---|
| 99 | duration=timedelta(minutes=30), |
---|
| 100 | distance=2, |
---|
| 101 | sport='swimming') |
---|
| 102 | root['john'].add_workout(workout) |
---|
| 103 | assert root['john'].activity_dates_tree == { |
---|
| 104 | 2018: {8: {'swimming': 1}, |
---|
| 105 | 11: {'cycling': 1, 'running': 1}} |
---|
| 106 | } |
---|
| 107 | # now add some more cycling in a different year |
---|
| 108 | # add a swimming workout on a different date, same year |
---|
| 109 | workout = Workout( |
---|
| 110 | start=datetime(2017, 4, 15, 15, 00, tzinfo=timezone.utc), |
---|
| 111 | duration=timedelta(minutes=(60*3)), |
---|
| 112 | distance=78, |
---|
| 113 | sport='cycling') |
---|
| 114 | root['john'].add_workout(workout) |
---|
| 115 | assert root['john'].activity_dates_tree == { |
---|
| 116 | 2017: {4: {'cycling': 1}}, |
---|
| 117 | 2018: {8: {'swimming': 1}, |
---|
| 118 | 11: {'cycling': 1, 'running': 1}} |
---|
| 119 | } |
---|