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
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.merge import MR_REF_PREFIX, MergeRequest
from pygitweb.auth_config import AuthConfig, LocalUser
from pygitweb.config import settings
from pygitweb.conftest import client_as_user
from pygitweb.merge_requests import merge_router
from pygitweb.permissions import Permission, PermissionsMap, permission_key
def _commit_chain(repo: pygit2.Repository) -> tuple[pygit2.Oid, pygit2.Oid]:
sig = pygit2.Signature("tester", "tester@example.com")
tb = repo.TreeBuilder()
tree = tb.write()
a = repo.create_commit(None, sig, sig, "a", tree, [])
repo.create_reference("refs/heads/main", a)
b = repo.create_commit(None, sig, sig, "b", tree, [a])
return a, b
_MR_AUTH = AuthConfig(
auth_mode="local",
local_users=[LocalUser(user="creator", password="secret"), LocalUser(user="merger", password="secret")],
oauth_permissions=PermissionsMap.model_validate({
"*": [],
permission_key(Permission.MR_CREATE, "demo"): ["creator"],
permission_key(Permission.MR_MERGE, "demo"): ["merger"],
}),
)
def _build_client() -> TestClient:
app = FastAPI()
app.include_router(merge_router, prefix="/mr")
return TestClient(app)
@pytest.fixture
def mr_env(tmp_path: Path) -> Generator[dict[str, str], None, None]:
root = tmp_path / "mr-perms"
root.mkdir()
repo_dir = root / "demo"
repo_dir.mkdir()
repo = pygit2.init_repository(str(repo_dir), bare=False)
_a, b = _commit_chain(repo)
mr = MergeRequest(
b,
repo.default_signature,
ours="refs/heads/main",
title="Feature MR",
name=f"{MR_REF_PREFIX}aaaabbbbccccdddd",
)
mr_oid = mr.write(repo)
projects_list = root / "projects.list"
projects_list.write_text("demo tester\n", encoding="utf-8")
with (
patch.object(settings, "PROJECTROOT", str(root)),
patch.object(settings, "PROJECTS_LIST", str(projects_list)),
patch.object(settings, "STRICT_EXPORT", False),
patch.object(settings, "EXPORT_OK", ""),
patch("pygitweb.auth_config.auth_config", _MR_AUTH),
patch("pygitweb.auth.auth_config", _MR_AUTH),
):
yield {
"client": _build_client(),
"project": "demo",
"mr_tag_oid": str(mr_oid),
"tip_b": str(b),
}
def test_mr_ff_open_when_auth_disabled(mr_env: dict[str, str]) -> None:
client = mr_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", False):
r = client.post(
"/mr/ff",
params={"project": mr_env["project"], "h": mr_env["mr_tag_oid"]},
follow_redirects=False,
)
assert r.status_code == 303
def test_mr_create_401_without_login(mr_env: dict[str, str]) -> None:
client = mr_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True):
r = client.post(
"/mr/create",
data={
"project": mr_env["project"],
"theirs": "refs/heads/main",
"ours": "refs/heads/main",
"title": "MR",
},
follow_redirects=False,
)
assert r.status_code == 401
def test_mr_create_forbidden_without_create_grant(mr_env: dict[str, str]) -> None:
client = mr_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "merger"):
r = client.post(
"/mr/create",
data={
"project": mr_env["project"],
"theirs": "refs/heads/main",
"ours": "refs/heads/main",
"title": "MR",
},
follow_redirects=False,
)
assert r.status_code == 403
def test_mr_ff_forbidden_without_merge_grant(mr_env: dict[str, str]) -> None:
client = mr_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "creator"):
r = client.post(
"/mr/ff",
params={"project": mr_env["project"], "h": mr_env["mr_tag_oid"]},
follow_redirects=False,
)
assert r.status_code == 403
def test_mr_ff_allowed_with_merge_grant(mr_env: dict[str, str]) -> None:
client = mr_env["client"]
assert isinstance(client, TestClient)
with patch.object(settings, "AUTH", True), client_as_user(client, "merger"):
r = client.post(
"/mr/ff",
params={"project": mr_env["project"], "h": mr_env["mr_tag_oid"]},
follow_redirects=False,
)
assert r.status_code == 303