1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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, TASK_REF_PREFIX, Board, Comment, Task
from pygitweb.auth_config import AuthConfig, LocalUser
from pygitweb.config import settings
from pygitweb.conftest import client_as_user
from pygitweb.permissions import Permission, permission_key, permissions_with_defaults
from pygitweb.tasks import EMPTY_TREE_OID, comment_router
_COMMENTS_AUTH = AuthConfig(
auth_mode="local",
local_users=[
LocalUser(user="admin", password="secret"),
LocalUser(user="viewer", password="secret"),
LocalUser(user="author", password="secret"),
],
oauth_permissions=permissions_with_defaults({
permission_key(Permission.COMMENTS, "demo"): ["viewer"],
permission_key(Permission.TASKS, "demo"): ["admin"],
}),
)
def _build_client() -> TestClient:
app = FastAPI()
app.include_router(comment_router, prefix="/comments")
return TestClient(app)
def _create_initial_commit(repo: pygit2.Repository, repo_dir: Path) -> None:
(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, [])
@pytest.fixture
def comment_env(tmp_path: Path) -> Generator[dict[str, str], None, None]:
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()
task.write(repo)
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),
}
def test_comment_delete_forbidden_without_comments_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, "admin"):
r = client.post(
"/comments/delete",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"comment": comment_env["comment_oid"],
},
)
assert r.status_code == 403
def test_comment_delete_allowed_with_comments_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, "viewer"):
r = client.post(
"/comments/delete",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"comment": comment_env["comment_oid"],
},
)
assert r.status_code == 200
def test_comment_modify_allowed_for_author_without_comments_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/modify",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"comment": comment_env["comment_oid"],
},
json={"content": "edited by author"},
)
assert r.status_code == 200
assert r.json()["content"] == "edited by author"
def test_comment_modify_forbidden_for_non_author_without_comments_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, "admin"):
r = client.post(
"/comments/modify",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"comment": comment_env["comment_oid"],
},
json={"content": "edited by admin"},
)
assert r.status_code == 403
def test_comment_modify_allowed_with_comments_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, "viewer"):
r = client.post(
"/comments/modify",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"comment": comment_env["comment_oid"],
},
json={"content": "edited by moderator"},
)
assert r.status_code == 200
assert r.json()["content"] == "edited by moderator"
def test_comment_create_open_when_auth_disabled(comment_env: dict[str, str]) -> None:
client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", False):
r = client.post(
"/comments/create",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"content": "note",
},
)
assert r.status_code == 200
def test_comment_create_401_without_login(comment_env: dict[str, str]) -> None:
client = comment_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True):
r = client.post(
"/comments/create",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"content": "note",
},
)
assert r.status_code == 401
def test_comment_create_allowed_with_comments_grant_only(comment_env: dict[str, str]) -> None:
"""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",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"content": "from viewer",
},
)
assert r.status_code == 200
def test_comment_create_forbidden_without_comments_grant(comment_env: dict[str, str]) -> None:
"""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",
params={
"project": comment_env["project"],
"board": comment_env["board"],
"task": comment_env["task"],
"content": "from admin",
},
)
assert r.status_code == 403