bmc_hub/tests/test_workflow_page_access.py

47 lines
2.0 KiB
Python
Raw Permalink Normal View History

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from fastapi.staticfiles import StaticFiles
from app.core.auth_dependencies import get_current_user
from app.core.auth_service import AuthService
from app.workflows import views, decisions
from app.tags.backend import views as tag_views
def client(monkeypatch, permissions=(), admin=False):
app = FastAPI()
app.mount('/static', StaticFiles(directory='static'), name='static')
app.include_router(views.router)
app.include_router(decisions.views)
app.include_router(tag_views.router)
user = {'id': 1, 'username': 'test', 'permissions': list(permissions), 'is_superadmin': admin}
app.dependency_overrides[get_current_user] = lambda: user
monkeypatch.setattr(AuthService, 'user_has_permission', lambda _, code: code in permissions)
return TestClient(app)
@pytest.mark.parametrize('path', ['/workflows', '/workflows/resources', '/workflows/operations', '/workflows/time/1', '/tags/classification'])
def test_pages_reject_users_without_group_access(monkeypatch, path):
assert client(monkeypatch).get(path).status_code == 403
@pytest.mark.parametrize('permissions,path', [
(['workflows.view'], '/workflows'),
(['workflows.view'], '/workflows/operations'),
(['workflows.view', 'workflows.manage'], '/workflows/resources'),
(['workflows.decisions.view'], '/workflows/time/1'),
(['system.admin'], '/tags/classification'),
])
def test_pages_render_for_matching_group_access(monkeypatch, permissions, path):
assert client(monkeypatch, permissions).get(path).status_code == 200
def test_reader_cannot_open_resource_editor(monkeypatch):
assert client(monkeypatch, ['workflows.view']).get('/workflows/resources').status_code == 403
def test_superadmin_can_open_all_pages(monkeypatch):
browser = client(monkeypatch, admin=True)
for path in ['/workflows', '/workflows/resources', '/workflows/operations', '/workflows/time/1', '/tags/classification']:
assert browser.get(path).status_code == 200