Skip to content

Commit

Permalink
Re #348 - Add test for backup, use proper datastore path
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtlmoon committed Jan 3, 2022
1 parent 023951a commit b33105d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
17 changes: 9 additions & 8 deletions changedetectionio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,15 +758,16 @@ def get_backup():
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
backupname = "changedetection-backup-{}.zip".format(int(time.time()))

# 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,
Expand All @@ -776,22 +777,22 @@ def get_backup():
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']
Expand All @@ -803,7 +804,7 @@ def get_backup():
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/<string:group>/<string:filename>", methods=['GET'])
def static_content(group, filename):
Expand Down
25 changes: 25 additions & 0 deletions changedetectionio/tests/test_backup.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b33105d

Please sign in to comment.