From 8f5cb68319f77a1791a2f57050769e6978c837ca Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Sun, 29 Jan 2023 15:37:42 +0100 Subject: [PATCH] Adding test --- .../tests/test_access_control.py | 52 +++++++++++++++++-- changedetectionio/tests/util.py | 6 +++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/changedetectionio/tests/test_access_control.py b/changedetectionio/tests/test_access_control.py index d84ed5774..f931f66ed 100644 --- a/changedetectionio/tests/test_access_control.py +++ b/changedetectionio/tests/test_access_control.py @@ -1,18 +1,34 @@ +from . util import live_server_setup, extract_UUID_from_client from flask import url_for -from . util import live_server_setup +import time -def test_check_access_control(app, client): +def test_check_access_control(app, client, live_server): # Still doesnt work, but this is closer. + live_server_setup(live_server) with app.test_client(use_cookies=True) as c: # Check we don't have any password protection enabled yet. res = c.get(url_for("settings_page")) assert b"Remove password" not in res.data - # Enable password check. + # add something that we can hit via diff page later + res = c.post( + url_for("import_page"), + data={"urls": url_for('test_random_content_endpoint', _external=True)}, + follow_redirects=True + ) + + assert b"1 Imported" in res.data + time.sleep(2) + res = client.get(url_for("form_watch_checknow"), follow_redirects=True) + assert b'1 watches queued for rechecking.' in res.data + time.sleep(2) + + # Enable password check and diff page access bypass res = c.post( url_for("settings_page"), data={"application-password": "foobar", + "application-shared_diff_access": "True", "requests-time_between_check-minutes": 180, 'application-fetch_backend': "html_requests"}, follow_redirects=True @@ -22,9 +38,15 @@ def test_check_access_control(app, client): # Check we hit the login res = c.get(url_for("index"), follow_redirects=True) - + # Should be logged out assert b"Login" in res.data + # The diff page should return something valid when logged out + res = client.get(url_for("diff_history_page", uuid="first")) + assert b'Random content' in res.data + + + # Menu should not be available yet # assert b"SETTINGS" not in res.data # assert b"BACKUP" not in res.data @@ -109,3 +131,25 @@ def test_check_access_control(app, client): assert b"Password protection enabled" not in res.data + # Now checking the diff access + # Enable password check and diff page access bypass + res = c.post( + url_for("settings_page"), + data={"application-password": "foobar", + # Should be disabled +# "application-shared_diff_access": "True", + "requests-time_between_check-minutes": 180, + 'application-fetch_backend': "html_requests"}, + follow_redirects=True + ) + + assert b"Password protection enabled." in res.data + + # Check we hit the login + res = c.get(url_for("index"), follow_redirects=True) + # Should be logged out + assert b"Login" in res.data + + # The diff page should return something valid when logged out + res = client.get(url_for("diff_history_page", uuid="first")) + assert b'Random content' not in res.data diff --git a/changedetectionio/tests/util.py b/changedetectionio/tests/util.py index c70160092..63f707a08 100644 --- a/changedetectionio/tests/util.py +++ b/changedetectionio/tests/util.py @@ -98,6 +98,12 @@ def wait_for_all_checks(client): def live_server_setup(live_server): + @live_server.app.route('/test-random-content-endpoint') + def test_random_content_endpoint(): + import secrets + return "Random content - {}\n".format(secrets.token_hex(64)) + + @live_server.app.route('/test-endpoint') def test_endpoint(): ctype = request.args.get('content_type')