|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ee/api/project_container_registry_protection_rules.html |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | + |
| 8 | +from gitlab.v4.objects import ProjectRegistryProtectionRule |
| 9 | + |
| 10 | +protected_registry_content = { |
| 11 | + "id": 1, |
| 12 | + "project_id": 7, |
| 13 | + "repository_path_pattern": "test/image", |
| 14 | + "minimum_access_level_for_push": "maintainer", |
| 15 | + "minimum_access_level_for_delete": "maintainer", |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def resp_list_protected_registries(): |
| 21 | + with responses.RequestsMock() as rsps: |
| 22 | + rsps.add( |
| 23 | + method=responses.GET, |
| 24 | + url="http://localhost/api/v4/projects/1/registry/protection/rules", |
| 25 | + json=[protected_registry_content], |
| 26 | + content_type="application/json", |
| 27 | + status=200, |
| 28 | + ) |
| 29 | + yield rsps |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def resp_create_protected_registry(): |
| 34 | + with responses.RequestsMock() as rsps: |
| 35 | + rsps.add( |
| 36 | + method=responses.POST, |
| 37 | + url="http://localhost/api/v4/projects/1/registry/protection/rules", |
| 38 | + json=protected_registry_content, |
| 39 | + content_type="application/json", |
| 40 | + status=201, |
| 41 | + ) |
| 42 | + yield rsps |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def resp_update_protected_registry(): |
| 47 | + updated_content = protected_registry_content.copy() |
| 48 | + updated_content["repository_path_pattern"] = "abc*" |
| 49 | + |
| 50 | + with responses.RequestsMock() as rsps: |
| 51 | + rsps.add( |
| 52 | + method=responses.PATCH, |
| 53 | + url="http://localhost/api/v4/projects/1/registry/protection/rules/1", |
| 54 | + json=updated_content, |
| 55 | + content_type="application/json", |
| 56 | + status=200, |
| 57 | + ) |
| 58 | + yield rsps |
| 59 | + |
| 60 | + |
| 61 | +def test_list_project_protected_registries(project, resp_list_protected_registries): |
| 62 | + protected_registry = project.registry_protection_rules.list()[0] |
| 63 | + assert isinstance(protected_registry, ProjectRegistryProtectionRule) |
| 64 | + assert protected_registry.repository_path_pattern == "test/image" |
| 65 | + |
| 66 | + |
| 67 | +def test_create_project_protected_registry(project, resp_create_protected_registry): |
| 68 | + protected_registry = project.registry_protection_rules.create( |
| 69 | + { |
| 70 | + "repository_path_pattern": "test/image", |
| 71 | + "minimum_access_level_for_push": "maintainer", |
| 72 | + } |
| 73 | + ) |
| 74 | + assert isinstance(protected_registry, ProjectRegistryProtectionRule) |
| 75 | + assert protected_registry.repository_path_pattern == "test/image" |
| 76 | + |
| 77 | + |
| 78 | +def test_update_project_protected_registry(project, resp_update_protected_registry): |
| 79 | + updated = project.registry_protection_rules.update( |
| 80 | + 1, {"repository_path_pattern": "abc*"} |
| 81 | + ) |
| 82 | + assert updated["repository_path_pattern"] == "abc*" |
0 commit comments