-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathtest_unit_gitlab_tasks.py
More file actions
248 lines (216 loc) · 6.27 KB
/
test_unit_gitlab_tasks.py
File metadata and controls
248 lines (216 loc) · 6.27 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
import pytest
import responses
from features.feature_external_resources.models import (
FeatureExternalResource,
ResourceType,
)
from features.models import Feature
from integrations.gitlab.constants import GitLabEventType
from integrations.gitlab.models import GitLabConfiguration
from integrations.gitlab.tasks import _parse_resource_url, post_gitlab_comment
from projects.models import Project
@pytest.mark.parametrize(
"url,expected",
[
(
"https://gitlab.example.com/group/project/-/merge_requests/42",
("group/project", "merge_requests", 42),
),
(
"https://gitlab.example.com/group/project/-/issues/7",
("group/project", "issues", 7),
),
(
"https://gitlab.example.com/group/project/-/work_items/7",
("group/project", "issues", 7),
),
(
"https://gitlab.example.com/org/sub-group/project/-/merge_requests/1",
("org/sub-group/project", "merge_requests", 1),
),
(
"https://gitlab.example.com/unknown/path/to/resource",
None,
),
(
"https://gitlab.example.com/group/project/-/issues/",
None,
),
(
"https://gitlab.example.com/-/issues/5",
None,
),
],
ids=[
"mr",
"issue",
"work-item",
"nested-group",
"unknown-format",
"no-iid",
"no-project-path",
],
)
def test_parse_resource_url__various_urls__returns_correct_tuple(
url: str,
expected: tuple[str, str, int] | None,
) -> None:
# Given
# When
result = _parse_resource_url(url)
# Then
assert result == expected
@pytest.mark.django_db
@responses.activate
def test_post_gitlab_comment__linked_resource__posts_comment(
project: Project,
feature: Feature,
gitlab_configuration: GitLabConfiguration,
) -> None:
# Given
FeatureExternalResource.objects.create(
url="https://gitlab.example.com/testgroup/testrepo/-/issues/1",
type=ResourceType.GITLAB_ISSUE,
feature=feature,
metadata='{"state": "opened"}',
)
responses.add(
responses.POST,
"https://gitlab.example.com/api/v4/projects/1/issues/1/notes",
json={"id": 1, "body": "comment"},
status=201,
)
# When
post_gitlab_comment(
project_id=project.id,
feature_id=feature.id,
feature_name=feature.name,
event_type=GitLabEventType.FLAG_UPDATED.value,
feature_states=[],
)
# Then
assert len(responses.calls) == 1
@pytest.mark.django_db
def test_post_gitlab_comment__no_config__returns_early(
project: Project,
feature: Feature,
) -> None:
# Given — no GitLabConfiguration exists
# When
post_gitlab_comment(
project_id=project.id,
feature_id=feature.id,
feature_name=feature.name,
event_type=GitLabEventType.FLAG_UPDATED.value,
feature_states=[],
)
# Then
assert GitLabConfiguration.objects.filter(project=project).count() == 0
@pytest.mark.django_db
def test_post_gitlab_comment__no_gitlab_project_id__returns_early(
project: Project,
feature: Feature,
) -> None:
# Given
GitLabConfiguration.objects.create(
project=project,
gitlab_instance_url="https://gitlab.example.com",
access_token="token",
webhook_secret="secret",
gitlab_project_id=None,
)
# When
post_gitlab_comment(
project_id=project.id,
feature_id=feature.id,
feature_name=feature.name,
event_type=GitLabEventType.FLAG_UPDATED.value,
feature_states=[],
)
# Then
assert True # no error raised, returns early
@pytest.mark.django_db
def test_post_gitlab_comment__no_linked_resources__returns_early(
project: Project,
feature: Feature,
gitlab_configuration: GitLabConfiguration,
) -> None:
# Given — no FeatureExternalResource linked
# When
post_gitlab_comment(
project_id=project.id,
feature_id=feature.id,
feature_name=feature.name,
event_type=GitLabEventType.FLAG_UPDATED.value,
feature_states=[],
)
# Then
assert True # no error raised, returns early
@pytest.mark.django_db
@responses.activate
def test_post_gitlab_comment__resource_removed__posts_to_url(
project: Project,
feature: Feature,
gitlab_configuration: GitLabConfiguration,
) -> None:
# Given
resource_url = "https://gitlab.example.com/testgroup/testrepo/-/issues/5"
responses.add(
responses.POST,
"https://gitlab.example.com/api/v4/projects/1/issues/5/notes",
json={"id": 1, "body": "unlinked"},
status=201,
)
# When
post_gitlab_comment(
project_id=project.id,
feature_id=feature.id,
feature_name=feature.name,
event_type=GitLabEventType.FEATURE_EXTERNAL_RESOURCE_REMOVED.value,
feature_states=[],
url=resource_url,
)
# Then
assert len(responses.calls) == 1
@pytest.mark.django_db
@responses.activate
def test_post_gitlab_comment__unparseable_url__skips_without_error(
project: Project,
feature: Feature,
gitlab_configuration: GitLabConfiguration,
) -> None:
# Given
FeatureExternalResource.objects.create(
url="https://gitlab.example.com/not/a/valid/resource",
type=ResourceType.GITLAB_ISSUE,
feature=feature,
metadata='{"state": "opened"}',
)
# When
post_gitlab_comment(
project_id=project.id,
feature_id=feature.id,
feature_name=feature.name,
event_type=GitLabEventType.FLAG_UPDATED.value,
feature_states=[],
)
# Then
assert len(responses.calls) == 0
@pytest.mark.django_db
def test_post_gitlab_comment__resource_removed_no_url__returns_early(
project: Project,
feature: Feature,
gitlab_configuration: GitLabConfiguration,
) -> None:
# Given — resource removed event with no URL
# When
post_gitlab_comment(
project_id=project.id,
feature_id=feature.id,
feature_name=feature.name,
event_type=GitLabEventType.FEATURE_EXTERNAL_RESOURCE_REMOVED.value,
feature_states=[],
url=None,
)
# Then
assert True # no error, returns early