[5ec3a0b] | 1 | import pytest |
---|
| 2 | from pyramid.security import Allow |
---|
| 3 | |
---|
| 4 | from ow.models.root import OpenWorkouts |
---|
| 5 | from ow.models.workout import Workout |
---|
| 6 | from ow.models.user import User |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | class TestUser(object): |
---|
| 10 | @pytest.fixture |
---|
| 11 | def root(self): |
---|
| 12 | root = OpenWorkouts() |
---|
| 13 | root['john'] = User(firstname='John', lastname='Doe', |
---|
| 14 | email='john.doe@example.net') |
---|
| 15 | root['john'].password = 's3cr3t' |
---|
| 16 | return root |
---|
| 17 | |
---|
| 18 | @pytest.fixture |
---|
| 19 | def workouts(self): |
---|
| 20 | workouts = [Workout(sport='running'), |
---|
| 21 | Workout(sport='cycling'), |
---|
| 22 | Workout(sport='swimming')] |
---|
| 23 | return workouts |
---|
| 24 | |
---|
| 25 | def test_user_attrs(self, root): |
---|
| 26 | assert root['john'].firstname == 'John' |
---|
| 27 | assert root['john'].lastname == 'Doe' |
---|
| 28 | assert root['john'].email == 'john.doe@example.net' |
---|
| 29 | |
---|
| 30 | def test__acl__(self, root): |
---|
| 31 | uid = str(root['john'].uid) |
---|
| 32 | permissions = [(Allow, uid, 'edit'), (Allow, uid, 'view')] |
---|
| 33 | assert root['john'].__acl__() == permissions |
---|
| 34 | |
---|
| 35 | def test__str__(self, root): |
---|
| 36 | email = root['john'].email |
---|
| 37 | uid = str(root['john'].uid) |
---|
| 38 | assert root['john'].__str__() == u'User: ' + email + ' (' + uid + ')' |
---|
| 39 | |
---|
| 40 | def test_fullname(self, root): |
---|
| 41 | assert root['john'].fullname == 'John Doe' |
---|
| 42 | |
---|
| 43 | def test_password_is_encrypted(self, root): |
---|
| 44 | assert root['john'].password != 's3cr3t' |
---|
| 45 | |
---|
| 46 | def test_check_wrong_password(self, root): |
---|
| 47 | assert not root['john'].check_password('badpass') |
---|
| 48 | |
---|
| 49 | def test_check_good_password(self, root): |
---|
| 50 | assert root['john'].check_password('s3cr3t') |
---|
| 51 | |
---|
| 52 | def test_add_workout(self, root, workouts): |
---|
| 53 | # First add all workouts at once |
---|
| 54 | for workout in workouts: |
---|
| 55 | root['john'].add_workout(workout) |
---|
| 56 | # Then check they are there, in the correct position/number |
---|
| 57 | for workout in workouts: |
---|
| 58 | index = workouts.index(workout) + 1 |
---|
| 59 | assert root['john'][str(index)] == workout |
---|
| 60 | |
---|
| 61 | def test_workouts(self, root, workouts): |
---|
| 62 | # First add all workouts at once |
---|
| 63 | for workout in workouts: |
---|
| 64 | root['john'].add_workout(workout) |
---|
| 65 | # workouts() will return the workouts sorted from newest to oldest |
---|
| 66 | workouts.reverse() |
---|
| 67 | assert list(root['john'].workouts()) == workouts |
---|
| 68 | assert list(root['john'].workout_ids()) == ['1', '2', '3'] |
---|
| 69 | assert root['john'].num_workouts == len(workouts) |
---|