skipped 3 lines 4 4 from flask import url_for 5 5 from . util import set_original_response, set_modified_response, set_more_modified_response, live_server_setup 6 6 import logging 7 - from changedetectionio.notification import default_notification_body, default_notification_title 7 + 8 + from changedetectionio.notification import ( 9 + default_notification_body, 10 + default_notification_format, 11 + default_notification_title, 12 + valid_notification_formats, 13 + ) 8 14 9 15 def test_setup(live_server): 10 16 live_server_setup(live_server) skipped 9 lines 20 26 21 27 # Re 360 - new install should have defaults set 22 28 res = client.get(url_for("settings_page")) 29 + notification_url = url_for('test_notification_endpoint', _external=True).replace('http', 'json') 30 + 23 31 assert default_notification_body.encode() in res.data 24 32 assert default_notification_title.encode() in res.data 25 33 34 + ##################### 35 + # Set this up for when we remove the notification from the watch, it should fallback with these details 36 + res = client.post( 37 + url_for("settings_page"), 38 + data={"application-notification_urls": notification_url, 39 + "application-notification_title": "fallback-title "+default_notification_title, 40 + "application-notification_body": "fallback-body "+default_notification_body, 41 + "application-notification_format": default_notification_format, 42 + "requests-time_between_check-minutes": 180, 43 + 'application-fetch_backend': "html_requests"}, 44 + follow_redirects=True 45 + ) 46 + 47 + assert b"Settings updated." in res.data 48 + 26 49 # When test mode is in BASE_URL env mode, we should see this already configured 27 50 env_base_url = os.getenv('BASE_URL', '').strip() 28 51 if len(env_base_url): skipped 18 lines 47 70 48 71 # Goto the edit page, add our ignore text 49 72 # Add our URL to the import page 50 - url = url_for('test_notification_endpoint', _external=True) 51 - notification_url = url.replace('http', 'json') 52 73 53 74 print (">>>> Notification URL: "+notification_url) 54 75 skipped 16 lines 71 92 "url": test_url, 72 93 "tag": "my tag", 73 94 "title": "my title", 74 - # No 'notification_use_default' here, so it's effectively False/off 75 95 "headers": "", 76 96 "fetch_backend": "html_requests"}) 77 97 skipped 81 lines 159 179 # be sure we see it in the output log 160 180 assert b'New ChangeDetection.io Notification - ' + test_url.encode('utf-8') in res.data 161 181 182 + set_original_response() 183 + res = client.post( 184 + url_for("edit_page", uuid="first"), 185 + data={ 186 + "url": test_url, 187 + "tag": "my tag", 188 + "title": "my title", 189 + "notification_urls": '', 190 + "notification_title": '', 191 + "notification_body": '', 192 + "notification_format": default_notification_format, 193 + "fetch_backend": "html_requests"}, 194 + follow_redirects=True 195 + ) 196 + assert b"Updated watch." in res.data 197 + 198 + time.sleep(2) 199 + 200 + # Verify what was sent as a notification, this file should exist 201 + with open("test-datastore/notification.txt", "r") as f: 202 + notification_submission = f.read() 203 + assert "fallback-title" in notification_submission 204 + assert "fallback-body" in notification_submission 205 + 162 206 # cleanup for the next 163 207 client.get( 164 208 url_for("form_delete", uuid="all"), skipped 16 lines 181 225 assert b"Watch added" in res.data 182 226 183 227 # Re #360 some validation 184 - res = client.post( 185 - url_for("edit_page", uuid="first"), 186 - data={"notification_urls": 'json://localhost/foobar', 187 - "notification_title": "", 188 - "notification_body": "", 189 - "notification_format": "Text", 190 - "url": test_url, 191 - "tag": "my tag", 192 - "title": "my title", 193 - "headers": "", 194 - "fetch_backend": "html_requests"}, 195 - follow_redirects=True 196 - ) 197 - assert b"Notification Body and Title is required when a Notification URL is used" in res.data 228 + # res = client.post( 229 + # url_for("edit_page", uuid="first"), 230 + # data={"notification_urls": 'json://localhost/foobar', 231 + # "notification_title": "", 232 + # "notification_body": "", 233 + # "notification_format": "Text", 234 + # "url": test_url, 235 + # "tag": "my tag", 236 + # "title": "my title", 237 + # "headers": "", 238 + # "fetch_backend": "html_requests"}, 239 + # follow_redirects=True 240 + # ) 241 + # assert b"Notification Body and Title is required when a Notification URL is used" in res.data198 242 199 243 # Now adding a wrong token should give us an error 200 244 res = client.post( skipped 16 lines 217 261 follow_redirects=True 218 262 ) 219 263 220 - # Check that the default VS watch specific notification is hit 221 - def test_check_notification_use_default(client, live_server): 222 - set_original_response() 223 - notification_url = url_for('test_notification_endpoint', _external=True).replace('http', 'json') 224 - test_url = url_for('test_endpoint', _external=True) 225 264 226 - res = client.post( 227 - url_for("form_quick_watch_add"), 228 - data={"url": test_url, "tag": ''}, 229 - follow_redirects=True 230 - ) 231 - assert b"Watch added" in res.data 232 265 233 - ## Setup the local one and enable it 234 - res = client.post( 235 - url_for("edit_page", uuid="first"), 236 - data={"notification_urls": notification_url, 237 - "notification_title": "watch-notification", 238 - "notification_body": "watch-body", 239 - 'notification_use_default': "True", 240 - "notification_format": "Text", 241 - "url": test_url, 242 - "tag": "my tag", 243 - "title": "my title", 244 - "headers": "", 245 - "fetch_backend": "html_requests"}, 246 - follow_redirects=True 247 - ) 248 - 249 - res = client.post( 250 - url_for("settings_page"), 251 - data={"application-notification_title": "global-notifications-title", 252 - "application-notification_body": "global-notifications-body\n", 253 - "application-notification_format": "Text", 254 - "application-notification_urls": notification_url, 255 - "requests-time_between_check-minutes": 180, 256 - "fetch_backend": "html_requests" 257 - }, 258 - follow_redirects=True 259 - ) 260 - 261 - # A change should by default trigger a notification of the global-notifications 262 - time.sleep(1) 263 - set_modified_response() 264 - client.get(url_for("form_watch_checknow"), follow_redirects=True) 265 - time.sleep(2) 266 - with open("test-datastore/notification.txt", "r") as f: 267 - assert 'global-notifications-title' in f.read() 268 - 269 - ## Setup the local one and enable it 270 - res = client.post( 271 - url_for("edit_page", uuid="first"), 272 - data={"notification_urls": notification_url, 273 - "notification_title": "watch-notification", 274 - "notification_body": "watch-body", 275 - # No 'notification_use_default' here, so it's effectively False/off = "dont use default, use this one" 276 - "notification_format": "Text", 277 - "url": test_url, 278 - "tag": "my tag", 279 - "title": "my title", 280 - "headers": "", 281 - "fetch_backend": "html_requests"}, 282 - follow_redirects=True 283 - ) 284 - set_original_response() 285 - 286 - client.get(url_for("form_watch_checknow"), follow_redirects=True) 287 - time.sleep(2) 288 - assert os.path.isfile("test-datastore/notification.txt") 289 - with open("test-datastore/notification.txt", "r") as f: 290 - assert 'watch-notification' in f.read() 291 - 292 - 293 - # cleanup for the next 294 - client.get( 295 - url_for("form_delete", uuid="all"), 296 - follow_redirects=True 297 - )