From 8bef7749d49efb54be973f316fc8b55140b507bd Mon Sep 17 00:00:00 2001 From: David Brochart Date: Thu, 10 Mar 2022 18:24:51 +0100 Subject: [PATCH] Add post-save hook test --- tests/test_files.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tests/test_files.py b/tests/test_files.py index d318cc4151..a7b063a927 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -91,20 +91,34 @@ async def test_contents_manager(jp_fetch, jp_serverapp, jp_root_dir): assert r.headers["content-type"] == "text/plain; charset=UTF-8" assert r.body.decode() == "foobar" + +async def test_save_hooks(jp_fetch, jp_serverapp): # define a first pre-save hook that will change the content of the file before saving - def hook1(model, path, **kwargs): + def pre_save_hook1(model, **kwargs): model["content"] += " was modified" # define a second pre-save hook that will change the content of the file before saving - # should be called after the first one - def hook2(model, path, **kwargs): + def pre_save_hook2(model, **kwargs): model["content"] += " twice!" + # define a first post-save hook that will change the 'last_modified' date + def post_save_hook1(model, **kwargs): + model["last_modified"] = "yesterday" + + # define a second post-save hook that will change the 'last_modified' date + def post_save_hook2(model, **kwargs): + model["last_modified"] += " or tomorrow!" + # register the pre-save hooks - jp_serverapp.contents_manager.register_pre_save_hook(hook1) - jp_serverapp.contents_manager.register_pre_save_hook(hook2) + jp_serverapp.contents_manager.register_pre_save_hook(pre_save_hook1) + jp_serverapp.contents_manager.register_pre_save_hook(pre_save_hook2) + + # register the post-save hooks + jp_serverapp.contents_manager.register_post_save_hook(post_save_hook1) + jp_serverapp.contents_manager.register_post_save_hook(post_save_hook2) # send a request to save a file, with an original content + # the 'last_modified' returned model field should have been modified by post_save_hook1 then post_save_hook2 r = await jp_fetch( "api/contents/test.txt", method="PUT", @@ -112,8 +126,10 @@ def hook2(model, path, **kwargs): {"format": "text", "path": "test.txt", "type": "file", "content": "original content"} ), ) + assert json.loads(r.body.decode())["last_modified"] == "yesterday or tomorrow!" - # read the file back, the original content should have been modified by hook1 then hook2 + # read the file back + # the original content should have been modified by pre_save_hook1 then pre_save_hook2 r = await jp_fetch("files/test.txt", method="GET") assert r.body.decode() == "original content was modified twice!"