|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ee/api/project_badges.html |
| 3 | +GitLab API: https://docs.gitlab.com/ee/api/group_badges.html |
| 4 | +""" |
| 5 | +import re |
| 6 | + |
| 7 | +import pytest |
| 8 | +import responses |
| 9 | + |
| 10 | +from gitlab.v4.objects import ProjectBadge, GroupBadge |
| 11 | + |
| 12 | +link_url = ( |
| 13 | + "http://example.com/ci_status.svg?project=example-org/example-project&ref=master" |
| 14 | +) |
| 15 | +image_url = "https://example.io/my/badge" |
| 16 | + |
| 17 | +rendered_link_url = ( |
| 18 | + "http://example.com/ci_status.svg?project=example-org/example-project&ref=master" |
| 19 | +) |
| 20 | +rendered_image_url = "https://example.io/my/badge" |
| 21 | + |
| 22 | +new_badge = { |
| 23 | + "link_url": link_url, |
| 24 | + "image_url": image_url, |
| 25 | +} |
| 26 | + |
| 27 | +badge_content = { |
| 28 | + "name": "Coverage", |
| 29 | + "id": 1, |
| 30 | + "link_url": link_url, |
| 31 | + "image_url": image_url, |
| 32 | + "rendered_link_url": rendered_image_url, |
| 33 | + "rendered_image_url": rendered_image_url, |
| 34 | +} |
| 35 | + |
| 36 | +preview_badge_content = { |
| 37 | + "link_url": link_url, |
| 38 | + "image_url": image_url, |
| 39 | + "rendered_link_url": rendered_link_url, |
| 40 | + "rendered_image_url": rendered_image_url, |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture() |
| 45 | +def resp_get_badge(): |
| 46 | + with responses.RequestsMock() as rsps: |
| 47 | + rsps.add( |
| 48 | + method=responses.GET, |
| 49 | + url=re.compile(r"http://localhost/api/v4/(projects|groups)/1/badges/1"), |
| 50 | + json=badge_content, |
| 51 | + content_type="application/json", |
| 52 | + status=200, |
| 53 | + ) |
| 54 | + yield rsps |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture() |
| 58 | +def resp_list_badges(): |
| 59 | + with responses.RequestsMock() as rsps: |
| 60 | + rsps.add( |
| 61 | + method=responses.GET, |
| 62 | + url=re.compile(r"http://localhost/api/v4/(projects|groups)/1/badges"), |
| 63 | + json=[badge_content], |
| 64 | + content_type="application/json", |
| 65 | + status=200, |
| 66 | + ) |
| 67 | + yield rsps |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture() |
| 71 | +def resp_create_badge(): |
| 72 | + with responses.RequestsMock() as rsps: |
| 73 | + rsps.add( |
| 74 | + method=responses.POST, |
| 75 | + url=re.compile(r"http://localhost/api/v4/(projects|groups)/1/badges"), |
| 76 | + json=badge_content, |
| 77 | + content_type="application/json", |
| 78 | + status=200, |
| 79 | + ) |
| 80 | + yield rsps |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture() |
| 84 | +def resp_update_badge(): |
| 85 | + updated_content = dict(badge_content) |
| 86 | + updated_content["link_url"] = "http://link_url" |
| 87 | + |
| 88 | + with responses.RequestsMock() as rsps: |
| 89 | + rsps.add( |
| 90 | + method=responses.PUT, |
| 91 | + url=re.compile(r"http://localhost/api/v4/(projects|groups)/1/badges/1"), |
| 92 | + json=updated_content, |
| 93 | + content_type="application/json", |
| 94 | + status=200, |
| 95 | + ) |
| 96 | + yield rsps |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture() |
| 100 | +def resp_delete_badge(no_content): |
| 101 | + with responses.RequestsMock() as rsps: |
| 102 | + rsps.add( |
| 103 | + method=responses.DELETE, |
| 104 | + url=re.compile(r"http://localhost/api/v4/(projects|groups)/1/badges/1"), |
| 105 | + json=no_content, |
| 106 | + content_type="application/json", |
| 107 | + status=204, |
| 108 | + ) |
| 109 | + yield rsps |
| 110 | + |
| 111 | + |
| 112 | +@pytest.fixture() |
| 113 | +def resp_preview_badge(): |
| 114 | + with responses.RequestsMock() as rsps: |
| 115 | + rsps.add( |
| 116 | + method=responses.GET, |
| 117 | + url=re.compile( |
| 118 | + r"http://localhost/api/v4/(projects|groups)/1/badges/render" |
| 119 | + ), |
| 120 | + json=preview_badge_content, |
| 121 | + content_type="application/json", |
| 122 | + status=200, |
| 123 | + ) |
| 124 | + yield rsps |
| 125 | + |
| 126 | + |
| 127 | +def test_list_project_badges(project, resp_list_badges): |
| 128 | + badges = project.badges.list() |
| 129 | + assert isinstance(badges, list) |
| 130 | + assert isinstance(badges[0], ProjectBadge) |
| 131 | + |
| 132 | + |
| 133 | +def test_list_group_badges(group, resp_list_badges): |
| 134 | + badges = group.badges.list() |
| 135 | + assert isinstance(badges, list) |
| 136 | + assert isinstance(badges[0], GroupBadge) |
| 137 | + |
| 138 | + |
| 139 | +def test_get_project_badge(project, resp_get_badge): |
| 140 | + badge = project.badges.get(1) |
| 141 | + assert isinstance(badge, ProjectBadge) |
| 142 | + assert badge.name == "Coverage" |
| 143 | + assert badge.id == 1 |
| 144 | + |
| 145 | + |
| 146 | +def test_get_group_badge(group, resp_get_badge): |
| 147 | + badge = group.badges.get(1) |
| 148 | + assert isinstance(badge, GroupBadge) |
| 149 | + assert badge.name == "Coverage" |
| 150 | + assert badge.id == 1 |
| 151 | + |
| 152 | + |
| 153 | +def test_delete_project_badge(project, resp_delete_badge): |
| 154 | + badge = project.badges.get(1, lazy=True) |
| 155 | + badge.delete() |
| 156 | + |
| 157 | + |
| 158 | +def test_delete_group_badge(group, resp_delete_badge): |
| 159 | + badge = group.badges.get(1, lazy=True) |
| 160 | + badge.delete() |
| 161 | + |
| 162 | + |
| 163 | +def test_create_project_badge(project, resp_create_badge): |
| 164 | + badge = project.badges.create(new_badge) |
| 165 | + assert isinstance(badge, ProjectBadge) |
| 166 | + assert badge.image_url == image_url |
| 167 | + |
| 168 | + |
| 169 | +def test_create_group_badge(group, resp_create_badge): |
| 170 | + badge = group.badges.create(new_badge) |
| 171 | + assert isinstance(badge, GroupBadge) |
| 172 | + assert badge.image_url == image_url |
| 173 | + |
| 174 | + |
| 175 | +def test_preview_project_badge(project, resp_preview_badge): |
| 176 | + output = project.badges.render( |
| 177 | + link_url=link_url, |
| 178 | + image_url=image_url, |
| 179 | + ) |
| 180 | + assert isinstance(output, dict) |
| 181 | + assert "rendered_link_url" in output |
| 182 | + assert "rendered_image_url" in output |
| 183 | + assert output["link_url"] == output["rendered_link_url"] |
| 184 | + assert output["image_url"] == output["rendered_image_url"] |
| 185 | + |
| 186 | + |
| 187 | +def test_preview_group_badge(group, resp_preview_badge): |
| 188 | + output = group.badges.render( |
| 189 | + link_url=link_url, |
| 190 | + image_url=image_url, |
| 191 | + ) |
| 192 | + assert isinstance(output, dict) |
| 193 | + assert "rendered_link_url" in output |
| 194 | + assert "rendered_image_url" in output |
| 195 | + assert output["link_url"] == output["rendered_link_url"] |
| 196 | + assert output["image_url"] == output["rendered_image_url"] |
| 197 | + |
| 198 | + |
| 199 | +def test_update_project_badge(project, resp_update_badge): |
| 200 | + badge = project.badges.get(1, lazy=True) |
| 201 | + badge.link_url = "http://link_url" |
| 202 | + badge.save() |
| 203 | + assert badge.link_url == "http://link_url" |
| 204 | + |
| 205 | + |
| 206 | +def test_update_group_badge(group, resp_update_badge): |
| 207 | + badge = group.badges.get(1, lazy=True) |
| 208 | + badge.link_url = "http://link_url" |
| 209 | + badge.save() |
| 210 | + assert badge.link_url == "http://link_url" |
0 commit comments