-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github.py
More file actions
368 lines (315 loc) · 10.6 KB
/
Copy pathtest_github.py
File metadata and controls
368 lines (315 loc) · 10.6 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import pytest
import respx
from core.integrations.github import (
GITHUB_API_URL,
GithubProjectV2Item,
GithubAPIError,
GithubSender,
parse_github_webhook,
prep_github_webhook,
)
from core.models import Webhook
from httpx import Response
def test_parse_github_webhook_raises_value_error_for_unsupported():
headers = {"X-Github-Event": "random_event"}
wh = Webhook(meta=headers, content={}, extra={})
with pytest.raises(ValueError):
parse_github_webhook(wh)
def test_github_project_created_event():
parser = GithubProjectV2Item(
action="projects_v2_item.created",
headers={},
content={
"sender": {"login": "testuser", "html_url": "https://github.com/testuser"},
"projects_v2_item": {
"content_type": "Issue",
"node_id": "test_node_id",
},
"action": "created",
},
extra={
"content": {
"__typename": "Issue",
"id": "I_randomIssueID",
"title": "Test Issue",
"url": "https://github.com/test-issue",
}
},
)
message = parser.as_discord_message()
assert message == (
"[@testuser](https://github.com/testuser) created "
"[Test Issue](https://github.com/test-issue)"
)
def test_github_project_edited_event_for_status_change():
parser = GithubProjectV2Item(
action="projects_v2_item.edited",
headers={},
content={
"sender": {"login": "testuser", "html_url": "https://github.com/testuser"},
"projects_v2_item": {
"content_type": "Issue",
"node_id": "test_node_id",
},
"action": "edited",
"changes": {
"field_value": {
"field_name": "Status",
"field_type": "single_select",
"from": {"name": "To Do"},
"to": {"name": "In Progress"},
}
},
},
extra={
"content": {
"__typename": "Issue",
"id": "I_randomIssueID",
"title": "Test Issue",
"url": "https://github.com/test-issue",
}
},
)
message = parser.as_discord_message()
assert message == (
"[@testuser](https://github.com/testuser) changed **Status** of "
"**[Test Issue](https://github.com/test-issue)** "
"from **To Do** to **In Progress**"
)
def test_github_project_edited_event_for_date_change():
parser = GithubProjectV2Item(
action="projects_v2_item.edited",
headers={},
content={
"sender": {"login": "testuser", "html_url": "https://github.com/testuser"},
"projects_v2_item": {
"content_type": "Issue",
"node_id": "test_node_id",
},
"action": "edited",
"changes": {
"field_value": {
"field_name": "Deadline",
"field_type": "date",
"from": "2024-01-01T10:20:30",
"to": "2025-01-05T20:30:10",
}
},
},
extra={
"content": {
"__typename": "Issue",
"id": "I_randomIssueID",
"title": "Test Issue",
"url": "https://github.com/test-issue",
}
},
)
message = parser.as_discord_message()
assert message == (
"[@testuser](https://github.com/testuser) changed **Deadline** of "
"**[Test Issue](https://github.com/test-issue)** "
"from **2024-01-01** to **2025-01-05**"
)
def test_github_project_item_draft_issue_created():
parser = GithubProjectV2Item(
action="projects_v2_item.created",
headers={},
content={
"sender": {"login": "testuser", "html_url": "https://github.com/testuser"},
"projects_v2_item": {},
"action": "created",
},
extra={
"content": {
"__typename": "DraftIssue",
"id": "DI_randomDraftIssueID",
"title": "Draft Title",
}
},
)
message = parser.as_discord_message()
assert message == "[@testuser](https://github.com/testuser) created Draft Title"
def test_github_project_item_edited_event_no_changes():
parser = GithubProjectV2Item(
action="projects_v2_item.edited",
headers={},
content={
"sender": {"login": "testuser", "html_url": "https://github.com/testuser"},
"projects_v2_item": {},
"action": "edited",
},
extra={
"content": {
"__typename": "Issue",
"id": "I_randomIssueID",
"title": "Test Issue",
"url": "https://github.com/test-issue",
}
},
)
message = parser.as_discord_message()
assert message == (
"[@testuser](https://github.com/testuser) changed "
"[Test Issue](https://github.com/test-issue)"
)
class TestGithubProjectV2Item:
def test_changes_for_single_select(self):
parser = GithubProjectV2Item(
action="changed",
headers={},
content={
"changes": {
"field_value": {
"field_name": "Status",
"field_type": "single_select",
"from": {"name": "To Do"},
"to": {"name": "In Progress"},
}
},
},
extra={},
)
assert parser.changes() == {
"from": "To Do",
"to": "In Progress",
"field": "Status",
}
def test_changes_for_date(self):
parser = GithubProjectV2Item(
action="changed",
headers={},
content={
"changes": {
"field_value": {
"field_name": "Deadline",
"field_type": "date",
"from": "2024-01-01T10:20:30",
"to": "2025-01-05T20:30:10",
}
},
},
extra={},
)
assert parser.changes() == {
"from": "2024-01-01",
"to": "2025-01-05",
"field": "Deadline",
}
def test_changes_for_unsupported_format(self):
parser = GithubProjectV2Item(
action="changed",
headers={},
content={
"changes": {
"field_value": {
"field_name": "Randomfield",
"field_type": "unsupported",
"from": "This",
"to": "That",
}
},
},
extra={},
)
assert parser.changes() == {
"from": "None",
"to": "None",
"field": "Randomfield",
}
def test_get_project_parses_project_correctly(self, github_data):
wh = Webhook(
meta={"X-Github-Event": "projects_v2_item"},
content=github_data["project_v2_item.edited"],
extra=github_data["query_result"],
)
gwh = parse_github_webhook(wh)
ghp = gwh.get_project()
assert ghp.title == "Board Project"
assert ghp.url == "https://github.com/orgs/EuroPython/projects/1337"
def test_get_sender_parses_sender_correctly(self, github_data):
wh = Webhook(
meta={"X-Github-Event": "projects_v2_item"},
content=github_data["project_v2_item.edited"],
extra=github_data["query_result"],
)
gwh = parse_github_webhook(wh)
sender = gwh.get_sender()
assert isinstance(sender, GithubSender)
assert sender.login == "github-project-automation[bot]"
assert sender.html_url == "https://github.com/apps/github-project-automation"
def test_sender_formats_sender_correctly(self, github_data):
wh = Webhook(
meta={"X-Github-Event": "projects_v2_item"},
content=github_data["project_v2_item.edited"],
extra=github_data["query_result"],
)
gwh = parse_github_webhook(wh)
assert isinstance(gwh.sender, str)
assert (
gwh.sender == "[@github-project-automation[bot]]("
"https://github.com/apps/github-project-automation"
")"
)
def test_prep_github_webhook_fails_if_event_not_supported():
wh = Webhook(meta={"X-Github-Event": "issue.fixed"})
with pytest.raises(ValueError):
prep_github_webhook(wh)
@respx.mock
@pytest.mark.django_db
def test_prep_github_webhook_fetches_extra_data_for_project_v2_item():
wh = Webhook(
meta={"X-Github-Event": "projects_v2_item"},
content={
"action": "random",
"projects_v2_item": {
"node_id": "PVTI_random_projectItemV2ID",
},
},
)
node = {
"project": {
"id": "PVT_Random_Project",
"title": "Random Project",
"url": "https://github.com/europython",
},
"content": {
"__typename": "Issue",
"id": "I_randomIssueID",
"title": "Test Issue",
"url": "https://github.com/test-issue",
},
}
mocked_response = {
"data": {
"node": node,
}
}
respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response))
wh = prep_github_webhook(wh)
assert wh.event == "projects_v2_item.random"
assert wh.extra == node
@respx.mock
def test_prep_github_webhook_reraises_exception_in_case_of_API_error():
wh = Webhook(
meta={"X-Github-Event": "projects_v2_item"},
content={
"projects_v2_item": {
"node_id": "PVTI_random_projectItemV2ID",
"action": "random",
}
},
)
respx.post(GITHUB_API_URL).mock(return_value=Response(500, json={"lol": "failed"}))
with pytest.raises(
GithubAPIError, match='GitHub API error: 500 - {"lol":"failed"}'
):
wh = prep_github_webhook(wh)
def test_parse_github_webhook_raises_exception_for_unsupported_events():
wh = Webhook(
meta={"X-Github-Event": "long_form_content"},
content={},
extra={"something": "extra"},
)
with pytest.raises(ValueError, match="Event not supported `long_form_content`"):
parse_github_webhook(wh)