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
- 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
+
18
123
def setUp (self ):
19
124
self .gl = Gitlab (
20
125
"http://localhost" ,
21
126
private_token = "private_token" ,
22
127
ssl_verify = True ,
23
128
api_version = 4 ,
24
129
)
130
+ self .project = self .gl .projects .get (1 , lazy = True )
131
+
25
132
133
+ class TestProjectSnippets (TestProject ):
26
134
def test_list_project_snippets (self ):
27
135
title = "Example Snippet Title"
28
136
visibility = "private"
@@ -47,7 +155,7 @@ def resp_list_snippet(url, request):
47
155
return response (200 , content , headers , None , 25 , request )
48
156
49
157
with HTTMock (resp_list_snippet ):
50
- snippets = self .gl . projects . get ( 1 , lazy = True ) .snippets .list ()
158
+ snippets = self .project .snippets .list ()
51
159
self .assertEqual (len (snippets ), 1 )
52
160
self .assertEqual (snippets [0 ].title , title )
53
161
self .assertEqual (snippets [0 ].visibility , visibility )
@@ -76,7 +184,7 @@ def resp_get_snippet(url, request):
76
184
return response (200 , content , headers , None , 25 , request )
77
185
78
186
with HTTMock (resp_get_snippet ):
79
- snippet = self .gl . projects . get ( 1 , lazy = True ) .snippets .get (1 )
187
+ snippet = self .project .snippets .get (1 )
80
188
self .assertEqual (snippet .title , title )
81
189
self .assertEqual (snippet .visibility , visibility )
82
190
@@ -123,7 +231,7 @@ def resp_create_snippet(url, request):
123
231
return response (200 , content , headers , None , 25 , request )
124
232
125
233
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 (
127
235
{
128
236
"title" : title ,
129
237
"file_name" : title ,
@@ -138,3 +246,46 @@ def resp_create_snippet(url, request):
138
246
snippet .save ()
139
247
self .assertEqual (snippet .title , title )
140
248
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