diff options
| author | Nejc Habjan <hab.nejc@gmail.com> | 2020-04-17 02:26:28 +0200 |
|---|---|---|
| committer | Nejc Habjan <nejc.habjan@siemens.com> | 2020-08-22 20:09:52 +0200 |
| commit | 76b2cadf1418e4ea2ac420ebba5a4b4f16fbd4c7 (patch) | |
| tree | 4dbe6ae57c4f03e1351f7a49e590cbbc6d31121a /gitlab/tests/objects/test_projects.py | |
| parent | 11383e70f74c70e6fe8a56f18b5b170db982f402 (diff) | |
| download | gitlab-76b2cadf1418e4ea2ac420ebba5a4b4f16fbd4c7.tar.gz | |
refactor: split unit tests by GitLab API resources
Diffstat (limited to 'gitlab/tests/objects/test_projects.py')
| -rw-r--r-- | gitlab/tests/objects/test_projects.py | 718 |
1 files changed, 188 insertions, 530 deletions
diff --git a/gitlab/tests/objects/test_projects.py b/gitlab/tests/objects/test_projects.py index fa105ae..7fefe3f 100644 --- a/gitlab/tests/objects/test_projects.py +++ b/gitlab/tests/objects/test_projects.py @@ -1,547 +1,205 @@ -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) +""" +GitLab API: https://docs.gitlab.com/ce/api/projects.html +""" +import pytest -@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) +from gitlab.v4.objects import Project +from httmock import urlmatch, response, with_httmock -@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) +from .mocks import headers -@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") +@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1", method="get") +def resp_get_project(url, request): + content = '{"name": "name", "id": 1}'.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) +@with_httmock(resp_get_project) +def test_get_project(gl): + data = gl.projects.get(1) + assert isinstance(data, Project) + assert data.name == "name" + assert data.id == 1 -@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) +@pytest.mark.skip(reason="missing test") +def test_list_projects(gl): + pass -@urlmatch( - scheme="http", - netloc="localhost", - path="/api/v4/projects/1/pipeline_schedules$", - method="post", -) -def resp_create_project_pipeline_schedule(url, request): - """Mock for creating project pipeline Schedules POST response.""" - content = """{ - "id": 14, - "description": "Build packages", - "ref": "master", - "cron": "0 1 * * 5", - "cron_timezone": "UTC", - "next_run_at": "2017-05-26T01:00:00.000Z", - "active": true, - "created_at": "2017-05-19T13:43:08.169Z", - "updated_at": "2017-05-19T13:43:08.169Z", - "last_pipeline": null, - "owner": { - "name": "Administrator", - "username": "root", - "id": 1, - "state": "active", - "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", - "web_url": "https://gitlab.example.com/root" - } -}""" - content = content.encode("utf-8") - return response(200, content, headers, None, 5, request) +@pytest.mark.skip(reason="missing test") +def test_list_user_projects(gl): + pass -@urlmatch( - scheme="http", - netloc="localhost", - path="/api/v4/projects/1/pipeline_schedules/14/play", - method="post", -) -def resp_play_project_pipeline_schedule(url, request): - """Mock for playing a project pipeline schedule POST response.""" - content = """{"message": "201 Created"}""" - content = content.encode("utf-8") - return response(200, content, headers, None, 5, request) +@pytest.mark.skip(reason="missing test") +def test_list_user_starred_projects(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_list_project_users(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_create_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_create_user_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_update_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_fork_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_list_project_forks(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_star_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_unstar_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_list_project_starrers(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_get_project_languages(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_archive_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_unarchive_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_remove_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_restore_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_upload_file(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_share_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_delete_shared_project_link(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_list_project_hooks(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_get_project_hook(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_create_project_hook(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_update_project_hook(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_delete_project_hook(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_create_forked_from_relationship(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_delete_forked_from_relationship(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_search_projects_by_name(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_project_housekeeping(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_get_project_push_rules(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_create_project_push_rule(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_update_project_push_rule(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_delete_project_push_rule(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_transfer_project(gl): + pass + + +@pytest.mark.skip(reason="missing test") +def test_project_pull_mirror(gl): + pass -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() - assert len(snippets) == 1 - assert snippets[0].title == title - assert 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) - assert snippet.title == title - assert 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, - } - ) - assert snippet.title == title - assert snippet.visibility == visibility - title = "new-title" - snippet.title = title - snippet.save() - assert snippet.title == title - assert snippet.visibility == visibility - - -class TestProjectExport(TestProject): - @with_httmock(resp_create_export) - def test_create_project_export(self): - export = self.project.exports.create() - assert 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() - assert 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() - assert isinstance(download, bytes) - assert 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") - assert 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() - assert 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) - assert isinstance(ret, dict) - assert ret["name"] == name - assert ret["full_path"] == "/".join((base_path, name)) - assert 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() - assert isinstance(mirrors, list) - assert isinstance(mirrors[0], ProjectRemoteMirror) - assert 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"}) - assert isinstance(mirror, ProjectRemoteMirror) - assert 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() - assert mirror.update_status == "finished" - assert mirror.only_protected_branches - - -class TestProjectServices(TestProject): - @with_httmock(resp_get_active_services) - def test_list_active_services(self): - services = self.project.services.list() - assert isinstance(services, list) - assert isinstance(services[0], ProjectService) - assert services[0].active - assert services[0].push_events - - def test_list_available_services(self): - services = self.project.services.available() - assert isinstance(services, list) - assert isinstance(services[0], str) - - @with_httmock(resp_get_service) - def test_get_service(self): - service = self.project.services.get("pipelines-email") - assert isinstance(service, ProjectService) - assert 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() - assert service.issues_events == True - - -class TestProjectPipelineSchedule(TestProject): - @with_httmock( - resp_create_project_pipeline_schedule, resp_play_project_pipeline_schedule - ) - def test_project_pipeline_schedule_play(self): - description = "Build packages" - cronline = "0 1 * * 5" - sched = self.project.pipelineschedules.create( - {"ref": "master", "description": description, "cron": cronline} - ) - self.assertIsNotNone(sched) - self.assertEqual(description, sched.description) - self.assertEqual(cronline, sched.cron) - - play_result = sched.play() - self.assertIsNotNone(play_result) - self.assertIn("message", play_result) - self.assertEqual("201 Created", play_result["message"]) +@pytest.mark.skip(reason="missing test") +def test_project_snapshot(gl): + pass |
