diff options
Diffstat (limited to 'gitlab/tests/objects/test_snippets.py')
-rw-r--r-- | gitlab/tests/objects/test_snippets.py | 135 |
1 files changed, 52 insertions, 83 deletions
diff --git a/gitlab/tests/objects/test_snippets.py b/gitlab/tests/objects/test_snippets.py index 86eb54c..7e8afc2 100644 --- a/gitlab/tests/objects/test_snippets.py +++ b/gitlab/tests/objects/test_snippets.py @@ -3,9 +3,8 @@ GitLab API: https://docs.gitlab.com/ce/api/project_snippets.html https://docs.gitlab.com/ee/api/snippets.html (todo) """ -from httmock import response, urlmatch, with_httmock - -from .mocks import headers +import pytest +import responses title = "Example Snippet Title" @@ -13,97 +12,67 @@ visibility = "private" new_title = "new-title" -@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) - - -@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) - - -@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) - - -@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"}""" % ( - new_title, - visibility, - ) - content = content.encode("utf-8") - return response(200, content, headers, None, 25, request) - - -@with_httmock(resp_list_snippet) -def test_list_project_snippets(project): +@pytest.fixture +def resp_snippet(): + content = { + "title": title, + "description": "More verbose snippet description", + "file_name": "example.txt", + "content": "source code with multiple lines", + "visibility": visibility, + } + + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: + rsps.add( + method=responses.GET, + url="http://localhost/api/v4/projects/1/snippets", + json=[content], + content_type="application/json", + status=200, + ) + rsps.add( + method=responses.GET, + url="http://localhost/api/v4/projects/1/snippets/1", + json=content, + content_type="application/json", + status=200, + ) + rsps.add( + method=responses.POST, + url="http://localhost/api/v4/projects/1/snippets", + json=content, + content_type="application/json", + status=200, + ) + + updated_content = dict(content) + updated_content["title"] = new_title + updated_content["visibility"] = visibility + + rsps.add( + method=responses.PUT, + url="http://localhost/api/v4/projects/1/snippets", + json=updated_content, + content_type="application/json", + status=200, + ) + yield rsps + + +def test_list_project_snippets(project, resp_snippet): snippets = project.snippets.list() assert len(snippets) == 1 assert snippets[0].title == title assert snippets[0].visibility == visibility -@with_httmock(resp_get_snippet) -def test_get_project_snippets(project): +def test_get_project_snippet(project, resp_snippet): snippet = project.snippets.get(1) assert snippet.title == title assert snippet.visibility == visibility -@with_httmock(resp_create_snippet, resp_update_snippet) -def test_create_update_project_snippets(project): +def test_create_update_project_snippets(project, resp_snippet): snippet = project.snippets.create( { "title": title, |