|
| 1 | +""" |
| 2 | +GitLab API: |
| 3 | +https://docs.gitlab.com/ee/api/releases/index.html |
| 4 | +https://docs.gitlab.com/ee/api/releases/links.html |
| 5 | +""" |
| 6 | +import re |
| 7 | + |
| 8 | +import pytest |
| 9 | +import responses |
| 10 | + |
| 11 | +from gitlab.v4.objects import ProjectReleaseLink |
| 12 | + |
| 13 | +encoded_tag_name = "v1%2E0%2E0" |
| 14 | +link_name = "hello-world" |
| 15 | +link_url = "https://gitlab.example.com/group/hello/-/jobs/688/artifacts/raw/bin/hello-darwin-amd64" |
| 16 | +direct_url = f"https://gitlab.example.com/group/hello/-/releases/{encoded_tag_name}/downloads/hello-world" |
| 17 | +new_link_type = "package" |
| 18 | +link_content = { |
| 19 | + "id": 2, |
| 20 | + "name": link_name, |
| 21 | + "url": link_url, |
| 22 | + "direct_asset_url": direct_url, |
| 23 | + "external": False, |
| 24 | + "link_type": "other", |
| 25 | +} |
| 26 | + |
| 27 | +links_url = re.compile( |
| 28 | + rf"http://localhost/api/v4/projects/1/releases/{encoded_tag_name}/assets/links" |
| 29 | +) |
| 30 | +link_id_url = re.compile( |
| 31 | + rf"http://localhost/api/v4/projects/1/releases/{encoded_tag_name}/assets/links/1" |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def resp_list_links(): |
| 37 | + with responses.RequestsMock() as rsps: |
| 38 | + rsps.add( |
| 39 | + method=responses.GET, |
| 40 | + url=links_url, |
| 41 | + json=[link_content], |
| 42 | + content_type="application/json", |
| 43 | + status=200, |
| 44 | + ) |
| 45 | + yield rsps |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def resp_get_link(): |
| 50 | + with responses.RequestsMock() as rsps: |
| 51 | + rsps.add( |
| 52 | + method=responses.GET, |
| 53 | + url=link_id_url, |
| 54 | + json=link_content, |
| 55 | + content_type="application/json", |
| 56 | + status=200, |
| 57 | + ) |
| 58 | + yield rsps |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def resp_create_link(): |
| 63 | + with responses.RequestsMock() as rsps: |
| 64 | + rsps.add( |
| 65 | + method=responses.POST, |
| 66 | + url=links_url, |
| 67 | + json=link_content, |
| 68 | + content_type="application/json", |
| 69 | + status=200, |
| 70 | + ) |
| 71 | + yield rsps |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +def resp_update_link(): |
| 76 | + updated_content = dict(link_content) |
| 77 | + updated_content["link_type"] = new_link_type |
| 78 | + |
| 79 | + with responses.RequestsMock() as rsps: |
| 80 | + rsps.add( |
| 81 | + method=responses.PUT, |
| 82 | + url=link_id_url, |
| 83 | + json=updated_content, |
| 84 | + content_type="application/json", |
| 85 | + status=200, |
| 86 | + ) |
| 87 | + yield rsps |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +def resp_delete_link(no_content): |
| 92 | + with responses.RequestsMock() as rsps: |
| 93 | + rsps.add( |
| 94 | + method=responses.DELETE, |
| 95 | + url=link_id_url, |
| 96 | + json=link_content, |
| 97 | + content_type="application/json", |
| 98 | + status=204, |
| 99 | + ) |
| 100 | + yield rsps |
| 101 | + |
| 102 | + |
| 103 | +def test_list_release_links(release, resp_list_links): |
| 104 | + links = release.links.list() |
| 105 | + assert isinstance(links, list) |
| 106 | + assert isinstance(links[0], ProjectReleaseLink) |
| 107 | + assert links[0].url == link_url |
| 108 | + |
| 109 | + |
| 110 | +def test_get_release_link(release, resp_get_link): |
| 111 | + link = release.links.get(1) |
| 112 | + assert isinstance(link, ProjectReleaseLink) |
| 113 | + assert link.url == link_url |
| 114 | + |
| 115 | + |
| 116 | +def test_create_release_link(release, resp_create_link): |
| 117 | + link = release.links.create({"url": link_url, "name": link_name}) |
| 118 | + assert isinstance(link, ProjectReleaseLink) |
| 119 | + assert link.url == link_url |
| 120 | + |
| 121 | + |
| 122 | +def test_update_release_link(release, resp_update_link): |
| 123 | + link = release.links.get(1, lazy=True) |
| 124 | + link.link_type = new_link_type |
| 125 | + link.save() |
| 126 | + assert link.link_type == new_link_type |
| 127 | + |
| 128 | + |
| 129 | +def test_delete_release_link(release, resp_delete_link): |
| 130 | + link = release.links.get(1, lazy=True) |
| 131 | + link.delete() |
0 commit comments