|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ce/api/packages.html |
| 3 | +""" |
| 4 | +import re |
| 5 | + |
| 6 | +import pytest |
| 7 | +import responses |
| 8 | + |
| 9 | +from gitlab.v4.objects import GroupPackage, ProjectPackage |
| 10 | + |
| 11 | + |
| 12 | +package_content = { |
| 13 | + "id": 1, |
| 14 | + "name": "com/mycompany/my-app", |
| 15 | + "version": "1.0-SNAPSHOT", |
| 16 | + "package_type": "maven", |
| 17 | + "_links": { |
| 18 | + "web_path": "/namespace1/project1/-/packages/1", |
| 19 | + "delete_api_path": "/namespace1/project1/-/packages/1", |
| 20 | + }, |
| 21 | + "created_at": "2019-11-27T03:37:38.711Z", |
| 22 | + "pipeline": { |
| 23 | + "id": 123, |
| 24 | + "status": "pending", |
| 25 | + "ref": "new-pipeline", |
| 26 | + "sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a", |
| 27 | + "web_url": "https://example.com/foo/bar/pipelines/47", |
| 28 | + "created_at": "2016-08-11T11:28:34.085Z", |
| 29 | + "updated_at": "2016-08-11T11:32:35.169Z", |
| 30 | + "user": { |
| 31 | + "name": "Administrator", |
| 32 | + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", |
| 33 | + }, |
| 34 | + }, |
| 35 | + "versions": [ |
| 36 | + { |
| 37 | + "id": 2, |
| 38 | + "version": "2.0-SNAPSHOT", |
| 39 | + "created_at": "2020-04-28T04:42:11.573Z", |
| 40 | + "pipeline": { |
| 41 | + "id": 234, |
| 42 | + "status": "pending", |
| 43 | + "ref": "new-pipeline", |
| 44 | + "sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a", |
| 45 | + "web_url": "https://example.com/foo/bar/pipelines/58", |
| 46 | + "created_at": "2016-08-11T11:28:34.085Z", |
| 47 | + "updated_at": "2016-08-11T11:32:35.169Z", |
| 48 | + "user": { |
| 49 | + "name": "Administrator", |
| 50 | + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + ], |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def resp_list_packages(): |
| 60 | + with responses.RequestsMock() as rsps: |
| 61 | + rsps.add( |
| 62 | + method=responses.GET, |
| 63 | + url=re.compile(r"http://localhost/api/v4/(groups|projects)/1/packages"), |
| 64 | + json=[package_content], |
| 65 | + content_type="application/json", |
| 66 | + status=200, |
| 67 | + ) |
| 68 | + yield rsps |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture |
| 72 | +def resp_get_package(): |
| 73 | + with responses.RequestsMock() as rsps: |
| 74 | + rsps.add( |
| 75 | + method=responses.GET, |
| 76 | + url="http://localhost/api/v4/projects/1/packages/1", |
| 77 | + json=package_content, |
| 78 | + content_type="application/json", |
| 79 | + status=200, |
| 80 | + ) |
| 81 | + yield rsps |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture |
| 85 | +def resp_delete_package(no_content): |
| 86 | + with responses.RequestsMock() as rsps: |
| 87 | + rsps.add( |
| 88 | + method=responses.DELETE, |
| 89 | + url="http://localhost/api/v4/projects/1/packages/1", |
| 90 | + json=no_content, |
| 91 | + content_type="application/json", |
| 92 | + status=204, |
| 93 | + ) |
| 94 | + yield rsps |
| 95 | + |
| 96 | + |
| 97 | +def test_list_project_packages(project, resp_list_packages): |
| 98 | + packages = project.packages.list() |
| 99 | + assert isinstance(packages, list) |
| 100 | + assert isinstance(packages[0], ProjectPackage) |
| 101 | + assert packages[0].version == "1.0-SNAPSHOT" |
| 102 | + |
| 103 | + |
| 104 | +def test_list_group_packages(group, resp_list_packages): |
| 105 | + packages = group.packages.list() |
| 106 | + assert isinstance(packages, list) |
| 107 | + assert isinstance(packages[0], GroupPackage) |
| 108 | + assert packages[0].version == "1.0-SNAPSHOT" |
| 109 | + |
| 110 | + |
| 111 | +def test_get_project_package(project, resp_get_package): |
| 112 | + package = project.packages.get(1) |
| 113 | + assert isinstance(package, ProjectPackage) |
| 114 | + assert package.version == "1.0-SNAPSHOT" |
| 115 | + |
| 116 | + |
| 117 | +def test_delete_project_package(project, resp_delete_package): |
| 118 | + package = project.packages.get(1, lazy=True) |
| 119 | + package.delete() |
0 commit comments