summaryrefslogtreecommitdiff
path: root/gitlab/tests/objects/test_users.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_users.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_users.py')
-rw-r--r--gitlab/tests/objects/test_users.py166
1 files changed, 96 insertions, 70 deletions
diff --git a/gitlab/tests/objects/test_users.py b/gitlab/tests/objects/test_users.py
index 88175d0..ec282cf 100644
--- a/gitlab/tests/objects/test_users.py
+++ b/gitlab/tests/objects/test_users.py
@@ -1,94 +1,120 @@
"""
GitLab API: https://docs.gitlab.com/ce/api/users.html
"""
-
-from httmock import response, urlmatch, with_httmock
+import pytest
+import responses
from gitlab.v4.objects import User, UserMembership, UserStatus
-from .mocks import headers
-
-
-@urlmatch(scheme="http", netloc="localhost", path="/api/v4/users/1", method="get")
-def resp_get_user(url, request):
- content = (
- '{"name": "name", "id": 1, "password": "password", '
- '"username": "username", "email": "email"}'
- )
- content = content.encode("utf-8")
- return response(200, content, headers, None, 5, request)
-
-
-@urlmatch(
- scheme="http", netloc="localhost", path="/api/v4/users/1/memberships", method="get",
-)
-def resp_get_user_memberships(url, request):
- content = """[
- {
- "source_id": 1,
- "source_name": "Project one",
- "source_type": "Project",
- "access_level": "20"
- },
- {
- "source_id": 3,
- "source_name": "Group three",
- "source_type": "Namespace",
- "access_level": "20"
- }
- ]"""
- content = content.encode("utf-8")
- return response(200, content, headers, None, 5, request)
-
-
-@urlmatch(
- scheme="http", netloc="localhost", path="/api/v4/users/1/activate", method="post",
-)
-def resp_activate(url, request):
- return response(201, {}, headers, None, 5, request)
-
-
-@urlmatch(
- scheme="http", netloc="localhost", path="/api/v4/users/1/deactivate", method="post",
-)
-def resp_deactivate(url, request):
- return response(201, {}, headers, None, 5, request)
-
-
-@urlmatch(
- scheme="http", netloc="localhost", path="/api/v4/users/1/status", method="get",
-)
-def resp_get_user_status(url, request):
- content = (
- '{"message": "test", "message_html": "<h1>Message</h1>", "emoji": "thumbsup"}'
- )
- content = content.encode("utf-8")
- return response(200, content, headers, None, 5, request)
-
-
-@with_httmock(resp_get_user)
-def test_get_user(gl):
+
+
+@pytest.fixture
+def resp_get_user():
+ content = {
+ "name": "name",
+ "id": 1,
+ "password": "password",
+ "username": "username",
+ "email": "email",
+ }
+
+ with responses.RequestsMock() as rsps:
+ rsps.add(
+ method=responses.GET,
+ url="http://localhost/api/v4/users/1",
+ json=content,
+ content_type="application/json",
+ status=200,
+ )
+ yield rsps
+
+
+@pytest.fixture
+def resp_get_user_memberships():
+ content = [
+ {
+ "source_id": 1,
+ "source_name": "Project one",
+ "source_type": "Project",
+ "access_level": "20",
+ },
+ {
+ "source_id": 3,
+ "source_name": "Group three",
+ "source_type": "Namespace",
+ "access_level": "20",
+ },
+ ]
+
+ with responses.RequestsMock() as rsps:
+ rsps.add(
+ method=responses.GET,
+ url="http://localhost/api/v4/users/1/memberships",
+ json=content,
+ content_type="application/json",
+ status=200,
+ )
+ yield rsps
+
+
+@pytest.fixture
+def resp_activate():
+ with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
+ rsps.add(
+ method=responses.POST,
+ url="http://localhost/api/v4/users/1/activate",
+ json={},
+ content_type="application/json",
+ status=201,
+ )
+ rsps.add(
+ method=responses.POST,
+ url="http://localhost/api/v4/users/1/deactivate",
+ json={},
+ content_type="application/json",
+ status=201,
+ )
+ yield rsps
+
+
+@pytest.fixture
+def resp_get_user_status():
+ content = {
+ "message": "test",
+ "message_html": "<h1>Message</h1>",
+ "emoji": "thumbsup",
+ }
+
+ with responses.RequestsMock() as rsps:
+ rsps.add(
+ method=responses.GET,
+ url="http://localhost/api/v4/users/1/status",
+ json=content,
+ content_type="application/json",
+ status=200,
+ )
+ yield rsps
+
+
+def test_get_user(gl, resp_get_user):
user = gl.users.get(1)
assert isinstance(user, User)
assert user.name == "name"
assert user.id == 1
-@with_httmock(resp_get_user_memberships)
-def test_user_memberships(user):
+def test_user_memberships(user, resp_get_user_memberships):
memberships = user.memberships.list()
assert isinstance(memberships[0], UserMembership)
assert memberships[0].source_type == "Project"
-@with_httmock(resp_get_user_status)
-def test_user_status(user):
+def test_user_status(user, resp_get_user_status):
status = user.status.get()
assert isinstance(status, UserStatus)
assert status.message == "test"
assert status.emoji == "thumbsup"
-@with_httmock(resp_activate, resp_deactivate)
-def test_user_activate_deactivate(user):
+def test_user_activate_deactivate(user, resp_activate):
user.activate()
user.deactivate()