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_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_search_searches_by_limit_and_offset(self): self.pool_manager.create(PoolTicket("NEW_APP")) self.pool_manager.create(PoolTicket("NEW_APP_ONE")) self.pool_manager.create(PoolTicket("OTHER")) result = self.pool_manager.search(limit=1, offset=1) self.assertEqual(1, len(result.get_data())) pool = result.get_data()[0] self.assertEqual("NEW_APP_ONE", pool.get_const()) self.assertEqual(3, result.get_full_count())
def test_search_searches_by_status(self): self.pool_manager.create(PoolTicket("NEW_APP")) pool = self.pool_manager.create(PoolTicket("NEW_APP_ONE")) self.pool_manager.update_status(pool.get_id(), self.pool_statuses.DISABLED.get_id()) self.pool_manager.create(PoolTicket("OTHER")) result = self.pool_manager.search( statuses=[self.pool_statuses.DISABLED.get_id()]) self.assertEqual(1, len(result.get_data()))
def test_create_pool_ticket_fails_on_const_error(self): with self.assertRaises(PoolConstError): PoolTicket("not_upper_case") self.fail("Did not fail on lower case") with self.assertRaises(PoolConstError): PoolTicket("HAS_NUMBERS_1234") self.fail("Did not fail on numbers") with self.assertRaises(PoolConstError): PoolTicket("HAS_SPECIAL_CHARACTER_%") self.fail("Did not fail on special character")
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_authorize_fails_when_not_pool_user(self): username = "******" password = "******" 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() self.pool_manager.create(pool_ticket) with self.assertRaises(AuthPoolUserNotFoundError): self.auth_pool_manager.authorize(access_id, secret, username, password) self.fail("Did not fail on missing pool user")
def create(self, pool_ticket: PoolTicket) -> Pool: """ Create pool Args: pool_ticket (PoolTicket): Contains generated access ID and secret Returns: Pool """ result = self.__pool_data.insert( const=pool_ticket.get_const(), status_id=self.__statuses.ACTIVE.get_id(), access_id=pool_ticket.get_access_id(), secret=self.__encrypt_secret(pool_ticket.get_secret()) ) if not result.get_status(): raise PoolCreateError("Could not create pool") return self.get(result.get_insert_id())
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_create_fails_on_create_error(self): pool_ticket = PoolTicket("SOME_NAME") self.postgres_conn_manager.insert = MagicMock( return_value=Result(False)) with self.assertRaises(PoolCreateError): self.pool_manager.create(pool_ticket) self.fail("Did not fail on create") self.postgres_conn_manager.insert.assert_called_once()
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_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_update_status_updates_status(self): const = "NEW_APP" pool_ticket = PoolTicket(const) pool = self.pool_manager.create(pool_ticket) new_status_id = self.pool_statuses.DISABLED.get_id() self.pool_manager.update_status(pool.get_id(), new_status_id) fetched_pool = self.pool_manager.get(pool.get_id()) self.assertEqual(new_status_id, fetched_pool.get_status().get_id())
def test_create_creates_pool(self): pool_ticket = PoolTicket("POOL_NAME") pool_data = { "id": 1, "uuid": "ERT-123", "const": pool_ticket.get_const(), "status_id": self.active_status.get_id() } self.postgres_conn_manager.insert = MagicMock( return_value=Result(True)) self.postgres_conn_manager.select = MagicMock( return_value=Result(True, "", [pool_data])) pool = self.pool_manager.create(pool_ticket) self.postgres_conn_manager.insert.assert_called_once() self.postgres_conn_manager.select.assert_called_once() self.assertEqual(pool_data["id"], pool.get_id()) self.assertEqual(pool_data["uuid"], pool.get_uuid()) self.assertEqual(pool_data["const"], pool.get_const()) self.assertEqual(pool_data["status_id"], pool.get_status().get_id())
def test_create_creates_pool(self): const = "NEW_APP" pool_ticket = PoolTicket(const) pool = self.pool_manager.create(pool_ticket) fetched_pool = self.pool_manager.get(pool.get_id()) self.assertEqual(fetched_pool.get_id(), pool.get_id()) self.assertEqual(fetched_pool.get_uuid(), pool.get_uuid()) self.assertEqual(fetched_pool.get_const(), pool.get_const()) self.assertEqual(fetched_pool.get_status().get_id(), pool.get_status().get_id())
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_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_sorts_result(self): username_1 = "sayre" username_2 = "other" 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(username_1), PasswordTicket("SomePassword#123"), NameTicket("Stephen", "Ayre")) user_2 = self.user_manager.create(UsernameTicket(username_2), 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()) result = self.user_pool_manager.search([pool_1.get_id()], 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_fake_user_id(self): pool = self.pool_manager.create(PoolTicket("MY_POOL")) with self.assertRaises(UserPoolAddError): self.user_pool_manager.add(pool.get_id(), 123456678) self.fail("Did not fail on fake user id")
def test_search_searches_by_const(self): self.pool_manager.create(PoolTicket("NEW_APP")) self.pool_manager.create(PoolTicket("NEW_APP_ONE")) self.pool_manager.create(PoolTicket("OTHER")) result = self.pool_manager.search(const="APP") self.assertEqual(2, len(result.get_data()))