test: add Forgejo saas wrapper unit tests
This commit is contained in:
parent
e47de33caf
commit
8372f92d29
1 changed files with 133 additions and 0 deletions
133
tests/unit/byteb4rb1e/utils/saas/test_forgejo.py
Normal file
133
tests/unit/byteb4rb1e/utils/saas/test_forgejo.py
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
"""Tests for the Forgejo API wrapper."""
|
||||
|
||||
import json
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
import pytest
|
||||
|
||||
from byteb4rb1e.utils.http.client import HttpResponse
|
||||
from byteb4rb1e.utils.saas import forgejo
|
||||
|
||||
HOST = "git.example.com"
|
||||
|
||||
|
||||
class _Recorder:
|
||||
"""Records http_client calls and replays a canned response."""
|
||||
|
||||
def __init__(self, response: HttpResponse) -> None:
|
||||
self.calls: List[Tuple[str, Dict[str, Any]]] = []
|
||||
self._response = response
|
||||
|
||||
def __call__(self, url: str, **kwargs: Any) -> HttpResponse:
|
||||
self.calls.append((url, kwargs))
|
||||
return self._response
|
||||
|
||||
|
||||
class TestApiUrl:
|
||||
|
||||
def test_host_only(self) -> None:
|
||||
assert forgejo.api_url(HOST) == "https://git.example.com/api/v1"
|
||||
|
||||
|
||||
class TestHttpHeaders:
|
||||
|
||||
def test_token_header(self) -> None:
|
||||
headers = forgejo.http_headers("s3cret")
|
||||
assert headers["Authorization"] == "token s3cret"
|
||||
assert headers["Accept"] == "application/json"
|
||||
assert headers["Content-Type"] == "application/json"
|
||||
|
||||
|
||||
class TestRepositoryExists:
|
||||
|
||||
def test_exists(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
recorder = _Recorder(HttpResponse(200, {}, b"{}"))
|
||||
monkeypatch.setattr(forgejo.http_client, "get", recorder)
|
||||
|
||||
assert forgejo.repository_exists(HOST, "tiara", "repo", "t") is True
|
||||
url, kwargs = recorder.calls[0]
|
||||
assert url == "https://git.example.com/api/v1/repos/tiara/repo"
|
||||
assert kwargs["headers"]["Authorization"] == "token t"
|
||||
|
||||
def test_missing(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
recorder = _Recorder(HttpResponse(404, {}, b""))
|
||||
monkeypatch.setattr(forgejo.http_client, "get", recorder)
|
||||
|
||||
assert forgejo.repository_exists(HOST, "tiara", "repo", "t") is False
|
||||
|
||||
|
||||
class TestCreateRepository:
|
||||
|
||||
def _create(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
org: Optional[str] = None,
|
||||
**kwargs: Any,
|
||||
) -> _Recorder:
|
||||
recorder = _Recorder(HttpResponse(201, {}, b"{}"))
|
||||
monkeypatch.setattr(forgejo.http_client, "post", recorder)
|
||||
forgejo.create_repository(HOST, "repo", "t", org=org, **kwargs)
|
||||
return recorder
|
||||
|
||||
def test_user_repo_endpoint(
|
||||
self, monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
recorder = self._create(monkeypatch)
|
||||
url, _ = recorder.calls[0]
|
||||
assert url == "https://git.example.com/api/v1/user/repos"
|
||||
|
||||
def test_org_repo_endpoint(
|
||||
self, monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
recorder = self._create(monkeypatch, org="byteb4rb1e")
|
||||
url, _ = recorder.calls[0]
|
||||
assert url == "https://git.example.com/api/v1/orgs/byteb4rb1e/repos"
|
||||
|
||||
def test_body(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
recorder = self._create(
|
||||
monkeypatch, description="demo", is_private=False,
|
||||
)
|
||||
_, kwargs = recorder.calls[0]
|
||||
body = json.loads(kwargs["data"].decode("utf-8"))
|
||||
assert body == {
|
||||
"name": "repo",
|
||||
"private": False,
|
||||
"description": "demo",
|
||||
}
|
||||
|
||||
def test_defaults_to_private(
|
||||
self, monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
recorder = self._create(monkeypatch)
|
||||
_, kwargs = recorder.calls[0]
|
||||
body = json.loads(kwargs["data"].decode("utf-8"))
|
||||
assert body["private"] is True
|
||||
|
||||
def test_auth_header(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
recorder = self._create(monkeypatch)
|
||||
_, kwargs = recorder.calls[0]
|
||||
assert kwargs["headers"]["Authorization"] == "token t"
|
||||
|
||||
def test_returns_response(
|
||||
self, monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
response = HttpResponse(201, {}, b'{"id": 1}')
|
||||
recorder = _Recorder(response)
|
||||
monkeypatch.setattr(forgejo.http_client, "post", recorder)
|
||||
|
||||
resp = forgejo.create_repository(HOST, "repo", "t")
|
||||
|
||||
assert resp is response
|
||||
|
||||
|
||||
class TestCloneUrls:
|
||||
|
||||
def test_ssh(self) -> None:
|
||||
assert forgejo.ssh_clone_url(HOST, "tiara", "repo") == (
|
||||
"git@git.example.com:tiara/repo.git"
|
||||
)
|
||||
|
||||
def test_https(self) -> None:
|
||||
assert forgejo.https_clone_url(HOST, "tiara", "repo") == (
|
||||
"https://git.example.com/tiara/repo.git"
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue