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_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_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 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_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_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_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_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_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_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_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_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_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_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_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 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()
def test_user_ticket_fails_on_bad_username(self): username_good_1 = "username" username_good_2 = "username123" username_good_3 = "123username" username_bad_1 = "123" username_bad_2 = "UserName123" username_bad_3 = "username#123" try: UsernameTicket(username_good_1) UsernameTicket(username_good_2) UsernameTicket(username_good_3) except Exception as e: self.fail(str(e)) with self.assertRaises(UserTicketUsernameError): UsernameTicket(username_bad_1) self.fail("Did not fail on bad 1") with self.assertRaises(UserTicketUsernameError): UsernameTicket(username_bad_2) self.fail("Did not fail on bad 2") with self.assertRaises(UserTicketUsernameError): UsernameTicket(username_bad_3) self.fail("Did not fail on bad 3")
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_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_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 test_authorize_authorizes_user(self): username = "******" password = "******" user = self.user_manager.create( UsernameTicket(username), PasswordTicket(password), NameTicket("First", "Last") ) token = self.auth_manager.authorize(username, password) token_decoded = jwt.decode(token.get_token(), self.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 register(): """ Register user Returns: json """ error_code_parser = UsersErrorCodeHandler() try: data = json.loads(request.get_data().decode()) user_manager = UserManager() user = user_manager.create( UsernameTicket(data["username"]), PasswordTicket(data["password"]), NameTicket(data["first_name"], data["last_name"]), ) return Response([UserTransformer(user)]).get_response() except Exception as e: return Response([], str(e), error_code_parser.get_code( type(e).__name__)).get_response()