Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_files_are_set_in_multiple_variables():
operations = {"variables": {"fileA": None, "fileB": None}}
files_map = {"0": ["variables.fileA"], "1": ["variables.fileB"]}
files = {"0": "A", "1": "B"}
assert combine_multipart_data(operations, files_map, files) == {
"variables": {"fileA": "A", "fileB": "B"}
}
def test_single_file_is_set_in_multiple_variables():
operations = {"variables": {"fileA": None, "fileB": None}}
files_map = {"0": ["variables.fileA", "variables.fileB"]}
files = {"0": True}
assert combine_multipart_data(operations, files_map, files) == {
"variables": {"fileA": True, "fileB": True}
}
def test_error_is_raised_if_file_paths_value_is_not_a_list():
operations = {"variables": {"file": None}}
files_map = {"0": "variables.file"}
files = {"0": True}
with pytest.raises(HttpBadRequestError):
assert combine_multipart_data(operations, files_map, files)
def test_error_is_raised_if_file_described_in_map_is_not_found():
operations = {"variables": {"file": None}}
files_map = {"0": ["variables.file"]}
files = {}
with pytest.raises(HttpBadRequestError):
assert combine_multipart_data(operations, files_map, files)
raise HttpBadRequestError("Malformed request data")
try:
operations = json.loads(form.getvalue("operations"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'operations' multipart field is not a valid JSON"
)
try:
files_map = json.loads(form.getvalue("map"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'map' multipart field is not a valid JSON"
)
return combine_multipart_data(operations, files_map, form)
def extract_data_from_multipart_request(self, request: HttpRequest):
try:
operations = json.loads(request.POST.get("operations"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'operations' multipart field is not a valid JSON"
)
try:
files_map = json.loads(request.POST.get("map"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'map' multipart field is not a valid JSON"
)
return combine_multipart_data(operations, files_map, request.FILES)
raise HttpBadRequestError("Malformed request data")
try:
operations = json.loads(form.getvalue("operations"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'operations' multipart field is not a valid JSON"
)
try:
files_map = json.loads(form.getvalue("map"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'map' multipart field is not a valid JSON"
)
return combine_multipart_data(operations, files_map, form)