|
| 1 | +import unittest |
| 2 | + |
| 3 | +from httmock import response, urlmatch, with_httmock |
| 4 | + |
| 5 | +import gitlab |
| 6 | +from .mocks import * # noqa |
| 7 | + |
| 8 | + |
| 9 | +@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1", method="get") |
| 10 | +def resp_get_group(url, request): |
| 11 | + content = '{"name": "name", "id": 1, "path": "path"}' |
| 12 | + content = content.encode("utf-8") |
| 13 | + return response(200, content, headers, None, 5, request) |
| 14 | + |
| 15 | + |
| 16 | +@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups", method="post") |
| 17 | +def resp_create_group(url, request): |
| 18 | + content = '{"name": "name", "id": 1, "path": "path"}' |
| 19 | + content = content.encode("utf-8") |
| 20 | + return response(200, content, headers, None, 5, request) |
| 21 | + |
| 22 | + |
| 23 | +@urlmatch( |
| 24 | + scheme="http", netloc="localhost", path="/api/v4/groups/import", method="post", |
| 25 | +) |
| 26 | +def resp_create_import(url, request): |
| 27 | + """Mock for Group import tests. |
| 28 | +
|
| 29 | + GitLab does not respond with import status for group imports. |
| 30 | + """ |
| 31 | + |
| 32 | + content = """{ |
| 33 | + "message": "202 Accepted" |
| 34 | + }""" |
| 35 | + content = content.encode("utf-8") |
| 36 | + return response(202, content, headers, None, 25, request) |
| 37 | + |
| 38 | + |
| 39 | +class TestGroup(unittest.TestCase): |
| 40 | + def setUp(self): |
| 41 | + self.gl = gitlab.Gitlab( |
| 42 | + "http://localhost", |
| 43 | + private_token="private_token", |
| 44 | + ssl_verify=True, |
| 45 | + api_version=4, |
| 46 | + ) |
| 47 | + |
| 48 | + @with_httmock(resp_get_group) |
| 49 | + def test_get_group(self): |
| 50 | + data = self.gl.groups.get(1) |
| 51 | + self.assertIsInstance(data, gitlab.v4.objects.Group) |
| 52 | + self.assertEqual(data.name, "name") |
| 53 | + self.assertEqual(data.path, "path") |
| 54 | + self.assertEqual(data.id, 1) |
| 55 | + |
| 56 | + @with_httmock(resp_create_group) |
| 57 | + def test_create_group(self): |
| 58 | + name, path = "name", "path" |
| 59 | + data = self.gl.groups.create({"name": name, "path": path}) |
| 60 | + self.assertIsInstance(data, gitlab.v4.objects.Group) |
| 61 | + self.assertEqual(data.name, name) |
| 62 | + self.assertEqual(data.path, path) |
| 63 | + |
| 64 | + |
| 65 | +class TestGroupExport(TestGroup): |
| 66 | + def setUp(self): |
| 67 | + super(TestGroupExport, self).setUp() |
| 68 | + self.group = self.gl.groups.get(1, lazy=True) |
| 69 | + |
| 70 | + @with_httmock(resp_create_export) |
| 71 | + def test_create_group_export(self): |
| 72 | + export = self.group.exports.create() |
| 73 | + self.assertEqual(export.message, "202 Accepted") |
| 74 | + |
| 75 | + @unittest.skip("GitLab API endpoint not implemented") |
| 76 | + @with_httmock(resp_create_export) |
| 77 | + def test_refresh_group_export_status(self): |
| 78 | + export = self.group.exports.create() |
| 79 | + export.refresh() |
| 80 | + self.assertEqual(export.export_status, "finished") |
| 81 | + |
| 82 | + @with_httmock(resp_create_export, resp_download_export) |
| 83 | + def test_download_group_export(self): |
| 84 | + export = self.group.exports.create() |
| 85 | + download = export.download() |
| 86 | + self.assertIsInstance(download, bytes) |
| 87 | + self.assertEqual(download, binary_content) |
| 88 | + |
| 89 | + |
| 90 | +class TestGroupImport(TestGroup): |
| 91 | + @with_httmock(resp_create_import) |
| 92 | + def test_import_group(self): |
| 93 | + group_import = self.gl.groups.import_group("file", "api-group", "API Group") |
| 94 | + self.assertEqual(group_import["message"], "202 Accepted") |
| 95 | + |
| 96 | + @unittest.skip("GitLab API endpoint not implemented") |
| 97 | + @with_httmock(resp_create_import) |
| 98 | + def test_refresh_group_import_status(self): |
| 99 | + group_import = self.group.imports.get() |
| 100 | + group_import.refresh() |
| 101 | + self.assertEqual(group_import.import_status, "finished") |
0 commit comments