def test_add_permission():
    old = 0
    with get_session_local() as session:
        old = session.query(PermissionDB).count()

    with override_get_user(is_superuser=True):
        client = get_client()
        for i in range(3):
            response = client.post(api_prefix, json=dict(name=uuid()))
            assert_response_success(response)

    with get_session_local() as session:
        assert session.query(PermissionDB).count() == old + 3
def test_search_user_search():
    with get_session_local() as session:
        role = generate_role()
        session.add(role)
        user_line = [generate_user() for _ in range(10)]
        for i, user in enumerate(user_line):
            user.name = str(i)
            user.sex = i % 2 + 1
        user2role_line = []
        session.add_all(user_line)
        session.flush()
        for user in user_line:
            user2role_line.append(
                generate_user2role(user_id=user.id, role_id=role.id))
        session.add_all(user2role_line)
        session.commit()

    client = get_client()
    with override_get_user():
        response = client.get(f'{api_prefix}/', params={'role_id': role.id})
        assert_response_fail(response)

    with override_get_user() as user:
        user.role_set.add(MANAGE_ROLE_PERMISSION_NAME.format(role_id=role.id))
        user.role_id_set.add(role.id)
        response = client.get(f'{api_prefix}/', params={'role_id': role.id})
        data = assert_response_success(response)
        assert len(data) == 10
        response = client.get(f'{api_prefix}/',
                              params={
                                  'role_id': role.id,
                                  'sex': 1
                              })
        data = assert_response_success(response)
        assert len(data) == 5
Beispiel #3
0
def test_register():
    client = get_client()
    table_data = (
        (right_user_data, True),
        (
            {
                'sex': 1,
                'phone': "132186818",  # wrong
                'password': "******",
            },
            False),
        (
            {
                'sex': 100,  # wrong
                'phone': "13218655818",
                'password': "******",
            },
            False),
        ({
            'sex': 0,
            'phone': "13218655818",
            'password': "******",
        }, False),
    )
    for data, ok in table_data:
        response = client.post(f"{api_prefix}/register", json=data)
        print(response.json())
        assert (response.status_code == 200) == ok
    with get_session_local() as session:
        assert 1 == session.query(UserDB).count()
Beispiel #4
0
def test_del_user_to_role():
    with get_session_local() as session:
        user1 = generate_user()
        user2 = generate_user()
        user3 = generate_user()
        role1 = generate_role()
        role2 = generate_role()
        session.add_all((user1, user2, user3, role1, role2))
        session.flush()
        session.add_all((generate_user2role(user1.id, role1.id),
                         generate_user2role(user1.id, role1.id),
                         generate_user2role(user2.id, role1.id),
                         generate_user2role(user2.id, role2.id),
                         generate_user2role(user3.id, role2.id)
                         ))
        session.commit()
        
        with override_get_user(is_superuser=True):
            client = get_client()
            response = client.post(f'{api_prefix}/{role1.id}/users:delete', json=[user1.id])
            assert_response_success(response)
            response = client.get(f'{api_prefix}/{role1.id}/users')
            data = assert_response_success(response)
            assert len(data) == 1
            
            response = client.post(f'{api_prefix}/{role1.id}/users:delete', json=[user2.id, user3.id])
            assert_response_success(response)
            response = client.get(f'{api_prefix}/{role1.id}/users')
            data = assert_response_success(response)
            assert len(data) == 0
            response = client.get(f'{api_prefix}/{role2.id}/users')
            data = assert_response_success(response)
            assert len(data) == 2
Beispiel #5
0
 def assert_user_identity(u1_id, u2_id, u3_id, i1, i2, i3):
     with get_session_local() as session:
         u1 = session.query(UserDB).get(u1_id)
         u2 = session.query(UserDB).get(u2_id)
         u3 = session.query(UserDB).get(u3_id)
         
         assert u1.user_identity == i1
         assert u2.user_identity == i2
         assert u3.user_identity == i3
def test_delete_permission():
    with get_session_local() as session:
        permission = generate_permission()
        session.add(permission)
        session.flush()
        old = session.query(PermissionDB).count()
        session.commit()

        with override_get_user(is_superuser=True):
            client = get_client()
            print(f'{api_prefix}/{permission.id}')
            response = client.delete(f'{api_prefix}/{permission.id}')
            assert_response_success(response)

        assert session.query(PermissionDB).count() == old - 1
def test_update_permission():
    with get_session_local() as session:
        permission = generate_permission()
        session.add(permission)
        session.commit()

        new_name = uuid()
        with override_get_user(is_superuser=True):
            client = get_client()
            response = client.put(f'{api_prefix}/{permission.id}',
                                  json=dict(name=new_name))
            assert_response_success(response)

        session.refresh(permission)
        assert permission.name == new_name
Beispiel #8
0
def test_update_user_as_secretary_and_cancel_user_as_secretary_if_no_user_group():
    with get_session_local() as session:
        u1 = generate_user()
        u2 = generate_user()
        u3 = generate_user()
        u1.user_identity = UserIdentity.ADMIN
        u3.user_identity = UserIdentity.ADMIN
        g1 = generate_role()
        g2 = generate_role()
        session.add_all((u1, u2, u3, g1, g2))
        session.commit()
    
    def assert_user_identity(u1_id, u2_id, u3_id, i1, i2, i3):
        with get_session_local() as session:
            u1 = session.query(UserDB).get(u1_id)
            u2 = session.query(UserDB).get(u2_id)
            u3 = session.query(UserDB).get(u3_id)
            
            assert u1.user_identity == i1
            assert u2.user_identity == i2
            assert u3.user_identity == i3
    
    client = get_client()
    with override_get_user(is_superuser=True):
        # test add
        response = client.post(f'{api_prefix}/{g1.id}/users:add', json=[u1.id, u2.id, u3.id])
        assert_response_success(response)
        assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN)
        
        # test add more
        response = client.post(f'{api_prefix}/{g2.id}/users:add', json=[u2.id, u3.id])
        assert_response_success(response)
        assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN)
        
        # test delete
        response = client.post(f'{api_prefix}/{g1.id}/users:delete', json=[u1.id, u2.id])
        assert_response_success(response)
        assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER, UserIdentity.ADMIN, UserIdentity.ADMIN)
        
        # test delete more
        response = client.post(f'{api_prefix}/{g2.id}/users:delete', json=[u2.id, u3.id])
        assert_response_success(response)
        assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER, UserIdentity.COMMON_USER, UserIdentity.ADMIN)
        
        # test delete all
        response = client.post(f'{api_prefix}/{g1.id}/users:delete', json=[u2.id, u3.id])
        assert_response_success(response)
        assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER, UserIdentity.COMMON_USER, UserIdentity.COMMON_USER)
Beispiel #9
0
def test_get_users_by_role_id_query():
    with get_session_local() as session:
        user1 = generate_user()
        user2 = generate_user()
        user3 = generate_user()
        role1 = generate_role()
        session.add_all((user1, user2, user3, role1))
        session.flush()
        session.add(User2RoleDB(user_id=user1.id, role_id=role1.id))
        session.add(User2RoleDB(user_id=user2.id, role_id=role1.id))
        session.commit()
        
        paginate = get_users_by_role_id(session, role1.id, PageInfo(page_id=1, page_size=10))
        assert len(paginate.items) == 2
        for user in paginate.items:
            assert user.id != user3.id
Beispiel #10
0
def test_update_role_():
    with get_session_local() as session:
        role1 = generate_role()
        role2 = generate_role()
        session.add_all((role1, role2))
        session.commit()
        
        with override_get_user(is_superuser=True):
            client = get_client()
            response = client.put(f'{api_prefix}/{role1.id}', json=dict(name='123'))
            data = assert_response_success(response)
            assert data['name'] == '123'
            session.refresh(role1)
            assert role1.name == '123'
            
            response = client.put(f'{api_prefix}/{role2.id}', json=dict(name='123'))
            assert_response_fail(response)
Beispiel #11
0
def test_pagination():
    with get_session_local() as session:
        # session = TestingSessionLocal()
        for i in range(10):
            session.add(generate_role())
        session.commit()

        p1 = Pagination(session.query(RoleDB), 1, 5)
        assert p1.total == 10
        assert len(p1.items) == 5

        p2 = Pagination(session.query(RoleDB), 2, 5)
        assert p2.total == 10
        assert len(p2.items) == 5

        p3 = Pagination(session.query(RoleDB), 2, 8)
        assert p3.total == 10
        assert len(p3.items) == 2
def test_permission_list():
    with get_session_local() as session:
        session.add_all(tuple(generate_permission() for i in range(20)))
        session.commit()

    with override_get_user(is_superuser=True):
        client = get_client()
        response = client.get(api_prefix, params=dict(page_id=1, page_size=10))
        data = assert_response_success(response)
        assert len(data) == 10

        response = client.get(api_prefix, params=dict(page_id=2, page_size=10))
        data = assert_response_success(response)
        assert len(data) == 10

        response = client.get(api_prefix, params=dict(page_id=2, page_size=15))
        data = assert_response_success(response)
        assert len(data) == 5
Beispiel #13
0
def test_role_list_by_superuser():
    with get_session_local() as session:
        for i in range(20):
            session.add(generate_role())
        session.commit()
    
    with override_get_user(is_superuser=True):
        client = get_client()
        response = client.get(api_prefix)
        print(api_prefix)
        assert_response_success(response)
        assert len(response.json()['data']) == 20
        
        response = client.get(api_prefix, params=dict(page_size=10))
        assert_response_success(response)
        assert len(response.json()['data']) == 10
        
        response = client.get(api_prefix, params=dict(page_id=2, page_size=10))
        assert_response_success(response)
        assert len(response.json()['data']) == 10
Beispiel #14
0
def test_add_role():
    with override_get_user(is_superuser=True):
        client = get_client()
        response = client.post(api_prefix, json=dict(name=uuid()))
        g1 = assert_response_success(response)
        assert g1['grand_id'] == ''
        assert g1['parent_id'] == 0
        
        response = client.post(api_prefix, json=dict(name=uuid()))
        g2 = assert_response_success(response)
        
        response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g1['id']))
        g3 = assert_response_success(response)
        assert g3['grand_id'] == f'|{g1["id"]}|'
        assert g3['parent_id'] == g1['id']
        
        response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g3['id']))
        g4 = assert_response_success(response)
        assert g4['grand_id'] == f'|{g1["id"]}|{g3["id"]}|'
        assert g4['parent_id'] == g3['id']
    
    with get_session_local() as session:
        assert session.query(RoleDB).count() == 4
Beispiel #15
0
def test_del_role():
    with get_session_local() as session:
        user11 = generate_user()
        user12 = generate_user()
        user2 = generate_user()
        user3 = generate_user()
        role1 = generate_role()
        role2 = generate_role()
        session.add_all((user11, user12, user2, user3, role1, role2))
        session.flush()
        session.add_all((generate_user2role(user11.id, role1.id),
                         generate_user2role(user12.id, role1.id),
                         generate_user2role(user2.id, role2.id)
                         ))
        session.commit()
        
        with override_get_user(is_superuser=True):
            client = get_client()
            response = client.delete(f'{api_prefix}/{role1.id}')
            assert_response_success(response)
            r: User2RoleDB = session.query(User2RoleDB).one()
            assert r.user_id == user2.id
            assert r.role_id == role2.id
Beispiel #16
0
def test_role_user_list():
    with override_get_user(is_superuser=True):
        client = get_client()
        response = client.post(api_prefix, json=dict(name=uuid()))
        g1 = assert_response_success(response)
        
        response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g1['id']))
        g2 = assert_response_success(response)
    
    with get_session_local() as session:
        user11 = generate_user()
        user12 = generate_user()
        user2 = generate_user()
        user3 = generate_user()
        session.add_all((user11, user12, user2, user3))
        session.flush()
        session.add_all((generate_user2role(user11.id, g1['id']),
                         generate_user2role(user12.id, g1['id']),
                         generate_user2role(user2.id, g2['id']),
                         generate_user2role(user2.id, g2['id'])
                         ))
        session.commit()
        
        with override_get_user() as user:
            user.role_id_set.add(g1['id'])
            user.role_set.add(g1['name'])
            client = get_client()
            response = client.get(f'{api_prefix}/{g1["id"]}/users')
            data = assert_response_success(response)
            assert len(data) == 2
            
            response = client.get(f'{api_prefix}/{g2["id"]}/users')
            data = assert_response_success(response)
            print(data)
            assert len(data) == 1
            assert data[0]['id'] == user2.id
Beispiel #17
0
def test_get_user_with_permission_and_group_by_id():
    with get_session_local() as session:
        # simple
        user = generate_user()
        role = generate_role()
        permission1 = generate_permission()
        permission2 = generate_permission()
        session.add_all((user, role, permission1, permission2))
        session.flush()

        session.add_all((generate_user2role(user.id, role.id),
                         generate_permission2role(permission1.id, role.id),
                         generate_permission2role(permission2.id, role.id)))
        session.commit()

        user = get_user_with_permission_and_group_by_id(session, user.id)
        assert user is not None
        assert user.permission_set == {permission1.name, permission2.name}
        assert user.role_set == {role.name}

    clean_all()
    with get_session_local() as session:
        # more group and permission
        user = generate_user()
        role1 = generate_role()
        role2 = generate_role()
        permission1 = generate_permission()
        permission2 = generate_permission()
        permission3 = generate_permission()
        session.add_all(
            (user, role1, role2, permission1, permission2, permission3))
        session.flush()

        session.add_all((generate_user2role(user.id, role1.id),
                         generate_user2role(user.id, role2.id),
                         generate_permission2role(permission1.id, role1.id),
                         generate_permission2role(permission2.id, role1.id),
                         generate_permission2role(permission3.id, role2.id)))
        session.commit()

        user = get_user_with_permission_and_group_by_id(session, user.id)
        assert user is not None
        assert user.permission_set == {
            permission1.name, permission2.name, permission3.name
        }
        assert user.role_set == {role1.name, role2.name}

    clean_all()
    with get_session_local() as session:
        # has group, no permission
        user = generate_user()
        role1 = generate_role()
        role2 = generate_role()
        session.add_all((user, role1, role2))
        session.flush()

        session.add_all((
            generate_user2role(user.id, role1.id),
            generate_user2role(user.id, role2.id),
        ))
        session.commit()

        user = get_user_with_permission_and_group_by_id(session, user.id)
        assert user is not None
        assert len(user.permission_set) == 0
        assert user.role_set == {role1.name, role2.name}

    clean_all()
    with get_session_local() as session:
        # no group, no permission
        user = generate_user()
        session.add(user)
        session.commit()

        user = get_user_with_permission_and_group_by_id(session, user.id)
        assert user is not None
        assert len(user.permission_set) == 0
        assert len(user.role_set) == 0