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 16, 2024
1 parent 670263c commit 1bc3611
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 317 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.
8 changes: 4 additions & 4 deletions pulp_file/tests/functional/api/test_crud_content_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ def test_create_file_content_from_chunked_upload(
# Upload the file and generate content
upload = gen_object_with_cleanup(pulpcore_bindings.UploadsApi, {"size": 256})
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_1, content_range="bytes 0-127/256"
upload_href=upload.pulp_href, file=str(file_1), content_range="bytes 0-127/256"
)
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_2, content_range="bytes 128-255/256"
upload_href=upload.pulp_href, file=str(file_2), content_range="bytes 128-255/256"
)
most_recent_path = str(uuid.uuid4())
response = file_bindings.ContentFilesApi.create(
Expand All @@ -239,10 +239,10 @@ def test_create_file_content_from_chunked_upload(
# Attempt to create a duplicate content by re-using the most recent relative path
upload = gen_object_with_cleanup(pulpcore_bindings.UploadsApi, {"size": 256})
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_1, content_range="bytes 0-127/256"
upload_href=upload.pulp_href, file=str(file_1), content_range="bytes 0-127/256"
)
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_2, content_range="bytes 128-255/256"
upload_href=upload.pulp_href, file=str(file_2), content_range="bytes 128-255/256"
)
response = file_bindings.ContentFilesApi.create(
upload=upload.pulp_href, relative_path=most_recent_path
Expand Down
10 changes: 0 additions & 10 deletions pulp_file/tests/functional/api/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,6 @@ def test_model_partial_update(file_repository_factory, file_bindings, monitor_ta
assert file_repo.pulp_labels == labels


@pytest.mark.parallel
def test_invalid_label_type(file_repository_factory):
"""Test that label doesn't accept non-dicts"""

with pytest.raises(ApiException) as e_info:
labels = "key_a" # str instead of dict
file_repository_factory(name=str(uuid4()), pulp_labels=labels)
assert e_info.value.status == 400


@pytest.mark.parallel
def test_invalid_labels(file_repository_factory):
"""Test that label keys and values are validated."""
Expand Down
12 changes: 7 additions & 5 deletions pulp_file/tests/functional/api/test_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ def _try_action(user, client, action, outcome, *args, **kwargs):
action_api = getattr(client, f"{action}_with_http_info")
try:
with user:
response, status, _ = action_api(*args, **kwargs, _return_http_data_only=False)
if isinstance(response, AsyncOperationResponse):
response = monitor_task(response.task)
response = action_api(*args, **kwargs)
if isinstance(response.data, AsyncOperationResponse):
response.data = monitor_task(response.data.task)
except ApiException as e:
assert e.status == outcome, f"{e}"
else:
assert status == outcome, f"User performed {action} when they shouldn't been able to"
return response
assert (
response.status_code == outcome
), f"User performed {action} when they shouldn't been able to"
return response.data

return _try_action

Expand Down
4 changes: 2 additions & 2 deletions pulp_file/tests/functional/api/test_remote_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_http_sync_ssl_tls_validation_off(
Test file on_demand sync with https:// serving from an untrusted certificate.
"""
remote_on_demand = file_remote_ssl_factory(
manifest_path=basic_manifest_path, policy="on_demand", tls_validation="false"
manifest_path=basic_manifest_path, policy="on_demand", tls_validation=False
)

_run_basic_sync_and_assert(
Expand All @@ -79,7 +79,7 @@ def test_http_sync_ssl_tls_validation_on(
Test file on_demand sync with https:// and a client connection configured to trust it.
"""
remote_on_demand = file_remote_ssl_factory(
manifest_path=basic_manifest_path, policy="on_demand", tls_validation="true"
manifest_path=basic_manifest_path, policy="on_demand", tls_validation=True
)

_run_basic_sync_and_assert(
Expand Down
9 changes: 5 additions & 4 deletions pulp_file/tests/functional/api/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ def test_sync_file_protocol_handler(
gen_object_with_cleanup,
monitor_task,
fixtures_cfg,
tmppath,
wget_recursive_download_on_host,
):
"""Test syncing from a file repository with the file:// protocol handler"""
wget_recursive_download_on_host(urljoin(fixtures_cfg.remote_fixtures_origin, "file/"), "/tmp")
wget_recursive_download_on_host(urljoin(fixtures_cfg.remote_fixtures_origin, "file/"), tmppath)
remote_kwargs = {
"url": "file:///tmp/file/PULP_MANIFEST",
"url": f"file:///{tmppath}/file/PULP_MANIFEST",
"policy": "immediate",
"name": str(uuid.uuid4()),
}
remote = gen_object_with_cleanup(file_bindings.RemotesFileApi, remote_kwargs)
files = set(os.listdir("/tmp/file/"))
files = set(os.listdir(tmppath / "file"))

body = RepositorySyncURL(remote=remote.pulp_href)
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)

# test that all the files are still present
assert set(os.listdir("/tmp/file/")) == files
assert set(os.listdir(tmppath / "file")) == files

file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)
assert file_repo.latest_version_href.endswith("/versions/1/")
Expand Down
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
Loading

0 comments on commit 1bc3611

Please sign in to comment.