Skip to content

Commit 8aab825

Browse files
committed
WIP Fix tests for new bindings version
1 parent ce25df5 commit 8aab825

23 files changed

+162
-297
lines changed

CHANGES/+python_bindings.removal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Updated the OpenAPI generator version used to generate python bindings to 7.10.0.
2+
This involves stricter client side validation of api calls when using the bindings.

pulp_file/tests/functional/api/test_rbac.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ def _try_action(user, client, action, outcome, *args, **kwargs):
2828
action_api = getattr(client, f"{action}_with_http_info")
2929
try:
3030
with user:
31-
response, status, _ = action_api(*args, **kwargs, _return_http_data_only=False)
32-
if isinstance(response, AsyncOperationResponse):
33-
response = monitor_task(response.task)
31+
response = action_api(*args, **kwargs)
32+
if isinstance(response.data, AsyncOperationResponse):
33+
response.data = monitor_task(response.data.task)
3434
except ApiException as e:
3535
assert e.status == outcome, f"{e}"
3636
else:
37-
assert status == outcome, f"User performed {action} when they shouldn't been able to"
38-
return response
37+
assert (
38+
response.status_code == outcome
39+
), f"User performed {action} when they shouldn't been able to"
40+
return response.data
3941

4042
return _try_action
4143

pulpcore/pytest_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def _random_artifact_factory(size=32, pulp_domain=None):
585585
kwargs["pulp_domain"] = pulp_domain
586586
temp_file = tmp_path / str(uuid.uuid4())
587587
temp_file.write_bytes(os.urandom(size))
588-
return pulpcore_bindings.ArtifactsApi.create(temp_file, **kwargs)
588+
return pulpcore_bindings.ArtifactsApi.create(str(temp_file), **kwargs)
589589

590590
return _random_artifact_factory
591591

pulpcore/tests/functional/api/test_api_docs.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Tests related to the api docs page."""
22

33
import pytest
4-
from pulpcore.client.pulpcore import ApiException
54

65

76
@pytest.fixture(scope="session")
@@ -15,7 +14,7 @@ def test_valid_credentials(pulpcore_bindings, pulp_docs_url):
1514
1615
Assert the API documentation is returned.
1716
"""
18-
response = pulpcore_bindings.client.request("GET", pulp_docs_url)
17+
response = pulpcore_bindings.client.rest_client.request("GET", pulp_docs_url)
1918
assert response.status == 200
2019

2120

@@ -26,7 +25,7 @@ def test_no_credentials(pulpcore_bindings, pulp_docs_url, anonymous_user):
2625
Assert the API documentation is returned.
2726
"""
2827
with anonymous_user:
29-
response = pulpcore_bindings.client.request("GET", pulp_docs_url)
28+
response = pulpcore_bindings.client.rest_client.request("GET", pulp_docs_url)
3029
assert response.status == 200
3130

3231

@@ -36,7 +35,6 @@ def test_http_method(pulpcore_bindings, pulp_docs_url):
3635
3736
Assert an error is returned.
3837
"""
39-
with pytest.raises(ApiException) as e:
40-
pulpcore_bindings.client.request("POST", pulp_docs_url)
38+
response = pulpcore_bindings.client.rest_client.request("POST", pulp_docs_url)
4139

42-
assert e.value.status == 405
40+
assert response.status == 405

pulpcore/tests/functional/api/test_auth.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
<https://docs.pulpproject.org/restapi.html#section/Authentication>`_.
55
"""
66

7-
import pytest
87
import json
9-
108
from base64 import b64encode
11-
from pulpcore.client.pulpcore import ApiException
9+
10+
import pytest
1211

1312
from pulpcore.app import settings
1413

@@ -20,9 +19,9 @@ def test_base_auth_success(pulpcore_bindings, pulp_admin_user):
2019
Assert that a response indicating success is returned.
2120
"""
2221
with pulp_admin_user:
23-
response, status, headers = pulpcore_bindings.ArtifactsApi.list_with_http_info()
24-
assert status == 200
25-
assert headers["Content-Type"] == "application/json"
22+
response = pulpcore_bindings.ArtifactsApi.list_with_http_info()
23+
assert response.status_code == 200
24+
assert response.headers["Content-Type"] == "application/json"
2625
# Maybe test correlation ID as well?
2726

2827

@@ -33,7 +32,7 @@ def test_base_auth_failure(pulpcore_bindings, invalid_user):
3332
Assert that a response indicating failure is returned.
3433
"""
3534
with invalid_user:
36-
with pytest.raises(ApiException) as e:
35+
with pytest.raises(pulpcore_bindings.ApiException) as e:
3736
pulpcore_bindings.ArtifactsApi.list()
3837

3938
assert e.value.status == 401
@@ -50,7 +49,7 @@ def test_base_auth_required(pulpcore_bindings, anonymous_user):
5049
Assert that a response indicating failure is returned.
5150
"""
5251
with anonymous_user:
53-
with pytest.raises(ApiException) as e:
52+
with pytest.raises(pulpcore_bindings.ApiException) as e:
5453
pulpcore_bindings.ArtifactsApi.list()
5554

5655
assert e.value.status == 401
@@ -79,8 +78,8 @@ def test_jq_header_remote_auth(pulpcore_bindings, anonymous_user):
7978
encoded_header = b64encode(bytes(header_content, "ascii"))
8079

8180
pulpcore_bindings.ArtifactsApi.api_client.default_headers["x-rh-identity"] = encoded_header
82-
_, status, _ = pulpcore_bindings.ArtifactsApi.list_with_http_info()
83-
assert status == 200
81+
response = pulpcore_bindings.ArtifactsApi.list_with_http_info()
82+
assert response.status_code == 200
8483

8584

8685
@pytest.mark.parallel
@@ -106,7 +105,7 @@ def test_jq_header_remote_auth_denied_by_wrong_header(pulpcore_bindings, anonymo
106105
encoded_header
107106
)
108107

109-
with pytest.raises(ApiException) as exception:
108+
with pytest.raises(pulpcore_bindings.ApiException) as exception:
110109
pulpcore_bindings.ArtifactsApi.list()
111110

112111
assert exception.value.status == 401
@@ -127,7 +126,7 @@ def test_jq_header_remote_auth_denied_by_wrong_content(pulpcore_bindings, anonym
127126

128127
pulpcore_bindings.ArtifactsApi.api_client.default_headers["x-rh-identity"] = encoded_header
129128

130-
with pytest.raises(ApiException) as exception:
129+
with pytest.raises(pulpcore_bindings.ApiException) as exception:
131130
pulpcore_bindings.ArtifactsApi.list()
132131

133132
assert exception.value.status == 401
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
def test_correlation_id(cid, pulpcore_bindings, monitor_task):
22
"""Test that a correlation can be passed as a header and logged."""
3-
response, status, headers = pulpcore_bindings.OrphansCleanupApi.cleanup_with_http_info({})
4-
monitor_task(response.task)
5-
task = pulpcore_bindings.TasksApi.read(response.task)
6-
assert headers["Correlation-ID"] == cid
3+
response = pulpcore_bindings.OrphansCleanupApi.cleanup_with_http_info({})
4+
task = monitor_task(response.data.task)
5+
assert response.headers["Correlation-ID"] == cid
76
assert task.logging_cid == cid

pulpcore/tests/functional/api/test_crd_artifacts.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import pytest
88

99
from django.conf import settings
10-
from pulpcore.client.pulpcore import ApiException
1110

1211

1312
@pytest.fixture
@@ -21,12 +20,12 @@ def pulpcore_random_file(tmp_path):
2120
return {"name": name, "size": 1024, "digest": digest}
2221

2322

24-
def _do_upload_valid_attrs(artifact_api, file, data):
23+
def _do_upload_valid_attrs(pulpcore_bindings, file, data):
2524
"""Upload a file with the given attributes."""
26-
artifact = artifact_api.create(file, **data)
25+
artifact = pulpcore_bindings.ArtifactsApi.create(str(file), **data)
2726
# assumes ALLOWED_CONTENT_CHECKSUMS does NOT contain "md5"
2827
assert artifact.md5 is None, "MD5 {}".format(artifact.md5)
29-
read_artifact = artifact_api.read(artifact.pulp_href)
28+
read_artifact = pulpcore_bindings.ArtifactsApi.read(artifact.pulp_href)
3029
# assumes ALLOWED_CONTENT_CHECKSUMS does NOT contain "md5"
3130
assert read_artifact.md5 is None
3231
for key, val in artifact.to_dict().items():
@@ -54,9 +53,7 @@ def test_upload_valid_attrs(pulpcore_bindings, pulpcore_random_file, monitor_tas
5453
monitor_task(
5554
pulpcore_bindings.OrphansCleanupApi.cleanup({"orphan_protection_time": 0}).task
5655
)
57-
_do_upload_valid_attrs(
58-
pulpcore_bindings.ArtifactsApi, pulpcore_random_file["name"], data
59-
)
56+
_do_upload_valid_attrs(pulpcore_bindings, pulpcore_random_file["name"], data)
6057

6158

6259
def test_upload_empty_file(pulpcore_bindings, tmp_path, monitor_task):
@@ -79,7 +76,7 @@ def test_upload_empty_file(pulpcore_bindings, tmp_path, monitor_task):
7976
pulpcore_bindings.OrphansCleanupApi.cleanup({"orphan_protection_time": 0}).task
8077
)
8178
data = {key: file_attrs[key] for key in keys}
82-
_do_upload_valid_attrs(pulpcore_bindings.ArtifactsApi, file, data)
79+
_do_upload_valid_attrs(pulpcore_bindings, file, data)
8380

8481

8582
@pytest.mark.parallel
@@ -98,16 +95,16 @@ def test_upload_invalid_attrs(pulpcore_bindings, pulpcore_random_file):
9895
for i in range(1, len(file_attrs) + 1):
9996
for keys in itertools.combinations(file_attrs, i):
10097
data = {key: file_attrs[key] for key in keys}
101-
_do_upload_invalid_attrs(pulpcore_bindings.ArtifactsApi, pulpcore_random_file, data)
98+
_do_upload_invalid_attrs(pulpcore_bindings, pulpcore_random_file, data)
10299

103100

104-
def _do_upload_invalid_attrs(artifact_api, file, data):
101+
def _do_upload_invalid_attrs(pulpcore_bindings, file, data):
105102
"""Upload a file with the given attributes."""
106-
with pytest.raises(ApiException) as e:
107-
artifact_api.create(file["name"], **data)
103+
with pytest.raises(pulpcore_bindings.ApiException) as e:
104+
pulpcore_bindings.ArtifactsApi.create(str(file["name"]), **data)
108105

109106
assert e.value.status == 400
110-
artifacts = artifact_api.list()
107+
artifacts = pulpcore_bindings.ArtifactsApi.list()
111108
for artifact in artifacts.results:
112109
assert artifact.sha256 != file["digest"]
113110

@@ -119,8 +116,8 @@ def test_upload_md5(pulpcore_bindings, pulpcore_random_file):
119116
Assumes ALLOWED_CONTENT_CHECKSUMS does NOT contain ``md5``
120117
"""
121118
file_attrs = {"md5": str(uuid.uuid4()), "size": pulpcore_random_file["size"]}
122-
with pytest.raises(ApiException) as e:
123-
pulpcore_bindings.ArtifactsApi.create(pulpcore_random_file["name"], **file_attrs)
119+
with pytest.raises(pulpcore_bindings.ApiException) as e:
120+
pulpcore_bindings.ArtifactsApi.create(str(pulpcore_random_file["name"]), **file_attrs)
124121

125122
assert e.value.status == 400
126123

@@ -141,7 +138,7 @@ def test_upload_mixed_attrs(pulpcore_bindings, pulpcore_random_file):
141138
{"sha256": str(uuid.uuid4()), "size": pulpcore_random_file["size"]},
142139
)
143140
for data in invalid_data:
144-
_do_upload_invalid_attrs(pulpcore_bindings.ArtifactsApi, pulpcore_random_file, data)
141+
_do_upload_invalid_attrs(pulpcore_bindings, pulpcore_random_file, data)
145142

146143

147144
@pytest.mark.parallel
@@ -152,19 +149,19 @@ def test_delete_artifact(pulpcore_bindings, pulpcore_random_file, gen_user):
152149
pytest.skip("this test only works for filesystem storage")
153150
media_root = settings.MEDIA_ROOT
154151

155-
artifact = pulpcore_bindings.ArtifactsApi.create(pulpcore_random_file["name"])
152+
artifact = pulpcore_bindings.ArtifactsApi.create(str(pulpcore_random_file["name"]))
156153
path_to_file = os.path.join(media_root, artifact.file)
157154
file_exists = os.path.exists(path_to_file)
158155
assert file_exists
159156

160157
# try to delete as a regular (non-admin) user
161158
regular_user = gen_user()
162-
with regular_user, pytest.raises(ApiException) as e:
159+
with regular_user, pytest.raises(pulpcore_bindings.ApiException) as e:
163160
pulpcore_bindings.ArtifactsApi.delete(artifact.pulp_href)
164161
assert e.value.status == 403
165162

166163
# destroy artifact api is not allowed, even for admins
167-
with pytest.raises(ApiException) as e:
164+
with pytest.raises(pulpcore_bindings.ApiException) as e:
168165
pulpcore_bindings.ArtifactsApi.delete(artifact.pulp_href)
169166
assert e.value.status == 403
170167

@@ -173,8 +170,8 @@ def test_delete_artifact(pulpcore_bindings, pulpcore_random_file, gen_user):
173170
def test_upload_artifact_as_a_regular_user(pulpcore_bindings, gen_user, pulpcore_random_file):
174171
"""Regular users do not have permission to upload artifacts."""
175172
regular_user = gen_user()
176-
with regular_user, pytest.raises(ApiException) as e:
177-
pulpcore_bindings.ArtifactsApi.create(pulpcore_random_file["name"])
173+
with regular_user, pytest.raises(pulpcore_bindings.ApiException) as e:
174+
pulpcore_bindings.ArtifactsApi.create(str(pulpcore_random_file["name"]))
178175
assert e.value.status == 403
179176

180177

@@ -184,14 +181,14 @@ def test_list_and_retrieve_artifact_as_a_regular_user(
184181
):
185182
"""Regular users are not allowed to list and/or retrieve artifacts."""
186183
regular_user = gen_user()
187-
artifact = pulpcore_bindings.ArtifactsApi.create(pulpcore_random_file["name"])
184+
artifact = pulpcore_bindings.ArtifactsApi.create(str(pulpcore_random_file["name"]))
188185

189186
# check if list is not allowed
190-
with regular_user, pytest.raises(ApiException) as e:
187+
with regular_user, pytest.raises(pulpcore_bindings.ApiException) as e:
191188
pulpcore_bindings.ArtifactsApi.list()
192189
assert e.value.status == 403
193190

194191
# check if retrieve is also not allowed
195-
with regular_user, pytest.raises(ApiException) as e:
192+
with regular_user, pytest.raises(pulpcore_bindings.ApiException) as e:
196193
pulpcore_bindings.ArtifactsApi.read(artifact.pulp_href)
197194
assert e.value.status == 403

pulpcore/tests/functional/api/test_filter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import random
33
import uuid
44

5-
from pulpcore.client.pulpcore.exceptions import ApiException, ApiTypeError
6-
75

86
# Warning: Do not use HEX digits here!
97
NAMES = (
@@ -97,7 +95,7 @@ def test_pulp_id_href_filter(
9795
assert redi_results.count == 0
9896

9997
# Test that filter fails when not a valid type
100-
with pytest.raises(ApiException) as exc:
98+
with pytest.raises(pulpcore_bindings.ApiException) as exc:
10199
pulpcore_bindings.ContentguardsApi.list(**{filter: ["hello"]})
102100

103101
assert exc.value.status == 400
@@ -135,7 +133,7 @@ def test_pulp_type_filter(
135133
assert "core/rbac" in c.pulp_href or "core/content_redirect" in c.pulp_href
136134

137135
# Test filtering by invalid pulp_type
138-
with pytest.raises(ApiException) as exc:
136+
with pytest.raises(pulpcore_bindings.ApiException) as exc:
139137
pulpcore_bindings.ContentguardsApi.list(pulp_type__in=["i.invalid"])
140138

141139
assert exc.value.status == 400
@@ -145,17 +143,19 @@ def test_pulp_type_filter(
145143
)
146144

147145
# Test filter does not exist on child viewsets
148-
with pytest.raises(ApiTypeError) as exc:
146+
with pytest.raises((pulpcore_bindings.ApiTypeError, ValueError)) as exc:
149147
pulpcore_bindings.ContentguardsRbacApi.list(pulp_type__in=["core.rbac"])
150148

151-
assert "Got an unexpected keyword argument 'pulp_type__in'" in str(exc.value)
149+
assert "nexpected keyword argument" in str(exc.value)
150+
assert "pulp_type__in" in str(exc.value)
152151

153-
with pytest.raises(ApiTypeError) as exc:
152+
with pytest.raises((pulpcore_bindings.ApiTypeError, ValueError)) as exc:
154153
pulpcore_bindings.ContentguardsContentRedirectApi.list(
155154
pulp_type__in=["core.content_redirect"]
156155
)
157156

158-
assert "Got an unexpected keyword argument 'pulp_type__in'" in str(exc.value)
157+
assert "nexpected keyword argument" in str(exc.value)
158+
assert "pulp_type__in" in str(exc.value)
159159

160160
@pytest.mark.parallel
161161
@pytest.mark.parametrize(
@@ -234,7 +234,7 @@ def test_q_filter_invalid(
234234
):
235235
"""Tests the "q" filter with invalid expressions."""
236236

237-
with pytest.raises(ApiException) as exc_info:
237+
with pytest.raises(pulpcore_bindings.ApiException) as exc_info:
238238
pulpcore_bindings.ContentguardsApi.list(q=q.format(*NAMES))
239239
assert exc_info.value.status == 400
240240
assert exc_info.value.body == exception_message

pulpcore/tests/functional/api/test_openpgp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ def test_key_upload(tmpdir, openpgp_keyring_factory, pulpcore_bindings, monitor_
133133
bob_pub.write_text(BOB_PUB, "UTF-8")
134134

135135
result = pulpcore_bindings.ContentOpenpgpPublickeyApi.create(
136-
file=alice_pub, repository=keyring.pulp_href
136+
file=str(alice_pub), repository=keyring.pulp_href
137137
)
138138
monitor_task(result.task)
139139
result = pulpcore_bindings.ContentOpenpgpPublickeyApi.create(
140-
file=bob_pub, repository=keyring.pulp_href
140+
file=str(bob_pub), repository=keyring.pulp_href
141141
)
142142
monitor_task(result.task)
143143
result = pulpcore_bindings.ContentOpenpgpPublickeyApi.create(
144-
file=alice_revoked, repository=keyring.pulp_href
144+
file=str(alice_revoked), repository=keyring.pulp_href
145145
)
146146
monitor_task(result.task)

pulpcore/tests/functional/api/test_status.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from django.conf import settings
66
from jsonschema import validate
7-
from pulpcore.client.pulpcore import ApiException
87

98

109
STATUS = {
@@ -96,10 +95,10 @@ def test_post_authenticated(
9695
assert post_attr not in attrs
9796
# Try anyway to POST to /status/
9897
status_url = f"{pulp_api_v3_url}status/"
99-
with pytest.raises(ApiException) as e:
100-
pulpcore_bindings.client.request("POST", status_url, headers={"User-Agent": test_path})
101-
102-
assert e.value.status == 405
98+
response = pulpcore_bindings.client.rest_client.request(
99+
"POST", status_url, headers={"User-Agent": test_path}
100+
)
101+
assert response.status == 405
103102

104103

105104
@pytest.mark.parallel

0 commit comments

Comments
 (0)