From b33105d5769f457d6805a544f3fdfcd8e64370f1 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 3 Jan 2022 20:16:21 +0100 Subject: [PATCH] Re #348 - Add test for backup, use proper datastore path --- changedetectionio/__init__.py | 17 +++++++++-------- changedetectionio/tests/test_backup.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 changedetectionio/tests/test_backup.py diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py index be1fc6be..6ef97442 100644 --- a/changedetectionio/__init__.py +++ b/changedetectionio/__init__.py @@ -758,7 +758,8 @@ def changedetection_app(config=None, datastore_o=None): from pathlib import Path # Remove any existing backup file, for now we just keep one file - for previous_backup_filename in Path(app.config['datastore_path']).rglob('changedetection-backup-*.zip'): + + for previous_backup_filename in Path(datastore_o.datastore_path).rglob('changedetection-backup-*.zip'): os.unlink(previous_backup_filename) # create a ZipFile object @@ -766,7 +767,7 @@ def changedetection_app(config=None, datastore_o=None): # We only care about UUIDS from the current index file uuids = list(datastore.data['watching'].keys()) - backup_filepath = os.path.join(app.config['datastore_path'], backupname) + backup_filepath = os.path.join(datastore_o.datastore_path, backupname) with zipfile.ZipFile(backup_filepath, "w", compression=zipfile.ZIP_DEFLATED, @@ -776,22 +777,22 @@ def changedetection_app(config=None, datastore_o=None): datastore.sync_to_json() # Add the index - zipObj.write(os.path.join(app.config['datastore_path'], "url-watches.json"), arcname="url-watches.json") + zipObj.write(os.path.join(datastore_o.datastore_path, "url-watches.json"), arcname="url-watches.json") # Add the flask app secret - zipObj.write(os.path.join(app.config['datastore_path'], "secret.txt"), arcname="secret.txt") + zipObj.write(os.path.join(datastore_o.datastore_path, "secret.txt"), arcname="secret.txt") # Add any snapshot data we find, use the full path to access the file, but make the file 'relative' in the Zip. - for txt_file_path in Path(app.config['datastore_path']).rglob('*.txt'): + for txt_file_path in Path(datastore_o.datastore_path).rglob('*.txt'): parent_p = txt_file_path.parent if parent_p.name in uuids: zipObj.write(txt_file_path, - arcname=str(txt_file_path).replace(app.config['datastore_path'], ''), + arcname=str(txt_file_path).replace(datastore_o.datastore_path, ''), compress_type=zipfile.ZIP_DEFLATED, compresslevel=8) # Create a list file with just the URLs, so it's easier to port somewhere else in the future - list_file = os.path.join(app.config['datastore_path'], "url-list.txt") + list_file = os.path.join(datastore_o.datastore_path, "url-list.txt") with open(list_file, "w") as f: for uuid in datastore.data['watching']: url = datastore.data['watching'][uuid]['url'] @@ -803,7 +804,7 @@ def changedetection_app(config=None, datastore_o=None): compress_type=zipfile.ZIP_DEFLATED, compresslevel=8) - return send_from_directory(app.config['datastore_path'], backupname, as_attachment=True) + return send_from_directory(datastore_o.datastore_path, backupname, as_attachment=True) @app.route("/static//", methods=['GET']) def static_content(group, filename): diff --git a/changedetectionio/tests/test_backup.py b/changedetectionio/tests/test_backup.py new file mode 100644 index 00000000..787d7fc0 --- /dev/null +++ b/changedetectionio/tests/test_backup.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 + +import time +from flask import url_for +from urllib.request import urlopen +from . util import set_original_response, set_modified_response, live_server_setup + + +def test_backup(client, live_server): + + live_server_setup(live_server) + + # Give the endpoint time to spin up + time.sleep(1) + + res = client.get( + url_for("get_backup"), + follow_redirects=True + ) + + # Should get the right zip content type + assert res.content_type == "application/zip" + # Should be PK/ZIP stream + assert res.data.count(b'PK') >= 2 +