diff --git a/sachet/server/views_common.py b/sachet/server/views_common.py index c32fe14..91d7b68 100644 --- a/sachet/server/views_common.py +++ b/sachet/server/views_common.py @@ -124,7 +124,12 @@ class ModelAPI(MethodView): return jsonify(resp), 404 patch_json = request.get_json() - orig_json = model_schema.dump(model) + + dump = model_schema.dump(model) + orig_json = {} + for field, opts in model_schema.fields.items(): + if not opts.dump_only and not opts.load_only: + orig_json[field] = dump[field] new_json = patch(orig_json, patch_json) diff --git a/tests/test_files.py b/tests/test_files.py index 660c964..961e4d4 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -58,6 +58,54 @@ class TestSuite: ) assert resp.status_code == 404 + def test_modification(self, client, users, auth, rand): + # create share + resp = client.post( + "/files", headers=auth("jeff"), json={"file_name": "content.bin"} + ) + data = resp.get_json() + url = data.get("url") + + upload_data = rand.randbytes(4000) + new_data = rand.randbytes(4000) + + # upload file to share + resp = client.post( + url + "/content", + headers=auth("jeff"), + data={ + "upload": FileStorage(stream=BytesIO(upload_data), filename="upload") + }, + content_type="multipart/form-data", + ) + + # modify metadata + resp = client.patch( + url, + headers=auth("jeff"), + json={"file_name": "new_bin.bin"}, + ) + assert resp.status_code == 200 + + # modify file contents + resp = client.put( + url + "/content", + headers=auth("jeff"), + data={ + "upload": FileStorage(stream=BytesIO(new_data), filename="upload") + }, + ) + assert resp.status_code == 200 + + # read file + resp = client.get( + url + "/content", + headers=auth("jeff"), + ) + assert resp.data == new_data + assert "filename=new_bin.bin" in resp.headers["Content-Disposition"].split("; ") + + def test_invalid(self, client, users, auth, rand): """Test invalid requests."""