Skip to content

Commit 9edf10f

Browse files
committed
refactor: rewrite unit tests for objects with responses
1 parent 76b2cad commit 9edf10f

24 files changed

+900
-940
lines changed

gitlab/tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ def gl():
1616
@pytest.fixture
1717
def gl_trailing():
1818
return gitlab.Gitlab(
19-
"http://localhost/",
20-
private_token="private_token",
21-
api_version=4
19+
"http://localhost/", private_token="private_token", api_version=4
2220
)
2321

2422

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

39+
4140
@pytest.fixture
4241
def group(gl):
4342
return gl.groups.get(1, lazy=True)

gitlab/tests/objects/conftest.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Common mocks for resources in gitlab.v4.objects"""
2+
3+
import re
4+
5+
import pytest
6+
import responses
7+
8+
9+
@pytest.fixture
10+
def binary_content():
11+
return b"binary content"
12+
13+
14+
@pytest.fixture
15+
def accepted_content():
16+
return {"message": "202 Accepted"}
17+
18+
19+
@pytest.fixture
20+
def created_content():
21+
return {"message": "201 Created"}
22+
23+
24+
@pytest.fixture
25+
def resp_export(accepted_content, binary_content):
26+
"""Common fixture for group and project exports."""
27+
export_status_content = {
28+
"id": 1,
29+
"description": "Itaque perspiciatis minima aspernatur",
30+
"name": "Gitlab Test",
31+
"name_with_namespace": "Gitlab Org / Gitlab Test",
32+
"path": "gitlab-test",
33+
"path_with_namespace": "gitlab-org/gitlab-test",
34+
"created_at": "2017-08-29T04:36:44.383Z",
35+
"export_status": "finished",
36+
"_links": {
37+
"api_url": "https://gitlab.test/api/v4/projects/1/export/download",
38+
"web_url": "https://gitlab.test/gitlab-test/download_export",
39+
},
40+
}
41+
42+
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
43+
rsps.add(
44+
method=responses.POST,
45+
url=re.compile(r".*/api/v4/(groups|projects)/1/export"),
46+
json=accepted_content,
47+
content_type="application/json",
48+
status=202,
49+
)
50+
rsps.add(
51+
method=responses.GET,
52+
url=re.compile(r".*/api/v4/(groups|projects)/1/export/download"),
53+
body=binary_content,
54+
content_type="application/octet-stream",
55+
status=200,
56+
)
57+
# Currently only project export supports status checks
58+
rsps.add(
59+
method=responses.GET,
60+
url="http://localhost/api/v4/projects/1/export",
61+
json=export_status_content,
62+
content_type="application/json",
63+
status=200,
64+
)
65+
yield rsps

gitlab/tests/objects/mocks.py

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
GitLab API: https://docs.gitlab.com/ce/api/appearance.html
3+
"""
4+
5+
import pytest
6+
import responses
7+
8+
9+
title = "GitLab Test Instance"
10+
description = "gitlab-test.example.com"
11+
new_title = "new-title"
12+
new_description = "new-description"
13+
14+
15+
@pytest.fixture
16+
def resp_application_appearance():
17+
content = {
18+
"title": title,
19+
"description": description,
20+
"logo": "/uploads/-/system/appearance/logo/1/logo.png",
21+
"header_logo": "/uploads/-/system/appearance/header_logo/1/header.png",
22+
"favicon": "/uploads/-/system/appearance/favicon/1/favicon.png",
23+
"new_project_guidelines": "Please read the FAQs for help.",
24+
"header_message": "",
25+
"footer_message": "",
26+
"message_background_color": "#e75e40",
27+
"message_font_color": "#ffffff",
28+
"email_header_and_footer_enabled": False,
29+
}
30+
31+
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
32+
rsps.add(
33+
method=responses.GET,
34+
url="http://localhost/api/v4/application/appearance",
35+
json=content,
36+
content_type="application/json",
37+
status=200,
38+
)
39+
40+
updated_content = dict(content)
41+
updated_content["title"] = new_title
42+
updated_content["description"] = new_description
43+
44+
rsps.add(
45+
method=responses.PUT,
46+
url="http://localhost/api/v4/application/appearance",
47+
json=updated_content,
48+
content_type="application/json",
49+
status=200,
50+
)
51+
yield rsps
52+
53+
54+
def test_get_update_appearance(gl, resp_application_appearance):
55+
appearance = gl.appearance.get()
56+
assert appearance.title == title
57+
assert appearance.description == description
58+
appearance.title = new_title
59+
appearance.description = new_description
60+
appearance.save()
61+
assert appearance.title == new_title
62+
assert appearance.description == new_description
63+
64+
65+
def test_update_appearance(gl, resp_application_appearance):
66+
resp = gl.appearance.update(title=new_title, description=new_description)

gitlab/tests/objects/test_application.py

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
GitLab API: https://docs.gitlab.com/ce/api/applications.html
3+
"""
4+
5+
import pytest
6+
import responses
7+
8+
9+
title = "GitLab Test Instance"
10+
description = "gitlab-test.example.com"
11+
new_title = "new-title"
12+
new_description = "new-description"
13+
14+
15+
@pytest.fixture
16+
def resp_application_create():
17+
content = {
18+
"name": "test_app",
19+
"redirect_uri": "http://localhost:8080",
20+
"scopes": ["api", "email"],
21+
}
22+
23+
with responses.RequestsMock() as rsps:
24+
rsps.add(
25+
method=responses.POST,
26+
url="http://localhost/api/v4/applications",
27+
json=content,
28+
content_type="application/json",
29+
status=200,
30+
)
31+
yield rsps
32+
33+
34+
def test_create_application(gl, resp_application_create):
35+
application = gl.applications.create(
36+
{
37+
"name": "test_app",
38+
"redirect_uri": "http://localhost:8080",
39+
"scopes": ["api", "email"],
40+
"confidential": False,
41+
}
42+
)
43+
assert application.name == "test_app"
44+
assert application.redirect_uri == "http://localhost:8080"
45+
assert application.scopes == ["api", "email"]

0 commit comments

Comments
 (0)