■ ■ ■ ■ ■ ■
changedetectionio/tests/test_xpath_selector.py
| 1 | + | #!/usr/bin/python3 |
| 2 | + | |
| 3 | + | import time |
| 4 | + | from flask import url_for |
| 5 | + | from . util import live_server_setup |
| 6 | + | |
| 7 | + | from ..html_tools import * |
| 8 | + | |
| 9 | + | def test_setup(live_server): |
| 10 | + | live_server_setup(live_server) |
| 11 | + | |
| 12 | + | def set_original_response(): |
| 13 | + | test_return_data = """<html> |
| 14 | + | <body> |
| 15 | + | Some initial text</br> |
| 16 | + | <p>Which is across multiple lines</p> |
| 17 | + | </br> |
| 18 | + | So let's see what happens. </br> |
| 19 | + | <div class="sametext">Some text thats the same</div> |
| 20 | + | <div class="changetext">Some text that will change</div> |
| 21 | + | </body> |
| 22 | + | </html> |
| 23 | + | """ |
| 24 | + | |
| 25 | + | with open("test-datastore/endpoint-content.txt", "w") as f: |
| 26 | + | f.write(test_return_data) |
| 27 | + | return None |
| 28 | + | |
| 29 | + | def set_modified_response(): |
| 30 | + | test_return_data = """<html> |
| 31 | + | <body> |
| 32 | + | Some initial text</br> |
| 33 | + | <p>Which is across multiple lines</p> |
| 34 | + | </br> |
| 35 | + | So let's see what happens. THIS CHANGES AND SHOULDNT TRIGGER A CHANGE</br> |
| 36 | + | <div class="sametext">Some text thats the same</div> |
| 37 | + | <div class="changetext">Some new text</div> |
| 38 | + | </body> |
| 39 | + | </html> |
| 40 | + | """ |
| 41 | + | |
| 42 | + | with open("test-datastore/endpoint-content.txt", "w") as f: |
| 43 | + | f.write(test_return_data) |
| 44 | + | |
| 45 | + | return None |
| 46 | + | |
| 47 | + | |
| 48 | + | def test_check_markup_xpath_filter_restriction(client, live_server): |
| 49 | + | sleep_time_for_fetch_thread = 3 |
| 50 | + | |
| 51 | + | xpath_filter = "//*[contains(@class, 'sametext')]" |
| 52 | + | |
| 53 | + | set_original_response() |
| 54 | + | |
| 55 | + | # Give the endpoint time to spin up |
| 56 | + | time.sleep(1) |
| 57 | + | |
| 58 | + | # Add our URL to the import page |
| 59 | + | test_url = url_for('test_endpoint', _external=True) |
| 60 | + | res = client.post( |
| 61 | + | url_for("import_page"), |
| 62 | + | data={"urls": test_url}, |
| 63 | + | follow_redirects=True |
| 64 | + | ) |
| 65 | + | assert b"1 Imported" in res.data |
| 66 | + | |
| 67 | + | # Trigger a check |
| 68 | + | client.get(url_for("api_watch_checknow"), follow_redirects=True) |
| 69 | + | |
| 70 | + | # Give the thread time to pick it up |
| 71 | + | time.sleep(sleep_time_for_fetch_thread) |
| 72 | + | |
| 73 | + | # Goto the edit page, add our ignore text |
| 74 | + | # Add our URL to the import page |
| 75 | + | res = client.post( |
| 76 | + | url_for("edit_page", uuid="first"), |
| 77 | + | data={"css_filter": xpath_filter, "url": test_url, "tag": "", "headers": "", 'fetch_backend': "html_requests"}, |
| 78 | + | follow_redirects=True |
| 79 | + | ) |
| 80 | + | assert b"Updated watch." in res.data |
| 81 | + | |
| 82 | + | # Give the thread time to pick it up |
| 83 | + | time.sleep(sleep_time_for_fetch_thread) |
| 84 | + | |
| 85 | + | # view it/reset state back to viewed |
| 86 | + | client.get(url_for("diff_history_page", uuid="first"), follow_redirects=True) |
| 87 | + | |
| 88 | + | # Make a change |
| 89 | + | set_modified_response() |
| 90 | + | |
| 91 | + | # Trigger a check |
| 92 | + | client.get(url_for("api_watch_checknow"), follow_redirects=True) |
| 93 | + | # Give the thread time to pick it up |
| 94 | + | time.sleep(sleep_time_for_fetch_thread) |
| 95 | + | |
| 96 | + | res = client.get(url_for("index")) |
| 97 | + | assert b'unviewed' not in res.data |
| 98 | + | |
| 99 | + | def test_xpath_validation(client, live_server): |
| 100 | + | |
| 101 | + | # Give the endpoint time to spin up |
| 102 | + | time.sleep(1) |
| 103 | + | |
| 104 | + | # Add our URL to the import page |
| 105 | + | test_url = url_for('test_endpoint', _external=True) |
| 106 | + | res = client.post( |
| 107 | + | url_for("import_page"), |
| 108 | + | data={"urls": test_url}, |
| 109 | + | follow_redirects=True |
| 110 | + | ) |
| 111 | + | assert b"1 Imported" in res.data |
| 112 | + | |
| 113 | + | res = client.post( |
| 114 | + | url_for("edit_page", uuid="first"), |
| 115 | + | data={"css_filter": "/something horrible", "url": test_url, "tag": "", "headers": "", 'fetch_backend': "html_requests"}, |
| 116 | + | follow_redirects=True |
| 117 | + | ) |
| 118 | + | assert b"is not a valid XPath expression" in res.data |