def test_list_users(database): """Test listing and searching for users.""" # -- Setup ---------------------------------------------------------------- # # Create a database with four active and one inactive user. with database.session() as session: users = UserManager(session) users.register_user('*****@*****.**', 'pwd1', verify=False) users.register_user('*****@*****.**', 'pwd1', verify=False) users.register_user('*****@*****.**', 'pwd1', verify=False) users.register_user('*****@*****.**', 'pwd1', verify=False) u = users.register_user('*****@*****.**', 'pwd1', verify=True) inactive_user_id = u.user_id # -- Test user listing ---------------------------------------------------- with database.session() as session: # There should be four users in the returned list. users = UserManager(session) assert len(users.list_users()) == 4 with database.session() as session: # After activating the inactive user, the listing contains five users. users = UserManager(session) users.activate_user(inactive_user_id) assert len(users.list_users()) == 5 # -- Test query prefixes -------------------------------------------------- with database.session() as session: # Query users by name prefix. users = UserManager(session) assert len(users.list_users(prefix='a')) == 3 assert len(users.list_users(prefix='ab')) == 2 assert len(users.list_users(prefix='ade')) == 1
def test_user_listing_serialization(database): """Test serialization of user listings.""" schema = validator('UserListing') view = UserSerializer() with database.session() as session: manager = UserManager(session) manager.register_user('alice', 'mypwd') manager.register_user('bob', 'mypwd') doc = view.user_listing(manager.list_users()) schema.validate(doc) assert len(doc[labels.USER_LIST]) == 2