def test_generate_random_pass(self, db_session): rand_str = User.generate_random_pass() assert len(rand_str) == 7 assert isinstance(rand_str, six.string_types) rand_str = User.generate_random_pass(20) assert len(rand_str) == 20
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 test_add_user(self, db_session): user = User(user_name='username', email='email', status=0) db_session.add(user) db_session.flush() user = db_session.query(User).filter(User.user_name == 'username') user = user.first() assert user.user_name == 'username' assert user.email == 'email' assert user.status == 0
def test_delete_object_with_flush(self, db_session): user = User(user_name='some_new_user', email='foo') assert user.id is None user.persist(flush=True, db_session=db_session) assert user.id is not None uid = user.id User.by_id(uid, db_session=db_session) is not None user.delete() assert User.by_id(uid, db_session=db_session) is None
def test_by_username_andsecurity_code_wrong_code(self, db_session): add_user(db_session) queried_user = User.by_user_name_and_security_code( user_name='username', security_code='wrong_code', db_session=db_session ) assert queried_user is None
def test_by_user_names_like(self, db_session): user1 = add_user(db_session, 'user1', 'email1') add_user(db_session, 'luser2', 'email2') add_user(db_session, 'noname', 'email3') queried_users = User.user_names_like('user%', db_session=db_session).all() assert len(queried_users) == 1 assert user1 == queried_users[0]
def test_by_username_andsecurity_code_not_existing(self, db_session): created_user = add_user(db_session) security_code = created_user.security_code queried_user = User.by_user_name_and_security_code( user_name='not_existing_user', security_code=security_code, db_session=db_session ) assert queried_user is None
def test_by_username_andsecurity_code_none(self, db_session): created_user = add_user(db_session) security_code = created_user.security_code found = User.by_user_name_and_security_code( user_name=None, security_code=security_code, db_session=db_session ) assert found is None
def test_by_user_names_one_none(self, db_session): user1 = add_user(db_session, 'user1', 'email1') add_user(db_session, 'user2', 'email2') user3 = add_user(db_session, 'user3', 'email3') queried_users = User.by_user_names(['user1', None, 'user3'], db_session=db_session).all() assert len(queried_users) == 2 assert user1 == queried_users[0] assert user3 == queried_users[1]
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_delete_object_with_flush(self, db_session): user = User(user_name="some_new_user", email="foo") assert user.id is None user.persist(flush=True, db_session=db_session) assert user.id is not None uid = user.id UserService.by_id(uid, db_session=db_session) is not None user.delete() assert UserService.by_id(uid, db_session=db_session) is None
def test_by_user_name_existing(self, db_session): created_user = add_user(db_session) queried_user = User.by_user_name('username', db_session=db_session) assert created_user == queried_user
def test_by_user_names_like_none(self, db_session): queried_users = User.user_names_like(None, db_session=db_session).all() assert [] == queried_users
def test_get_keys(self, db_session): keys = User._get_keys() assert len(keys) == 9
def test_by_user_name_not_existing(self, db_session): add_user(db_session) queried_user = User.by_user_name('not_existing_user', db_session=db_session) assert queried_user is None
def test_delete_user(self, db_session): add_user(db_session) to_delete = User.by_user_name('username', db_session=db_session) db_session.delete(to_delete) db_session.commit()
def test_by_mail_and_username_none(self, db_session): found = User.by_email_and_username('email', None, db_session=db_session) assert found is None
def test_by_email_none(self, db_session): add_user(db_session) queried_user = User.by_email(None, db_session=db_session) assert queried_user is None
def test_add_object_without_flush(self, db_session): user = User(user_name="some_new_user", email="foo") assert user.id is None user.persist(db_session=db_session) assert user.id is None
def test_by_email_wrong_email(self, db_session): add_user(db_session) queried_user = User.by_email('wrong_email', db_session=db_session) assert queried_user is None
def test_by_mail_and_username(self, db_session): created_user = add_user(db_session) queried_user = User.by_email_and_username('email', 'username', db_session=db_session) assert created_user == queried_user
def test_by_mail_and_username_wrong_username(self, db_session): add_user(db_session) queried_user = User.by_email_and_username('email', 'wrong_username', db_session=db_session) assert queried_user is None
def test_by_user_name_none(self, db_session): queried_user = User.by_user_name(None, db_session=db_session) assert queried_user is None
def test_by_email(self, db_session): created_user = add_user(db_session) queried_user = User.by_email('email', db_session=db_session) assert created_user == queried_user
def test_add_object_with_flush(self, db_session): user = User(user_name='some_new_user', email='foo') assert user.id is None user.persist(flush=True, db_session=db_session) assert user.id is not None
def test_generate_random_string(self, db_session): rand_str = User.generate_random_string() assert len(rand_str) == 7 assert isinstance(rand_str, six.string_types)
def add_user(i, db_name, db_session): user = User(id=i, user_name='username_{}_db_{}'.format(i, db_name), email='email_{}'.format(i), status=0) db_session.add(user)
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])