|
1 | 1 | """ |
2 | 2 | GitLab API: https://docs.gitlab.com/ce/api/system_hooks.html |
| 3 | +GitLab API: https://docs.gitlab.com/ce/api/groups.html#hooks |
| 4 | +GitLab API: https://docs.gitlab.com/ee/api/projects.html#hooks |
3 | 5 | """ |
| 6 | + |
| 7 | +import re |
| 8 | + |
4 | 9 | import pytest |
5 | 10 | import responses |
6 | 11 |
|
7 | | -from gitlab.v4.objects import Hook |
| 12 | +from gitlab.v4.objects import GroupHook, Hook, ProjectHook |
| 13 | + |
| 14 | +hooks_content = [ |
| 15 | + { |
| 16 | + "id": 1, |
| 17 | + "url": "testurl", |
| 18 | + "push_events": True, |
| 19 | + "tag_push_events": True, |
| 20 | + }, |
| 21 | + { |
| 22 | + "id": 2, |
| 23 | + "url": "testurl_second", |
| 24 | + "push_events": False, |
| 25 | + "tag_push_events": False, |
| 26 | + }, |
| 27 | +] |
| 28 | + |
| 29 | +hook_content = hooks_content[0] |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def resp_hooks_list(): |
| 34 | + with responses.RequestsMock() as rsps: |
| 35 | + rsps.add( |
| 36 | + method=responses.GET, |
| 37 | + url=re.compile(r"http://localhost/api/v4/((groups|projects)/1/|)hooks"), |
| 38 | + json=hooks_content, |
| 39 | + content_type="application/json", |
| 40 | + status=200, |
| 41 | + ) |
| 42 | + yield rsps |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def resp_hook_get(): |
| 47 | + with responses.RequestsMock() as rsps: |
| 48 | + rsps.add( |
| 49 | + method=responses.GET, |
| 50 | + url=re.compile(r"http://localhost/api/v4/((groups|projects)/1/|)hooks/1"), |
| 51 | + json=hook_content, |
| 52 | + content_type="application/json", |
| 53 | + status=200, |
| 54 | + ) |
| 55 | + yield rsps |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def resp_hook_create(): |
| 60 | + with responses.RequestsMock() as rsps: |
| 61 | + rsps.add( |
| 62 | + method=responses.POST, |
| 63 | + url=re.compile(r"http://localhost/api/v4/((groups|projects)/1/|)hooks"), |
| 64 | + json=hook_content, |
| 65 | + content_type="application/json", |
| 66 | + status=200, |
| 67 | + ) |
| 68 | + yield rsps |
8 | 69 |
|
9 | 70 |
|
10 | 71 | @pytest.fixture |
11 | | -def resp_get_hook(): |
12 | | - content = {"url": "testurl", "id": 1} |
| 72 | +def resp_hook_update(): |
| 73 | + with responses.RequestsMock() as rsps: |
| 74 | + pattern = re.compile(r"http://localhost/api/v4/((groups|projects)/1/|)hooks/1") |
| 75 | + rsps.add( |
| 76 | + method=responses.GET, |
| 77 | + url=pattern, |
| 78 | + json=hook_content, |
| 79 | + content_type="application/json", |
| 80 | + status=200, |
| 81 | + ) |
| 82 | + rsps.add( |
| 83 | + method=responses.PUT, |
| 84 | + url=pattern, |
| 85 | + json=hook_content, |
| 86 | + content_type="application/json", |
| 87 | + status=200, |
| 88 | + ) |
| 89 | + yield rsps |
| 90 | + |
13 | 91 |
|
| 92 | +@pytest.fixture |
| 93 | +def resp_hook_delete(): |
14 | 94 | with responses.RequestsMock() as rsps: |
| 95 | + pattern = re.compile(r"http://localhost/api/v4/((groups|projects)/1/|)hooks/1") |
15 | 96 | rsps.add( |
16 | 97 | method=responses.GET, |
17 | | - url="http://localhost/api/v4/hooks/1", |
18 | | - json=content, |
| 98 | + url=pattern, |
| 99 | + json=hook_content, |
19 | 100 | content_type="application/json", |
20 | 101 | status=200, |
21 | 102 | ) |
| 103 | + rsps.add( |
| 104 | + method=responses.DELETE, |
| 105 | + url=pattern, |
| 106 | + status=204, |
| 107 | + ) |
22 | 108 | yield rsps |
23 | 109 |
|
24 | 110 |
|
25 | | -def test_hooks(gl, resp_get_hook): |
| 111 | +def test_list_system_hooks(gl, resp_hooks_list): |
| 112 | + hooks = gl.hooks.list() |
| 113 | + assert hooks[0].id == 1 |
| 114 | + assert hooks[0].url == "testurl" |
| 115 | + assert hooks[1].id == 2 |
| 116 | + assert hooks[1].url == "testurl_second" |
| 117 | + |
| 118 | + |
| 119 | +def test_get_system_hook(gl, resp_hook_get): |
26 | 120 | data = gl.hooks.get(1) |
27 | 121 | assert isinstance(data, Hook) |
28 | 122 | assert data.url == "testurl" |
29 | 123 | assert data.id == 1 |
| 124 | + |
| 125 | + |
| 126 | +def test_create_system_hook(gl, resp_hook_create): |
| 127 | + hook = gl.hooks.create(hook_content) |
| 128 | + assert hook.url == "testurl" |
| 129 | + assert hook.push_events is True |
| 130 | + assert hook.tag_push_events is True |
| 131 | + |
| 132 | + |
| 133 | +# there is no update method for system hooks |
| 134 | + |
| 135 | + |
| 136 | +def test_delete_system_hook(gl, resp_hook_delete): |
| 137 | + hook = gl.hooks.get(1) |
| 138 | + hook.delete() |
| 139 | + gl.hooks.delete(1) |
| 140 | + |
| 141 | + |
| 142 | +def test_list_group_hooks(group, resp_hooks_list): |
| 143 | + hooks = group.hooks.list() |
| 144 | + assert hooks[0].id == 1 |
| 145 | + assert hooks[0].url == "testurl" |
| 146 | + assert hooks[1].id == 2 |
| 147 | + assert hooks[1].url == "testurl_second" |
| 148 | + |
| 149 | + |
| 150 | +def test_get_group_hook(group, resp_hook_get): |
| 151 | + data = group.hooks.get(1) |
| 152 | + assert isinstance(data, GroupHook) |
| 153 | + assert data.url == "testurl" |
| 154 | + assert data.id == 1 |
| 155 | + |
| 156 | + |
| 157 | +def test_create_group_hook(group, resp_hook_create): |
| 158 | + hook = group.hooks.create(hook_content) |
| 159 | + assert hook.url == "testurl" |
| 160 | + assert hook.push_events is True |
| 161 | + assert hook.tag_push_events is True |
| 162 | + |
| 163 | + |
| 164 | +def test_update_group_hook(group, resp_hook_update): |
| 165 | + hook = group.hooks.get(1) |
| 166 | + assert hook.id == 1 |
| 167 | + hook.url = "testurl_more" |
| 168 | + hook.save() |
| 169 | + |
| 170 | + |
| 171 | +def test_delete_group_hook(group, resp_hook_delete): |
| 172 | + hook = group.hooks.get(1) |
| 173 | + hook.delete() |
| 174 | + group.hooks.delete(1) |
| 175 | + |
| 176 | + |
| 177 | +def test_list_project_hooks(project, resp_hooks_list): |
| 178 | + hooks = project.hooks.list() |
| 179 | + assert hooks[0].id == 1 |
| 180 | + assert hooks[0].url == "testurl" |
| 181 | + assert hooks[1].id == 2 |
| 182 | + assert hooks[1].url == "testurl_second" |
| 183 | + |
| 184 | + |
| 185 | +def test_get_project_hook(project, resp_hook_get): |
| 186 | + data = project.hooks.get(1) |
| 187 | + assert isinstance(data, ProjectHook) |
| 188 | + assert data.url == "testurl" |
| 189 | + assert data.id == 1 |
| 190 | + |
| 191 | + |
| 192 | +def test_create_project_hook(project, resp_hook_create): |
| 193 | + hook = project.hooks.create(hook_content) |
| 194 | + assert hook.url == "testurl" |
| 195 | + assert hook.push_events is True |
| 196 | + assert hook.tag_push_events is True |
| 197 | + |
| 198 | + |
| 199 | +def test_update_project_hook(project, resp_hook_update): |
| 200 | + hook = project.hooks.get(1) |
| 201 | + assert hook.id == 1 |
| 202 | + hook.url = "testurl_more" |
| 203 | + hook.save() |
| 204 | + |
| 205 | + |
| 206 | +def test_delete_project_hook(project, resp_hook_delete): |
| 207 | + hook = project.hooks.get(1) |
| 208 | + hook.delete() |
| 209 | + project.hooks.delete(1) |
0 commit comments