Project: pygitweb

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