|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ee/api/project_packages_protection_rules.html |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | + |
| 8 | +from gitlab.v4.objects import ProjectPackageProtectionRule |
| 9 | + |
| 10 | +protected_package_content = { |
| 11 | + "id": 1, |
| 12 | + "project_id": 7, |
| 13 | + "package_name_pattern": "v*", |
| 14 | + "package_type": "npm", |
| 15 | + "minimum_access_level_for_push": "maintainer", |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def resp_list_protected_packages(): |
| 21 | + with responses.RequestsMock() as rsps: |
| 22 | + rsps.add( |
| 23 | + method=responses.GET, |
| 24 | + url="http://localhost/api/v4/projects/1/packages/protection/rules", |
| 25 | + json=[protected_package_content], |
| 26 | + content_type="application/json", |
| 27 | + status=200, |
| 28 | + ) |
| 29 | + yield rsps |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def resp_create_protected_package(): |
| 34 | + with responses.RequestsMock() as rsps: |
| 35 | + rsps.add( |
| 36 | + method=responses.POST, |
| 37 | + url="http://localhost/api/v4/projects/1/packages/protection/rules", |
| 38 | + json=protected_package_content, |
| 39 | + content_type="application/json", |
| 40 | + status=201, |
| 41 | + ) |
| 42 | + yield rsps |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def resp_update_protected_package(): |
| 47 | + updated_content = protected_package_content.copy() |
| 48 | + updated_content["package_name_pattern"] = "abc*" |
| 49 | + |
| 50 | + with responses.RequestsMock() as rsps: |
| 51 | + rsps.add( |
| 52 | + method=responses.PATCH, |
| 53 | + url="http://localhost/api/v4/projects/1/packages/protection/rules/1", |
| 54 | + json=updated_content, |
| 55 | + content_type="application/json", |
| 56 | + status=200, |
| 57 | + ) |
| 58 | + yield rsps |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def resp_delete_protected_package(): |
| 63 | + with responses.RequestsMock() as rsps: |
| 64 | + rsps.add( |
| 65 | + method=responses.DELETE, |
| 66 | + url="http://localhost/api/v4/projects/1/packages/protection/rules/1", |
| 67 | + status=204, |
| 68 | + ) |
| 69 | + yield rsps |
| 70 | + |
| 71 | + |
| 72 | +def test_list_project_protected_packages(project, resp_list_protected_packages): |
| 73 | + protected_package = project.package_protection_rules.list()[0] |
| 74 | + assert isinstance(protected_package, ProjectPackageProtectionRule) |
| 75 | + assert protected_package.package_type == "npm" |
| 76 | + |
| 77 | + |
| 78 | +def test_create_project_protected_package(project, resp_create_protected_package): |
| 79 | + protected_package = project.package_protection_rules.create( |
| 80 | + { |
| 81 | + "package_name_pattern": "v*", |
| 82 | + "package_type": "npm", |
| 83 | + "minimum_access_level_for_push": "maintainer", |
| 84 | + } |
| 85 | + ) |
| 86 | + assert isinstance(protected_package, ProjectPackageProtectionRule) |
| 87 | + assert protected_package.package_type == "npm" |
| 88 | + |
| 89 | + |
| 90 | +def test_update_project_protected_package(project, resp_update_protected_package): |
| 91 | + updated = project.package_protection_rules.update( |
| 92 | + 1, {"package_name_pattern": "abc*"} |
| 93 | + ) |
| 94 | + assert updated["package_name_pattern"] == "abc*" |
| 95 | + |
| 96 | + |
| 97 | +def test_delete_project_protected_package(project, resp_delete_protected_package): |
| 98 | + project.package_protection_rules.delete(1) |
0 commit comments