|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ee/api/status_checks.html |
| 3 | +""" |
| 4 | + |
| 5 | +importpytest |
| 6 | +importresponses |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +defstatus_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 | +defupdated_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 | +defresp_list_status_checks(status_check): |
| 53 | +withresponses.RequestsMock(assert_all_requests_are_fired=False)asrsps: |
| 54 | +rsps.add( |
| 55 | +method=responses.GET, |
| 56 | +url="http://localhost/api/v4/projects/1/external_status_checks", |
| 57 | +json=[status_check], |
| 58 | +content_type="application/json", |
| 59 | +status=200, |
| 60 | + ) |
| 61 | +yieldrsps |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +defresp_create_status_checks(status_check): |
| 66 | +withresponses.RequestsMock(assert_all_requests_are_fired=False)asrsps: |
| 67 | +rsps.add( |
| 68 | +method=responses.POST, |
| 69 | +url="http://localhost/api/v4/projects/1/external_status_checks", |
| 70 | +json=status_check, |
| 71 | +content_type="application/json", |
| 72 | +status=200, |
| 73 | + ) |
| 74 | +yieldrsps |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +defresp_update_status_checks(updated_status_check): |
| 79 | +withresponses.RequestsMock(assert_all_requests_are_fired=False)asrsps: |
| 80 | +rsps.add( |
| 81 | +method=responses.PUT, |
| 82 | +url="http://localhost/api/v4/groups/1/external_status_checks", |
| 83 | +json=updated_status_check, |
| 84 | +content_type="application/json", |
| 85 | +status=200, |
| 86 | + ) |
| 87 | +yieldrsps |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +defresp_delete_status_checks(): |
| 92 | +content= [] |
| 93 | + |
| 94 | +withresponses.RequestsMock(assert_all_requests_are_fired=False)asrsps: |
| 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 | +yieldrsps |
| 108 | + |
| 109 | + |
| 110 | +deftest_list_status_checks(gl,resp_list_status_checks): |
| 111 | +status_checks=gl.projects.get(1,lazy=True).status_checks.list() |
| 112 | +assertlen(status_checks)==1 |
| 113 | +assertstatus_checks[0].name=="MR blocker" |
| 114 | + |
| 115 | + |
| 116 | +deftest_create_status_checks(gl,resp_create_status_checks): |
| 117 | +access_token=gl.projects.get(1,lazy=True).status_checks.create( |
| 118 | + {"name":"MR blocker","external_url":"https://example.com/mr-blocker"} |
| 119 | + ) |
| 120 | +assertaccess_token.name=="MR blocker" |
| 121 | +assertaccess_token.external_url=="https://example.com/mr-blocker" |
| 122 | + |
| 123 | + |
| 124 | +deftest_delete_status_checks(gl,resp_delete_status_checks): |
| 125 | +gl.projects.get(1,lazy=True).status_checks.delete(1) |
| 126 | +status_checks=gl.projects.get(1,lazy=True).status_checks.list() |
| 127 | +assertlen(status_checks)==0 |