Project: pygitweb

e6c6972 from __future__ import annotations
from collections.abc import Generator
from pathlib import Path
from unittest.mock import patch
import pygit2
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pygittools.tasks import BOARD_REF_PREFIX, Board
1168630
e6c6972 from pygitweb.auth_config import AuthConfig, LocalUser
from pygitweb.config import settings
from pygitweb.conftest import client_as_user
fbd311b from pygitweb.permissions import Permission, permission_key, permissions_with_defaults
e6c6972 from pygitweb.tasks import EMPTY_TREE_OID, task_router
_TASKS_AUTH = AuthConfig(
73c9c30 auth_mode="local",
local_users=[LocalUser(user="admin", password="secret"), LocalUser(user="viewer", password="secret")],
oauth_permissions=permissions_with_defaults({
permission_key(Permission.TASKS, "demo"): ["admin"],
}),
e6c6972 )
def _build_client() -> TestClient:
73c9c30 app = FastAPI()
app.include_router(task_router, prefix="/tasks")
return TestClient(app)
e6c6972
def _create_initial_commit(repo: pygit2.Repository, repo_dir: Path) -> None:
73c9c30 (repo_dir / "README.md").write_text("seed\n", encoding="utf-8")
index = repo.index
index.add("README.md")
index.write()
tree = index.write_tree()
sig = pygit2.Signature("tester", "tester@example.com")
repo.create_commit("HEAD", sig, sig, "initial", tree, [])
e6c6972
@pytest.fixture
def task_env(tmp_path: Path) -> Generator[dict[str, str | TestClient], None, None]:
73c9c30 repo_dir = tmp_path / "demo"
repo = pygit2.init_repository(str(repo_dir), bare=False)
_create_initial_commit(repo, repo_dir)
board = Board(EMPTY_TREE_OID, f"{BOARD_REF_PREFIX}Tasks", tagger="", description="")
board.write(repo)
with (
patch.object(settings, "PROJECTROOT", str(tmp_path)),
patch.object(settings, "PROJECTS_LIST", str(tmp_path)),
patch.object(settings, "STRICT_EXPORT", False),
patch.object(settings, "EXPORT_OK", ""),
patch.object(settings, "LIST_ALL", True),
patch("pygitweb.auth_config.auth_config", _TASKS_AUTH),
patch("pygitweb.auth.auth_config", _TASKS_AUTH),
):
yield {"client": _build_client(), "project": "demo", "board": "Tasks"}
e6c6972
def test_task_post_open_when_auth_disabled(task_env: dict[str, str | TestClient]) -> None:
73c9c30 client = task_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", False):
r = client.post(
"/tasks/create",
ddcd285 json={"project": task_env["project"], "board": task_env["board"], "title": "New task"},
73c9c30 )
assert r.status_code == 200
e6c6972
def test_task_create_401_without_login(task_env: dict[str, str | TestClient]) -> None:
73c9c30 client = task_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True):
r = client.post(
"/tasks/create",
ddcd285 json={"project": task_env["project"], "board": task_env["board"], "title": "New task"},
73c9c30 )
assert r.status_code == 401
e6c6972
def test_task_create_allowed_with_grant(task_env: dict[str, str | TestClient]) -> None:
73c9c30 client = task_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "admin"):
r = client.post(
"/tasks/create",
ddcd285 json={"project": task_env["project"], "board": task_env["board"], "title": "Granted task"},
73c9c30 )
assert r.status_code == 200
e6c6972
def test_task_create_forbidden_without_grant(task_env: dict[str, str | TestClient]) -> None:
73c9c30 client = task_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "viewer"):
r = client.post(
"/tasks/create",
ddcd285 json={"project": task_env["project"], "board": task_env["board"], "title": "Denied task"},
73c9c30 )
assert r.status_code == 403
e6c6972
def test_task_update_forbidden_without_grant(task_env: dict[str, str | TestClient]) -> None:
73c9c30 client = task_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "viewer"):
r = client.post(
"/tasks/update",
ddcd285 json={
73c9c30 "project": task_env["project"],
"board": task_env["board"],
"task": "refs/tags/tasks/task_missing",
ddcd285 "title": "nope",
73c9c30 },
)
assert r.status_code == 403