Initial commit: Role-based data visualisation platform for DMH hospital admissions (Phases 1-3)
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import pytest
|
||||
from typing import Generator
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.database import Base, get_db
|
||||
from app.main import app
|
||||
from app.models.user import User, UserRole
|
||||
from app.core.security import hash_password, create_access_token
|
||||
|
||||
# In-memory SQLite database for fast isolated unit tests
|
||||
TEST_DATABASE_URL = "sqlite:///:memory:"
|
||||
|
||||
test_engine = create_engine(
|
||||
TEST_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=test_engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def setup_test_db():
|
||||
Base.metadata.create_all(bind=test_engine)
|
||||
yield
|
||||
Base.metadata.drop_all(bind=test_engine)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_session() -> Generator:
|
||||
connection = test_engine.connect()
|
||||
transaction = connection.begin()
|
||||
session = TestingSessionLocal(bind=connection)
|
||||
|
||||
yield session
|
||||
|
||||
session.close()
|
||||
transaction.rollback()
|
||||
connection.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(db_session) -> Generator:
|
||||
def override_get_db():
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
pass
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
with TestClient(app) as test_client:
|
||||
yield test_client
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_user(db_session) -> User:
|
||||
user = db_session.query(User).filter(User.username == "test_admin").first()
|
||||
if not user:
|
||||
user = User(
|
||||
username="test_admin",
|
||||
email="admin@test.com",
|
||||
hashed_password=hash_password("AdminPass123!"),
|
||||
full_name="Test Administrator",
|
||||
role=UserRole.SUPER_ADMIN.value,
|
||||
is_active=True
|
||||
)
|
||||
db_session.add(user)
|
||||
db_session.commit()
|
||||
db_session.refresh(user)
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def viewer_user(db_session) -> User:
|
||||
user = db_session.query(User).filter(User.username == "test_viewer").first()
|
||||
if not user:
|
||||
user = User(
|
||||
username="test_viewer",
|
||||
email="viewer@test.com",
|
||||
hashed_password=hash_password("ViewerPass123!"),
|
||||
full_name="Test Regular Viewer",
|
||||
role=UserRole.VIEWER.value,
|
||||
is_active=True
|
||||
)
|
||||
db_session.add(user)
|
||||
db_session.commit()
|
||||
db_session.refresh(user)
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_token(admin_user) -> str:
|
||||
return create_access_token(data={"sub": str(admin_user.id), "username": admin_user.username, "role": admin_user.role})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def viewer_token(viewer_user) -> str:
|
||||
return create_access_token(data={"sub": str(viewer_user.id), "username": viewer_user.username, "role": viewer_user.role})
|
||||
@@ -0,0 +1,53 @@
|
||||
def test_login_success(client, admin_user):
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={"username_or_email": "test_admin", "password": "AdminPass123!"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "access_token" in data
|
||||
assert data["token_type"] == "Bearer"
|
||||
assert data["user"]["username"] == "test_admin"
|
||||
assert data["user"]["role"] == "super_admin"
|
||||
|
||||
|
||||
def test_login_invalid_password(client, admin_user):
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={"username_or_email": "test_admin", "password": "WrongPassword!"}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert "Incorrect username or password" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_login_nonexistent_user(client):
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={"username_or_email": "nonexistent_user", "password": "any_password"}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_get_current_user_me(client, admin_token):
|
||||
response = client.get(
|
||||
"/api/v1/auth/me",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["username"] == "test_admin"
|
||||
assert data["email"] == "admin@test.com"
|
||||
|
||||
|
||||
def test_get_current_user_unauthorized(client):
|
||||
response = client.get("/api/v1/auth/me")
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_logout(client, admin_token):
|
||||
response = client.post(
|
||||
"/api/v1/auth/logout",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["message"] == "Successfully logged out"
|
||||
@@ -0,0 +1,154 @@
|
||||
import io
|
||||
import pytest
|
||||
|
||||
|
||||
SAMPLE_CSV = """srNo,mrdNumber,Gender,Age,PatientCategoryName,District,State,Country,Nationality,AdmissionDate,DischargeDate,bedNumber,BedLocation,BuildingName,AdmittingDoctorName,AdmittingDoctorSpecialityName,CorporateCompanyName,PolicyName,AdmissionType
|
||||
1,1001,Male,45,GNL,Pune,Maharashtra,India,Indian,01-01-2025 10:00,03-01-2025 12:00,101,Wing A,DMHRC,Dr. Jane Doe,MEDICINE,,,Emergency
|
||||
2,1002,Female,32,TPA,Mumbai,Maharashtra,India,Indian,02-01-2025 11:30,05-01-2025 16:45,202,Wing B,DMHSS,Dr. John Smith,CARDIOLOGY,Care Health,TPA,Walk-in
|
||||
3,1003,Male,60,TPA,Pune,Maharashtra,India,Indian,03-01-2025 09:15,04-01-2025 14:00,303,Wing C,DMHRC,Dr. Alice Brown,MEDICINE,Star Health,TPA,Emergency
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admission_dataset(client, admin_token):
|
||||
csv_file = io.BytesIO(SAMPLE_CSV.encode("utf-8"))
|
||||
upload_res = client.post(
|
||||
"/api/v1/datasets/upload",
|
||||
data={"name": "Admissions Query Test", "description": "For query tests"},
|
||||
files={"file": ("query_test.csv", csv_file, "text/csv")},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert upload_res.status_code == 201
|
||||
ds = upload_res.json()["dataset"]
|
||||
|
||||
# Configure role permission for viewer with row rule: BuildingName == 'DMHRC'
|
||||
client.post(
|
||||
"/api/v1/permissions",
|
||||
json={
|
||||
"dataset_id": ds["id"],
|
||||
"role": "viewer",
|
||||
"can_view": True,
|
||||
"hidden_column_ids": [],
|
||||
"row_rules": [
|
||||
{
|
||||
"column_name": "BuildingName",
|
||||
"operator": "eq",
|
||||
"rule_value": "DMHRC"
|
||||
}
|
||||
]
|
||||
},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
return ds
|
||||
|
||||
|
||||
def test_admin_queries_chart_successfully(client, admin_token, admission_dataset):
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "bar",
|
||||
"dimension": "AdmittingDoctorSpecialityName",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert res["chart_type"] == "bar"
|
||||
assert "MEDICINE" in res["categories"]
|
||||
assert "CARDIOLOGY" in res["categories"]
|
||||
assert res["total_records"] == 3
|
||||
|
||||
|
||||
def test_viewer_row_rules_enforced(client, viewer_token, admission_dataset):
|
||||
# Viewer is restricted to DMHRC only (rows 1 and 3, total 2 rows)
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "bar",
|
||||
"dimension": "BuildingName",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert res["categories"] == ["DMHRC"]
|
||||
assert res["total_records"] == 2
|
||||
assert "DMHSS" not in res["categories"]
|
||||
|
||||
|
||||
def test_querying_restricted_sensitive_column_fails(client, viewer_token, admission_dataset):
|
||||
# mrdNumber is sensitive by default, viewer cannot query it
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "bar",
|
||||
"dimension": "mrdNumber",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert "restricted" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_kpi_query(client, admin_token, admission_dataset):
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "kpi",
|
||||
"dimension": "srNo",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert res["kpi_value"] == 3
|
||||
|
||||
|
||||
def test_stacked_bar_query(client, admin_token, admission_dataset):
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "stacked_bar",
|
||||
"dimension": "PatientCategoryName",
|
||||
"secondary_dimension": "Gender",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert len(res["series"]) >= 2
|
||||
series_names = [s["name"] for s in res["series"]]
|
||||
assert "Male" in series_names or "Female" in series_names
|
||||
|
||||
|
||||
def test_filter_options_endpoint(client, viewer_token, admission_dataset):
|
||||
response = client.get(
|
||||
f"/api/v1/charts/filter-options/{admission_dataset['id']}",
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
opts = response.json()["options"]
|
||||
# Viewer row rule restricts to DMHRC
|
||||
buildings = [b["value"] for b in opts.get("BuildingName", [])]
|
||||
assert "DMHRC" in buildings
|
||||
assert "DMHSS" not in buildings
|
||||
@@ -0,0 +1,78 @@
|
||||
import io
|
||||
|
||||
|
||||
SAMPLE_CSV_DATA = """srNo,mrdNumber,Gender,Age,PatientCategoryName,District,State,Country,Nationality,AdmissionDate,DischargeDate,bedNumber,BedLocation,BuildingName,AdmittingDoctorName,AdmittingDoctorSpecialityName,CorporateCompanyName,PolicyName,AdmissionType
|
||||
1,1001,Male,45,GNL,Pune,Maharashtra,India,Indian,01-01-2025 10:00,03-01-2025 12:00,101,Wing A,DMHRC,Dr. Jane Doe,CARDIOLOGY,,,Emergency
|
||||
2,1002,Female,32,TPA,Mumbai,Maharashtra,India,Indian,02-01-2025 11:30,05-01-2025 16:45,202,Wing B,DMHSS,Dr. John Smith,NEUROLOGY,Care Health,TPA,Walk-in
|
||||
"""
|
||||
|
||||
|
||||
def test_viewer_cannot_upload_dataset(client, viewer_token):
|
||||
csv_file = io.BytesIO(SAMPLE_CSV_DATA.encode("utf-8"))
|
||||
response = client.post(
|
||||
"/api/v1/datasets/upload",
|
||||
data={"name": "Test Sample Dataset", "description": "Test only"},
|
||||
files={"file": ("sample.csv", csv_file, "text/csv")},
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_admin_uploads_dataset_successfully(client, admin_token):
|
||||
csv_file = io.BytesIO(SAMPLE_CSV_DATA.encode("utf-8"))
|
||||
response = client.post(
|
||||
"/api/v1/datasets/upload",
|
||||
data={"name": "Hospital Admissions Test", "description": "Test admissions"},
|
||||
files={"file": ("sample.csv", csv_file, "text/csv")},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 201
|
||||
result = response.json()
|
||||
assert "registered successfully" in result["message"]
|
||||
dataset = result["dataset"]
|
||||
assert dataset["row_count"] == 2
|
||||
assert dataset["column_count"] == 19
|
||||
|
||||
# Check column metadata
|
||||
columns = dataset["columns"]
|
||||
assert len(columns) == 19
|
||||
|
||||
# Check sensitive flags
|
||||
col_map = {c["column_name"]: c for c in columns}
|
||||
assert col_map["mrdNumber"]["is_sensitive"] is True
|
||||
assert col_map["mrdNumber"]["is_identifier"] is True
|
||||
assert col_map["AdmittingDoctorName"]["is_sensitive"] is True
|
||||
assert col_map["Gender"]["is_sensitive"] is False
|
||||
|
||||
|
||||
def test_admin_updates_column_metadata(client, admin_token):
|
||||
# First upload a dataset for this test
|
||||
csv_file = io.BytesIO(SAMPLE_CSV_DATA.encode("utf-8"))
|
||||
upload_res = client.post(
|
||||
"/api/v1/datasets/upload",
|
||||
data={"name": "Hospital Admissions Test 2", "description": "Test admissions 2"},
|
||||
files={"file": ("sample2.csv", csv_file, "text/csv")},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert upload_res.status_code == 201
|
||||
ds_id = upload_res.json()["dataset"]["id"]
|
||||
|
||||
# Fetch full dataset with columns
|
||||
detail_res = client.get(
|
||||
f"/api/v1/datasets/{ds_id}",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert detail_res.status_code == 200
|
||||
columns = detail_res.json()["columns"]
|
||||
gender_col = next(c for c in columns if c["column_name"] == "Gender")
|
||||
|
||||
# Update Gender column to sensitive
|
||||
patch_res = client.patch(
|
||||
f"/api/v1/datasets/{ds_id}/columns/{gender_col['id']}",
|
||||
json={"display_name": "Patient Gender", "is_sensitive": True},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert patch_res.status_code == 200
|
||||
updated_col = patch_res.json()
|
||||
assert updated_col["display_name"] == "Patient Gender"
|
||||
assert updated_col["is_sensitive"] is True
|
||||
@@ -0,0 +1,119 @@
|
||||
import pytest
|
||||
from app.models.dataset import Dataset, DatasetColumn
|
||||
from app.models.user import UserRole
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_dataset(db_session):
|
||||
ds = db_session.query(Dataset).filter(Dataset.name == "Perm Test Dataset").first()
|
||||
if not ds:
|
||||
ds = Dataset(
|
||||
name="Perm Test Dataset",
|
||||
source_type="file",
|
||||
table_name="ds_perm_test",
|
||||
row_count=100,
|
||||
column_count=3
|
||||
)
|
||||
db_session.add(ds)
|
||||
db_session.flush()
|
||||
|
||||
c1 = DatasetColumn(dataset_id=ds.id, column_name="id", display_name="ID", data_type="integer", is_sensitive=False, is_identifier=True)
|
||||
c2 = DatasetColumn(dataset_id=ds.id, column_name="doctor", display_name="Doctor", data_type="string", is_sensitive=True, is_identifier=False)
|
||||
c3 = DatasetColumn(dataset_id=ds.id, column_name="department", display_name="Department", data_type="string", is_sensitive=False, is_identifier=False)
|
||||
db_session.add_all([c1, c2, c3])
|
||||
db_session.commit()
|
||||
db_session.refresh(ds)
|
||||
return ds
|
||||
|
||||
|
||||
def test_viewer_cannot_set_permissions(client, viewer_token, sample_dataset):
|
||||
payload = {
|
||||
"dataset_id": sample_dataset.id,
|
||||
"role": "viewer",
|
||||
"can_view": True,
|
||||
"hidden_column_ids": [],
|
||||
"row_rules": []
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/permissions",
|
||||
json=payload,
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_admin_sets_permissions(client, admin_token, sample_dataset):
|
||||
payload = {
|
||||
"dataset_id": sample_dataset.id,
|
||||
"role": "viewer",
|
||||
"can_view": True,
|
||||
"can_export": False,
|
||||
"hidden_column_ids": [sample_dataset.columns[2].id], # hide department
|
||||
"row_rules": [
|
||||
{
|
||||
"column_name": "department",
|
||||
"operator": "eq",
|
||||
"rule_value": "CARDIOLOGY"
|
||||
}
|
||||
]
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/permissions",
|
||||
json=payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["can_view"] is True
|
||||
assert len(data["column_permissions"]) == 1
|
||||
assert len(data["row_rules"]) == 1
|
||||
assert data["row_rules"][0]["rule_value"] == "CARDIOLOGY"
|
||||
|
||||
|
||||
def test_get_effective_permissions_super_admin(client, admin_token, sample_dataset):
|
||||
response = client.get(
|
||||
f"/api/v1/permissions/effective/{sample_dataset.id}",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
eff = response.json()
|
||||
assert eff["can_view"] is True
|
||||
assert eff["can_export"] is True
|
||||
# Admin has all columns allowed, none hidden
|
||||
assert "doctor" in eff["allowed_columns"]
|
||||
assert len(eff["row_rules"]) == 0
|
||||
|
||||
|
||||
def test_get_effective_permissions_with_preview_role(client, admin_token, sample_dataset):
|
||||
# Set permission for viewer role first
|
||||
client.post(
|
||||
"/api/v1/permissions",
|
||||
json={
|
||||
"dataset_id": sample_dataset.id,
|
||||
"role": "viewer",
|
||||
"can_view": True,
|
||||
"can_export": False,
|
||||
"hidden_column_ids": [sample_dataset.columns[2].id],
|
||||
"row_rules": [
|
||||
{
|
||||
"column_name": "department",
|
||||
"operator": "eq",
|
||||
"rule_value": "CARDIOLOGY"
|
||||
}
|
||||
]
|
||||
},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
|
||||
# Preview as viewer role
|
||||
response = client.get(
|
||||
f"/api/v1/permissions/effective/{sample_dataset.id}?preview_role=viewer",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
eff = response.json()
|
||||
# Sensitive column doctor is hidden for non-admin
|
||||
assert "doctor" in eff["hidden_columns"]
|
||||
assert "department" in eff["hidden_columns"]
|
||||
assert len(eff["row_rules"]) == 1
|
||||
assert eff["row_rules"][0]["rule_value"] == "CARDIOLOGY"
|
||||
@@ -0,0 +1,83 @@
|
||||
def test_admin_can_list_users(client, admin_token):
|
||||
response = client.get(
|
||||
"/api/v1/users",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
users = response.json()
|
||||
assert isinstance(users, list)
|
||||
assert len(users) >= 1
|
||||
|
||||
|
||||
def test_viewer_cannot_list_users(client, viewer_token):
|
||||
response = client.get(
|
||||
"/api/v1/users",
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert "Super admin privileges required" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_admin_creates_user(client, admin_token):
|
||||
new_user_payload = {
|
||||
"username": "new_clinical_user",
|
||||
"email": "clinical@dmh.org",
|
||||
"password": "Password123!",
|
||||
"full_name": "Dr. Sarah",
|
||||
"role": "viewer",
|
||||
"is_active": True
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/users",
|
||||
json=new_user_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 201
|
||||
created = response.json()
|
||||
assert created["username"] == "new_clinical_user"
|
||||
assert created["email"] == "clinical@dmh.org"
|
||||
assert created["role"] == "viewer"
|
||||
|
||||
|
||||
def test_cannot_create_duplicate_username(client, admin_token, admin_user):
|
||||
payload = {
|
||||
"username": admin_user.username,
|
||||
"email": "another_email@dmh.org",
|
||||
"password": "Password123!",
|
||||
"role": "viewer"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/users",
|
||||
json=payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 409
|
||||
|
||||
|
||||
def test_admin_updates_user(client, admin_token, viewer_user):
|
||||
update_payload = {
|
||||
"full_name": "Updated Name",
|
||||
"role": "department_manager"
|
||||
}
|
||||
response = client.put(
|
||||
f"/api/v1/users/{viewer_user.id}",
|
||||
json=update_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
updated = response.json()
|
||||
assert updated["full_name"] == "Updated Name"
|
||||
assert updated["role"] == "department_manager"
|
||||
|
||||
|
||||
def test_list_roles(client, viewer_token):
|
||||
response = client.get(
|
||||
"/api/v1/roles",
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
roles = response.json()
|
||||
assert len(roles) >= 4
|
||||
role_names = [r["role"] for r in roles]
|
||||
assert "super_admin" in role_names
|
||||
assert "viewer" in role_names
|
||||
Reference in New Issue
Block a user