Skip to content

Commit 4ffaf1d

Browse files
authored
Merge pull request #1040 from nejch/test/project-export-import
test: update tests and params for project export/import
2 parents ad7e2bf + 9b16614 commit 4ffaf1d

File tree

4 files changed

+164
-33
lines changed

4 files changed

+164
-33
lines changed

gitlab/tests/objects/test_projects.py

Lines changed: 156 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,129 @@
88
import requests
99
from gitlab import * # noqa
1010
from gitlab.v4.objects import * # noqa
11-
from httmock import HTTMock, urlmatch, response # noqa
11+
from httmock import HTTMock, urlmatch, response, with_httmock # noqa
1212

1313

1414
headers = {"content-type": "application/json"}
15+
binary_content = b"binary content"
1516

1617

17-
class TestProjectSnippets(unittest.TestCase):
18+
@urlmatch(
19+
scheme="http", netloc="localhost", path="/api/v4/projects/1/export", method="post",
20+
)
21+
def resp_create_export(url, request):
22+
"""Common mock for Project Export tests."""
23+
content = """{
24+
"message": "202 Accepted"
25+
}"""
26+
content = content.encode("utf-8")
27+
return response(202, content, headers, None, 25, request)
28+
29+
30+
@urlmatch(
31+
scheme="http", netloc="localhost", path="/api/v4/projects/1/export", method="get",
32+
)
33+
def resp_export_status(url, request):
34+
"""Mock for Project Export GET response."""
35+
content = """{
36+
"id": 1,
37+
"description": "Itaque perspiciatis minima aspernatur",
38+
"name": "Gitlab Test",
39+
"name_with_namespace": "Gitlab Org / Gitlab Test",
40+
"path": "gitlab-test",
41+
"path_with_namespace": "gitlab-org/gitlab-test",
42+
"created_at": "2017-08-29T04:36:44.383Z",
43+
"export_status": "finished",
44+
"_links": {
45+
"api_url": "https://gitlab.test/api/v4/projects/1/export/download",
46+
"web_url": "https://gitlab.test/gitlab-test/download_export"
47+
}
48+
}
49+
"""
50+
content = content.encode("utf-8")
51+
return response(200, content, headers, None, 25, request)
52+
53+
54+
@urlmatch(
55+
scheme="http",
56+
netloc="localhost",
57+
path="/api/v4/projects/1/export/download",
58+
method="get",
59+
)
60+
def resp_download_export(url, request):
61+
"""Mock for Project Export Download GET response."""
62+
headers = {"content-type": "application/octet-stream"}
63+
content = binary_content
64+
return response(200, content, headers, None, 25, request)
65+
66+
67+
@urlmatch(
68+
scheme="http", netloc="localhost", path="/api/v4/projects/import", method="post",
69+
)
70+
def resp_import_project(url, request):
71+
"""Mock for Project Import POST response."""
72+
content = """{
73+
"id": 1,
74+
"description": null,
75+
"name": "api-project",
76+
"name_with_namespace": "Administrator / api-project",
77+
"path": "api-project",
78+
"path_with_namespace": "root/api-project",
79+
"created_at": "2018-02-13T09:05:58.023Z",
80+
"import_status": "scheduled"
81+
}"""
82+
content = content.encode("utf-8")
83+
return response(200, content, headers, None, 25, request)
84+
85+
86+
@urlmatch(
87+
scheme="http", netloc="localhost", path="/api/v4/projects/1/import", method="get",
88+
)
89+
def resp_import_status(url, request):
90+
"""Mock for Project Import GET response."""
91+
content = """{
92+
"id": 1,
93+
"description": "Itaque perspiciatis minima aspernatur corporis consequatur.",
94+
"name": "Gitlab Test",
95+
"name_with_namespace": "Gitlab Org / Gitlab Test",
96+
"path": "gitlab-test",
97+
"path_with_namespace": "gitlab-org/gitlab-test",
98+
"created_at": "2017-08-29T04:36:44.383Z",
99+
"import_status": "finished"
100+
}"""
101+
content = content.encode("utf-8")
102+
return response(200, content, headers, None, 25, request)
103+
104+
105+
@urlmatch(
106+
scheme="http", netloc="localhost", path="/api/v4/import/github", method="post",
107+
)
108+
def resp_import_github(url, request):
109+
"""Mock for GitHub Project Import POST response."""
110+
content = """{
111+
"id": 27,
112+
"name": "my-repo",
113+
"full_path": "/root/my-repo",
114+
"full_name": "Administrator / my-repo"
115+
}"""
116+
content = content.encode("utf-8")
117+
return response(200, content, headers, None, 25, request)
118+
119+
120+
class TestProject(unittest.TestCase):
121+
"""Base class for GitLab Project tests."""
122+
18123
def setUp(self):
19124
self.gl = Gitlab(
20125
"http://localhost",
21126
private_token="private_token",
22127
ssl_verify=True,
23128
api_version=4,
24129
)
130+
self.project = self.gl.projects.get(1, lazy=True)
131+
25132

133+
class TestProjectSnippets(TestProject):
26134
def test_list_project_snippets(self):
27135
title = "Example Snippet Title"
28136
visibility = "private"
@@ -47,7 +155,7 @@ def resp_list_snippet(url, request):
47155
return response(200, content, headers, None, 25, request)
48156

49157
with HTTMock(resp_list_snippet):
50-
snippets = self.gl.projects.get(1, lazy=True).snippets.list()
158+
snippets = self.project.snippets.list()
51159
self.assertEqual(len(snippets), 1)
52160
self.assertEqual(snippets[0].title, title)
53161
self.assertEqual(snippets[0].visibility, visibility)
@@ -76,7 +184,7 @@ def resp_get_snippet(url, request):
76184
return response(200, content, headers, None, 25, request)
77185

78186
with HTTMock(resp_get_snippet):
79-
snippet = self.gl.projects.get(1, lazy=True).snippets.get(1)
187+
snippet = self.project.snippets.get(1)
80188
self.assertEqual(snippet.title, title)
81189
self.assertEqual(snippet.visibility, visibility)
82190

@@ -123,7 +231,7 @@ def resp_create_snippet(url, request):
123231
return response(200, content, headers, None, 25, request)
124232

125233
with HTTMock(resp_create_snippet, resp_update_snippet):
126-
snippet = self.gl.projects.get(1, lazy=True).snippets.create(
234+
snippet = self.project.snippets.create(
127235
{
128236
"title": title,
129237
"file_name": title,
@@ -138,3 +246,46 @@ def resp_create_snippet(url, request):
138246
snippet.save()
139247
self.assertEqual(snippet.title, title)
140248
self.assertEqual(snippet.visibility, visibility)
249+
250+
251+
class TestProjectExport(TestProject):
252+
@with_httmock(resp_create_export)
253+
def test_create_project_export(self):
254+
export = self.project.exports.create()
255+
self.assertEqual(export.message, "202 Accepted")
256+
257+
@with_httmock(resp_create_export, resp_export_status)
258+
def test_refresh_project_export_status(self):
259+
export = self.project.exports.create()
260+
export.refresh()
261+
self.assertEqual(export.export_status, "finished")
262+
263+
@with_httmock(resp_create_export, resp_download_export)
264+
def test_download_project_export(self):
265+
export = self.project.exports.create()
266+
download = export.download()
267+
self.assertIsInstance(download, bytes)
268+
self.assertEqual(download, binary_content)
269+
270+
271+
class TestProjectImport(TestProject):
272+
@with_httmock(resp_import_project)
273+
def test_import_project(self):
274+
project_import = self.gl.projects.import_project("file", "api-project")
275+
self.assertEqual(project_import["import_status"], "scheduled")
276+
277+
@with_httmock(resp_import_status)
278+
def test_refresh_project_import_status(self):
279+
project_import = self.project.imports.get()
280+
project_import.refresh()
281+
self.assertEqual(project_import.import_status, "finished")
282+
283+
@with_httmock(resp_import_github)
284+
def test_import_github(self):
285+
base_path = "/root"
286+
name = "my-repo"
287+
ret = self.gl.projects.import_github("githubkey", 1234, base_path, name)
288+
self.assertIsInstance(ret, dict)
289+
self.assertEqual(ret["name"], name)
290+
self.assertEqual(ret["full_path"], "/".join((base_path, name)))
291+
self.assertTrue(ret["full_name"].endswith(name))

gitlab/tests/test_gitlab.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -937,33 +937,6 @@ def resp_update_submodule(url, request):
937937
self.assertEqual(ret["message"], "Message")
938938
self.assertEqual(ret["id"], "ed899a2f4b50b4370feeea94676502b42383c746")
939939

940-
def test_import_github(self):
941-
@urlmatch(
942-
scheme="http",
943-
netloc="localhost",
944-
path="/api/v4/import/github",
945-
method="post",
946-
)
947-
def resp_import_github(url, request):
948-
headers = {"content-type": "application/json"}
949-
content = """{
950-
"id": 27,
951-
"name": "my-repo",
952-
"full_path": "/root/my-repo",
953-
"full_name": "Administrator / my-repo"
954-
}"""
955-
content = content.encode("utf-8")
956-
return response(200, content, headers, None, 25, request)
957-
958-
with HTTMock(resp_import_github):
959-
base_path = "/root"
960-
name = "my-repo"
961-
ret = self.gl.projects.import_github("githubkey", 1234, base_path, name)
962-
self.assertIsInstance(ret, dict)
963-
self.assertEqual(ret["name"], name)
964-
self.assertEqual(ret["full_path"], "/".join((base_path, name)))
965-
self.assertTrue(ret["full_name"].endswith(name))
966-
967940
def test_applications(self):
968941
content = '{"name": "test_app", "redirect_uri": "http://localhost:8080", "scopes": ["api", "email"]}'
969942
json_content = json.loads(content)

gitlab/v4/objects.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4839,6 +4839,7 @@ def import_project(
48394839
self,
48404840
file,
48414841
path,
4842+
name=None,
48424843
namespace=None,
48434844
overwrite=False,
48444845
override_params=None,
@@ -4868,6 +4869,8 @@ def import_project(
48684869
if override_params:
48694870
for k, v in override_params.items():
48704871
data["override_params[%s]" % k] = v
4872+
if name is not None:
4873+
data["name"] = name
48714874
if namespace:
48724875
data["namespace"] = namespace
48734876
return self.gitlab.http_post(

tools/python_test_v4.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,9 +962,13 @@
962962
ex.download(streamed=True, action=f.write)
963963

964964
output = gl.projects.import_project(
965-
open("/tmp/gitlab-export.tgz", "rb"), "imported_project"
965+
open("/tmp/gitlab-export.tgz", "rb"), "imported_project", name="Imported Project"
966966
)
967967
project_import = gl.projects.get(output["id"], lazy=True).imports.get()
968+
969+
assert project_import.path == "imported_project"
970+
assert project_import.name == "Imported Project"
971+
968972
count = 0
969973
while project_import.import_status != "finished":
970974
time.sleep(1)

0 commit comments

Comments
 (0)