Skip to content

Commit

Permalink
WIP Fix tests for new bindings version
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Dec 13, 2024
1 parent ce25df5 commit a2bff51
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 292 deletions.
2 changes: 2 additions & 0 deletions CHANGES/+python_bindings.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Updated the OpenAPI generator version used to generate python bindings to 7.10.0.
This involves stricter client side validation of api calls when using the bindings.
2 changes: 1 addition & 1 deletion pulpcore/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def _random_artifact_factory(size=32, pulp_domain=None):
kwargs["pulp_domain"] = pulp_domain
temp_file = tmp_path / str(uuid.uuid4())
temp_file.write_bytes(os.urandom(size))
return pulpcore_bindings.ArtifactsApi.create(temp_file, **kwargs)
return pulpcore_bindings.ArtifactsApi.create(str(temp_file), **kwargs)

return _random_artifact_factory

Expand Down
10 changes: 4 additions & 6 deletions pulpcore/tests/functional/api/test_api_docs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Tests related to the api docs page."""

import pytest
from pulpcore.client.pulpcore import ApiException


@pytest.fixture(scope="session")
Expand All @@ -15,7 +14,7 @@ def test_valid_credentials(pulpcore_bindings, pulp_docs_url):
Assert the API documentation is returned.
"""
response = pulpcore_bindings.client.request("GET", pulp_docs_url)
response = pulpcore_bindings.client.rest_client.request("GET", pulp_docs_url)
assert response.status == 200


Expand All @@ -26,7 +25,7 @@ def test_no_credentials(pulpcore_bindings, pulp_docs_url, anonymous_user):
Assert the API documentation is returned.
"""
with anonymous_user:
response = pulpcore_bindings.client.request("GET", pulp_docs_url)
response = pulpcore_bindings.client.rest_client.request("GET", pulp_docs_url)
assert response.status == 200


Expand All @@ -36,7 +35,6 @@ def test_http_method(pulpcore_bindings, pulp_docs_url):
Assert an error is returned.
"""
with pytest.raises(ApiException) as e:
pulpcore_bindings.client.request("POST", pulp_docs_url)
response = pulpcore_bindings.client.rest_client.request("POST", pulp_docs_url)

assert e.value.status == 405
assert response.status == 405
23 changes: 11 additions & 12 deletions pulpcore/tests/functional/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
<https://docs.pulpproject.org/restapi.html#section/Authentication>`_.
"""

import pytest
import json

from base64 import b64encode
from pulpcore.client.pulpcore import ApiException

import pytest

from pulpcore.app import settings

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


Expand All @@ -33,7 +32,7 @@ def test_base_auth_failure(pulpcore_bindings, invalid_user):
Assert that a response indicating failure is returned.
"""
with invalid_user:
with pytest.raises(ApiException) as e:
with pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.list()

assert e.value.status == 401
Expand All @@ -50,7 +49,7 @@ def test_base_auth_required(pulpcore_bindings, anonymous_user):
Assert that a response indicating failure is returned.
"""
with anonymous_user:
with pytest.raises(ApiException) as e:
with pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.list()

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

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


@pytest.mark.parallel
Expand All @@ -106,7 +105,7 @@ def test_jq_header_remote_auth_denied_by_wrong_header(pulpcore_bindings, anonymo
encoded_header
)

with pytest.raises(ApiException) as exception:
with pytest.raises(pulpcore_bindings.ApiException) as exception:
pulpcore_bindings.ArtifactsApi.list()

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

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

with pytest.raises(ApiException) as exception:
with pytest.raises(pulpcore_bindings.ApiException) as exception:
pulpcore_bindings.ArtifactsApi.list()

assert exception.value.status == 401
7 changes: 3 additions & 4 deletions pulpcore/tests/functional/api/test_correlation_id.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
def test_correlation_id(cid, pulpcore_bindings, monitor_task):
"""Test that a correlation can be passed as a header and logged."""
response, status, headers = pulpcore_bindings.OrphansCleanupApi.cleanup_with_http_info({})
monitor_task(response.task)
task = pulpcore_bindings.TasksApi.read(response.task)
assert headers["Correlation-ID"] == cid
response = pulpcore_bindings.OrphansCleanupApi.cleanup_with_http_info({})
task = monitor_task(response.data.task)
assert response.headers["Correlation-ID"] == cid
assert task.logging_cid == cid
45 changes: 21 additions & 24 deletions pulpcore/tests/functional/api/test_crd_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest

from django.conf import settings
from pulpcore.client.pulpcore import ApiException


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


def _do_upload_valid_attrs(artifact_api, file, data):
def _do_upload_valid_attrs(pulpcore_bindings, file, data):
"""Upload a file with the given attributes."""
artifact = artifact_api.create(file, **data)
artifact = pulpcore_bindings.ArtifactsApi.create(str(file), **data)
# assumes ALLOWED_CONTENT_CHECKSUMS does NOT contain "md5"
assert artifact.md5 is None, "MD5 {}".format(artifact.md5)
read_artifact = artifact_api.read(artifact.pulp_href)
read_artifact = pulpcore_bindings.ArtifactsApi.read(artifact.pulp_href)
# assumes ALLOWED_CONTENT_CHECKSUMS does NOT contain "md5"
assert read_artifact.md5 is None
for key, val in artifact.to_dict().items():
Expand Down Expand Up @@ -54,9 +53,7 @@ def test_upload_valid_attrs(pulpcore_bindings, pulpcore_random_file, monitor_tas
monitor_task(
pulpcore_bindings.OrphansCleanupApi.cleanup({"orphan_protection_time": 0}).task
)
_do_upload_valid_attrs(
pulpcore_bindings.ArtifactsApi, pulpcore_random_file["name"], data
)
_do_upload_valid_attrs(pulpcore_bindings, pulpcore_random_file["name"], data)


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


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


def _do_upload_invalid_attrs(artifact_api, file, data):
def _do_upload_invalid_attrs(pulpcore_bindings, file, data):
"""Upload a file with the given attributes."""
with pytest.raises(ApiException) as e:
artifact_api.create(file["name"], **data)
with pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.create(str(file["name"]), **data)

assert e.value.status == 400
artifacts = artifact_api.list()
artifacts = pulpcore_bindings.ArtifactsApi.list()
for artifact in artifacts.results:
assert artifact.sha256 != file["digest"]

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

assert e.value.status == 400

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


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

artifact = pulpcore_bindings.ArtifactsApi.create(pulpcore_random_file["name"])
artifact = pulpcore_bindings.ArtifactsApi.create(str(pulpcore_random_file["name"]))
path_to_file = os.path.join(media_root, artifact.file)
file_exists = os.path.exists(path_to_file)
assert file_exists

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

# destroy artifact api is not allowed, even for admins
with pytest.raises(ApiException) as e:
with pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.delete(artifact.pulp_href)
assert e.value.status == 403

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


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

# check if list is not allowed
with regular_user, pytest.raises(ApiException) as e:
with regular_user, pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.list()
assert e.value.status == 403

# check if retrieve is also not allowed
with regular_user, pytest.raises(ApiException) as e:
with regular_user, pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.read(artifact.pulp_href)
assert e.value.status == 403
18 changes: 9 additions & 9 deletions pulpcore/tests/functional/api/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import random
import uuid

from pulpcore.client.pulpcore.exceptions import ApiException, ApiTypeError


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

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

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

# Test filtering by invalid pulp_type
with pytest.raises(ApiException) as exc:
with pytest.raises(pulpcore_bindings.ApiException) as exc:
pulpcore_bindings.ContentguardsApi.list(pulp_type__in=["i.invalid"])

assert exc.value.status == 400
Expand All @@ -145,17 +143,19 @@ def test_pulp_type_filter(
)

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

assert "Got an unexpected keyword argument 'pulp_type__in'" in str(exc.value)
assert "nexpected keyword argument" in str(exc.value)
assert "pulp_type__in" in str(exc.value)

with pytest.raises(ApiTypeError) as exc:
with pytest.raises((pulpcore_bindings.ApiTypeError, ValueError)) as exc:
pulpcore_bindings.ContentguardsContentRedirectApi.list(
pulp_type__in=["core.content_redirect"]
)

assert "Got an unexpected keyword argument 'pulp_type__in'" in str(exc.value)
assert "nexpected keyword argument" in str(exc.value)
assert "pulp_type__in" in str(exc.value)

@pytest.mark.parallel
@pytest.mark.parametrize(
Expand Down Expand Up @@ -234,7 +234,7 @@ def test_q_filter_invalid(
):
"""Tests the "q" filter with invalid expressions."""

with pytest.raises(ApiException) as exc_info:
with pytest.raises(pulpcore_bindings.ApiException) as exc_info:
pulpcore_bindings.ContentguardsApi.list(q=q.format(*NAMES))
assert exc_info.value.status == 400
assert exc_info.value.body == exception_message
6 changes: 3 additions & 3 deletions pulpcore/tests/functional/api/test_openpgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ def test_key_upload(tmpdir, openpgp_keyring_factory, pulpcore_bindings, monitor_
bob_pub.write_text(BOB_PUB, "UTF-8")

result = pulpcore_bindings.ContentOpenpgpPublickeyApi.create(
file=alice_pub, repository=keyring.pulp_href
file=str(alice_pub), repository=keyring.pulp_href
)
monitor_task(result.task)
result = pulpcore_bindings.ContentOpenpgpPublickeyApi.create(
file=bob_pub, repository=keyring.pulp_href
file=str(bob_pub), repository=keyring.pulp_href
)
monitor_task(result.task)
result = pulpcore_bindings.ContentOpenpgpPublickeyApi.create(
file=alice_revoked, repository=keyring.pulp_href
file=str(alice_revoked), repository=keyring.pulp_href
)
monitor_task(result.task)
9 changes: 4 additions & 5 deletions pulpcore/tests/functional/api/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.conf import settings
from jsonschema import validate
from pulpcore.client.pulpcore import ApiException


STATUS = {
Expand Down Expand Up @@ -96,10 +95,10 @@ def test_post_authenticated(
assert post_attr not in attrs
# Try anyway to POST to /status/
status_url = f"{pulp_api_v3_url}status/"
with pytest.raises(ApiException) as e:
pulpcore_bindings.client.request("POST", status_url, headers={"User-Agent": test_path})

assert e.value.status == 405
response = pulpcore_bindings.client.rest_client.request(
"POST", status_url, headers={"User-Agent": test_path}
)
assert response.status == 405


@pytest.mark.parallel
Expand Down
Loading

0 comments on commit a2bff51

Please sign in to comment.