init
This commit is contained in:
commit
fe636432d0
154 files changed
+30381
No files matched your search
@@ -0,0 +1,119 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
from app.database import SessionLocal
|
||||
from app.models import Department, Document, DocumentVersion, Group, User, UserDepartmentAccess
|
||||
from app.security import hash_password
|
||||
|
||||
|
||||
def _token(client, username: str, password: str):
|
||||
response = client.post("/api/auth/login", json={"username": username, "password": password})
|
||||
return response.json()["access_token"]
|
||||
|
||||
|
||||
def _headers(client, username: str, password: str):
|
||||
return {"Authorization": f"Bearer {_token(client, username, password)}"}
|
||||
|
||||
|
||||
def _seed_access_fixture() -> tuple[int, int]:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
super_admin = db.query(User).filter(User.username == "admin").first()
|
||||
assert super_admin is not None
|
||||
|
||||
blocked_department = Department(name=f"blocked-dept-{uuid.uuid4().hex[:6]}", description="blocked")
|
||||
db.add(blocked_department)
|
||||
db.commit()
|
||||
db.refresh(blocked_department)
|
||||
|
||||
scoped_user = User(
|
||||
username="scoped-user",
|
||||
full_name="Scoped User",
|
||||
password_hash=hash_password("scoped123"),
|
||||
role="user",
|
||||
is_active=True,
|
||||
primary_department_id=1,
|
||||
)
|
||||
db.add(scoped_user)
|
||||
db.commit()
|
||||
db.refresh(scoped_user)
|
||||
|
||||
allowed_group = Group(name=f"allowed-{uuid.uuid4().hex[:8]}", created_by_id=super_admin.id, department_id=1)
|
||||
blocked_group = Group(
|
||||
name=f"blocked-{uuid.uuid4().hex[:8]}",
|
||||
created_by_id=super_admin.id,
|
||||
department_id=blocked_department.id,
|
||||
)
|
||||
db.add_all([allowed_group, blocked_group])
|
||||
db.commit()
|
||||
db.refresh(allowed_group)
|
||||
db.refresh(blocked_group)
|
||||
|
||||
db.add(UserDepartmentAccess(user_id=scoped_user.id, department_id=1))
|
||||
db.commit()
|
||||
|
||||
allowed_document = Document(group_id=allowed_group.id, title="allowed-doc", created_by_id=super_admin.id, is_deleted=False)
|
||||
blocked_document = Document(group_id=blocked_group.id, title="blocked-doc", created_by_id=super_admin.id, is_deleted=False)
|
||||
db.add_all([allowed_document, blocked_document])
|
||||
db.commit()
|
||||
db.refresh(allowed_document)
|
||||
db.refresh(blocked_document)
|
||||
|
||||
db.add_all(
|
||||
[
|
||||
DocumentVersion(
|
||||
document_id=allowed_document.id,
|
||||
version_no=1,
|
||||
is_current=True,
|
||||
source_filename="allowed.docx",
|
||||
source_ext=".docx",
|
||||
source_path=str(Path("allowed.docx")),
|
||||
source_sha256=uuid.uuid4().hex,
|
||||
file_size=12,
|
||||
uploader_id=super_admin.id,
|
||||
convert_status="success",
|
||||
sync_status="unsynced",
|
||||
),
|
||||
DocumentVersion(
|
||||
document_id=blocked_document.id,
|
||||
version_no=1,
|
||||
is_current=True,
|
||||
source_filename="blocked.docx",
|
||||
source_ext=".docx",
|
||||
source_path=str(Path("blocked.docx")),
|
||||
source_sha256=uuid.uuid4().hex,
|
||||
file_size=12,
|
||||
uploader_id=super_admin.id,
|
||||
convert_status="success",
|
||||
sync_status="unsynced",
|
||||
),
|
||||
]
|
||||
)
|
||||
db.commit()
|
||||
return allowed_group.id, blocked_group.id
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def test_groups_and_files_are_scoped_by_user_access(client):
|
||||
allowed_group_id, blocked_group_id = _seed_access_fixture()
|
||||
headers = _headers(client, "scoped-user", "scoped123")
|
||||
|
||||
groups_response = client.get("/api/groups", headers=headers)
|
||||
assert groups_response.status_code == 200
|
||||
groups = groups_response.json()["data"]
|
||||
assert len(groups) == 1
|
||||
assert groups[0]["id"] == allowed_group_id
|
||||
|
||||
summary_response = client.get("/api/groups/summary", headers=headers)
|
||||
assert summary_response.status_code == 200
|
||||
assert summary_response.json()["knowledge_base_count"] == 1
|
||||
assert summary_response.json()["file_count"] == 1
|
||||
|
||||
allowed_files = client.get(f"/api/groups/{allowed_group_id}/files", headers=headers)
|
||||
blocked_files = client.get(f"/api/groups/{blocked_group_id}/files", headers=headers)
|
||||
assert allowed_files.status_code == 200
|
||||
assert len(allowed_files.json()["data"]) == 1
|
||||
assert blocked_files.status_code == 403
|
||||
Reference in new issue
Block a user