Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RHINENG-15180): Fix CSV data export with unicode chars #2178

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update test_handle_create_export_unicode
rh-pre-commit.version: 2.3.2
rh-pre-commit.check-secrets: ENABLED
fstavela committed Jan 17, 2025
commit 4faa29b74aa5abe825b15d6bb2cc8f5d78b136b9
13 changes: 13 additions & 0 deletions tests/helpers/api_utils.py
Original file line number Diff line number Diff line change
@@ -2,15 +2,18 @@
import math
from base64 import b64encode
from datetime import timedelta
from http import HTTPStatus
from itertools import product
from struct import unpack
from typing import Any
from urllib.parse import parse_qs
from urllib.parse import quote_plus as url_quote
from urllib.parse import urlencode
from urllib.parse import urlsplit
from urllib.parse import urlunsplit

import dateutil.parser
from requests import Response

from app.auth.identity import IdentityType
from tests.helpers.test_utils import now
@@ -610,3 +613,13 @@ def assert_resource_types_pagination(
assert links["next"] is None

assert links["last"] == f"{expected_path_base}?per_page={expected_per_page}&page={expected_number_of_pages}"


def mocked_export_post(_self: Any, url: str, *, data: bytes, **_: Any) -> Response:
# This will raise UnicodeDecodeError if not correctly encoded or AttributeError if data is str
data.decode("utf-8")
response = Response()
response.url = url
response.status_code = HTTPStatus.ACCEPTED
response._content = b"Export successful"
return response
7 changes: 3 additions & 4 deletions tests/test_export_service.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@
from app.auth.identity import Identity
from app.culling import Timestamps
from app.culling import _Config as CullingConfig
from app.exceptions import InventoryException
from app.queue.export_service import _format_export_data
from app.queue.export_service import create_export
from app.queue.export_service import get_host_list
@@ -23,6 +22,7 @@
from tests.helpers.api_utils import HOST_READ_ALLOWED_RBAC_RESPONSE_FILES
from tests.helpers.api_utils import HOST_READ_PROHIBITED_RBAC_RESPONSE_FILES
from tests.helpers.api_utils import create_mock_rbac_response
from tests.helpers.api_utils import mocked_export_post
from tests.helpers.db_utils import db_host
from tests.helpers.test_utils import USER_IDENTITY

@@ -38,6 +38,7 @@ def test_handle_create_export_happy_path(mock_post, db_create_host, flask_app, i


@pytest.mark.parametrize("format", ("json", "csv"))
@mock.patch("requests.Session.post", new=mocked_export_post)
def test_handle_create_export_unicode(db_create_host, flask_app, inventory_config, format):
with flask_app.app.app_context():
host_to_create = db_host()
@@ -47,9 +48,7 @@ def test_handle_create_export_unicode(db_create_host, flask_app, inventory_confi
validated_msg = parse_export_service_message(es_utils.create_export_message_mock(format=format))
base64_x_rh_identity = validated_msg["data"]["resource_request"]["x_rh_identity"]

# Should raise InventoryException, not UnicodeEncodeError
with pytest.raises(InventoryException):
create_export(validated_msg, base64_x_rh_identity, inventory_config)
assert create_export(validated_msg, base64_x_rh_identity, inventory_config)


@mock.patch("requests.Session.post", autospec=True)