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_activate_user(database): """Test creating inactive users and activating them.""" # -- Test creating an inactive user --------------------------------------- username = '******' password = '******' with database.session() as session: users = UserManager(session) user = users.register_user(username, password, verify=True) user_id = user.user_id # Attempt to login will raise error with pytest.raises(err.UnknownUserError): users.login_user(username, password) # -- Test activate user --------------------------------------------------- with database.session() as session: # After activating the user login should succeed. users = UserManager(session) auth = OpenAccessAuth(session) active_user = users.activate_user(user_id) assert active_user.user_id == user_id assert active_user.name == username assert active_user.api_key is None user = users.login_user(username, password) assert user.name == username assert user.api_key is not None assert auth.authenticate(user.api_key.value).user_id == user_id # Activate the same user twice should not raise an error active_user = users.activate_user(user_id) assert active_user.user_id == user_id assert active_user.name == username assert active_user.api_key is not None