|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ee/api/status_checks.html |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def external_status_check(): |
| 11 | + return { |
| 12 | + "id": 1, |
| 13 | + "name": "MR blocker", |
| 14 | + "project_id": 1, |
| 15 | + "external_url": "https://example.com/mr-blocker", |
| 16 | + "hmac": True, |
| 17 | + "protected_branches": [ |
| 18 | + { |
| 19 | + "id": 1, |
| 20 | + "project_id": 1, |
| 21 | + "name": "main", |
| 22 | + "created_at": "2020-10-12T14:04:50.787Z", |
| 23 | + "updated_at": "2020-10-12T14:04:50.787Z", |
| 24 | + "code_owner_approval_required": False, |
| 25 | + } |
| 26 | + ], |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def updated_external_status_check(): |
| 32 | + return { |
| 33 | + "id": 1, |
| 34 | + "name": "Updated MR blocker", |
| 35 | + "project_id": 1, |
| 36 | + "external_url": "https://example.com/mr-blocker", |
| 37 | + "hmac": True, |
| 38 | + "protected_branches": [ |
| 39 | + { |
| 40 | + "id": 1, |
| 41 | + "project_id": 1, |
| 42 | + "name": "main", |
| 43 | + "created_at": "2020-10-12T14:04:50.787Z", |
| 44 | + "updated_at": "2020-10-12T14:04:50.787Z", |
| 45 | + "code_owner_approval_required": False, |
| 46 | + } |
| 47 | + ], |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def resp_list_external_status_checks(external_status_check): |
| 53 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: |
| 54 | + rsps.add( |
| 55 | + method=responses.GET, |
| 56 | + url="http://localhost/api/v4/projects/1/external_status_checks", |
| 57 | + json=[external_status_check], |
| 58 | + content_type="application/json", |
| 59 | + status=200, |
| 60 | + ) |
| 61 | + yield rsps |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def resp_create_external_status_checks(external_status_check): |
| 66 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: |
| 67 | + rsps.add( |
| 68 | + method=responses.POST, |
| 69 | + url="http://localhost/api/v4/projects/1/external_status_checks", |
| 70 | + json=external_status_check, |
| 71 | + content_type="application/json", |
| 72 | + status=200, |
| 73 | + ) |
| 74 | + yield rsps |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def resp_update_external_status_checks(updated_external_status_check): |
| 79 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: |
| 80 | + rsps.add( |
| 81 | + method=responses.PUT, |
| 82 | + url="http://localhost/api/v4/groups/1/external_status_checks", |
| 83 | + json=updated_external_status_check, |
| 84 | + content_type="application/json", |
| 85 | + status=200, |
| 86 | + ) |
| 87 | + yield rsps |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +def resp_delete_external_status_checks(): |
| 92 | + content = [] |
| 93 | + |
| 94 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: |
| 95 | + rsps.add( |
| 96 | + method=responses.DELETE, |
| 97 | + url="http://localhost/api/v4/projects/1/external_status_checks/1", |
| 98 | + status=204, |
| 99 | + ) |
| 100 | + rsps.add( |
| 101 | + method=responses.GET, |
| 102 | + url="http://localhost/api/v4/projects/1/external_status_checks", |
| 103 | + json=content, |
| 104 | + content_type="application/json", |
| 105 | + status=200, |
| 106 | + ) |
| 107 | + yield rsps |
| 108 | + |
| 109 | + |
| 110 | +def test_list_external_status_checks(gl, resp_list_external_status_checks): |
| 111 | + status_checks = gl.projects.get(1, lazy=True).external_status_checks.list() |
| 112 | + assert len(status_checks) == 1 |
| 113 | + assert status_checks[0].name == "MR blocker" |
| 114 | + |
| 115 | + |
| 116 | +def test_create_external_status_checks(gl, resp_create_external_status_checks): |
| 117 | + access_token = gl.projects.get(1, lazy=True).external_status_checks.create( |
| 118 | + {"name": "MR blocker", "external_url": "https://example.com/mr-blocker"} |
| 119 | + ) |
| 120 | + assert access_token.name == "MR blocker" |
| 121 | + assert access_token.external_url == "https://example.com/mr-blocker" |
| 122 | + |
| 123 | + |
| 124 | +def test_delete_external_status_checks(gl, resp_delete_external_status_checks): |
| 125 | + gl.projects.get(1, lazy=True).external_status_checks.delete(1) |
| 126 | + status_checks = gl.projects.get(1, lazy=True).external_status_checks.list() |
| 127 | + assert len(status_checks) == 0 |
0 commit comments