Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

добавление ручки для получения данных обо всех пользователях #41

Merged
merged 28 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
376378a
добавление ручки для получения данных обо всех пользователях
Zimovchik Jul 16, 2024
8d900e9
lint
Zimovchik Jul 16, 2024
837a393
lint
Zimovchik Jul 16, 2024
c843f19
Update user.py
Zimovchik Jul 16, 2024
7734ef4
fix
Zimovchik Jul 16, 2024
805709e
Merge branch 'main' of https://github.com/profcomff/userdata-api into…
Zimovchik Jul 17, 2024
91ed1cc
request change
Zimovchik Jul 17, 2024
473d186
fix
Zimovchik Jul 17, 2024
e694bd6
fix
Zimovchik Jul 17, 2024
ba8861a
fix
Zimovchik Jul 17, 2024
3815d0f
fix
Zimovchik Jul 17, 2024
8898f71
requested changes fix
Zimovchik Jul 18, 2024
4ce654a
Merge branch 'main' into Zimovchik
Zimovchik Jul 20, 2024
affcddc
Update test_users_get.py
Zimovchik Jul 29, 2024
2fa881b
Merge branch 'main' into Zimovchik
Zimovchik Jul 29, 2024
e07d4dd
redone response structure+tests
Zimovchik Jul 31, 2024
f9685f4
Merge branch 'Zimovchik' of https://github.com/profcomff/userdata-api…
Zimovchik Jul 31, 2024
c697333
Update test_users_get.py
Zimovchik Jul 31, 2024
7bcd5a4
response format change
Zimovchik Aug 2, 2024
81d6322
fix
Zimovchik Aug 2, 2024
0276540
deleting categories in tests
Zimovchik Aug 3, 2024
a434a5b
maybe fix
Zimovchik Aug 11, 2024
6d83333
get/{id} scopes bug fix
Zimovchik Aug 12, 2024
9251663
fix
Zimovchik Aug 12, 2024
20e90ad
{tuple : list | value} ->> {param : {owner_id : ...}}
Zimovchik Aug 12, 2024
aa561e8
minor fixes
Zimovchik Aug 12, 2024
4c9d634
fix
Zimovchik Aug 12, 2024
7292fe3
fix
Zimovchik Aug 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/test_routes/test_user_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def test_get(client, dbsession, source, info_no_scopes):
assert info1.category.name == response.json()["items"][0]["category"]
assert info1.param.name == response.json()["items"][0]["param"]
assert info1.value == response.json()["items"][0]["value"]
dbsession.delete(info1)
dbsession.commit()


@pytest.mark.authenticated(user_id=1)
Expand All @@ -26,6 +28,7 @@ def test_get_no_all_scopes(client, dbsession, source, info_no_scopes):
response = client.get(f"/user/{info1.owner_id}")
assert response.status_code == 200
assert info1.category.name not in response.json()
dbsession.delete(info1)
dbsession.commit()


Expand Down Expand Up @@ -79,6 +82,10 @@ def test_get_a_few(client, dbsession, category_no_scopes, source):
dbsession.delete(param2)
dbsession.delete(param3)
dbsession.delete(param4)
dbsession.flush()
dbsession.delete(category1)
dbsession.delete(category2)
dbsession.delete(category3)
dbsession.commit()


Expand Down Expand Up @@ -147,6 +154,10 @@ def test_get_a_few_with_trust_level(client, dbsession, category_no_scopes, sourc
dbsession.delete(param2)
dbsession.delete(param3)
dbsession.delete(param4)
dbsession.flush()
dbsession.delete(category1)
dbsession.delete(category2)
dbsession.delete(category3)
dbsession.commit()


Expand Down Expand Up @@ -181,4 +192,6 @@ def test_get_last_most_trusted(client, dbsession, category_no_scopes, source):
dbsession.delete(info4)
dbsession.flush()
dbsession.delete(param1)
dbsession.flush()
dbsession.delete(category1)
dbsession.commit()
156 changes: 156 additions & 0 deletions tests/test_routes/test_users_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
from time import sleep

import pytest

from userdata_api.models.db import Info, Param
from userdata_api.utils.utils import random_string


@pytest.mark.authenticated("userdata.info.admin")
def test_get(client, dbsession, category_no_scopes, source):
source = source()
category1 = category_no_scopes()
category2 = category_no_scopes()
category3 = category_no_scopes()
param1 = Param(
name=f"test{random_string()}", category_id=category1.id, type="last", changeable=True, is_required=True
)
param2 = Param(
name=f"test{random_string()}", category_id=category1.id, type="last", changeable=True, is_required=True
)
param3 = Param(
name=f"test{random_string()}", category_id=category2.id, type="last", changeable=True, is_required=True
)
param4 = Param(
name=f"test{random_string()}", category_id=category3.id, type="last", changeable=True, is_required=True
)
dbsession.add_all([param1, param2, param3, param4])
dbsession.flush()
info1 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param1.id, owner_id=0)
info2 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param2.id, owner_id=1)
info3 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param3.id, owner_id=0)
info4 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param4.id, owner_id=1)
dbsession.add_all([info1, info2, info3, info4])
dbsession.commit()
response = client.get(f"/user", params={"users": [0, 1], "categories": [category1.id, category2.id, category3.id]})
assert response.status_code == 200
assert {"user_id": 1, "category": category1.name, "param": info2.param.name, "value": info2.value} in list(
response.json()["items"]
)
assert {"user_id": 1, "category": category3.name, "param": info4.param.name, "value": info4.value} in list(
response.json()["items"]
)
assert {"user_id": 0, "category": category1.name, "param": info1.param.name, "value": info1.value} in list(
response.json()["items"]
)
assert {"user_id": 0, "category": category2.name, "param": info3.param.name, "value": info3.value} in list(
response.json()["items"]
)
dbsession.delete(info1)
dbsession.delete(info2)
dbsession.delete(info3)
dbsession.delete(info4)
dbsession.flush()
dbsession.delete(param1)
dbsession.delete(param2)
dbsession.delete(param3)
dbsession.delete(param4)
dbsession.flush()
dbsession.delete(category1)
dbsession.delete(category2)
dbsession.delete(category3)
dbsession.commit()


@pytest.mark.authenticated("userdata.info.admin")
def test_get_some_users(client, dbsession, category_no_scopes, source):
source = source()
category1 = category_no_scopes()
param1 = Param(
name=f"test{random_string()}", category_id=category1.id, type="last", changeable=True, is_required=True
)
dbsession.add_all([param1])
dbsession.flush()
info1 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param1.id, owner_id=1)
info2 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param1.id, owner_id=2)
info3 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param1.id, owner_id=3)
dbsession.add_all([info1, info2, info3])
dbsession.commit()
response = client.get(f"/user", params={"users": [1, 2], "categories": [category1.id]})
assert response.status_code == 200
assert {"user_id": 1, "category": category1.name, "param": param1.name, "value": info1.value} in list(
response.json()["items"]
)
assert {"user_id": 2, "category": category1.name, "param": param1.name, "value": info2.value} in list(
response.json()["items"]
)
assert {"user_id": 3, "category": category1.name, "param": param1.name, "value": info3.value} not in list(
response.json()["items"]
)
response = client.get(f"/user", params={"users": [3], "categories": [category1.id]})
assert response.status_code == 200
assert len(response.json()["items"]) == 1
assert {"user_id": 3, "category": category1.name, "param": param1.name, "value": info3.value} in list(
response.json()["items"]
)
dbsession.delete(info1)
dbsession.delete(info2)
dbsession.delete(info3)
dbsession.flush()
dbsession.delete(param1)
dbsession.flush()
dbsession.delete(category1)
dbsession.commit()


@pytest.mark.authenticated("userdata.info.admin")
def test_get_some_categories(client, dbsession, category_no_scopes, source):
source = source()
category1 = category_no_scopes()
category2 = category_no_scopes()
category3 = category_no_scopes()
param1 = Param(
name=f"test{random_string()}", category_id=category1.id, type="last", changeable=True, is_required=True
)
param2 = Param(
name=f"test{random_string()}", category_id=category2.id, type="last", changeable=True, is_required=True
)
param3 = Param(
name=f"test{random_string()}", category_id=category3.id, type="last", changeable=True, is_required=True
)
dbsession.add_all([param1, param2, param3])
dbsession.flush()
info1 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param1.id, owner_id=1)
info2 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param2.id, owner_id=1)
info3 = Info(value=f"test{random_string()}", source_id=source.id, param_id=param3.id, owner_id=1)
dbsession.add_all([info1, info2, info3])
dbsession.commit()
response = client.get(f"/user", params={"users": [1], "categories": [category1.id, category2.id]})
assert response.status_code == 200
assert {"user_id": 1, "category": category1.name, "param": info1.param.name, "value": info1.value} in list(
response.json()["items"]
)
assert {"user_id": 1, "category": category2.name, "param": info2.param.name, "value": info2.value} in list(
response.json()["items"]
)
assert {"user_id": 1, "category": category3.name, "param": info3.param.name, "value": info3.value} not in list(
response.json()["items"]
)

response = client.get(f"/user", params={"users": [1], "categories": [category3.id]})
assert {"user_id": 1, "category": category3.name, "param": info3.param.name, "value": info3.value} in list(
response.json()["items"]
)

dbsession.delete(info1)
dbsession.delete(info2)
dbsession.delete(info3)
dbsession.flush()
dbsession.delete(param1)
dbsession.delete(param2)
dbsession.delete(param3)
dbsession.flush()
dbsession.delete(category1)
dbsession.delete(category2)
dbsession.delete(category3)
dbsession.commit()
27 changes: 23 additions & 4 deletions userdata_api/routes/user.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from typing import Any

from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Query
from fastapi_sqlalchemy import db

from userdata_api.models.db import Category, Info
from userdata_api.schemas.response_model import StatusResponseModel
from userdata_api.schemas.user import UserInfoGet, UserInfoUpdate
from userdata_api.schemas.user import UserInfoGet, UserInfoUpdate, UsersInfoGet
from userdata_api.utils.user import get_user_info as get
from userdata_api.utils.user import get_users_info_batch as get_users
from userdata_api.utils.user import patch_user_info as patch


Expand All @@ -14,7 +17,8 @@

@user.get("/{id}", response_model=UserInfoGet)
async def get_user_info(
id: int, user: dict[str, Any] = Depends(UnionAuth(scopes=[], allow_none=False, auto_error=True))
id: int,
user: dict[str, Any] = Depends(UnionAuth(scopes=[], allow_none=False, auto_error=True)),
) -> UserInfoGet:
"""
Получить информацию о пользователе
Expand Down Expand Up @@ -63,4 +67,19 @@ async def update_user(
:return:
"""
await patch(new_info, id, user)
return StatusResponseModel(status='Success', message='User patch succeeded', ru="Изменение успешно")
return StatusResponseModel(status="Success", message="User patch succeeded", ru="Изменение успешно")


@user.get("", response_model=UsersInfoGet, response_model_exclude_unset=True)
async def get_users_info(
users: list[int] = Query(),
categories: list[int] = Query(),
user: dict[str, Any] = Depends(UnionAuth(scopes=["userdata.info.admin"], allow_none=False, auto_error=True)),
) -> UsersInfoGet:
"""
Получить информацию о пользователях.
:param users: список id юзеров, про которых нужно вернуть информацию
:param categories: список id категорий, параметры которых нужно вернуть
:return: список данных о пользователях и данных категориях
"""
return UsersInfoGet.model_validate(await get_users(users, categories, user))
8 changes: 8 additions & 0 deletions userdata_api/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class UserInfo(Base):
value: str | None = None


class ExtendedUserInfo(UserInfo):
user_id: int


class UserInfoGet(Base):
items: list[UserInfo]

Expand All @@ -24,5 +28,9 @@ def unique_validator(cls, v):
return v


class UsersInfoGet(Base):
items: list[ExtendedUserInfo]


class UserInfoUpdate(UserInfoGet):
source: constr(min_length=1)
Loading
Loading