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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
"""
GitLab API: https://docs.gitlab.com/ce/api/groups.html
"""
import re
import pytest
import responses
import gitlab
from gitlab.v4.objects import (
GroupDescendantGroup,
GroupLDAPGroupLink,
GroupSAMLGroupLink,
GroupSubgroup,
)
from gitlab.v4.objects.projects import GroupProject, SharedProject
content = {"name": "name", "id": 1, "path": "path"}
ldap_group_links_content = [
{
"cn": None,
"group_access": 40,
"provider": "ldapmain",
"filter": "(memberOf=cn=some_group,ou=groups,ou=fake_ou,dc=sub_dc,dc=example,dc=tld)",
}
]
saml_group_links_content = [{"name": "saml-group-1", "access_level": 10}]
create_saml_group_link_request_body = {
"saml_group_name": "saml-group-1",
"access_level": 10,
}
projects_content = [
{
"id": 9,
"description": "foo",
"default_branch": "master",
"name": "Html5 Boilerplate",
"name_with_namespace": "Experimental / Html5 Boilerplate",
"path": "html5-boilerplate",
"path_with_namespace": "h5bp/html5-boilerplate",
"namespace": {"id": 5, "name": "Experimental", "path": "h5bp", "kind": "group"},
}
]
subgroup_descgroup_content = [
{
"id": 2,
"name": "Bar Group",
"path": "foo/bar",
"description": "A subgroup of Foo Group",
"visibility": "public",
"share_with_group_lock": False,
"require_two_factor_authentication": False,
"two_factor_grace_period": 48,
"project_creation_level": "developer",
"auto_devops_enabled": None,
"subgroup_creation_level": "owner",
"emails_disabled": None,
"mentions_disabled": None,
"lfs_enabled": True,
"default_branch_protection": 2,
"avatar_url": "http://gitlab.example.com/uploads/group/avatar/1/bar.jpg",
"web_url": "http://gitlab.example.com/groups/foo/bar",
"request_access_enabled": False,
"full_name": "Bar Group",
"full_path": "foo/bar",
"file_template_project_id": 1,
"parent_id": 123,
"created_at": "2020-01-15T12:36:29.590Z",
},
]
push_rules_content = {
"id": 2,
"created_at": "2020-08-17T19:09:19.580Z",
"commit_message_regex": "[a-zA-Z]",
"commit_message_negative_regex": "[x+]",
"branch_name_regex": "[a-z]",
"deny_delete_tag": True,
"member_check": True,
"prevent_secrets": True,
"author_email_regex": "^[A-Za-z0-9.]+@gitlab.com$",
"file_name_regex": "(exe)$",
"max_file_size": 100,
}
@pytest.fixture
def resp_groups():
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups/1",
json=content,
content_type="application/json",
status=200,
)
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups",
json=[content],
content_type="application/json",
status=200,
)
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/groups",
json=content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_list_group_projects():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url=re.compile(r"http://localhost/api/v4/groups/1/projects(/shared)?"),
json=projects_content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_list_subgroups_descendant_groups():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url=re.compile(
r"http://localhost/api/v4/groups/1/(subgroups|descendant_groups)"
),
json=subgroup_descgroup_content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_create_import(accepted_content):
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/groups/import",
json=accepted_content,
content_type="application/json",
status=202,
)
yield rsps
@pytest.fixture
def resp_transfer_group():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/groups/1/transfer",
json=content,
content_type="application/json",
status=200,
match=[
responses.matchers.json_params_matcher({"group_id": "test-namespace"})
],
)
yield rsps
@pytest.fixture
def resp_list_push_rules_group():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups/1/push_rule",
json=push_rules_content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_create_push_rules_group():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/groups/1/push_rule",
json=push_rules_content,
content_type="application/json",
status=201,
)
yield rsps
@pytest.fixture
def resp_update_push_rules_group():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups/1/push_rule",
json=push_rules_content,
content_type="application/json",
status=200,
)
rsps.add(
method=responses.PUT,
url="http://localhost/api/v4/groups/1/push_rule",
json=push_rules_content,
content_type="application/json",
status=201,
)
yield rsps
@pytest.fixture
def resp_delete_push_rules_group(no_content):
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups/1/push_rule",
json=push_rules_content,
content_type="application/json",
status=200,
)
rsps.add(
method=responses.DELETE,
url="http://localhost/api/v4/groups/1/push_rule",
json=no_content,
content_type="application/json",
status=204,
)
yield rsps
@pytest.fixture
def resp_list_ldap_group_links(no_content):
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups/1/ldap_group_links",
json=ldap_group_links_content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_list_saml_group_links():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups/1/saml_group_links",
json=saml_group_links_content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_get_saml_group_link():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/groups/1/saml_group_links/saml-group-1",
json=saml_group_links_content[0],
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_create_saml_group_link():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/groups/1/saml_group_links",
match=[
responses.matchers.json_params_matcher(
create_saml_group_link_request_body
)
],
json=saml_group_links_content[0],
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_delete_saml_group_link(no_content):
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/groups/1/saml_group_links",
match=[
responses.matchers.json_params_matcher(
create_saml_group_link_request_body
)
],
json=saml_group_links_content[0],
content_type="application/json",
status=200,
)
rsps.add(
method=responses.DELETE,
url="http://localhost/api/v4/groups/1/saml_group_links/saml-group-1",
json=no_content,
content_type="application/json",
status=204,
)
yield rsps
def test_get_group(gl, resp_groups):
data = gl.groups.get(1)
assert isinstance(data, gitlab.v4.objects.Group)
assert data.name == "name"
assert data.path == "path"
assert data.id == 1
def test_create_group(gl, resp_groups):
name, path = "name", "path"
data = gl.groups.create({"name": name, "path": path})
assert isinstance(data, gitlab.v4.objects.Group)
assert data.name == name
assert data.path == path
def test_create_group_export(group, resp_export):
export = group.exports.create()
assert export.message == "202 Accepted"
def test_list_group_projects(group, resp_list_group_projects):
projects = group.projects.list()
assert isinstance(projects[0], GroupProject)
assert projects[0].path == projects_content[0]["path"]
def test_list_group_shared_projects(group, resp_list_group_projects):
projects = group.shared_projects.list()
assert isinstance(projects[0], SharedProject)
assert projects[0].path == projects_content[0]["path"]
def test_list_group_subgroups(group, resp_list_subgroups_descendant_groups):
subgroups = group.subgroups.list()
assert isinstance(subgroups[0], GroupSubgroup)
assert subgroups[0].path == subgroup_descgroup_content[0]["path"]
def test_list_group_descendant_groups(group, resp_list_subgroups_descendant_groups):
descendant_groups = group.descendant_groups.list()
assert isinstance(descendant_groups[0], GroupDescendantGroup)
assert descendant_groups[0].path == subgroup_descgroup_content[0]["path"]
def test_list_ldap_group_links(group, resp_list_ldap_group_links):
ldap_group_links = group.ldap_group_links.list()
assert isinstance(ldap_group_links[0], GroupLDAPGroupLink)
assert ldap_group_links[0].provider == ldap_group_links_content[0]["provider"]
@pytest.mark.skip("GitLab API endpoint not implemented")
def test_refresh_group_export_status(group, resp_export):
export = group.exports.create()
export.refresh()
assert export.export_status == "finished"
def test_download_group_export(group, resp_export, binary_content):
export = group.exports.create()
download = export.download()
assert isinstance(download, bytes)
assert download == binary_content
def test_import_group(gl, resp_create_import):
group_import = gl.groups.import_group("file", "api-group", "API Group")
assert group_import["message"] == "202 Accepted"
@pytest.mark.skip("GitLab API endpoint not implemented")
def test_refresh_group_import_status(group, resp_groups):
group_import = group.imports.get()
group_import.refresh()
assert group_import.import_status == "finished"
def test_transfer_group(gl, resp_transfer_group):
group = gl.groups.get(1, lazy=True)
group.transfer("test-namespace")
def test_list_group_push_rules(group, resp_list_push_rules_group):
pr = group.pushrules.get()
assert pr
assert pr.deny_delete_tag
def test_create_group_push_rule(group, resp_create_push_rules_group):
group.pushrules.create({"deny_delete_tag": True})
def test_update_group_push_rule(
group,
resp_update_push_rules_group,
):
pr = group.pushrules.get()
pr.deny_delete_tag = False
pr.save()
def test_delete_group_push_rule(group, resp_delete_push_rules_group):
pr = group.pushrules.get()
pr.delete()
def test_list_saml_group_links(group, resp_list_saml_group_links):
saml_group_links = group.saml_group_links.list()
assert isinstance(saml_group_links[0], GroupSAMLGroupLink)
assert saml_group_links[0].name == saml_group_links_content[0]["name"]
assert (
saml_group_links[0].access_level == saml_group_links_content[0]["access_level"]
)
def test_get_saml_group_link(group, resp_get_saml_group_link):
saml_group_link = group.saml_group_links.get("saml-group-1")
assert isinstance(saml_group_link, GroupSAMLGroupLink)
assert saml_group_link.name == saml_group_links_content[0]["name"]
assert saml_group_link.access_level == saml_group_links_content[0]["access_level"]
def test_create_saml_group_link(group, resp_create_saml_group_link):
saml_group_link = group.saml_group_links.create(create_saml_group_link_request_body)
assert isinstance(saml_group_link, GroupSAMLGroupLink)
assert (
saml_group_link.name == create_saml_group_link_request_body["saml_group_name"]
)
assert (
saml_group_link.access_level
== create_saml_group_link_request_body["access_level"]
)
def test_delete_saml_group_link(group, resp_delete_saml_group_link):
saml_group_link = group.saml_group_links.create(create_saml_group_link_request_body)
saml_group_link.delete()
|