|
8 | 8 | import requests
|
9 | 9 | from gitlab import * # noqa
|
10 | 10 | from gitlab.v4.objects import * # noqa
|
11 |
| -from httmock import HTTMock, urlmatch, response # noqa |
| 11 | +from httmock import HTTMock, urlmatch, response, with_httmock # noqa |
12 | 12 |
|
13 | 13 |
|
14 | 14 | headers = {"content-type": "application/json"}
|
| 15 | +binary_content = b"binary content" |
15 | 16 |
|
16 | 17 |
|
17 | 18 | class TestProject(unittest.TestCase):
|
@@ -143,3 +144,71 @@ def resp_create_snippet(url, request):
|
143 | 144 | snippet.save()
|
144 | 145 | self.assertEqual(snippet.title, title)
|
145 | 146 | self.assertEqual(snippet.visibility, visibility)
|
| 147 | + |
| 148 | + |
| 149 | +@urlmatch( |
| 150 | + scheme="http", netloc="localhost", path="/api/v4/projects/1/export", method="post", |
| 151 | +) |
| 152 | +def resp_create_export(url, request): |
| 153 | + """Common mock for Project Export tests""" |
| 154 | + content = """{ |
| 155 | + "message": "202 Accepted" |
| 156 | + }""" |
| 157 | + content = content.encode("utf-8") |
| 158 | + return response(202, content, headers, None, 25, request) |
| 159 | + |
| 160 | + |
| 161 | +@urlmatch( |
| 162 | + scheme="http", netloc="localhost", path="/api/v4/projects/1/export", method="get", |
| 163 | +) |
| 164 | +def resp_export_status(url, request): |
| 165 | + """mock for Project Export GET response""" |
| 166 | + content = """{ |
| 167 | + "id": 1, |
| 168 | + "description": "Itaque perspiciatis minima aspernatur", |
| 169 | + "name": "Gitlab Test", |
| 170 | + "name_with_namespace": "Gitlab Org / Gitlab Test", |
| 171 | + "path": "gitlab-test", |
| 172 | + "path_with_namespace": "gitlab-org/gitlab-test", |
| 173 | + "created_at": "2017-08-29T04:36:44.383Z", |
| 174 | + "export_status": "finished", |
| 175 | + "_links": { |
| 176 | + "api_url": "https://gitlab.test/api/v4/projects/1/export/download", |
| 177 | + "web_url": "https://gitlab.test/gitlab-test/download_export" |
| 178 | + } |
| 179 | + } |
| 180 | + """ |
| 181 | + content = content.encode("utf-8") |
| 182 | + return response(200, content, headers, None, 25, request) |
| 183 | + |
| 184 | + |
| 185 | +@urlmatch( |
| 186 | + scheme="http", |
| 187 | + netloc="localhost", |
| 188 | + path="/api/v4/projects/1/export/download", |
| 189 | + method="get", |
| 190 | +) |
| 191 | +def resp_download_export(url, request): |
| 192 | + headers = {"content-type": "application/octet-stream"} |
| 193 | + content = binary_content |
| 194 | + return response(200, content, headers, None, 25, request) |
| 195 | + |
| 196 | + |
| 197 | +class TestProjectExport(TestProject): |
| 198 | + @with_httmock(resp_create_export) |
| 199 | + def test_create_project_export(self): |
| 200 | + export = self.project.exports.create() |
| 201 | + self.assertEqual(export.message, "202 Accepted") |
| 202 | + |
| 203 | + @with_httmock(resp_create_export, resp_export_status) |
| 204 | + def test_refresh_project_export_status(self): |
| 205 | + export = self.project.exports.create() |
| 206 | + export.refresh() |
| 207 | + self.assertEqual(export.export_status, "finished") |
| 208 | + |
| 209 | + @with_httmock(resp_create_export, resp_download_export) |
| 210 | + def test_download_project_export(self): |
| 211 | + export = self.project.exports.create() |
| 212 | + download = export.download() |
| 213 | + self.assertIsInstance(download, bytes) |
| 214 | + self.assertEqual(download, binary_content) |
0 commit comments