#!/usr/bin/env python3 from .util import set_original_response, live_server_setup, wait_for_all_checks from flask import url_for import io from zipfile import ZipFile import re import time def test_backup(client, live_server, measure_memory_usage, datastore_path): # live_server_setup(live_server) # Setup on conftest per function set_original_response(datastore_path=datastore_path) # Add our URL to the import page res = client.post( url_for("imports.import_page"), data={"urls": url_for('test_endpoint', _external=True)+"?somechar=őőőőőőőő"}, follow_redirects=True ) assert b"1 Imported" in res.data wait_for_all_checks(client) # Launch the thread in the background to create the backup res = client.get( url_for("backups.request_backup"), follow_redirects=True ) time.sleep(4) res = client.get( url_for("backups.index"), follow_redirects=True ) # Can see the download link to the backup assert b'= 2 backup = ZipFile(io.BytesIO(res.data)) l = backup.namelist() # Check for UUID-based txt files (history and snapshot) uuid4hex_txt = re.compile('^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}.*txt', re.I) txt_files = list(filter(uuid4hex_txt.match, l)) # Should be two txt files in the archive (history and the snapshot) assert len(txt_files) == 2 # Check for watch.json files (new format) uuid4hex_json = re.compile('^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}/watch\.json$', re.I) json_files = list(filter(uuid4hex_json.match, l)) # Should be one watch.json file in the archive (the imported watch) assert len(json_files) == 1, f"Expected 1 watch.json file, found {len(json_files)}: {json_files}" # Check for changedetection.json (settings file) assert 'changedetection.json' in l, "changedetection.json should be in backup" # Get the latest one res = client.get( url_for("backups.remove_backups"), follow_redirects=True ) assert b'No backups found.' in res.data