def test_users_for_perms(self, db_session): user = User(user_name="aaa", email="aaa", status=0) UserService.set_password(user, "password") aaa_perm = UserPermission(perm_name="aaa") bbb_perm = UserPermission(perm_name="bbb") bbb2_perm = UserPermission(perm_name="bbb") user.user_permissions.append(aaa_perm) user.user_permissions.append(bbb_perm) user2 = User(user_name="bbb", email="bbb", status=0) UserService.set_password(user2, "password") user2.user_permissions.append(bbb2_perm) user3 = User(user_name="ccc", email="ccc", status=0) UserService.set_password(user3, "password") group = add_group(db_session) group.users.append(user3) db_session.add(user) db_session.add(user2) db_session.flush() users = UserService.users_for_perms(["aaa"], db_session=db_session) assert len(users.all()) == 1 assert users[0].user_name == "aaa" users = UserService.users_for_perms(["bbb"], db_session=db_session).all() assert len(users) == 2 assert ["aaa", "bbb"] == sorted([u.user_name for u in users]) users = UserService.users_for_perms(["aaa", "bbb", "manage_apps"], db_session=db_session) assert ["aaa", "bbb", "ccc"] == sorted([u.user_name for u in users])
def test_users_for_perms(self, db_session): user = User(user_name='aaa', email='aaa', status=0) user.set_password('password') aaa_perm = UserPermission(perm_name='aaa') bbb_perm = UserPermission(perm_name='bbb') bbb2_perm = UserPermission(perm_name='bbb') user.user_permissions.append(aaa_perm) user.user_permissions.append(bbb_perm) user2 = User(user_name='bbb', email='bbb', status=0) user2.set_password('password') user2.user_permissions.append(bbb2_perm) user3 = User(user_name='ccc', email='ccc', status=0) user3.set_password('password') group = add_group(db_session, ) group.users.append(user3) db_session.add(user) db_session.add(user2) db_session.flush() users = User.users_for_perms(['aaa'], db_session=db_session) assert len(users.all()) == 1 assert users[0].user_name == 'aaa' users = User.users_for_perms(['bbb'], db_session=db_session).all() assert len(users) == 2 assert ['aaa', 'bbb'] == sorted([u.user_name for u in users]) users = User.users_for_perms(['aaa', 'bbb', 'manage_apps'], db_session=db_session) assert ['aaa', 'bbb', 'ccc'] == sorted([u.user_name for u in users])
def test_by_user_and_perm(self, db_session): add_user(db_session) user_permission = UserPermission.by_user_and_perm( 1, 'root', db_session=db_session) assert user_permission.user_id == 1 assert user_permission.perm_name == 'root'
def test_by_user_and_perm(self, db_session): add_user(db_session) user_permission = UserPermission.by_user_and_perm(1, 'root', db_session=db_session) assert user_permission.user_id == 1 assert user_permission.perm_name == 'root'
def add_user(db_session, user_name='username', email='email', perms=['root', 'alter_users']): user = User(user_name=user_name, email=email, status=0) user.set_password('password') for perm in perms: u_perm = UserPermission(perm_name=perm) user.user_permissions.append(u_perm) db_session.add(user) db_session.flush() return user
def add_user(db_session, user_name="username", email="email", perms=["root", "alter_users"]): user = User(user_name=user_name, email=email, status=0) UserService.set_password(user, "password") for perm in perms: u_perm = UserPermission(perm_name=perm) user.user_permissions.append(u_perm) db_session.add(user) db_session.flush() return user
def test_by_user_and_perm_wrong_permname(self, db_session): add_user(db_session) user_permission = UserPermission.by_user_and_perm( 1, 'wrong', db_session=db_session) assert user_permission is None
def test_by_user_and_perm_wrong_username(self, db_session): add_user(db_session) user_permission = UserPermission.by_user_and_perm( 999, 'root', db_session=db_session) assert user_permission is None
def test_repr(self, db_session): user_permission = UserPermission(user_id=1, perm_name='perm') assert repr(user_permission) == '<UserPermission: perm>'
def test_by_user_and_perm_wrong_permname(self, db_session): add_user(db_session) user_permission = UserPermission.by_user_and_perm(1, 'wrong', db_session=db_session) assert user_permission is None
def test_by_user_and_perm_wrong_username(self, db_session): add_user(db_session) user_permission = UserPermission.by_user_and_perm(999, 'root', db_session=db_session) assert user_permission is None
def test_repr(self, db_session): user_permission = UserPermission(user_id=1, perm_name="perm") assert repr(user_permission) == "<UserPermission: perm>"