SHA256
forked from course-team/tjhlp-gitea
192 lines
6.2 KiB
Python
192 lines
6.2 KiB
Python
"""client.py 单元测试: 使用 respx mock HTTP 层"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Generator
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
from pydantic import HttpUrl, SecretStr
|
|
|
|
from tjhlp.client import GiteaClient, GiteaClientError
|
|
from tjhlp.config import GiteaConfig
|
|
|
|
|
|
@pytest.fixture()
|
|
def config() -> GiteaConfig:
|
|
return GiteaConfig(
|
|
url=HttpUrl("http://gitea.test"),
|
|
token=SecretStr("test-token"),
|
|
)
|
|
|
|
|
|
@pytest.fixture()
|
|
def client(config: GiteaConfig) -> Generator[GiteaClient]:
|
|
c = GiteaClient(config)
|
|
yield c
|
|
c.close()
|
|
|
|
|
|
class TestClientInit:
|
|
def test_base_url(self, config: GiteaConfig):
|
|
c = GiteaClient(config)
|
|
assert c.base_url == "http://gitea.test/api/v1"
|
|
c.close()
|
|
|
|
def test_base_url_strips_trailing_slash(self):
|
|
cfg = GiteaConfig(url=HttpUrl("http://gitea.test/"), token=SecretStr("tok"))
|
|
c = GiteaClient(cfg)
|
|
assert c.base_url == "http://gitea.test/api/v1"
|
|
c.close()
|
|
|
|
def test_context_manager(self, config: GiteaConfig):
|
|
with GiteaClient(config) as c:
|
|
assert c.base_url == "http://gitea.test/api/v1"
|
|
|
|
|
|
class TestVersion:
|
|
@respx.mock
|
|
def test_get_version(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/version").respond(json={"version": "1.25.5"})
|
|
assert client.get_version() == "1.25.5"
|
|
|
|
|
|
class TestErrorHandling:
|
|
@respx.mock
|
|
def test_404_raises(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/users/nonexistent").respond(
|
|
status_code=404, json={"message": "not found"}
|
|
)
|
|
with pytest.raises(GiteaClientError) as exc_info:
|
|
client._get("/users/nonexistent")
|
|
assert exc_info.value.status_code == 404
|
|
|
|
@respx.mock
|
|
def test_500_raises(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/version").respond(
|
|
status_code=500, json={"message": "internal error"}
|
|
)
|
|
with pytest.raises(GiteaClientError) as exc_info:
|
|
client.get_version()
|
|
assert exc_info.value.status_code == 500
|
|
|
|
|
|
class TestGetUser:
|
|
@respx.mock
|
|
def test_existing_user(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/users/student1").respond(
|
|
json={"id": 1, "login": "student1", "full_name": "Student One"}
|
|
)
|
|
user = client.get_user("student1")
|
|
assert user is not None
|
|
assert user.login == "student1"
|
|
|
|
@respx.mock
|
|
def test_nonexistent_user(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/users/nobody").respond(
|
|
status_code=404, json={"message": "not found"}
|
|
)
|
|
user = client.get_user("nobody")
|
|
assert user is None
|
|
|
|
|
|
class TestGetOrg:
|
|
@respx.mock
|
|
def test_existing_org(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/orgs/tjhlp").respond(json={"id": 1, "name": "tjhlp"})
|
|
org = client.get_org("tjhlp")
|
|
assert org is not None
|
|
assert org.name == "tjhlp"
|
|
|
|
@respx.mock
|
|
def test_nonexistent_org(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/orgs/nope").respond(
|
|
status_code=404, json={"message": "not found"}
|
|
)
|
|
assert client.get_org("nope") is None
|
|
|
|
|
|
class TestPagination:
|
|
@respx.mock
|
|
def test_single_page(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/admin/users").respond(
|
|
json=[{"id": 1, "login": "a"}, {"id": 2, "login": "b"}]
|
|
)
|
|
users = client.list_users()
|
|
assert len(users) == 2
|
|
|
|
@respx.mock
|
|
def test_multi_page(self, client: GiteaClient):
|
|
# First page returns exactly `limit` items -> triggers page 2
|
|
page1 = [{"id": i, "login": f"u{i}"} for i in range(50)]
|
|
page2 = [{"id": 50, "login": "u50"}]
|
|
|
|
route = respx.get("http://gitea.test/api/v1/admin/users")
|
|
route.side_effect = [
|
|
httpx.Response(200, json=page1),
|
|
httpx.Response(200, json=page2),
|
|
]
|
|
users = client.list_users()
|
|
assert len(users) == 51
|
|
|
|
@respx.mock
|
|
def test_empty_result(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/admin/users").respond(json=[])
|
|
users = client.list_users()
|
|
assert len(users) == 0
|
|
|
|
|
|
class TestCreateUser:
|
|
@respx.mock
|
|
def test_create_user(self, client: GiteaClient):
|
|
respx.post("http://gitea.test/api/v1/admin/users").respond(
|
|
json={
|
|
"id": 1,
|
|
"login": "2659999",
|
|
"full_name": "赵一伤",
|
|
"email": "[email protected]",
|
|
}
|
|
)
|
|
user = client.create_user("2659999", "password", "[email protected]", "赵一伤")
|
|
assert user.login == "2659999"
|
|
assert user.full_name == "赵一伤"
|
|
|
|
|
|
class TestLabels:
|
|
@respx.mock
|
|
def test_create_label(self, client: GiteaClient):
|
|
respx.post("http://gitea.test/api/v1/repos/org/repo/labels").respond(
|
|
json={"id": 1, "name": "bug", "color": "#ff0000", "description": "Bug report"}
|
|
)
|
|
label = client.create_label("org", "repo", "bug", "#ff0000", "Bug report")
|
|
assert label.name == "bug"
|
|
|
|
@respx.mock
|
|
def test_list_labels_empty(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/repos/org/repo/labels").respond(json=[])
|
|
labels = client.list_labels("org", "repo")
|
|
assert labels == []
|
|
|
|
|
|
class TestGetTeamByName:
|
|
@respx.mock
|
|
def test_found(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/orgs/org/teams").respond(
|
|
json=[
|
|
{"id": 1, "name": "ta", "permission": "write", "units": []},
|
|
{"id": 2, "name": "students", "permission": "read", "units": []},
|
|
]
|
|
)
|
|
team = client.get_team_by_name("org", "ta")
|
|
assert team is not None
|
|
assert team.id == 1
|
|
|
|
@respx.mock
|
|
def test_not_found(self, client: GiteaClient):
|
|
respx.get("http://gitea.test/api/v1/orgs/org/teams").respond(
|
|
json=[{"id": 1, "name": "ta", "permission": "write", "units": []}]
|
|
)
|
|
team = client.get_team_by_name("org", "nonexistent")
|
|
assert team is None
|