-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_callbacks.py
More file actions
260 lines (205 loc) · 10.2 KB
/
test_callbacks.py
File metadata and controls
260 lines (205 loc) · 10.2 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from __future__ import annotations
import json
import httpx
import pytest
import posthook
from posthook import CallbackError, CallbackResult
from posthook._callbacks import ack, async_ack, async_nack, nack
# ─── Helpers ────────────────────────────────────────────────────────
def _mock_response(
status_code: int,
body: dict | None = None,
text: str = "",
) -> httpx.Response:
if body is not None:
return httpx.Response(status_code, json=body)
return httpx.Response(status_code, text=text)
# ─── Sync ack() ─────────────────────────────────────────────────────
class TestAckSync:
def test_success_applied(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(200, {"data": {"status": "completed"}}),
)
result = ack("https://api.posthook.io/ack/token123")
assert result == CallbackResult(applied=True, status="completed")
def test_success_not_applied(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Server returns 200 but status is not 'completed' (idempotent no-op)."""
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(200, {"data": {"status": "nacked"}}),
)
result = ack("https://api.posthook.io/ack/token123")
assert result == CallbackResult(applied=False, status="nacked")
def test_404_not_found(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(404),
)
result = ack("https://api.posthook.io/ack/token123")
assert result == CallbackResult(applied=False, status="not_found")
def test_409_conflict(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(409),
)
result = ack("https://api.posthook.io/ack/token123")
assert result == CallbackResult(applied=False, status="conflict")
def test_401_raises_callback_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(401, text="unauthorized"),
)
with pytest.raises(CallbackError, match="ack failed: 401"):
ack("https://api.posthook.io/ack/token123")
def test_410_raises_callback_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(410, text="gone"),
)
with pytest.raises(CallbackError, match="ack failed: 410"):
ack("https://api.posthook.io/ack/token123")
def test_500_raises_callback_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(500, text="internal error"),
)
with pytest.raises(CallbackError, match="ack failed: 500"):
ack("https://api.posthook.io/ack/token123")
def test_json_body_sent(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Verify that a JSON body is serialized and Content-Type is set."""
captured: dict = {}
def mock_post(url: str, **kwargs) -> httpx.Response:
captured["content"] = kwargs.get("content")
captured["headers"] = kwargs.get("headers")
return _mock_response(200, {"data": {"status": "completed"}})
monkeypatch.setattr(httpx, "post", mock_post)
ack("https://api.posthook.io/ack/token123", body={"done": True})
assert json.loads(captured["content"]) == {"done": True}
assert captured["headers"]["Content-Type"] == "application/json"
def test_no_body_no_content_type(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Without a body, no Content-Type header should be sent."""
captured: dict = {}
def mock_post(url: str, **kwargs) -> httpx.Response:
captured["content"] = kwargs.get("content")
captured["headers"] = kwargs.get("headers")
return _mock_response(200, {"data": {"status": "completed"}})
monkeypatch.setattr(httpx, "post", mock_post)
ack("https://api.posthook.io/ack/token123")
assert captured["content"] is None
assert captured["headers"] == {}
# ─── Sync nack() ────────────────────────────────────────────────────
class TestNackSync:
def test_success_applied(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(200, {"data": {"status": "nacked"}}),
)
result = nack("https://api.posthook.io/nack/token123")
assert result == CallbackResult(applied=True, status="nacked")
def test_success_not_applied(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(200, {"data": {"status": "completed"}}),
)
result = nack("https://api.posthook.io/nack/token123")
assert result == CallbackResult(applied=False, status="completed")
def test_404_not_found(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(404),
)
result = nack("https://api.posthook.io/nack/token123")
assert result == CallbackResult(applied=False, status="not_found")
def test_409_conflict(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(409),
)
result = nack("https://api.posthook.io/nack/token123")
assert result == CallbackResult(applied=False, status="conflict")
def test_410_raises_callback_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "post",
lambda url, **kw: _mock_response(410, text="gone"),
)
with pytest.raises(CallbackError, match="nack failed: 410"):
nack("https://api.posthook.io/nack/token123")
# ─── Async ack/nack ─────────────────────────────────────────────────
class _MockAsyncClient:
"""Replaces httpx.AsyncClient for async tests."""
def __init__(self, handler):
self._handler = handler
async def __aenter__(self):
return self
async def __aexit__(self, *args):
pass
async def post(self, url, **kwargs):
return self._handler(url, **kwargs)
class TestAsyncAck:
async def test_success_applied(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "AsyncClient",
lambda **kw: _MockAsyncClient(
lambda url, **kw: _mock_response(200, {"data": {"status": "completed"}}),
),
)
result = await async_ack("https://api.posthook.io/ack/token123")
assert result == CallbackResult(applied=True, status="completed")
async def test_410_raises_callback_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "AsyncClient",
lambda **kw: _MockAsyncClient(
lambda url, **kw: _mock_response(410, text="gone"),
),
)
with pytest.raises(CallbackError, match="ack failed: 410"):
await async_ack("https://api.posthook.io/ack/token123")
async def test_500_raises_callback_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "AsyncClient",
lambda **kw: _MockAsyncClient(
lambda url, **kw: _mock_response(500, text="boom"),
),
)
with pytest.raises(CallbackError, match="ack failed: 500"):
await async_ack("https://api.posthook.io/ack/token123")
class TestAsyncNack:
async def test_success_applied(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "AsyncClient",
lambda **kw: _MockAsyncClient(
lambda url, **kw: _mock_response(200, {"data": {"status": "nacked"}}),
),
)
result = await async_nack("https://api.posthook.io/nack/token123")
assert result == CallbackResult(applied=True, status="nacked")
async def test_409_conflict(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "AsyncClient",
lambda **kw: _MockAsyncClient(
lambda url, **kw: _mock_response(409),
),
)
result = await async_nack("https://api.posthook.io/nack/token123")
assert result == CallbackResult(applied=False, status="conflict")
async def test_410_raises_callback_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
httpx, "AsyncClient",
lambda **kw: _MockAsyncClient(
lambda url, **kw: _mock_response(410, text="gone"),
),
)
with pytest.raises(CallbackError, match="nack failed: 410"):
await async_nack("https://api.posthook.io/nack/token123")
# ─── Importability ───────────────────────────────────────────────────
class TestExports:
def test_callback_result_importable(self) -> None:
assert hasattr(posthook, "CallbackResult")
def test_callback_error_importable(self) -> None:
assert hasattr(posthook, "CallbackError")
def test_ack_importable(self) -> None:
assert hasattr(posthook, "ack")
assert hasattr(posthook, "nack")
assert hasattr(posthook, "async_ack")
assert hasattr(posthook, "async_nack")