Skip to content

♻️ fix improper test & improper enqueueing for localresponse #237

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

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 19 additions & 5 deletions mindee/input/local_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hmac
import io
import json
import os
from pathlib import Path
from typing import Any, BinaryIO, Dict, Union

Expand All @@ -15,18 +16,31 @@ class LocalResponse:
"""File object of the local response."""

def __init__(self, input_file: Union[BinaryIO, str, Path, bytes]):
if isinstance(input_file, BinaryIO):
self._file = input_file
if isinstance(input_file, (BinaryIO, io.BufferedReader)):
str_stripped = (
input_file.read().decode("utf-8").replace("\r", "").replace("\n", "")
)
self._file = io.BytesIO(str_stripped.encode("utf-8"))
self._file.seek(0)
elif isinstance(input_file, (str, Path)):
elif isinstance(input_file, Path) or (
isinstance(input_file, str) and os.path.exists(input_file)
):
with open(input_file, "r", encoding="utf-8") as file:
self._file = io.BytesIO(
file.read().replace("\r", "").replace("\n", "").encode()
)
elif isinstance(input_file, str):
self._file = io.BytesIO(
input_file.replace("\r", "").replace("\n", "").encode("utf-8")
)
elif isinstance(input_file, bytes):
self._file = io.BytesIO(input_file)
str_stripped = (
input_file.decode("utf-8").replace("\r", "").replace("\n", "")
)
self._file = io.BytesIO(str_stripped.encode("utf-8"))
self._file.seek(0)
else:
raise MindeeError("Incompatible type for input.")
raise MindeeError(f"Incompatible type for input '{type(input_file)}'.")

@property
def as_dict(self) -> Dict[str, Any]:
Expand Down
3 changes: 2 additions & 1 deletion tests/Input/test_local_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def file_path():


def test_valid_file_local_response(dummy_secret_key, signature, file_path):
local_response = LocalResponse(file_path)
with open(file_path, "rb") as file:
local_response = LocalResponse(file)
assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
dummy_secret_key, "invalid signature"
Expand Down
Loading