From 35d0c744541327cb4c9cd7e0c8703e621240ff5d Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 15 Dec 2021 19:54:30 +0100 Subject: [PATCH] Re #308 - Adding test and including settings in clone operation (#309) --- changedetectionio/__init__.py | 4 ++++ changedetectionio/store.py | 19 +++++++++++++---- changedetectionio/tests/test_clone.py | 30 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 changedetectionio/tests/test_clone.py diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py index 88341426..e2e1f677 100644 --- a/changedetectionio/__init__.py +++ b/changedetectionio/__init__.py @@ -793,6 +793,10 @@ def changedetection_app(config=None, datastore_o=None): @login_required def api_clone(): uuid = request.args.get('uuid') + # More for testing, possible to return the first/only + if uuid == 'first': + uuid = list(datastore.data['watching'].keys()).pop() + new_uuid = datastore.clone(uuid) update_q.put(new_uuid) flash('Cloned.') diff --git a/changedetectionio/store.py b/changedetectionio/store.py index bb70672f..7dca4290 100644 --- a/changedetectionio/store.py +++ b/changedetectionio/store.py @@ -260,7 +260,8 @@ class ChangeDetectionStore: def clone(self, uuid): url = self.data['watching'][uuid]['url'] tag = self.data['watching'][uuid]['tag'] - new_uuid = self.add_watch(url=url, tag=tag) + extras = self.data['watching'][uuid] + new_uuid = self.add_watch(url=url, tag=tag, extras=extras) return new_uuid def url_exists(self, url): @@ -318,17 +319,27 @@ class ChangeDetectionStore: self.needs_write = True return changes_removed - def add_watch(self, url, tag): + def add_watch(self, url, tag, extras=None): + if extras is None: + extras = {} + with self.lock: # @todo use a common generic version of this new_uuid = str(uuid_builder.uuid4()) _blank = deepcopy(self.generic_definition) _blank.update({ 'url': url, - 'tag': tag, - 'uuid': new_uuid + 'tag': tag }) + # Incase these are copied across, assume it's a reference and deepcopy() + apply_extras = deepcopy(extras) + for k in ['uuid', 'history', 'last_checked', 'last_changed', 'newest_history_key', 'previous_md5', 'viewed']: + if k in apply_extras: + del apply_extras[k] + + _blank.update(apply_extras) + self.data['watching'][new_uuid] = _blank # Get the directory ready diff --git a/changedetectionio/tests/test_clone.py b/changedetectionio/tests/test_clone.py new file mode 100644 index 00000000..b8856798 --- /dev/null +++ b/changedetectionio/tests/test_clone.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +import time +from flask import url_for +from . util import live_server_setup + + + +def test_trigger_functionality(client, live_server): + + live_server_setup(live_server) + + # Give the endpoint time to spin up + time.sleep(1) + + # Add our URL to the import page + res = client.post( + url_for("import_page"), + data={"urls": "https://changedetection.io"}, + follow_redirects=True + ) + assert b"1 Imported" in res.data + + + res = client.get( + url_for("api_clone", uuid="first"), + follow_redirects=True + ) + + assert b"Cloned." in res.data