mirror of
https://github.com/sartography/spiffworkflow-backend.git
synced 2025-02-23 21:08:18 +00:00
do not allow updating files with blank contents w/ burnettk
This commit is contained in:
parent
a3d76b5e85
commit
c94aebacb9
@ -292,9 +292,9 @@ paths:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#components/schemas/File"
|
||||
# process_model_file_save
|
||||
# process_model_file_update
|
||||
put:
|
||||
operationId: spiffworkflow_backend.routes.process_api_blueprint.process_model_file_save
|
||||
operationId: spiffworkflow_backend.routes.process_api_blueprint.process_model_file_update
|
||||
summary: save the contents to the given file
|
||||
tags:
|
||||
- Spec Files
|
||||
|
@ -50,6 +50,7 @@ def process_group_add(body):
|
||||
def process_group_delete(process_group_id):
|
||||
"""Process_group_delete."""
|
||||
ProcessModelService().process_group_delete(process_group_id)
|
||||
return Response(json.dumps({"ok": True}), status=200, mimetype="application/json")
|
||||
|
||||
|
||||
def process_group_update(process_group_id, body):
|
||||
@ -93,7 +94,6 @@ def process_model_add(body):
|
||||
def process_model_delete(process_group_id, process_model_id):
|
||||
"""Process_model_delete."""
|
||||
ProcessModelService().process_model_delete(process_model_id)
|
||||
print("process_model_delete")
|
||||
return Response(json.dumps({"ok": True}), status=200, mimetype="application/json")
|
||||
|
||||
|
||||
@ -142,10 +142,20 @@ def get_file(process_model_id, file_name):
|
||||
return FileSchema().dump(file)
|
||||
|
||||
|
||||
def process_model_file_save(process_model_id, file_name):
|
||||
def process_model_file_update(process_model_id, file_name):
|
||||
"""Process_model_file_save."""
|
||||
process_model = ProcessModelService().get_spec(process_model_id)
|
||||
SpecFileService.update_file(process_model, file_name, request.get_data())
|
||||
|
||||
request_file = get_file_from_request()
|
||||
request_file_contents = request_file.stream.read()
|
||||
if not request_file_contents:
|
||||
raise ApiError(
|
||||
code="file_contents_empty",
|
||||
message="Given request file does not have any content",
|
||||
status_code=400,
|
||||
)
|
||||
|
||||
SpecFileService.update_file(process_model, file_name, request_file_contents)
|
||||
return Response(json.dumps({"ok": True}), status=200, mimetype="application/json")
|
||||
|
||||
|
||||
@ -153,7 +163,7 @@ def add_file(process_model_id):
|
||||
"""Add_file."""
|
||||
process_model_service = ProcessModelService()
|
||||
process_model = process_model_service.get_spec(process_model_id)
|
||||
request_file = connexion.request.files["file"]
|
||||
request_file = get_file_from_request()
|
||||
file = SpecFileService.add_file(
|
||||
process_model, request_file.filename, request_file.stream.read()
|
||||
)
|
||||
@ -226,3 +236,13 @@ def process_instance_list(process_model_id, page=1, per_page=100):
|
||||
},
|
||||
}
|
||||
return Response(json.dumps(response_json), status=200, mimetype="application/json")
|
||||
|
||||
def get_file_from_request():
|
||||
request_file = connexion.request.files.get("file")
|
||||
if not request_file:
|
||||
raise ApiError(
|
||||
code="no_file_given",
|
||||
message="Given request does not contain a file",
|
||||
status_code=400,
|
||||
)
|
||||
return request_file
|
||||
|
@ -156,12 +156,51 @@ def test_process_group_update(app, client: FlaskClient, with_bpmn_file_cleanup):
|
||||
print("test_process_group_update")
|
||||
|
||||
|
||||
def test_process_model_file_save(app, client: FlaskClient, with_bpmn_file_cleanup):
|
||||
"""Test_process_model_file_save."""
|
||||
def test_process_model_file_update_fails_if_no_file_given(app, client: FlaskClient, with_bpmn_file_cleanup):
|
||||
"""Test_process_model_file_update."""
|
||||
create_spec_file(app, client)
|
||||
|
||||
spec = load_test_spec(app, "random_fact")
|
||||
data = {"key1": "THIS DATA"}
|
||||
user = find_or_create_user()
|
||||
response = client.put(
|
||||
"/v1.0/process-models/%s/file/random_fact.svg" % spec.id,
|
||||
data=data,
|
||||
follow_redirects=True,
|
||||
content_type="multipart/form-data",
|
||||
headers=logged_in_headers(user),
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.json["code"] == "no_file_given"
|
||||
|
||||
|
||||
def test_process_model_file_update_fails_if_contents_is_empty(app, client: FlaskClient, with_bpmn_file_cleanup):
|
||||
"""Test_process_model_file_update."""
|
||||
create_spec_file(app, client)
|
||||
|
||||
spec = load_test_spec(app, "random_fact")
|
||||
data = {"file": (io.BytesIO(b""), "random_fact.svg")}
|
||||
user = find_or_create_user()
|
||||
response = client.put(
|
||||
"/v1.0/process-models/%s/file/random_fact.svg" % spec.id,
|
||||
data=data,
|
||||
follow_redirects=True,
|
||||
content_type="multipart/form-data",
|
||||
headers=logged_in_headers(user),
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.json["code"] == "file_contents_empty"
|
||||
|
||||
|
||||
def test_process_model_file_update(app, client: FlaskClient, with_bpmn_file_cleanup):
|
||||
"""Test_process_model_file_update."""
|
||||
original_file = create_spec_file(app, client)
|
||||
|
||||
spec = load_test_spec(app, "random_fact")
|
||||
data = {"file": (io.BytesIO(b"THIS_IS_NEW_DATA"), "random_fact.svg")}
|
||||
new_file_contents = b"THIS_IS_NEW_DATA"
|
||||
data = {"file": (io.BytesIO(new_file_contents), "random_fact.svg")}
|
||||
user = find_or_create_user()
|
||||
response = client.put(
|
||||
"/v1.0/process-models/%s/file/random_fact.svg" % spec.id,
|
||||
@ -181,6 +220,7 @@ def test_process_model_file_save(app, client: FlaskClient, with_bpmn_file_cleanu
|
||||
assert response.status_code == 200
|
||||
updated_file = json.loads(response.get_data(as_text=True))
|
||||
assert original_file != updated_file
|
||||
assert updated_file["file_contents"] == new_file_contents.decode()
|
||||
|
||||
|
||||
def test_get_file(app, client: FlaskClient, with_bpmn_file_cleanup):
|
||||
|
Loading…
x
Reference in New Issue
Block a user