def create_test_resident(self): """ Creates a test resident for use in testing """ self.test_resident_info = ('*****@*****.**', 'Bob', 'Ross', 'RESIDENT') self.test_admin_info = ('*****@*****.**', 'Bob', 'Ross', 'ADMIN') self.test_user = UserService.create_user(*self.test_resident_info) self.test_admin = UserService.create_user(*self.test_admin_info) self.test_resident = ResidentService.get_resident_by_id( self.test_user.id) self.test_room = RoomService.create_room('1', 'Good', 'Single')
def test_edit_user_duplicate_email(self): """ Tests edit resident to an existing email """ UserService.create_user('*****@*****.**', '', '', 'ADMIN') with self.assertRaises(ValueError): UserService.edit_user(self.test_user.id, '*****@*****.**', 'John', 'Smith') self.assertEqual(self.test_user.email, self.test_user_info[0]) self.assertEqual(self.test_user.first_name, self.test_user_info[1]) self.assertEqual(self.test_user.last_name, self.test_user_info[2])
def create_test_users(self): """ Creates two RESIDENT accounts and one ADMIN account for use in testing """ DataGen.create_default_room() self.test_resident_1 = UserService.create_user('*****@*****.**', 'Bob', 'Ross', 'RESIDENT') self.test_resident_2 = UserService.create_user('*****@*****.**', 'Joe', 'Smith', 'RESIDENT') self.test_admin = UserService.create_user('*****@*****.**', 'Jim', 'White', 'ADMIN')
def test_create_user_doesnt_send_email(self, mock_mail): """ Tests that creating a user without a default password doesn't send an email. Args: mock_mail: The mock_client for the patched function. This allows us to check if an email would get sent without actually sending one. """ UserService.create_user('*****@*****.**', 'Bob', 'Ross', 'ADMIN', password='******') mock_mail.assert_not_called()
def test_delete_user(self): """ Tests that a user can be deleted """ user = UserService.create_user('*****@*****.**', '', '', 'STAFF') expected = User.query.count() - 1 # check if deletion successful UserService.delete_user(user.id) self.assertEqual(expected, User.query.count())
def test_create_user_sends_email(self, mock_mail): """ Tests that creating a user sends an email. Args: mock_mail: The mock_client for the patched function. This allows us to check if an email would get sent without actually sending one. """ expected_substitutions = { 'first_name': 'Bob', 'last_name': 'Ross', 'role': 'admin', 'confirm_url': ANY } UserService.create_user('*****@*****.**', 'Bob', 'Ross', 'ADMIN') mock_mail.assert_called_once_with('*****@*****.**', 'User Account Creation', templates['user_creation_email'], substitutions=expected_substitutions)
def test_delete_user_resident(self): """ Tests that deleting a user deletes their resident info """ resident = UserService.create_user('*****@*****.**', 'John', 'Smith', 'RESIDENT') expected_user = User.query.count() - 1 expected_resident = Resident.query.count() - 1 UserService.delete_user(resident.id) self.assertEqual(User.query.count(), expected_user) self.assertEqual(Resident.query.count(), expected_resident)
def test_invalid_add_resident_to_room(self): """ Tests that non-resident users cannot be added to a room """ test_user_info = ('*****@*****.**', 'Bob', 'Ross', 'ADMIN') test_user = UserService.create_user(*test_user_info) with self.assertRaises(ValueError): RoomService.add_resident_to_room(test_user.email, self.test_room.number) resident = ResidentService.get_resident_by_id(test_user.id) self.assertIsNone(resident) self.assertFalse(resident in self.test_room.occupants)
def test_add_resident_to_room(self): """ Tests that residents can be added to a room """ test_user_info = ('*****@*****.**', 'Bob', 'Ross', 'RESIDENT') test_user = UserService.create_user(*test_user_info) resident = ResidentService.get_resident_by_id(test_user.id) old_room = RoomService.get_room_by_number('') self.assertTrue(resident in old_room.occupants) RoomService.add_resident_to_room(test_user.email, self.test_room.number) self.assertEqual(resident.room_number, self.test_room.number) self.assertTrue(resident in self.test_room.occupants) self.assertFalse(resident in old_room.occupants)
def test_get_admin_count(self): """ Tests that get_admin_count returns the correct number of users""" UserService.create_user('*****@*****.**', 'Bob', 'Ross', 'ADMIN') expected = 2 self.assertEqual(ManagerService.get_admin_count(), expected)
def create_test_user(self): """ Creates a test user """ self.test_user_info = ('*****@*****.**', 'Bob', 'Ross', 'ADMIN') self.test_user = UserService.create_user(*self.test_user_info)
def create_test_weekly_meal_plan(self): """ Creates a meal plan of WEEKLY plan type for use in testing """ UserService.create_user('*****@*****.**', '', '', 'RESIDENT') self.test_meal_plan_info = (10, 'WEEKLY', '*****@*****.**') self.test_meal_plan = MealService.create_meal_plan_for_resident_by_email( *self.test_meal_plan_info)
def setUp(self): """ Runs before every test and clears relevant tables """ super().setUp() test_user_info = ('*****@*****.**', 'Bob', 'Smith', 'RESIDENT') UserService.create_user(*test_user_info) self.test_user = UserService.get_user_by_email('*****@*****.**')