class TestUserRepository(unittest.TestCase): def setUp(self): self.user_repository = UserRepository(test_database_connection()) self.user_repository.delete_all() self.test_user_one = User('test_user_one', 'testpw') self.test_user_two = User('test_user_two', 'test') self.user_repository.create_user(self.test_user_one) self.user_repository.create_user(self.test_user_two) def test_get_all_users(self): users = self.user_repository.get_all_users() self.assertEqual(len(users), 2) self.assertEqual(users[0].username, self.test_user_one.username) def test_login_user(self): user = self.user_repository.login_user(self.test_user_one) self.assertEqual(user.username, self.test_user_one.username) def test_login_user_fails_with_wrong_credentials(self): invalid_user = User('test_user_one', 'passwordwrong') user = self.user_repository.login_user(invalid_user) self.assertEqual(user, None) def test_create_user(self): user = User('masamainio', 'anttiangeli') created_user = self.user_repository.create_user(user) users = self.user_repository.get_all_users() self.assertEqual(created_user.username, 'masamainio') self.assertEqual(len(users), 3) def test_get_single_user(self): user = self.user_repository.get_single_user(self.test_user_one) self.assertEqual(user.username, self.test_user_one.username) def test_get_single_user_returns_none_if_not_found(self): fake_user = User('risu', 'mies') user = self.user_repository.get_single_user(fake_user) self.assertEqual(user, None)
class TestUserRepository(unittest.TestCase): TEST_USER_NAME = "testuser" TEST_USER_PASSWORD = "******" def setUp(self): database = Database() database.init_database() self._fake_settings_repository = FakeSettingsRepository() self._user_repository = UserRepository(database, self._fake_settings_repository) self._user_repository.create_user( TestUserRepository.TEST_USER_NAME, TestUserRepository.TEST_USER_PASSWORD) def test_cannot_create_existing_user(self): user = self._user_repository.create_user( TestUserRepository.TEST_USER_NAME, TestUserRepository.TEST_USER_PASSWORD) self.assertIsNone(user) def test_can_create_new_user(self): user = self._user_repository.create_user("newuser", "newpassword") self.assertIsNotNone(user) self.assertEqual("newuser", user.username) self.assertTrue(user.verify_password("newpassword")) def test_can_find_existing_user(self): user = self._user_repository.get_user( TestUserRepository.TEST_USER_NAME) self.assertIsNotNone(user) self.assertEqual(TestUserRepository.TEST_USER_NAME, user.username) self.assertTrue( user.verify_password(TestUserRepository.TEST_USER_PASSWORD)) def test_cannot_find_nonexisting_user(self): user = self._user_repository.get_user("nosuchuser") self.assertIsNone(user) def test_user_gets_assigned_correct_settings(self): user = self._user_repository.get_user( TestUserRepository.TEST_USER_NAME) user_settings = self._fake_settings_repository.get_settings_by_username( TestUserRepository.TEST_USER_NAME) self.assertEqual(id(user.settings), id(user_settings)) def test_saving_non_existing_user_settings_does_nothing(self): self._user_repository.save_settings(None) self.assertEqual(0, self._fake_settings_repository.user_settings_saved()) def test_saving_user_settings_calls_settings_repository(self): user = self._user_repository.get_user( TestUserRepository.TEST_USER_NAME) self.assertEqual( 0, self._fake_settings_repository.settings_saved_by_username( user.username)) self._user_repository.save_settings(user) self.assertEqual( 1, self._fake_settings_repository.settings_saved_by_username( user.username))
class TestPasswordRepository(unittest.TestCase): def setUp(self): # Reset tables users, passwords self.password_repository = PasswordRepository(test_database_connection()) self.user_repository = UserRepository(test_database_connection()) self.password_repository.delete_all_passwords() self.user_repository.delete_all() self.test_user_one = User('testiuser', 'testiuser666', 1) self.test_user_two = User('secondtestuesr', 'testingggggg', 2) self.test_user_one = self.user_repository.create_user(self.test_user_one) self.test_user_two = self.user_repository.create_user(self.test_user_two) self.test_password_one = Password('matti', 'teppo', 'www.vauva.fi') self.test_password_two = Password('testpassword', 'testpassword2', 'www.coinbase.com') self.test_password_one.set_user_id(self.test_user_one.user_id) self.test_password_two.set_user_id(self.test_user_one.user_id) def test_create_password(self): self.password_repository.create_password(self.test_password_one) passwords = self.password_repository.get_all_passwords() self.assertEqual(len(passwords), 1) self.assertEqual(passwords[0].username, self.test_password_one.username) def test_get_all_users(self): user_id = self.test_user_one.user_id for i in range(5): password = Password( f'user{i}', f'password{i}', f'site{i}', user_id ) self.password_repository.create_password(password) passwords = self.password_repository.get_all_passwords() self.assertEqual(len(passwords), 5) self.assertEqual(passwords[2].username, 'user2') def test_get_all_passwords_by_user(self): user_id = self.test_user_one.user_id for i in range(5): password = Password( f'user{i}', f'password{i}', f'site{i}', user_id ) self.password_repository.create_password(password) user_id = self.test_user_two.user_id for i in range(5, 10): password = Password( f'user{i}', f'password{i}', f'site{i}', user_id ) self.password_repository.create_password(password) passwords_by_user_one = self.password_repository.get_all_passwords_by_user(self.test_user_one) self.assertEqual(len(passwords_by_user_one), 5) self.assertEqual(passwords_by_user_one[1].user_id, self.test_user_one.user_id) def test_tuple_to_password_returns_none_if_no_password_given(self): result = self.password_repository.tuple_to_password(None) self.assertIsNone(result)