76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
import uuid
|
|
from datetime import datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from app.api.v1 import studies as studies_api
|
|
from app.models.study import Study
|
|
from app.models.study_member import StudyMember
|
|
from app.models.user import User, UserStatus
|
|
|
|
|
|
def _make_user(*, is_admin: bool = False) -> User:
|
|
return User(
|
|
id=uuid.uuid4(),
|
|
email="cra-pm@test.com",
|
|
password_hash="hashed",
|
|
full_name="CRA PM",
|
|
clinical_department="Clinical",
|
|
is_admin=is_admin,
|
|
status=UserStatus.ACTIVE,
|
|
)
|
|
|
|
|
|
def _make_study() -> Study:
|
|
return Study(
|
|
id=uuid.uuid4(),
|
|
code="STUDY-001",
|
|
name="测试项目",
|
|
status="ACTIVE",
|
|
is_locked=False,
|
|
visit_schedule=[],
|
|
active_roles=[],
|
|
created_at=datetime.now(timezone.utc),
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_studies_returns_project_role_for_non_admin(monkeypatch):
|
|
current_user = _make_user()
|
|
study = _make_study()
|
|
member = StudyMember(
|
|
id=uuid.uuid4(),
|
|
study_id=study.id,
|
|
user_id=current_user.id,
|
|
role_in_study="PM",
|
|
is_active=True,
|
|
)
|
|
|
|
async def fake_list_studies_for_user(_db, _user_id, skip=0, limit=100):
|
|
return [study]
|
|
|
|
async def fake_get_member(_db, _study_id, _user_id):
|
|
return member
|
|
|
|
monkeypatch.setattr(studies_api.study_crud, "list_studies_for_user", fake_list_studies_for_user)
|
|
monkeypatch.setattr(studies_api.member_crud, "get_member", fake_get_member)
|
|
|
|
result = await studies_api.list_studies(db=object(), current_user=current_user)
|
|
|
|
assert result["items"][0].role_in_study == "PM"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_studies_does_not_map_system_admin_to_project_admin(monkeypatch):
|
|
current_user = _make_user(is_admin=True)
|
|
study = _make_study()
|
|
|
|
async def fake_list_studies(_db, skip=0, limit=100):
|
|
return [study]
|
|
|
|
monkeypatch.setattr(studies_api.study_crud, "list_studies", fake_list_studies)
|
|
|
|
result = await studies_api.list_studies(db=object(), current_user=current_user)
|
|
|
|
assert result["items"][0].role_in_study is None
|