88import requests
99from gitlab import * # noqa
1010from 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
1414headers = {"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 ))
0 commit comments