-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtest_feedback.py
More file actions
116 lines (96 loc) · 3.83 KB
/
test_feedback.py
File metadata and controls
116 lines (96 loc) · 3.83 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
from unittest.mock import patch
from fastapi.testclient import TestClient
from api.main import app
FEEDBACK_PATH = "/api/feedback"
VALID_TRACE_ID = "trace-abc-123"
DEVELOPER = "ola"
OTHER_DEVELOPER = "kari"
DEVELOPER_HEADER = {"X-Developer": DEVELOPER}
def _post_feedback(payload, headers=None):
return TestClient(app).post(FEEDBACK_PATH, json=payload, headers=headers)
class TestFeedbackEndpoint:
def test_thumbs_up_writes_score_and_returns_204(self):
with (
patch("api.routes.feedback.get_trace_developer", return_value=DEVELOPER),
patch("api.routes.feedback.score_validation") as mock_score,
):
response = _post_feedback(
{"trace_id": VALID_TRACE_ID, "thumbs_up": True},
headers=DEVELOPER_HEADER,
)
assert response.status_code == 204
mock_score.assert_called_once_with(
name="user_feedback",
passed=True,
trace_id=VALID_TRACE_ID,
comment=None,
)
def test_thumbs_down_with_comment_is_forwarded(self):
with (
patch("api.routes.feedback.get_trace_developer", return_value=DEVELOPER),
patch("api.routes.feedback.score_validation") as mock_score,
):
response = _post_feedback(
{
"trace_id": VALID_TRACE_ID,
"thumbs_up": False,
"comment": "Svaret var ikke nyttig.",
},
headers=DEVELOPER_HEADER,
)
assert response.status_code == 204
mock_score.assert_called_once_with(
name="user_feedback",
passed=False,
trace_id=VALID_TRACE_ID,
comment="Svaret var ikke nyttig.",
)
def test_missing_developer_header_returns_400(self):
with patch("api.routes.feedback.score_validation") as mock_score:
response = _post_feedback({"trace_id": VALID_TRACE_ID, "thumbs_up": True})
assert response.status_code == 400
mock_score.assert_not_called()
def test_unknown_trace_owner_returns_403(self):
with (
patch("api.routes.feedback.get_trace_developer", return_value=None),
patch("api.routes.feedback.score_validation") as mock_score,
):
response = _post_feedback(
{"trace_id": VALID_TRACE_ID, "thumbs_up": True},
headers=DEVELOPER_HEADER,
)
assert response.status_code == 403
mock_score.assert_not_called()
def test_owner_mismatch_returns_403(self):
with (
patch(
"api.routes.feedback.get_trace_developer", return_value=OTHER_DEVELOPER
),
patch("api.routes.feedback.score_validation") as mock_score,
):
response = _post_feedback(
{"trace_id": VALID_TRACE_ID, "thumbs_up": True},
headers=DEVELOPER_HEADER,
)
assert response.status_code == 403
mock_score.assert_not_called()
def test_empty_trace_id_returns_422(self):
with patch("api.routes.feedback.score_validation") as mock_score:
response = _post_feedback(
{"trace_id": "", "thumbs_up": True},
headers=DEVELOPER_HEADER,
)
assert response.status_code == 422
mock_score.assert_not_called()
def test_comment_over_max_length_returns_422(self):
with patch("api.routes.feedback.score_validation") as mock_score:
response = _post_feedback(
{
"trace_id": VALID_TRACE_ID,
"thumbs_up": True,
"comment": "x" * 10001,
},
headers=DEVELOPER_HEADER,
)
assert response.status_code == 422
mock_score.assert_not_called()