-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tasks.py
More file actions
223 lines (193 loc) · 6.38 KB
/
test_tasks.py
File metadata and controls
223 lines (193 loc) · 6.38 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
import logging
from django.conf import settings
import pytest
import respx
from core.integrations.github import GITHUB_API_URL
from core.models import DiscordMessage, Webhook
from core.tasks import (
process_github_webhook,
process_internal_webhook,
process_webhook,
process_zammad_webhook,
)
from django.utils import timezone
from django_tasks import TaskResultStatus
from httpx import Response
@pytest.mark.django_db
def test_process_internal_webhook_handles_internal_webhook_correctly():
wh = Webhook.objects.create(
source="internal",
event="test1",
content={
"random": "content",
},
extra={},
)
process_internal_webhook(wh)
dm = DiscordMessage.objects.get()
assert dm.content == "Webhook content: {'random': 'content'}"
assert dm.channel_id == "12345"
assert dm.channel_name == "#test-channel"
@pytest.mark.django_db
def test_process_internal_webhook_fails_if_incorrect_source():
wh = Webhook(source="asdf")
with pytest.raises(ValueError):
process_internal_webhook(wh)
@pytest.mark.django_db
def test_process_webhook_fails_if_unsupported_source():
wh = Webhook.objects.create(
source="asdf",
event="test1",
content={},
extra={},
)
# If the task is enqueued the errors are not emited.
# Instead we have to check the result
result = process_webhook.enqueue(str(wh.uuid))
assert result.status == TaskResultStatus.FAILED
assert any("Unsupported source asdf" in str(e) for e in result.errors)
@pytest.mark.django_db
def test_process_github_webhook_logs_unsupported_event(caplog):
wh = Webhook.objects.create(
source="github",
event="",
meta={"X-Github-Event": "testrandom"},
content={},
extra={},
)
with caplog.at_level(logging.INFO):
process_github_webhook(wh)
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "INFO"
assert (
caplog.records[0].message
== f"Not processing Github Webhook {wh.uuid}: Event `testrandom` not supported"
)
@pytest.mark.django_db
@respx.mock
def test_process_github_webhook_skips_a_message_when_unsupported_project(
github_data,
):
wh = Webhook.objects.create(
source="github",
event="",
meta={"X-Github-Event": "projects_v2_item"},
content=github_data["project_v2_item.edited"],
extra={},
)
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))
process_github_webhook(wh)
# Skip the message but mark as processed
assert DiscordMessage.objects.count() == 0
assert wh.processed_at is not None
assert wh.processed_at < timezone.now()
assert wh.event == "projects_v2_item.edited"
@pytest.mark.django_db
@respx.mock
def test_process_github_webhook_creates_a_message_from_supported(
github_data,
):
wh = Webhook.objects.create(
source="github",
event="",
meta={"X-Github-Event": "projects_v2_item"},
content=github_data["project_v2_item.edited"],
extra={},
)
node = {
"project": {
"id": "PVT_Test_Board_Project",
"title": "Test Board 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))
process_github_webhook(wh)
dm = DiscordMessage.objects.get()
assert wh.processed_at is not None
assert wh.processed_at < timezone.now()
assert wh.event == "projects_v2_item.edited"
assert dm.channel_id == settings.DISCORD_BOARD_CHANNEL_ID
assert dm.channel_name == settings.DISCORD_BOARD_CHANNEL_NAME
assert dm.content == (
"GitHub: [@github-project-automation[bot]]"
"(https://github.com/apps/github-project-automation)"
" changed **Status** of "
"**[Test Issue](https://github.com/test-issue)**"
" from **Done** to **In progress**"
)
assert dm.sent_at is None
@pytest.mark.django_db
@respx.mock
def test_process_zammad_webhook_creates_a_message_from_supported_queue():
wh = Webhook.objects.create(
source="zammad",
event="",
meta={},
content={
"ticket": {
"id": 123,
"group": {"id": "123", "name": "TestZammad Helpdesk"},
"updated_by": {"firstname": "Cookie", "lastname": "Monster"},
"title": "Test Ticket Title",
"owner": {"firstname": "Kermit", "lastname": "TheFrog"},
"state": "open",
"number": "13374321",
"customer": {"firstname": "Cookie", "lastname": "Monster"},
"created_at": str(timezone.now()),
"updated_at": str(timezone.now()),
"article_ids": [1],
},
"article": {
"sender": "Customer",
"internal": False,
"ticket_id": 123,
"created_at": str(timezone.now()),
"created_by": {"firstname": "Cookie", "lastname": "Monster"},
"subject": "New Cookies please",
},
},
extra={},
)
process_zammad_webhook(wh)
dm = DiscordMessage.objects.get()
assert wh.processed_at is not None
assert wh.processed_at < timezone.now()
assert wh.event == "new_ticket_created"
assert dm.channel_id == settings.DISCORD_HELPDESK_CHANNEL_ID
assert dm.channel_name == settings.DISCORD_HELPDESK_CHANNEL_NAME
assert dm.content == (
"Zammad: "
"TestZammad Helpdesk: Cookie created new ticket "
"https://servicedesk.europython.eu/#ticket/zoom/123"
)
assert dm.sent_at is None