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
c3ea5b9 from pygittools.tasks import BOARD_REF_PREFIX, EMPTY_TREE_OID, TASK_REF_PREFIX, Board, Comment, Task
1168630
e6c6972 from pygitweb.auth_config import AuthConfig, LocalUser
from pygitweb.config import settings
from pygitweb.conftest import client_as_user
0b04b9f from pygitweb.permissions import (
WILDCARD_PERMISSION_KEY,
Permission,
permission_key,
permissions_with_defaults,
)
c3ea5b9 from pygitweb.tasks import comment_router
e6c6972
_COMMENTS_AUTH = AuthConfig(
73c9c30 auth_mode="local",
local_users=[
LocalUser(user="admin", password="secret"),
LocalUser(user="viewer", password="secret"),
LocalUser(user="author", password="secret"),
0b04b9f LocalUser(user="root", password="secret"),
73c9c30 ],
oauth_permissions=permissions_with_defaults({
0b04b9f WILDCARD_PERMISSION_KEY: ["root"],
73c9c30 permission_key(Permission.COMMENTS, "demo"): ["viewer"],
permission_key(Permission.TASKS, "demo"): ["admin"],
}),
e6c6972 )
def _build_client() -> TestClient:
73c9c30 app = FastAPI()
app.include_router(comment_router, prefix="/comments")
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 comment_env(tmp_path: Path) -> Generator[dict[str, str], None, None]:
73c9c30 repo_dir = tmp_path / "demo"
repo = pygit2.init_repository(str(repo_dir), bare=False)
_create_initial_commit(repo, repo_dir)
board_ref = f"{BOARD_REF_PREFIX}Tasks"
board = Board(EMPTY_TREE_OID, board_ref, tagger="", description="")
task_ref = f"{TASK_REF_PREFIX}task_1"
task = Task(EMPTY_TREE_OID, task_ref, tagger="", title="Task", description="")
task_oid = task.write(repo)
comment = Comment(task_oid, "author <author@local>", "seed note")
comment_oid = comment.write(repo)
task.comments = [str(comment_oid)]
task.update_message()
0b04b9f task_oid = task.write(repo)
73c9c30 board.tasks = [str(task_oid)]
board.update_message()
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", _COMMENTS_AUTH),
patch("pygitweb.auth.auth_config", _COMMENTS_AUTH),
):
client = _build_client()
yield {
"client": client,
"project": "demo",
"board": "Tasks",
"task": task_ref,
"comment_oid": str(comment_oid),
}
e6c6972
0b04b9f def _delete_params(comment_env: dict[str, str]) -> dict[str, str]:
return {
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"comment": comment_env["comment_oid"],
}
def _modify_params(comment_env: dict[str, str]) -> dict[str, str]:
return _delete_params(comment_env)
def test_comment_delete_forbidden_for_non_author_without_star_grant(comment_env: dict[str, str]) -> None:
73c9c30 client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "admin"):
0b04b9f r = client.post("/comments/delete", params=_delete_params(comment_env))
73c9c30 assert r.status_code == 403
dc408b2
0b04b9f def test_comment_delete_forbidden_with_comments_grant_for_non_author(comment_env: dict[str, str]) -> None:
"""pgw.comments allows posting, not deleting other users' comments."""
73c9c30 client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "viewer"):
0b04b9f r = client.post("/comments/delete", params=_delete_params(comment_env))
assert r.status_code == 403
def test_comment_delete_allowed_with_star_grant(comment_env: dict[str, str]) -> None:
client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "root"):
r = client.post("/comments/delete", params=_delete_params(comment_env))
assert r.status_code == 200
def test_comment_delete_allowed_for_author_without_star_grant(comment_env: dict[str, str]) -> None:
client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "author"):
r = client.post("/comments/delete", params=_delete_params(comment_env))
73c9c30 assert r.status_code == 200
dc408b2
def test_comment_modify_allowed_for_author_without_comments_grant(comment_env: dict[str, str]) -> None:
73c9c30 client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "author"):
r = client.post(
"/comments/modify",
0b04b9f params=_modify_params(comment_env),
a1cb33a json={"content": "edited by author"},
73c9c30 )
assert r.status_code == 200
assert r.json()["content"] == "edited by author"
dc408b2
def test_comment_modify_forbidden_for_non_author_without_comments_grant(comment_env: dict[str, str]) -> None:
73c9c30 client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "admin"):
r = client.post(
"/comments/modify",
0b04b9f params=_modify_params(comment_env),
a1cb33a json={"content": "edited by admin"},
73c9c30 )
assert r.status_code == 403
dc408b2
0b04b9f def test_comment_modify_forbidden_with_comments_grant_for_non_author(comment_env: dict[str, str]) -> None:
73c9c30 client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "viewer"):
r = client.post(
"/comments/modify",
0b04b9f params=_modify_params(comment_env),
a1cb33a json={"content": "edited by moderator"},
73c9c30 )
0b04b9f assert r.status_code == 403
dc408b2
e6c6972 def test_comment_create_open_when_auth_disabled(comment_env: dict[str, str]) -> None:
73c9c30 client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", False):
r = client.post(
"/comments/create",
a1cb33a params={"project": comment_env["project"], "board": comment_env["board"], "task": comment_env["task"]},
json={"content": "note"},
73c9c30 )
assert r.status_code == 200
e6c6972
def test_comment_create_401_without_login(comment_env: dict[str, str]) -> None:
73c9c30 client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True):
r = client.post(
"/comments/create",
a1cb33a params={"project": comment_env["project"], "board": comment_env["board"], "task": comment_env["task"]},
json={"content": "note"},
73c9c30 )
assert r.status_code == 401
e6c6972
def test_comment_create_allowed_with_comments_grant_only(comment_env: dict[str, str]) -> None:
73c9c30 """User with COMMENTS but not TASKS can add a comment."""
client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "viewer"):
r = client.post(
"/comments/create",
a1cb33a params={"project": comment_env["project"], "board": comment_env["board"], "task": comment_env["task"]},
json={"content": "from viewer"},
73c9c30 )
assert r.status_code == 200
e6c6972
def test_comment_create_forbidden_without_comments_grant(comment_env: dict[str, str]) -> None:
73c9c30 """User with TASKS but not COMMENTS cannot add a comment."""
client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "admin"):
r = client.post(
"/comments/create",
a1cb33a params={"project": comment_env["project"], "board": comment_env["board"], "task": comment_env["task"]},
json={"content": "from admin"},
73c9c30 )
assert r.status_code == 403