-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_error_codes.py
More file actions
98 lines (88 loc) · 2.66 KB
/
Copy pathtest_error_codes.py
File metadata and controls
98 lines (88 loc) · 2.66 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
"""Parametrized checks that API errors include stable code fields."""
from __future__ import annotations
import json
from types import SimpleNamespace
import pytest
from api.error_codes import ErrorCode
from tests.conftest import assert_error_response
@pytest.mark.parametrize(
"method,path,kwargs,status,code",
[
(
"get",
"/api/search?q=test&limit=abc",
{},
400,
ErrorCode.SEARCH_INVALID_LIMIT,
),
(
"get",
"/api/search?q=",
{},
400,
ErrorCode.SEARCH_EMPTY_QUERY,
),
(
"get",
"/api/search?q=test&since_days=foo",
{},
400,
ErrorCode.SEARCH_INVALID_SINCE_DAYS,
),
(
"get",
"/api/sessions/test-project/nonexistent",
{},
404,
ErrorCode.SESSION_NOT_FOUND,
),
(
"get",
"/api/sessions/test-project/../../x/session_abc123",
{},
400,
ErrorCode.INVALID_PATH,
),
(
"post",
"/api/export",
{"json": {"since": "bad"}},
400,
ErrorCode.INVALID_SINCE_MODE,
),
(
"post",
"/api/export",
{"data": "[]", "content_type": "application/json"},
400,
ErrorCode.INVALID_REQUEST_BODY,
),
],
)
def test_error_codes_on_endpoints(client, method, path, kwargs, status, code):
fn = getattr(client, method)
resp = fn(path, **kwargs)
assert resp.status_code == status
assert_error_response(resp, expected_code=code)
def test_bulk_export_empty_includes_export_nothing_code(client_empty):
resp = client_empty.post("/api/export", json={"since": "all"})
assert resp.status_code == 422
assert_error_response(resp, expected_code="EXPORT_NOTHING_TO_EXPORT")
def test_search_index_unavailable_code(client_single, monkeypatch):
monkeypatch.setattr(
"api.search._search_via_index",
lambda *_args, **_kwargs: SimpleNamespace(
hits=None,
fts_exhausted=False,
index_locked_without_hits=True,
),
)
monkeypatch.setattr(
"api.search._search_live_scan",
lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("live scan failed")),
)
resp = client_single.get("/api/search?q=test")
assert resp.status_code == 503
body_text = json.dumps(resp.get_json())
assert_error_response(resp, expected_code=ErrorCode.SEARCH_INDEX_UNAVAILABLE)
assert "live scan failed" not in body_text