diff options
author | Nejc Habjan <nejc.habjan@siemens.com> | 2020-08-23 21:16:20 +0200 |
---|---|---|
committer | Nejc Habjan <nejc.habjan@siemens.com> | 2020-08-23 21:19:47 +0200 |
commit | 204782a117f77f367dee87aa2c70822587829147 (patch) | |
tree | b7d68fc7e4833139cc8c1d2360059b90ee43b3de /gitlab/tests/objects/test_services.py | |
parent | 76b2cadf1418e4ea2ac420ebba5a4b4f16fbd4c7 (diff) | |
download | gitlab-refactor/split-unit-tests.tar.gz |
refactor: rewrite unit tests for objects with responsesrefactor/split-unit-tests
Diffstat (limited to 'gitlab/tests/objects/test_services.py')
-rw-r--r-- | gitlab/tests/objects/test_services.py | 163 |
1 files changed, 61 insertions, 102 deletions
diff --git a/gitlab/tests/objects/test_services.py b/gitlab/tests/objects/test_services.py index a0cded7..5b2bcb8 100644 --- a/gitlab/tests/objects/test_services.py +++ b/gitlab/tests/objects/test_services.py @@ -2,110 +2,71 @@ GitLab API: https://docs.gitlab.com/ce/api/services.html """ -from httmock import urlmatch, response, with_httmock +import pytest +import responses from gitlab.v4.objects import ProjectService -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 = """{ +@pytest.fixture +def resp_service(): + 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) - - -@with_httmock(resp_get_active_services) -def test_list_active_services(project): + "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, + } + + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: + rsps.add( + method=responses.GET, + url="http://localhost/api/v4/projects/1/services", + json=[content], + content_type="application/json", + status=200, + ) + rsps.add( + method=responses.GET, + url="http://localhost/api/v4/projects/1/services", + json=content, + content_type="application/json", + status=200, + ) + rsps.add( + method=responses.GET, + url="http://localhost/api/v4/projects/1/services/pipelines-email", + json=content, + content_type="application/json", + status=200, + ) + updated_content = dict(content) + updated_content["issues_events"] = False + rsps.add( + method=responses.PUT, + url="http://localhost/api/v4/projects/1/services/pipelines-email", + json=updated_content, + content_type="application/json", + status=200, + ) + yield rsps + + +def test_list_active_services(project, resp_service): services = project.services.list() assert isinstance(services, list) assert isinstance(services[0], ProjectService) @@ -113,22 +74,20 @@ def test_list_active_services(project): assert services[0].push_events -def test_list_available_services(project): +def test_list_available_services(project, resp_service): services = project.services.available() assert isinstance(services, list) assert isinstance(services[0], str) -@with_httmock(resp_get_service) -def test_get_service(project): +def test_get_service(project, resp_service): service = project.services.get("pipelines-email") assert isinstance(service, ProjectService) assert service.push_events is True -@with_httmock(resp_get_service, resp_update_service) -def test_update_service(project): +def test_update_service(project, resp_service): service = project.services.get("pipelines-email") - service.issues_events = True + service.issues_events = False service.save() - assert service.issues_events is True + assert service.issues_events is False |