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
示例#2
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
def test_manage_reset_password():
    with override_get_user(is_superuser=True) as user:
        client = get_client()
        response = client.post(f'{api_prefix}/reset-password', json=[user.id])
        data = assert_response_success(response)
        assert data == {'count': 1}

        response = client.post('/v1/api/users/login',
                               json=dict(phone=user.phone,
                                         password='******'))
        assert_response_success(response)
def test_update_op_user_():
    with override_get_user(is_superuser=True) as user:
        client = get_client()
        response = client.put(f'{api_prefix}/{user.id}',
                              json={
                                  'sex': 1,
                                  'phone': '13218655818',
                                  'name': '小明',
                                  'address': '123'
                              })
        assert_response_success(response)
示例#5
0
def test_role_list_by_user_group():
    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()))
        g2 = assert_response_success(response)
        
        response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g1['id']))
        g3 = assert_response_success(response)
        
        response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g3['id']))
        g4 = assert_response_success(response)
    
    with override_get_user() as user:
        user.role_id_set.add(g2['id'])
        user.role_set.add(g2['name'])
        
        client = get_client()
        response = client.get(api_prefix)
        data = assert_response_success(response)
        assert len(data) == 1
    
    with override_get_user() as user:
        user.role_id_set.add(g3['id'])
        user.role_set.add(g3['name'])
        
        client = get_client()
        response = client.get(api_prefix)
        data = assert_response_success(response)
        assert len(data) == 2
    
    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(api_prefix)
        data = assert_response_success(response)
        assert len(data) == 3
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_manager_create_user():
    with override_get_user(is_superuser=True):
        client = get_client()
        response = client.post(f'{api_prefix}',
                               json={
                                   'sex': 1,
                                   'phone': '13218655818',
                                   'name': '小明'
                               })
        assert_response_success(response)

    response = client.post('/v1/api/users/login',
                           json=dict(phone='13218655818', password=''))
    assert_response_success(response)
示例#8
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
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
示例#11
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)
示例#12
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)
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
示例#14
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
示例#15
0
def test_update_delete_not_find():
    not_exist_id = -1
    
    with override_get_user(is_superuser=True):
        client = get_client()
        response = client.post(f'{api_prefix}/{not_exist_id}/users:delete', json=[1, 2])
        assert_response_fail(response)
        assert response.json()['code'] == 404
        
        response = client.post(f'{api_prefix}/{not_exist_id}/users:add', json=[1, 2])
        assert_response_fail(response)
        assert response.json()['code'] == 404
        
        response = client.delete(f'{api_prefix}/{not_exist_id}')
        assert_response_fail(response)
        assert response.json()['code'] == 404
        
        response = client.put(f'{api_prefix}/{not_exist_id}', json=dict(name=uuid()))
        assert_response_fail(response)
        assert response.json()['code'] == 404
示例#16
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
示例#17
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
def test_read_op_user_():
    with override_get_user(is_superuser=True) as user:
        client = get_client()
        response = client.get(f'{api_prefix}/{user.id}')
        assert_response_success(response)