-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
114 lines (89 loc) · 4.3 KB
/
test_api.py
File metadata and controls
114 lines (89 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
Tests for tind_client.api.
"""
import pytest
import requests_mock as req_mock # noqa: F401 — activates the requests_mock fixture
from tind_client.api import tind_download, tind_get
from tind_client.errors import AuthorizationError, TooManyRequestsError
BASE_URL = "https://tind.example.edu"
API_KEY = "test-api-key"
pytestmark = pytest.mark.usefixtures("tind_env")
def test_tind_get_success(requests_mock: req_mock.Mocker) -> None:
"""tind_get returns (200, body) on a successful request."""
requests_mock.get(f"{BASE_URL}/record/1/", text="OK", status_code=200)
status, text = tind_get("record/1/", api_key=API_KEY, api_url=BASE_URL)
assert status == 200
assert text == "OK"
def test_tind_get_with_params(requests_mock: req_mock.Mocker) -> None:
"""tind_get forwards query parameters to the HTTP request."""
requests_mock.get(f"{BASE_URL}/record/1/", text="<xml/>", status_code=200)
status, _ = tind_get(
"record/1/", api_key=API_KEY, api_url=BASE_URL, params={"of": "xm"}
)
assert status == 200
assert requests_mock.last_request is not None
assert requests_mock.last_request.qs == {"of": ["xm"]}
def test_tind_get_raises_on_401(requests_mock: req_mock.Mocker) -> None:
"""tind_get raises AuthorizationError on HTTP 401."""
requests_mock.get(f"{BASE_URL}/record/1/", status_code=401)
with pytest.raises(AuthorizationError):
tind_get("record/1/", api_key=API_KEY, api_url=BASE_URL)
def test_tind_get_raises_on_429(requests_mock: req_mock.Mocker) -> None:
"""Ensure tind_get raises TooManyRequestsError on HTTP 429."""
requests_mock.get(f"{BASE_URL}/record/1/", status_code=429)
with pytest.raises(TooManyRequestsError):
tind_get("record/1/", api_key=API_KEY, api_url=BASE_URL)
def test_tind_get_raises_on_500(requests_mock: req_mock.Mocker) -> None:
"""tind_get propagates an HTTPError on HTTP 5xx."""
requests_mock.get(f"{BASE_URL}/record/1/", status_code=500)
with pytest.raises(Exception):
tind_get("record/1/", api_key=API_KEY, api_url=BASE_URL)
def test_tind_get_missing_api_key(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""tind_get raises AuthorizationError when API key is empty."""
monkeypatch.delenv("TIND_API_KEY", raising=False)
with pytest.raises(AuthorizationError):
tind_get("record/1/", api_key="", api_url=BASE_URL)
def test_tind_download_success(
requests_mock: req_mock.Mocker, tmp_path: pytest.TempPathFactory
) -> None:
"""tind_download saves the file and returns its path."""
url = f"{BASE_URL}/files/12345/download"
requests_mock.get(
url,
content=b"file content",
status_code=200,
headers={"Content-Disposition": 'attachment; filename="report.pdf"'},
)
status, path = tind_download(url, str(tmp_path), api_key=API_KEY)
assert status == 200
assert path.endswith("report.pdf")
def test_tind_download_uses_url_filename_fallback(
requests_mock: req_mock.Mocker, tmp_path: pytest.TempPathFactory
) -> None:
"""tind_download falls back to extracting the filename from the URL."""
url = f"{BASE_URL}/files/myfile/download"
requests_mock.get(url, content=b"data", status_code=200)
status, path = tind_download(url, str(tmp_path), api_key=API_KEY)
assert status == 200
assert "myfile" in path
def test_tind_download_raises_on_401(requests_mock: req_mock.Mocker) -> None:
"""tind_download raises AuthorizationError on HTTP 401."""
url = f"{BASE_URL}/files/12345/download"
requests_mock.get(url, status_code=401)
with pytest.raises(AuthorizationError):
tind_download(url, "/tmp", api_key=API_KEY)
def test_tind_download_raises_on_429(requests_mock: req_mock.Mocker) -> None:
"""Ensure tind_download raises TooManyRequestsError on HTTP 429."""
url = f"{BASE_URL}/files/12345/download"
requests_mock.get(url, status_code=429)
with pytest.raises(TooManyRequestsError):
tind_download(url, "/tmp", api_key=API_KEY)
def test_tind_download_non_200_returns_empty(requests_mock: req_mock.Mocker) -> None:
"""tind_download returns (status, '') for non-200, non-error responses."""
url = f"{BASE_URL}/files/12345/download"
requests_mock.get(url, status_code=404)
status, path = tind_download(url, "/tmp", api_key=API_KEY)
assert status == 404
assert path == ""