def test_lazy_overrides_non_lazy_users(self): # given user = User('user') user2 = User('user2') # when result = Task.from_dict({'users': [user]}, lazy={'users': [user2]}) # then self.assertIsInstance(result, Task) self.assertEqual([user2], list(result.users))
def test_constructor_sets_is_admin(self): # when user = User('*****@*****.**', 'hashed_password', False) # then self.assertFalse(user.is_admin) # when user = User('*****@*****.**', 'hashed_password', True) # then self.assertTrue(user.is_admin)
def test_users_non_empty_yields_same(self): # given user = User('user') # when result = Task.from_dict({'users': [user]}) # then self.assertIsInstance(result, Task) self.assertEqual([user], list(result.users))
def test_generates_repr_string(self): # given user = User(email='*****@*****.**') user.id = 123 #when r = repr(user) # then self.assertEqual('User(\'[email protected]\', id=123)', r)
def create_user(self, email, hashed_password=None, is_admin=False, lazy=None): return User(email=email, hashed_password=hashed_password, is_admin=is_admin, lazy=lazy)
def test_generates_str_string(self): # given user = User(email='*****@*****.**') user.id = 123 #when r = str(user) # then fmt = 'User(\'[email protected]\', user id=123, id=[{}])' expected = fmt.format(id(user)) self.assertEqual(expected, r)
def test_unauthorized_nonadmin_cannot_access_task(self): # given task = Task('task') user = User('*****@*****.**') self.pl.add(task) self.pl.add(user) self.pl.commit() # when result = TaskUserOps.is_user_authorized_or_admin(task, user) # then self.assertFalse(result)
def test_unauthorized_admin_can_edit_private_task(self): # given task = Task('task') user = User('*****@*****.**', None, True) self.pl.add(task) self.pl.add(user) self.pl.commit() # when result = TaskUserOps.user_can_edit_task(task, user) # then self.assertTrue(result)
def test_unauthorized_nonadmin_cannot_view_private_task(self): # given task = Task('task') user = User('*****@*****.**') self.pl.add(task) self.pl.add(user) self.pl.commit() # when result = TaskUserOps.user_can_view_task(task, user) # then self.assertFalse(result)
def test_unauthorized_nonadmin_cannot_edit_public_task(self): # given task = Task('task', is_public=True) user = User('*****@*****.**') self.pl.add(task) self.pl.add(user) self.pl.commit() # when result = TaskUserOps.user_can_edit_task(task, user) # then self.assertFalse(result)
def test_authorized_admin_can_edit_public_task(self): # given task = Task('task', is_public=True) user = User('*****@*****.**', None, True) self.pl.add(task) self.pl.add(user) task.users.append(user) self.pl.commit() # when result = TaskUserOps.user_can_edit_task(task, user) # then self.assertTrue(result)
def test_authorized_admin_can_access_task(self): # given task = Task('task') user = User('*****@*****.**', None, True) self.pl.add(task) self.pl.add(user) task.users.append(user) self.pl.commit() # when result = TaskUserOps.is_user_authorized_or_admin(task, user) # then self.assertTrue(result)
def test_to_dict_returns_correct_values(self): # when user = User('*****@*****.**', '12345') # then self.assertEqual( { 'email': '*****@*****.**', 'hashed_password': '******', 'is_admin': False, 'id': None, 'tasks': [] }, user.to_dict())
def test_to_dict_all_fields_returns_correct_values(self): # when user = User('*****@*****.**', '12345') # then self.assertEqual( { 'email': '*****@*****.**', 'hashed_password': '******', 'is_admin': False, 'id': None, 'tasks': [] }, user.to_dict({ user.FIELD_ID, user.FIELD_EMAIL, user.FIELD_HASHED_PASSWORD, user.FIELD_IS_ADMIN, user.FIELD_TASKS }))
def test_to_dict_field_hashed_password_returns_hashed_password(self): # when user = User('*****@*****.**', '12345') # then self.assertEqual({'hashed_password': '******'}, user.to_dict({user.FIELD_HASHED_PASSWORD}))
def setUp(self): self.task = Task('task') self.user = User('user')
def test_to_dict_field_is_admin_returns_is_admin(self): # when user = User('*****@*****.**', '12345') # then self.assertEqual({'is_admin': False}, user.to_dict({user.FIELD_IS_ADMIN}))
def test_constructor_sets_hashed_password(self): # when user = User('*****@*****.**', 'hashed_password') # then self.assertEqual('hashed_password', user.hashed_password)
def test_to_dict_field_email_returns_email(self): # when user = User('*****@*****.**', '12345') # then self.assertEqual({'email': '*****@*****.**'}, user.to_dict({user.FIELD_EMAIL}))
def test_is_authenticated_always_true(self): # when user = User('*****@*****.**', 'hashed_password') # then self.assertTrue(user.is_authenticated)
def test_is_anonymous_always_false(self): # when user = User('*****@*****.**', 'hashed_password') # then self.assertFalse(user.is_anonymous)
def test_get_id_gets_email(self): # when user = User('*****@*****.**', 'hashed_password') # then self.assertEqual('*****@*****.**', user.get_id())
def test_to_dict_field_id_returns_id(self): # when user = User('*****@*****.**', '12345') # then self.assertEqual({'id': None}, user.to_dict({user.FIELD_ID}))
def test_to_dict_field_tasks_returns_tasks(self): # when user = User('*****@*****.**', '12345') # then self.assertEqual({'tasks': []}, user.to_dict({user.FIELD_TASKS}))
def test_constructor_default_id_is_none(self): # when user = User('*****@*****.**', 'hashed_password') # then self.assertIsNone(user.id)
def test_constructor_default_is_admin_is_false(self): # when user = User('*****@*****.**', 'hashed_password') # then self.assertFalse(user.is_admin)