Revision 9d825eaa
Added by koszko over 1 year ago
| test/unit/test_patterns_query_manager.py | ||
|---|---|---|
| 18 | 18 |
# CC0 1.0 Universal License for more details. |
| 19 | 19 |
|
| 20 | 20 |
import pytest |
| 21 |
import re |
|
| 22 | 21 |
import json |
| 23 | 22 |
from selenium.webdriver.support.ui import WebDriverWait |
| 24 | 23 |
|
| ... | ... | |
| 35 | 34 |
'payloads': payloads |
| 36 | 35 |
} |
| 37 | 36 |
|
| 38 |
content_script_tree_re = re.compile(r'this.haketilo_pattern_tree = (.*);') |
|
| 39 |
def extract_tree_data(content_script_text): |
|
| 40 |
return json.loads(content_script_tree_re.search(content_script_text)[1]) |
|
| 41 |
|
|
| 42 |
content_script_mapping_re = re.compile(r'this.haketilo_mappings = (.*);') |
|
| 43 |
def extract_mappings_data(content_script_text): |
|
| 44 |
return json.loads(content_script_mapping_re.search(content_script_text)[1]) |
|
| 37 |
def get_content_script_values(driver, content_script): |
|
| 38 |
""" |
|
| 39 |
Allow easy extraction of 'this.something = ...' values from generated |
|
| 40 |
content script and verify the content script is syntactically correct. |
|
| 41 |
""" |
|
| 42 |
return driver.execute_script( |
|
| 43 |
''' |
|
| 44 |
function value_holder() {
|
|
| 45 |
%s; |
|
| 46 |
return this; |
|
| 47 |
} |
|
| 48 |
return value_holder.call({});
|
|
| 49 |
''' % content_script) |
|
| 45 | 50 |
|
| 46 | 51 |
# Fields that are not relevant for testing are omitted from these mapping |
| 47 | 52 |
# definitions. |
| ... | ... | |
| 87 | 92 |
execute_in_page( |
| 88 | 93 |
''' |
| 89 | 94 |
const [initial_mappings, initial_blocking] = arguments.slice(0, 2); |
| 90 |
let mappingchange, blockingchange; |
|
| 95 |
let mappingchange, blockingchange, settingchange;
|
|
| 91 | 96 |
|
| 92 | 97 |
haketilodb.track.mapping = function (cb) {
|
| 93 | 98 |
mappingchange = cb; |
| ... | ... | |
| 99 | 104 |
|
| 100 | 105 |
return [{}, initial_blocking];
|
| 101 | 106 |
} |
| 107 |
haketilodb.track.settings = function (cb) {
|
|
| 108 |
settingchange = cb; |
|
| 109 |
|
|
| 110 |
return [{}, [{name: "default_allow", value: true}]];
|
|
| 111 |
} |
|
| 102 | 112 |
|
| 103 | 113 |
let last_script; |
| 104 | 114 |
let unregister_called = 0; |
| ... | ... | |
| 110 | 120 |
} |
| 111 | 121 |
browser = {contentScripts: {register: register_mock}};
|
| 112 | 122 |
|
| 113 |
returnval(start()); |
|
| 123 |
returnval(start("abracadabra"));
|
|
| 114 | 124 |
''', |
| 115 | 125 |
sample_mappings[0:2], sample_blocking[0:2]) |
| 116 | 126 |
|
| ... | ... | |
| 125 | 135 |
dict([('~allow', 1),
|
| 126 | 136 |
*[(f'inject-{fruit}', {'identifier': f'{fruit}-{best_pattern}'})
|
| 127 | 137 |
for fruit in ('banana', 'orange')]])
|
| 128 |
assert tree == extract_tree_data(content_script) |
|
| 138 |
cs_values = get_content_script_values(driver, content_script) |
|
| 139 |
assert cs_values['haketilo_secret'] == 'abracadabra' |
|
| 140 |
assert cs_values['haketilo_pattern_tree'] == tree |
|
| 141 |
assert cs_values['haketilo_default_allow'] == True |
|
| 129 | 142 |
assert deregistrations == 0 |
| 130 | 143 |
|
| 131 | 144 |
def condition_all_added(driver): |
| 132 | 145 |
last_script = execute_in_page('returnval(last_script);')
|
| 146 |
cs_values = get_content_script_values(driver, last_script) |
|
| 133 | 147 |
nums = [i for i in range(len(sample_blocking)) if i > 1] |
| 134 |
return (all([('gotmyown%sdoma' % i) in last_script for i in nums]) and
|
|
| 148 |
return (cs_values['haketilo_default_allow'] == False and |
|
| 149 |
all([('gotmyown%sdoma' % i) in last_script for i in nums]) and
|
|
| 135 | 150 |
all([m['identifier'] in last_script for m in sample_mappings])) |
| 136 | 151 |
|
| 137 | 152 |
execute_in_page( |
| 138 | 153 |
''' |
| 154 |
const new_setting_val = {name: "default_allow", value: false};
|
|
| 155 |
settingchange({key: "default_allow", new_val: new_setting_val});
|
|
| 139 | 156 |
for (const mapping of arguments[0]) |
| 140 | 157 |
mappingchange({key: mapping.identifier, new_val: mapping});
|
| 141 | 158 |
for (const blocking of arguments[1]) |
| ... | ... | |
| 163 | 180 |
|
| 164 | 181 |
def condition_all_removed(driver): |
| 165 | 182 |
content_script = execute_in_page('returnval(last_script);')
|
| 166 |
return extract_tree_data(content_script) == {}
|
|
| 183 |
cs_values = get_content_script_values(driver, content_script) |
|
| 184 |
return cs_values['haketilo_pattern_tree'] == {}
|
|
| 167 | 185 |
|
| 168 | 186 |
execute_in_page( |
| 169 | 187 |
''' |
Also available in: Unified diff
add new root content script