Project: pygitweb
from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
import pygit2
from pygitweb.config import settings
from pygitweb.git_helpers import (
clear_config_cache,
git_get_type,
resolve_mr_opener,
signature_from_principal_email,
)
from pygitweb.permissions import PermissionPrincipal
class TestProjectWrappers:
def test_git_get_type_via_project_name(self, tmp_path: Path) -> None:
root = tmp_path / "root"
project = root / "demo"
project.mkdir(parents=True)
repo = pygit2.init_repository(str(project), bare=False)
tb = repo.TreeBuilder()
tree = tb.write()
sig = pygit2.Signature("t", "t@example.com")
repo.create_commit("HEAD", sig, sig, "init", tree, [])
clear_config_cache()
with patch.object(settings, "PROJECTROOT", str(root)):
assert git_get_type("demo", "HEAD") == "commit"
clear_config_cache()
class TestResolveMrOpener:
def test_prefers_principal_email(self, tmp_path: Path) -> None:
repo = pygit2.init_repository(str(tmp_path / "bare"), bare=True)
principal = PermissionPrincipal(username="alice", email="alice@idp.example")
opener, warning = resolve_mr_opener(repo, principal)
assert warning is None
assert opener.email == "alice@idp.example"
assert opener.name == "alice"
def test_falls_back_to_repo_git_user(self, tmp_path: Path) -> None:
repo_dir = tmp_path / "configured"
pygit2.init_repository(str(repo_dir), bare=True)
config_path = repo_dir / "config"
config_path.write_text(
f"{config_path.read_text(encoding='utf-8')}\n[user]\n\tname = Repo Bot\n\temail = bot@example.com\n",
encoding="utf-8",
)
repo = pygit2.Repository(str(repo_dir))
opener, warning = resolve_mr_opener(repo, None)
assert warning is None
assert opener.email == "bot@example.com"
def test_falls_back_to_user_credentials(self, tmp_path: Path) -> None:
repo = pygit2.init_repository(str(tmp_path / "bare"), bare=True)
principal = PermissionPrincipal(username="bob", email=None)
opener, warning = resolve_mr_opener(repo, principal)
assert warning is None
assert opener.name == "bob"
assert opener.email == "bob@local"
def test_uses_default_with_warning(self, tmp_path: Path) -> None:
repo = pygit2.init_repository(str(tmp_path / "bare"), bare=True)
opener, warning = resolve_mr_opener(repo, None)
assert warning is not None
assert opener.email == "noreply@pygitweb.local"
def test_signature_from_principal_email_requires_email(self) -> None:
assert signature_from_principal_email(PermissionPrincipal(username="x", email=None)) is None
assert signature_from_principal_email(PermissionPrincipal(username="x", email=" ")) is None