summaryrefslogtreecommitdiff
path: root/gitlab/tests/objects/test_projects.py
blob: 6a2840ad41991151f62bd0ad17b26c80916daaff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import unittest
import gitlab
import os
import pickle
import tempfile
import json
import unittest
import requests
from gitlab import *  # noqa
from gitlab.v4.objects import *  # noqa
from httmock import HTTMock, urlmatch, response, with_httmock  # noqa

from .mocks import *  # noqa


@urlmatch(
    scheme="http", netloc="localhost", path="/api/v4/projects/1/export", method="get",
)
def resp_export_status(url, request):
    """Mock for Project Export GET response."""
    content = """{
      "id": 1,
      "description": "Itaque perspiciatis minima aspernatur",
      "name": "Gitlab Test",
      "name_with_namespace": "Gitlab Org / Gitlab Test",
      "path": "gitlab-test",
      "path_with_namespace": "gitlab-org/gitlab-test",
      "created_at": "2017-08-29T04:36:44.383Z",
      "export_status": "finished",
      "_links": {
        "api_url": "https://gitlab.test/api/v4/projects/1/export/download",
        "web_url": "https://gitlab.test/gitlab-test/download_export"
      }
    }
    """
    content = content.encode("utf-8")
    return response(200, content, headers, None, 25, request)


@urlmatch(
    scheme="http", netloc="localhost", path="/api/v4/projects/import", method="post",
)
def resp_import_project(url, request):
    """Mock for Project Import POST response."""
    content = """{
      "id": 1,
      "description": null,
      "name": "api-project",
      "name_with_namespace": "Administrator / api-project",
      "path": "api-project",
      "path_with_namespace": "root/api-project",
      "created_at": "2018-02-13T09:05:58.023Z",
      "import_status": "scheduled"
    }"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 25, request)


@urlmatch(
    scheme="http", netloc="localhost", path="/api/v4/projects/1/import", method="get",
)
def resp_import_status(url, request):
    """Mock for Project Import GET response."""
    content = """{
      "id": 1,
      "description": "Itaque perspiciatis minima aspernatur corporis consequatur.",
      "name": "Gitlab Test",
      "name_with_namespace": "Gitlab Org / Gitlab Test",
      "path": "gitlab-test",
      "path_with_namespace": "gitlab-org/gitlab-test",
      "created_at": "2017-08-29T04:36:44.383Z",
      "import_status": "finished"
    }"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 25, request)


@urlmatch(
    scheme="http", netloc="localhost", path="/api/v4/import/github", method="post",
)
def resp_import_github(url, request):
    """Mock for GitHub Project Import POST response."""
    content = """{
    "id": 27,
    "name": "my-repo",
    "full_path": "/root/my-repo",
    "full_name": "Administrator / my-repo"
    }"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 25, request)


@urlmatch(
    scheme="http",
    netloc="localhost",
    path="/api/v4/projects/1/remote_mirrors",
    method="get",
)
def resp_get_remote_mirrors(url, request):
    """Mock for Project Remote Mirrors GET response."""
    content = """[
      {
        "enabled": true,
        "id": 101486,
        "last_error": null,
        "last_successful_update_at": "2020-01-06T17:32:02.823Z",
        "last_update_at": "2020-01-06T17:32:02.823Z",
        "last_update_started_at": "2020-01-06T17:31:55.864Z",
        "only_protected_branches": true,
        "update_status": "finished",
        "url": "https://*****:*****@gitlab.com/gitlab-org/security/gitlab.git"
      }
    ]"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 5, request)


@urlmatch(
    scheme="http",
    netloc="localhost",
    path="/api/v4/projects/1/remote_mirrors",
    method="post",
)
def resp_create_remote_mirror(url, request):
    """Mock for Project Remote Mirrors POST response."""
    content = """{
        "enabled": false,
        "id": 101486,
        "last_error": null,
        "last_successful_update_at": null,
        "last_update_at": null,
        "last_update_started_at": null,
        "only_protected_branches": false,
        "update_status": "none",
        "url": "https://*****:*****@example.com/gitlab/example.git"
    }"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 5, request)


@urlmatch(
    scheme="http",
    netloc="localhost",
    path="/api/v4/projects/1/remote_mirrors/1",
    method="put",
)
def resp_update_remote_mirror(url, request):
    """Mock for Project Remote Mirrors PUT response."""
    content = """{
        "enabled": false,
        "id": 101486,
        "last_error": null,
        "last_successful_update_at": "2020-01-06T17:32:02.823Z",
        "last_update_at": "2020-01-06T17:32:02.823Z",
        "last_update_started_at": "2020-01-06T17:31:55.864Z",
        "only_protected_branches": true,
        "update_status": "finished",
        "url": "https://*****:*****@gitlab.com/gitlab-org/security/gitlab.git"
    }"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 5, request)


@urlmatch(
    scheme="http",
    netloc="localhost",
    path="/api/v4/projects/1/services/pipelines-email",
    method="put",
)
def resp_update_service(url, request):
    """Mock for Service update PUT response."""
    content = """{
        "id": 100152,
        "title": "Pipelines emails",
        "slug": "pipelines-email",
        "created_at": "2019-01-14T08:46:43.637+01:00",
        "updated_at": "2019-07-01T14:10:36.156+02:00",
        "active": true,
        "commit_events": true,
        "push_events": true,
        "issues_events": true,
        "confidential_issues_events": true,
        "merge_requests_events": true,
        "tag_push_events": true,
        "note_events": true,
        "confidential_note_events": true,
        "pipeline_events": true,
        "wiki_page_events": true,
        "job_events": true,
        "comment_on_event_enabled": true,
        "project_id": 1
    }"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 5, request)


@urlmatch(
    scheme="http",
    netloc="localhost",
    path="/api/v4/projects/1/services/pipelines-email",
    method="get",
)
def resp_get_service(url, request):
    """Mock for Service GET response."""
    content = """{
        "id": 100152,
        "title": "Pipelines emails",
        "slug": "pipelines-email",
        "created_at": "2019-01-14T08:46:43.637+01:00",
        "updated_at": "2019-07-01T14:10:36.156+02:00",
        "active": true,
        "commit_events": true,
        "push_events": true,
        "issues_events": true,
        "confidential_issues_events": true,
        "merge_requests_events": true,
        "tag_push_events": true,
        "note_events": true,
        "confidential_note_events": true,
        "pipeline_events": true,
        "wiki_page_events": true,
        "job_events": true,
        "comment_on_event_enabled": true,
        "project_id": 1
    }"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 5, request)


@urlmatch(
    scheme="http", netloc="localhost", path="/api/v4/projects/1/services", method="get",
)
def resp_get_active_services(url, request):
    """Mock for active Services GET response."""
    content = """[{
        "id": 100152,
        "title": "Pipelines emails",
        "slug": "pipelines-email",
        "created_at": "2019-01-14T08:46:43.637+01:00",
        "updated_at": "2019-07-01T14:10:36.156+02:00",
        "active": true,
        "commit_events": true,
        "push_events": true,
        "issues_events": true,
        "confidential_issues_events": true,
        "merge_requests_events": true,
        "tag_push_events": true,
        "note_events": true,
        "confidential_note_events": true,
        "pipeline_events": true,
        "wiki_page_events": true,
        "job_events": true,
        "comment_on_event_enabled": true,
        "project_id": 1
    }]"""
    content = content.encode("utf-8")
    return response(200, content, headers, None, 5, request)


class TestProject(unittest.TestCase):
    """Base class for GitLab Project tests."""

    def setUp(self):
        self.gl = Gitlab(
            "http://localhost",
            private_token="private_token",
            ssl_verify=True,
            api_version="4",
        )
        self.project = self.gl.projects.get(1, lazy=True)


class TestProjectSnippets(TestProject):
    def test_list_project_snippets(self):
        title = "Example Snippet Title"
        visibility = "private"

        @urlmatch(
            scheme="http",
            netloc="localhost",
            path="/api/v4/projects/1/snippets",
            method="get",
        )
        def resp_list_snippet(url, request):
            content = """[{
            "title": "%s",
            "description": "More verbose snippet description",
            "file_name": "example.txt",
            "content": "source code with multiple lines",
            "visibility": "%s"}]""" % (
                title,
                visibility,
            )
            content = content.encode("utf-8")
            return response(200, content, headers, None, 25, request)

        with HTTMock(resp_list_snippet):
            snippets = self.project.snippets.list()
            self.assertEqual(len(snippets), 1)
            self.assertEqual(snippets[0].title, title)
            self.assertEqual(snippets[0].visibility, visibility)

    def test_get_project_snippets(self):
        title = "Example Snippet Title"
        visibility = "private"

        @urlmatch(
            scheme="http",
            netloc="localhost",
            path="/api/v4/projects/1/snippets/1",
            method="get",
        )
        def resp_get_snippet(url, request):
            content = """{
            "title": "%s",
            "description": "More verbose snippet description",
            "file_name": "example.txt",
            "content": "source code with multiple lines",
            "visibility": "%s"}""" % (
                title,
                visibility,
            )
            content = content.encode("utf-8")
            return response(200, content, headers, None, 25, request)

        with HTTMock(resp_get_snippet):
            snippet = self.project.snippets.get(1)
            self.assertEqual(snippet.title, title)
            self.assertEqual(snippet.visibility, visibility)

    def test_create_update_project_snippets(self):
        title = "Example Snippet Title"
        visibility = "private"

        @urlmatch(
            scheme="http",
            netloc="localhost",
            path="/api/v4/projects/1/snippets",
            method="put",
        )
        def resp_update_snippet(url, request):
            content = """{
            "title": "%s",
            "description": "More verbose snippet description",
            "file_name": "example.txt",
            "content": "source code with multiple lines",
            "visibility": "%s"}""" % (
                title,
                visibility,
            )
            content = content.encode("utf-8")
            return response(200, content, headers, None, 25, request)

        @urlmatch(
            scheme="http",
            netloc="localhost",
            path="/api/v4/projects/1/snippets",
            method="post",
        )
        def resp_create_snippet(url, request):
            content = """{
            "title": "%s",
            "description": "More verbose snippet description",
            "file_name": "example.txt",
            "content": "source code with multiple lines",
            "visibility": "%s"}""" % (
                title,
                visibility,
            )
            content = content.encode("utf-8")
            return response(200, content, headers, None, 25, request)

        with HTTMock(resp_create_snippet, resp_update_snippet):
            snippet = self.project.snippets.create(
                {
                    "title": title,
                    "file_name": title,
                    "content": title,
                    "visibility": visibility,
                }
            )
            self.assertEqual(snippet.title, title)
            self.assertEqual(snippet.visibility, visibility)
            title = "new-title"
            snippet.title = title
            snippet.save()
            self.assertEqual(snippet.title, title)
            self.assertEqual(snippet.visibility, visibility)


class TestProjectExport(TestProject):
    @with_httmock(resp_create_export)
    def test_create_project_export(self):
        export = self.project.exports.create()
        self.assertEqual(export.message, "202 Accepted")

    @with_httmock(resp_create_export, resp_export_status)
    def test_refresh_project_export_status(self):
        export = self.project.exports.create()
        export.refresh()
        self.assertEqual(export.export_status, "finished")

    @with_httmock(resp_create_export, resp_download_export)
    def test_download_project_export(self):
        export = self.project.exports.create()
        download = export.download()
        self.assertIsInstance(download, bytes)
        self.assertEqual(download, binary_content)


class TestProjectImport(TestProject):
    @with_httmock(resp_import_project)
    def test_import_project(self):
        project_import = self.gl.projects.import_project("file", "api-project")
        self.assertEqual(project_import["import_status"], "scheduled")

    @with_httmock(resp_import_status)
    def test_refresh_project_import_status(self):
        project_import = self.project.imports.get()
        project_import.refresh()
        self.assertEqual(project_import.import_status, "finished")

    @with_httmock(resp_import_github)
    def test_import_github(self):
        base_path = "/root"
        name = "my-repo"
        ret = self.gl.projects.import_github("githubkey", 1234, base_path, name)
        self.assertIsInstance(ret, dict)
        self.assertEqual(ret["name"], name)
        self.assertEqual(ret["full_path"], "/".join((base_path, name)))
        self.assertTrue(ret["full_name"].endswith(name))


class TestProjectRemoteMirrors(TestProject):
    @with_httmock(resp_get_remote_mirrors)
    def test_list_project_remote_mirrors(self):
        mirrors = self.project.remote_mirrors.list()
        self.assertIsInstance(mirrors, list)
        self.assertIsInstance(mirrors[0], ProjectRemoteMirror)
        self.assertTrue(mirrors[0].enabled)

    @with_httmock(resp_create_remote_mirror)
    def test_create_project_remote_mirror(self):
        mirror = self.project.remote_mirrors.create({"url": "https://example.com"})
        self.assertIsInstance(mirror, ProjectRemoteMirror)
        self.assertEqual(mirror.update_status, "none")

    @with_httmock(resp_create_remote_mirror, resp_update_remote_mirror)
    def test_update_project_remote_mirror(self):
        mirror = self.project.remote_mirrors.create({"url": "https://example.com"})
        mirror.only_protected_branches = True
        mirror.save()
        self.assertEqual(mirror.update_status, "finished")
        self.assertTrue(mirror.only_protected_branches)


class TestProjectServices(TestProject):
    @with_httmock(resp_get_active_services)
    def test_list_active_services(self):
        services = self.project.services.list()
        self.assertIsInstance(services, list)
        self.assertIsInstance(services[0], ProjectService)
        self.assertTrue(services[0].active)
        self.assertTrue(services[0].push_events)

    def test_list_available_services(self):
        services = self.project.services.available()
        self.assertIsInstance(services, list)
        self.assertIsInstance(services[0], str)

    @with_httmock(resp_get_service)
    def test_get_service(self):
        service = self.project.services.get("pipelines-email")
        self.assertIsInstance(service, ProjectService)
        self.assertEqual(service.push_events, True)

    @with_httmock(resp_get_service, resp_update_service)
    def test_update_service(self):
        service = self.project.services.get("pipelines-email")
        service.issues_events = True
        service.save()
        self.assertEqual(service.issues_events, True)