60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from unittest.mock import MagicMock
|
|
|
|
from gitea_backup.client import GiteaClient, inject_token, wiki_clone_url
|
|
|
|
|
|
class UrlHelpersTest(unittest.TestCase):
|
|
def test_inject_token_keeps_port(self) -> None:
|
|
url = inject_token("http://192.168.1.9:3000/alice/app.git", "s3cret")
|
|
self.assertEqual(url, "http://oauth2:s3cret@192.168.1.9:3000/alice/app.git")
|
|
|
|
def test_wiki_url(self) -> None:
|
|
self.assertEqual(
|
|
wiki_clone_url("http://gitea/alice/app.git"),
|
|
"http://gitea/alice/app.wiki.git",
|
|
)
|
|
|
|
|
|
class ListReposTest(unittest.TestCase):
|
|
def test_deduplicates_and_paginates(self) -> None:
|
|
client = GiteaClient.__new__(GiteaClient)
|
|
client.base_url = "http://192.168.1.9:3000"
|
|
client.token = "t"
|
|
calls: list[tuple[str, dict]] = []
|
|
|
|
def fake_get(path: str, params: dict | None = None):
|
|
calls.append((path, dict(params or {})))
|
|
response = MagicMock()
|
|
page = int((params or {}).get("page", 1))
|
|
if path == "/api/v1/repos/search":
|
|
if page == 1:
|
|
response.json.return_value = {
|
|
"data": [
|
|
{"full_name": "alice/app", "clone_url": "http://g/alice/app.git"},
|
|
{"full_name": "bob/lib", "clone_url": "http://g/bob/lib.git"},
|
|
]
|
|
}
|
|
else:
|
|
response.json.return_value = {"data": []}
|
|
elif path == "/api/v1/user/repos":
|
|
response.json.return_value = [
|
|
{"full_name": "alice/app", "clone_url": "http://g/alice/app.git"},
|
|
]
|
|
else:
|
|
response.json.return_value = []
|
|
return response
|
|
|
|
client._get = fake_get # type: ignore[method-assign]
|
|
repos = client.list_repos()
|
|
names = [r["full_name"] for r in repos]
|
|
self.assertEqual(names, ["alice/app", "bob/lib"])
|
|
search_pages = [p for path, p in calls if path == "/api/v1/repos/search"]
|
|
self.assertGreaterEqual(len(search_pages), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|