/
conftest.py
423 lines (305 loc) 路 11.4 KB
/
conftest.py
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
from datetime import date, timedelta
import os
from typing import Callable, Optional, Generator, List
import pytest
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend as crypto_default_backend
from gitlab import Gitlab
from gitlab.v4.objects import Group, Project, User, ProjectAccessToken, GroupAccessToken
from gitlabform.gitlab import AccessLevel, GitLab
from tests.acceptance import (
allowed_codes,
create_group,
create_groups,
delete_groups,
create_project,
get_random_name,
create_users,
get_random_password,
)
from cli_ui.tests import MessageRecorder
@pytest.fixture()
def message_recorder():
message_recorder = MessageRecorder()
message_recorder.start()
yield message_recorder
message_recorder.stop()
@pytest.fixture(scope="session")
def gl():
return Gitlab(os.getenv("GITLAB_URL"), private_token=os.getenv("GITLAB_TOKEN"))
@pytest.fixture(autouse=True)
def requires_license(gl: Gitlab, request):
if not request.node.get_closest_marker("requires_license"):
return
gitlab_license = gl.get_license()
if not gitlab_license or gitlab_license["expired"]:
pytest.skip("this test requires a GitLab license (Paid/Trial)")
@pytest.fixture(scope="session")
def root_user(gl):
return gl.users.list(username="root")[0]
@pytest.fixture(scope="class")
def group(gl: Gitlab) -> Generator[Optional[Group], None, None]:
group_name = get_random_name("group")
gitlab_group = create_group(group_name)
yield gitlab_group
with allowed_codes(404):
gl.groups.delete(group_name)
@pytest.fixture(scope="function")
def group_for_function(gl: Gitlab):
group_name = get_random_name("group")
gitlab_group = create_group(group_name)
yield gitlab_group
with allowed_codes(404):
gl.groups.delete(group_name)
@pytest.fixture(scope="class")
def other_group(gl: Gitlab):
# TODO: deduplicate this - it's a copy and paste from the above fixture
group_name = get_random_name("group")
group = create_group(group_name)
yield group
with allowed_codes(404):
gl.groups.delete(group_name)
@pytest.fixture(scope="class")
def third_group(gl: Gitlab):
# TODO: deduplicate this - it's a copy and paste from the above fixture
group_name = get_random_name("group")
group = create_group(group_name)
yield group
with allowed_codes(404):
gl.groups.delete(group_name)
@pytest.fixture(scope="class")
def subgroup(gl: Gitlab, group: Group):
subgroup_name = get_random_name("subgroup")
gitlab_subgroup = create_group(subgroup_name, group.id)
yield gitlab_subgroup
with allowed_codes(404):
gl.groups.delete(f"{group.full_path}/{subgroup_name}")
@pytest.fixture(scope="class")
def other_subgroup(gl: Gitlab, group: Group):
# TODO: deduplicate this - it's a copy and paste from the above fixture
subgroup_name = get_random_name("subgroup")
gitlab_subgroup = create_group(subgroup_name, group.id)
yield gitlab_subgroup
with allowed_codes(404):
gl.groups.delete(f"{group.full_path}/{subgroup_name}")
@pytest.fixture(scope="class")
def project(gl: Gitlab, group: Group):
yield from create_project_in_group(group, "project")
@pytest.fixture(scope="class")
def project_in_subgroup(gl: Gitlab, subgroup: Group):
yield from create_project_in_group(subgroup, "project_in_subgroup")
@pytest.fixture(scope="class")
def project_in_other_subgroup(gl: Gitlab, other_subgroup: Group):
yield from create_project_in_group(other_subgroup, "project_in_other_subgroup")
@pytest.fixture(scope="function")
def project_for_function(gl: Gitlab, group: Group):
yield from create_project_in_group(group, "project_for_function")
@pytest.fixture(scope="class")
def other_project(gl: Gitlab, group):
yield from create_project_in_group(group, "other_project")
def create_project_in_group(group, entity_name):
project_name = get_random_name(entity_name)
gitlab_project = create_project(group, project_name)
yield gitlab_project
gitlab_project.delete()
@pytest.fixture(scope="class")
def groups(users):
no_of_groups = 4
group_name_base = get_random_name("group")
groups = create_groups(group_name_base, no_of_groups)
yield groups
delete_groups(group_name_base, no_of_groups)
@pytest.fixture(scope="class")
def group_to_invite_to_project(
gl: Gitlab,
) -> Generator[Callable[[Project, AccessLevel], Group], None, None]:
group_name_base = get_random_name("group")
created_groups: List[Group] = []
def _group_to_invite_to_project(
project: Project, access_level: AccessLevel = AccessLevel.DEVELOPER
) -> Group:
group_index = len(created_groups) + 1
group_name = f"{group_name_base}_{group_index}"
gitlab_group = create_group(group_name)
project.share(gitlab_group.id, access_level)
created_groups.append(gitlab_group)
return gitlab_group
yield _group_to_invite_to_project
for group in created_groups:
with allowed_codes(404):
gl.groups.delete(group.id)
@pytest.fixture(scope="class")
def users(group):
no_of_users = 4
username_base = get_random_name("user")
users = create_users(username_base, no_of_users)
yield users
for user in users:
user.delete()
@pytest.fixture(scope="class")
def other_users():
# TODO: deduplicate this - it's a copy and paste from the above fixture
no_of_users = 4
username_base = get_random_name("user")
users = create_users(username_base, no_of_users)
yield users
for user in users:
user.delete()
@pytest.fixture(scope="class")
def branch(project):
yield from create_branch(project, "branch")
@pytest.fixture(scope="class")
def other_branch(project):
yield from create_branch(project, "other_branch")
@pytest.fixture(scope="function")
def branch_for_function(project_for_function):
yield from create_branch(project_for_function, "branch_for_function")
@pytest.fixture(scope="function")
def other_branch_for_function(project_for_function):
yield from create_branch(project_for_function, "other_branch_for_function")
def create_branch(project_to_create_branch_on, entity_name):
name = get_random_name(entity_name)
branch = project_to_create_branch_on.branches.create(
{"branch": name, "ref": "main"}
)
yield name
branch.delete()
@pytest.fixture(scope="class")
def tag(project):
name = get_random_name("tag")
tag = project.tags.create({"tag_name": name, "ref": "main"})
yield name
# Normally the tag deletion would be here for post-test cleanup.
# But protected tags can't be deleted. It has to be unprotect first
# and then delete. Since the containing project gets deleted anyways,
# it should be fine if the tag deletion doesn't happen from this fixture.
#
# tag.delete()
@pytest.fixture(scope="class")
def make_user(
gl, project
) -> Generator[Callable[[AccessLevel, bool], User], None, None]:
username_base = get_random_name("user")
created_users: List[User] = []
def _make_user(
level: AccessLevel = AccessLevel.DEVELOPER, add_to_project: bool = True
) -> User:
last_id = len(created_users) + 1
username = f"{username_base}_{last_id}"
user = gl.users.create(
{
"username": username,
"email": username + "@example.com",
"name": username + " Example",
"password": get_random_password(),
}
)
if add_to_project:
project.members.create({"user_id": user.id, "access_level": level.value})
created_users.append(user)
return user
yield _make_user
for user in created_users:
with allowed_codes(404):
gl.users.delete(user.id)
@pytest.fixture(scope="class")
def make_project_access_token(
project,
) -> Generator[
Callable[[Project, AccessLevel, List[str]], ProjectAccessToken], None, None
]:
token_name_base = get_random_name("user")
created_tokens: List[ProjectAccessToken] = []
def _make_project_access_token(
target_project=project,
level: AccessLevel = AccessLevel.DEVELOPER,
scopes: List[str] = ["api"],
) -> ProjectAccessToken:
last_id = len(created_tokens) + 1
token_name = f"{token_name_base}_{last_id}_bot"
expires_at = (date.today() + timedelta(days=30)).isoformat()
token = target_project.access_tokens.create(
{
"access_level": level,
"name": token_name,
"scopes": scopes,
"expires_at": expires_at,
}
)
created_tokens.append(token)
return token
yield _make_project_access_token
for token in created_tokens:
with allowed_codes(404):
project.access_tokens.delete(token.id)
@pytest.fixture(scope="class")
def make_group_access_token(
group,
) -> Generator[Callable[[AccessLevel, List[str]], GroupAccessToken], None, None]:
token_name_base = get_random_name("user")
created_tokens: List[GroupAccessToken] = []
def _make_group_access_token(
level: AccessLevel = AccessLevel.DEVELOPER, scopes: List[str] = ["api"]
) -> GroupAccessToken:
last_id = len(created_tokens) + 1
token_name = f"{token_name_base}_{last_id}_bot"
expires_at = (date.today() + timedelta(days=30)).isoformat()
token = group.access_tokens.create(
{
"access_level": level,
"name": token_name,
"scopes": scopes,
"expires_at": expires_at,
}
)
created_tokens.append(token)
return token
yield _make_group_access_token
for token in created_tokens:
with allowed_codes(404):
group.access_tokens.delete(token.id)
@pytest.fixture(scope="class")
def three_members(make_user):
member1 = make_user()
member2 = make_user()
member3 = make_user()
yield [member1.name, member2.name, member3.name]
@pytest.fixture(scope="function")
def outsider_user(gl):
username = get_random_name("outsider_user")
user = gl.users.create(
{
"username": username,
"email": username + "@example.com",
"name": username + " Example",
"password": get_random_password(),
}
)
yield user
gl.users.delete(user.id)
@pytest.fixture(scope="function")
def public_ssh_key():
key = rsa.generate_private_key(
backend=crypto_default_backend(), public_exponent=65537, key_size=2048
)
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH, crypto_serialization.PublicFormat.OpenSSH
)
yield public_key.decode("UTF-8")
@pytest.fixture(scope="function")
def other_public_ssh_key():
# TODO: deduplicate this - it's a copy and paste from the above fixture
key = rsa.generate_private_key(
backend=crypto_default_backend(), public_exponent=65537, key_size=2048
)
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH, crypto_serialization.PublicFormat.OpenSSH
)
yield public_key.decode("UTF-8")
@pytest.fixture(scope="class")
def token_from_env_var():
token = os.environ["GITLAB_TOKEN"]
del os.environ["GITLAB_TOKEN"]
yield token
os.environ["GITLAB_TOKEN"] = token