Skip to content

Commit

Permalink
refactor: rewrite unit tests for objects with responses
Browse files Browse the repository at this point in the history
  • Loading branch information
nejch committed Aug 23, 2020
1 parent 76b2cad commit 204782a
Show file tree
Hide file tree
Showing 24 changed files with 900 additions and 940 deletions.
5 changes: 2 additions & 3 deletions gitlab/tests/conftest.py
Expand Up @@ -16,9 +16,7 @@ def gl():
@pytest.fixture
def gl_trailing():
return gitlab.Gitlab(
"http://localhost/",
private_token="private_token",
api_version=4
"http://localhost/", private_token="private_token", api_version=4
)


Expand All @@ -38,6 +36,7 @@ def default_config(tmpdir):
config_path.write(valid_config)
return str(config_path)


@pytest.fixture
def group(gl):
return gl.groups.get(1, lazy=True)
Expand Down
65 changes: 65 additions & 0 deletions gitlab/tests/objects/conftest.py
@@ -0,0 +1,65 @@
"""Common mocks for resources in gitlab.v4.objects"""

import re

import pytest
import responses


@pytest.fixture
def binary_content():
return b"binary content"


@pytest.fixture
def accepted_content():
return {"message": "202 Accepted"}


@pytest.fixture
def created_content():
return {"message": "201 Created"}


@pytest.fixture
def resp_export(accepted_content, binary_content):
"""Common fixture for group and project exports."""
export_status_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",
},
}

with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(
method=responses.POST,
url=re.compile(r".*/api/v4/(groups|projects)/1/export"),
json=accepted_content,
content_type="application/json",
status=202,
)
rsps.add(
method=responses.GET,
url=re.compile(r".*/api/v4/(groups|projects)/1/export/download"),
body=binary_content,
content_type="application/octet-stream",
status=200,
)
# Currently only project export supports status checks
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/projects/1/export",
json=export_status_content,
content_type="application/json",
status=200,
)
yield rsps
35 changes: 0 additions & 35 deletions gitlab/tests/objects/mocks.py

This file was deleted.

66 changes: 66 additions & 0 deletions gitlab/tests/objects/test_appearance.py
@@ -0,0 +1,66 @@
"""
GitLab API: https://docs.gitlab.com/ce/api/appearance.html
"""

import pytest
import responses


title = "GitLab Test Instance"
description = "gitlab-test.example.com"
new_title = "new-title"
new_description = "new-description"


@pytest.fixture
def resp_application_appearance():
content = {
"title": title,
"description": description,
"logo": "/uploads/-/system/appearance/logo/1/logo.png",
"header_logo": "/uploads/-/system/appearance/header_logo/1/header.png",
"favicon": "/uploads/-/system/appearance/favicon/1/favicon.png",
"new_project_guidelines": "Please read the FAQs for help.",
"header_message": "",
"footer_message": "",
"message_background_color": "#e75e40",
"message_font_color": "#ffffff",
"email_header_and_footer_enabled": False,
}

with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/application/appearance",
json=content,
content_type="application/json",
status=200,
)

updated_content = dict(content)
updated_content["title"] = new_title
updated_content["description"] = new_description

rsps.add(
method=responses.PUT,
url="http://localhost/api/v4/application/appearance",
json=updated_content,
content_type="application/json",
status=200,
)
yield rsps


def test_get_update_appearance(gl, resp_application_appearance):
appearance = gl.appearance.get()
assert appearance.title == title
assert appearance.description == description
appearance.title = new_title
appearance.description = new_description
appearance.save()
assert appearance.title == new_title
assert appearance.description == new_description


def test_update_appearance(gl, resp_application_appearance):
resp = gl.appearance.update(title=new_title, description=new_description)
108 changes: 0 additions & 108 deletions gitlab/tests/objects/test_application.py

This file was deleted.

45 changes: 45 additions & 0 deletions gitlab/tests/objects/test_applications.py
@@ -0,0 +1,45 @@
"""
GitLab API: https://docs.gitlab.com/ce/api/applications.html
"""

import pytest
import responses


title = "GitLab Test Instance"
description = "gitlab-test.example.com"
new_title = "new-title"
new_description = "new-description"


@pytest.fixture
def resp_application_create():
content = {
"name": "test_app",
"redirect_uri": "http://localhost:8080",
"scopes": ["api", "email"],
}

with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/applications",
json=content,
content_type="application/json",
status=200,
)
yield rsps


def test_create_application(gl, resp_application_create):
application = gl.applications.create(
{
"name": "test_app",
"redirect_uri": "http://localhost:8080",
"scopes": ["api", "email"],
"confidential": False,
}
)
assert application.name == "test_app"
assert application.redirect_uri == "http://localhost:8080"
assert application.scopes == ["api", "email"]

0 comments on commit 204782a

Please sign in to comment.