|
| 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 | +importre |
| 7 | + |
| 8 | +importpytest |
| 9 | +importresponses |
| 10 | + |
| 11 | +fromgitlab.v4.objectsimportProjectReleaseLink |
| 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 | +defresp_list_links(): |
| 37 | +withresponses.RequestsMock()asrsps: |
| 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 | +yieldrsps |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +defresp_get_link(): |
| 50 | +withresponses.RequestsMock()asrsps: |
| 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 | +yieldrsps |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +defresp_create_link(): |
| 63 | +withresponses.RequestsMock()asrsps: |
| 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 | +yieldrsps |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +defresp_update_link(): |
| 76 | +updated_content=dict(link_content) |
| 77 | +updated_content["link_type"]=new_link_type |
| 78 | + |
| 79 | +withresponses.RequestsMock()asrsps: |
| 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 | +yieldrsps |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +defresp_delete_link(no_content): |
| 92 | +withresponses.RequestsMock()asrsps: |
| 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 | +yieldrsps |
| 101 | + |
| 102 | + |
| 103 | +deftest_list_release_links(release,resp_list_links): |
| 104 | +links=release.links.list() |
| 105 | +assertisinstance(links,list) |
| 106 | +assertisinstance(links[0],ProjectReleaseLink) |
| 107 | +assertlinks[0].url==link_url |
| 108 | + |
| 109 | + |
| 110 | +deftest_get_release_link(release,resp_get_link): |
| 111 | +link=release.links.get(1) |
| 112 | +assertisinstance(link,ProjectReleaseLink) |
| 113 | +assertlink.url==link_url |
| 114 | + |
| 115 | + |
| 116 | +deftest_create_release_link(release,resp_create_link): |
| 117 | +link=release.links.create({"url":link_url,"name":link_name}) |
| 118 | +assertisinstance(link,ProjectReleaseLink) |
| 119 | +assertlink.url==link_url |
| 120 | + |
| 121 | + |
| 122 | +deftest_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 | +assertlink.link_type==new_link_type |
| 127 | + |
| 128 | + |
| 129 | +deftest_delete_release_link(release,resp_delete_link): |
| 130 | +link=release.links.get(1,lazy=True) |
| 131 | +link.delete() |