Skip to content

Commit

Permalink
♻️ fix improper test & improper enqueueing for localresponse (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianMindee authored May 24, 2024
1 parent 011b38b commit 2cbe6c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
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

0 comments on commit 2cbe6c3

Please sign in to comment.