def test_search_searches_with_limit_and_offset(self): pool_1 = self.pool_manager.create(PoolTicket("MY_POOL")) pool_2 = self.pool_manager.create(PoolTicket("MY_POOL_TWO")) user_1 = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) user_2 = self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) user_3 = self.user_manager.create(UsernameTicket("other1"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_pool_manager.add(pool_1.get_id(), user_1.get_id()) self.user_pool_manager.add(pool_2.get_id(), user_1.get_id()) self.user_pool_manager.add(pool_1.get_id(), user_2.get_id()) self.user_pool_manager.add(pool_1.get_id(), user_3.get_id()) result = self.user_pool_manager.search([pool_1.get_id()], limit=1, offset=1, sorts={"username": 1}) self.assertEqual(3, result.get_full_count()) self.assertEqual(1, len(result.get_data())) user_found: User = result.get_data()[0] found_username = user_3.get_username() self.assertEqual(found_username, user_found.get_username())
def test_user_ticket_fails_on_bad_name(self): with self.assertRaises(UserTicketNameError): NameTicket("", "Johnson") self.fail("Did not fail on first name empty") with self.assertRaises(UserTicketNameError): NameTicket("John", "") self.fail("Did not fail on last name empty")
def test_search_only_searches_super_users(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Smith")) self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_super_manager.add(user.get_id()) result = self.user_super_manager.search() self.assertEqual(1, result.get_full_count())
def test_search_only_searches_pool_users(self): pool = self.pool_manager.create(PoolTicket("MY_POOL")) user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Bob", "Ayre")) self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_pool_manager.add(pool.get_id(), user.get_id()) result = self.user_pool_manager.search([pool.get_id()]) self.assertEqual(1, result.get_full_count())
def test_search_searches_by_first_name(self): self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) user_2 = self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Bob", "Ayre")) search = user_2.get_first_name() result = self.user_manager.search(first_name=search) self.assertEqual(1, result.get_full_count()) user_found: User = result.get_data()[0] self.assertEqual(search, user_found.get_first_name())
def test_update_updates_user(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("First", "Last")) new_name = NameTicket("Newfirst", "Newlast") self.user_manager.update(user.get_id(), new_name) user_fetched = self.user_manager.get(user.get_id()) self.assertEqual(new_name.get_first_name(), user_fetched.get_first_name()) self.assertEqual(new_name.get_last_name(), user_fetched.get_last_name())
def test_search_searches_by_status(self): self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Smith")) user = self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_manager.update_status(user.get_id(), self.user_statuses.DELETED.get_id()) result = self.user_manager.search( statuses=[self.user_statuses.DELETED.get_id()]) self.assertEqual(1, result.get_full_count()) user_found: User = result.get_data()[0] self.assertEqual(user.get_username(), user_found.get_username())
def update(self, user_id, names: NameTicket) -> User: """ Update user information Args: user_id (ID): User ID names (NameTicket): Object for validating first and last name Returns: User """ result = self.__user_data.update(user_id, first_name=names.get_first_name(), last_name=names.get_last_name()) if not result.get_status(): raise UserUpdateError("Could not update user") return self.get(user_id)
def test_search_searches_by_multiple_pools(self): pool_1 = self.pool_manager.create(PoolTicket("MY_POOL")) pool_2 = self.pool_manager.create(PoolTicket("MY_POOL_TWO")) user_1 = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) user_2 = self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_pool_manager.add(pool_1.get_id(), user_1.get_id()) self.user_pool_manager.add(pool_2.get_id(), user_2.get_id()) self.user_pool_manager.add(pool_2.get_id(), user_1.get_id()) result = self.user_pool_manager.search( [pool_1.get_id(), pool_2.get_id()]) self.assertEqual(2, result.get_full_count())
def test_create_creates_user(self): username = "******" password = "******" first_name = "John" last_name = "Johnson" data = { "id": 1, "uuid": "ERT-123", "username": username, "first_name": first_name, "last_name": last_name, "status_id": self.status_active.get_id() } self.postgres_conn_manager.insert = MagicMock( return_value=Result(True)) self.postgres_conn_manager.select = MagicMock( return_value=Result(True, "", [data])) user = self.user_manager.create(UsernameTicket(username), PasswordTicket(password), NameTicket(first_name, last_name)) self.postgres_conn_manager.insert.assert_called_once() self.postgres_conn_manager.select.assert_called_once() self.assertEqual(data["id"], user.get_id()) self.assertEqual(data["uuid"], user.get_uuid()) self.assertEqual(data["username"], user.get_username()) self.assertEqual(data["first_name"], user.get_first_name()) self.assertEqual(data["last_name"], user.get_last_name()) self.assertEqual(data["status_id"], user.get_status().get_id())
def test_update_updates_user(self): self.postgres_conn_manager.query = MagicMock(return_value=Result(True)) user_id = 1 first_name = "John" last_name = "Johnson" data = { "id": user_id, "uuid": "ERT-123", "username": "******", "first_name": first_name, "last_name": last_name, "status_id": self.status_active.get_id() } self.postgres_conn_manager.select = MagicMock( return_value=Result(True, "", [data])) try: user = self.user_manager.update(user_id, NameTicket("John", "Johnson")) self.postgres_conn_manager.query.assert_called_once() self.postgres_conn_manager.select.assert_called_once() self.assertEqual(data["first_name"], user.get_first_name()) self.assertEqual(data["last_name"], user.get_last_name()) self.assertEqual(data["id"], user.get_id()) except Exception as e: self.fail(str(e))
def test_authorize_authorizes_user(self): username = "******" password = "******" user = self.user_manager.create(UsernameTicket(username), PasswordTicket(password), NameTicket("First", "Last")) pool_ticket = PoolTicket("MY_APP") secret = pool_ticket.get_secret() access_id = pool_ticket.get_access_id() pool = self.pool_manager.create(pool_ticket) self.user_pool_manager.add(pool.get_id(), user.get_id()) token = self.auth_pool_manager.authorize(access_id, secret, username, password) token_decoded = jwt.decode(token.get_token(), secret, algorithms=["HS256"]) self.assertEqual(4, len(token_decoded.items())) self.assertEqual(user.get_uuid(), token_decoded["uuid"]) self.assertEqual(user.get_username(), token_decoded["username"]) self.assertEqual(user.get_first_name(), token_decoded["first_name"]) self.assertEqual(user.get_last_name(), token_decoded["last_name"])
def test_add_adds_super_user(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("Somepassword#123"), NameTicket("Stephen", "Ayre")) self.user_super_manager.add(user.get_id()) result = self.user_super_manager.search() self.assertEqual(1, len(result.get_data()))
def test_add_fails_on_fake_pool_id(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("Somepassword#123"), NameTicket("Stephen", "Ayre")) with self.assertRaises(UserPoolAddError): self.user_pool_manager.add(12345678, user.get_id()) self.fail("Did not fail on fake pool id")
def test_update_fails_on_invalid_status_id(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) with self.assertRaises(UserUpdateError): self.user_manager.update_status(user.get_id(), 567567) self.fail("Did not fail on invalid status ID")
def test_create_creates_user(self): username_ticket = UsernameTicket("sayre") password_ticket = PasswordTicket("SomePassword#123") name_ticket = NameTicket("Stephen", "Ayre") user_created = self.user_manager.create(username_ticket, password_ticket, name_ticket) user_fetched = self.user_manager.get(user_created.get_id()) self.assertEqual(user_created.get_id(), user_fetched.get_id()) self.assertEqual(user_created.get_uuid(), user_fetched.get_uuid()) self.assertEqual(username_ticket.get(), user_created.get_username()) self.assertEqual(name_ticket.get_first_name(), user_created.get_first_name()) self.assertEqual(name_ticket.get_last_name(), user_created.get_last_name()) self.assertEqual(self.user_statuses.ACTIVE.get_id(), user_created.get_status().get_id())
def test_search_sorts_result(self): username_1 = "sayre" username_2 = "other" self.user_manager.create(UsernameTicket(username_1), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Smith")) self.user_manager.create(UsernameTicket(username_2), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) result = self.user_manager.search(sorts={"username": 1}) sort_1 = username_2 sort_2 = username_1 data = result.get_data() self.assertEqual(sort_1, data[0].get_username()) self.assertEqual(sort_2, data[1].get_username())
def test_add_fails_on_duplicate_user(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("Somepassword#123"), NameTicket("Stephen", "Ayre")) self.user_super_manager.add(user.get_id()) with self.assertRaises(UserSuperAddError): self.user_super_manager.add(user.get_id()) self.fail("Did not fail on duplicate super user")
def test_add_adds_user_to_pool(self): pool = self.pool_manager.create(PoolTicket("MY_POOL")) user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("Somepassword#123"), NameTicket("Stephen", "Ayre")) self.user_pool_manager.add(pool.get_id(), user.get_id()) result = self.user_pool_manager.search([pool.get_id()]) self.assertEqual(1, len(result.get_data()))
def test_add_fails_on_duplicate_add(self): pool = self.pool_manager.create(PoolTicket("MY_POOL")) user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("Somepassword#123"), NameTicket("Stephen", "Ayre")) self.user_pool_manager.add(pool.get_id(), user.get_id()) with self.assertRaises(UserPoolAddError): self.user_pool_manager.add(pool.get_id(), user.get_id()) self.fail("Did not fail on duplicate")
def test_search_searches_with_limit_and_offset(self): self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Smith")) self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) user_3 = self.user_manager.create(UsernameTicket("other1"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) result = self.user_manager.search(limit=1, offset=1, sorts={"username": 1}) self.assertEqual(3, result.get_full_count()) self.assertEqual(1, len(result.get_data())) user_found: User = result.get_data()[0] found_username = user_3.get_username() self.assertEqual(found_username, user_found.get_username())
def test_authorize_fails_when_not_super_user(self): username = "******" password = "******" self.user_manager.create(UsernameTicket(username), PasswordTicket(password), NameTicket("First", "Last")) with self.assertRaises(AuthSuperUserNotFoundError): self.auth_super_manager.authorize(username, password) self.fail("Did not fail on missing super user")
def test_search_searches_by_first_name(self): pool_1 = self.pool_manager.create(PoolTicket("MY_POOL")) pool_2 = self.pool_manager.create(PoolTicket("MY_POOL_TWO")) user_1 = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Bob", "Ayre")) user_2 = self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_pool_manager.add(pool_1.get_id(), user_1.get_id()) self.user_pool_manager.add(pool_2.get_id(), user_1.get_id()) self.user_pool_manager.add(pool_1.get_id(), user_2.get_id()) search = user_1.get_first_name() result = self.user_pool_manager.search([pool_1.get_id()], first_name=search) self.assertEqual(1, result.get_full_count()) user_found: User = result.get_data()[0] self.assertEqual(search, user_found.get_first_name())
def test_update_status_updates_status(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_manager.update_status(user.get_id(), self.user_statuses.DELETED.get_id()) user_fetched = self.user_manager.get(user.get_id()) self.assertEqual(self.user_statuses.DELETED.get_id(), user_fetched.get_status().get_id())
def test_create_fails_on_duplicate_username(self): username = UsernameTicket("sayre") password_ticket = PasswordTicket("SomePassword#123") name_ticket = NameTicket("Stephen", "Ayre") self.user_manager.create(username, password_ticket, name_ticket) with self.assertRaises(UserCreateError): self.user_manager.create(username, password_ticket, name_ticket) self.fail("Did not fail on duplicate username")
def test_search_searches_by_status(self): pool_1 = self.pool_manager.create(PoolTicket("MY_POOL")) pool_2 = self.pool_manager.create(PoolTicket("MY_POOL_TWO")) user_1 = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) user_2 = self.user_manager.create(UsernameTicket("other"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) self.user_pool_manager.add(pool_1.get_id(), user_1.get_id()) self.user_pool_manager.add(pool_2.get_id(), user_1.get_id()) self.user_pool_manager.add(pool_1.get_id(), user_2.get_id()) self.user_manager.update_status(user_1.get_id(), self.user_statuses.DELETED.get_id()) result = self.user_pool_manager.search( [pool_1.get_id()], statuses=[self.user_statuses.DELETED.get_id()]) self.assertEqual(1, result.get_full_count()) user_found: User = result.get_data()[0] self.assertEqual(user_1.get_username(), user_found.get_username())
def test_update_fails_on_update(self): self.postgres_conn_manager.query = MagicMock( return_value=Result(False)) self.postgres_conn_manager.select = MagicMock( return_value=Result(True)) with self.assertRaises(UserUpdateError): self.user_manager.update(1, NameTicket("John", "Johnson")) self.fail("Did not fail") self.postgres_conn_manager.query.assert_called_once() self.postgres_conn_manager.select.assert_not_called()
def test_update_password_updates_password(self): user = self.user_manager.create(UsernameTicket("sayre"), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) password = PasswordTicket("NewPassword#123") try: self.user_manager.update_password(user.get_id(), password) except UserUpdateError as e: self.fail(str(e))
def create(self, username: UsernameTicket, password: PasswordTicket, names: NameTicket) -> User: """ Create user Args: username (UsernameTicket): Object that validates username password (PasswordTicket): Object that validates password names (NameTicket): Object that validates first and last name Returns: User """ result = self.__user_data.insert( **{ "username": username.get(), "password": self.__encrypt_password(password.get()), "first_name": names.get_first_name(), "last_name": names.get_last_name(), "status_id": self.__statuses.ACTIVE.get_id() }) if not result.get_status(): raise UserCreateError("Could not create user") return self.get(result.get_insert_id())
def test_create_fails_on_create_error(self): self.postgres_conn_manager.insert = MagicMock( return_value=Result(False)) self.postgres_conn_manager.select = MagicMock( return_value=Result(True)) with self.assertRaises(UserCreateError): self.user_manager.create(UsernameTicket("username123"), PasswordTicket("Password#123"), NameTicket("John", "Johnson")) self.fail("Did not fail") self.postgres_conn_manager.insert.assert_called_once() self.postgres_conn_manager.select.assert_not_called()