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
from collections.abc import Generator
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from pygitweb.auth_config import AuthConfig, LocalUser
from pygitweb.main import app
from pygitweb.permissions import PERMISSION_ADD_PROJECTS, PermissionsMap
_ADDPROJECTS_AUTH = AuthConfig(
auth_mode="local",
local_users=[LocalUser(user="admin", password="secret"), LocalUser(user="viewer", password="secret")],
oauth_permissions=PermissionsMap.model_validate({
"*": [],
PERMISSION_ADD_PROJECTS: ["admin"],
}),
)
@pytest.fixture
def client() -> Generator[TestClient, None, None]:
with TestClient(app) as c:
yield c
@pytest.fixture
def addprojects_auth() -> Generator[AuthConfig, None, None]:
with (
patch("pygitweb.auth_config.auth_config", _ADDPROJECTS_AUTH),
patch("pygitweb.auth.auth_config", _ADDPROJECTS_AUTH),
patch("pygitweb.main.auth_config", _ADDPROJECTS_AUTH),
):
yield _ADDPROJECTS_AUTH
def _login(client: TestClient, username: str) -> None:
client.post(
"/login",
data={"username": username, "password": "secret", "next": "/"},
follow_redirects=False,
)
def test_projectnamevalid_open_when_auth_disabled(client: TestClient) -> None:
from pygitweb.config import settings
with (
patch.object(settings, "AUTH", False),
patch("pygitweb.main.project_visible_in_list", return_value=False),
):
r = client.get("/projectnamevalid", params={"name": "newproj"})
assert r.status_code == 200
def test_addproject_page_open_when_auth_disabled(client: TestClient) -> None:
from pygitweb.config import settings
with patch.object(settings, "AUTH", False):
r = client.get("/addproject")
assert r.status_code == 200
def test_projectnamevalid_401_without_login(client: TestClient, addprojects_auth: AuthConfig) -> None:
from pygitweb.config import settings
with patch.object(settings, "AUTH", True):
r = client.get("/projectnamevalid", params={"name": "newproj"})
assert r.status_code == 401
def test_projectnamevalid_allowed_with_grant(client: TestClient, addprojects_auth: AuthConfig) -> None:
from pygitweb.config import settings
with (
patch.object(settings, "AUTH", True),
patch("pygitweb.main.project_visible_in_list", return_value=False),
):
_login(client, "admin")
r = client.get("/projectnamevalid", params={"name": "newproj"})
assert r.status_code == 200
def test_projectnamevalid_forbidden_without_grant(client: TestClient, addprojects_auth: AuthConfig) -> None:
from pygitweb.config import settings
with (
patch.object(settings, "AUTH", True),
patch("pygitweb.main.project_visible_in_list", return_value=False),
):
_login(client, "viewer")
r = client.get("/projectnamevalid", params={"name": "newproj"})
assert r.status_code == 403
def test_addproject_page_readable_without_grant(client: TestClient, addprojects_auth: AuthConfig) -> None:
from pygitweb.config import settings
with patch.object(settings, "AUTH", True):
_login(client, "viewer")
r = client.get("/addproject")
assert r.status_code == 200
def test_addproject_post_forbidden_without_grant(client: TestClient, addprojects_auth: AuthConfig) -> None:
from pygitweb.config import settings
with (
patch.object(settings, "AUTH", True),
patch("pygitweb.main.project_visible_in_list", return_value=False),
):
_login(client, "viewer")
r = client.post("/addproject", data={"project_name": "newproj"}, follow_redirects=False)
assert r.status_code == 403