summaryrefslogtreecommitdiff
path: root/gitlab/tests/objects/test_snippets.py
diff options
context:
space:
mode:
authorNejc Habjan <nejc.habjan@siemens.com>2020-08-23 21:16:20 +0200
committerNejc Habjan <nejc.habjan@siemens.com>2020-08-23 21:19:47 +0200
commit204782a117f77f367dee87aa2c70822587829147 (patch)
treeb7d68fc7e4833139cc8c1d2360059b90ee43b3de /gitlab/tests/objects/test_snippets.py
parent76b2cadf1418e4ea2ac420ebba5a4b4f16fbd4c7 (diff)
downloadgitlab-refactor/split-unit-tests.tar.gz
refactor: rewrite unit tests for objects with responsesrefactor/split-unit-tests
Diffstat (limited to 'gitlab/tests/objects/test_snippets.py')
-rw-r--r--gitlab/tests/objects/test_snippets.py135
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,