1
|
# SPDX-License-Identifier: CC0-1.0
|
2
|
|
3
|
"""
|
4
|
Haketilo unit tests - repository querying
|
5
|
"""
|
6
|
|
7
|
# This file is part of Haketilo
|
8
|
#
|
9
|
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
|
10
|
#
|
11
|
# This program is free software: you can redistribute it and/or modify
|
12
|
# it under the terms of the CC0 1.0 Universal License as published by
|
13
|
# the Creative Commons Corporation.
|
14
|
#
|
15
|
# This program is distributed in the hope that it will be useful,
|
16
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
# CC0 1.0 Universal License for more details.
|
19
|
|
20
|
import pytest
|
21
|
import json
|
22
|
from selenium.webdriver.support.ui import WebDriverWait
|
23
|
from selenium.common.exceptions import ElementNotInteractableException
|
24
|
|
25
|
from ..extension_crafting import ExtraHTML
|
26
|
from ..script_loader import load_script
|
27
|
from .utils import *
|
28
|
|
29
|
def reload_with_target(driver, target):
|
30
|
current_url = driver.execute_script('return location.href')
|
31
|
driver.execute_script(
|
32
|
'''
|
33
|
window.location.href = arguments[0];
|
34
|
window.location.reload();
|
35
|
''',
|
36
|
f'{current_url}#{target}')
|
37
|
|
38
|
unprivileged_page_info = {
|
39
|
'url': 'https://example_a.com/something',
|
40
|
'allow': False
|
41
|
}
|
42
|
|
43
|
mapping_page_info = {
|
44
|
**unprivileged_page_info,
|
45
|
'mapping': 'm1',
|
46
|
'payload': {'identifier': 'res1'}
|
47
|
}
|
48
|
|
49
|
mocked_page_infos = {
|
50
|
'privileged': {
|
51
|
'url': 'moz-extension://<some-id>/file.html',
|
52
|
'privileged': True
|
53
|
},
|
54
|
'blocked_default': unprivileged_page_info,
|
55
|
'allowed_default': {
|
56
|
**unprivileged_page_info,
|
57
|
'allow': True
|
58
|
},
|
59
|
'blocked_rule': {
|
60
|
**unprivileged_page_info,
|
61
|
'mapping': '~allow'
|
62
|
},
|
63
|
'allowed_rule': {
|
64
|
**unprivileged_page_info,
|
65
|
'allow': True,
|
66
|
'mapping': '~allow'
|
67
|
},
|
68
|
'mapping': mapping_page_info,
|
69
|
'error_deciding_policy': {
|
70
|
**mapping_page_info,
|
71
|
'error': {'haketilo_error_type': 'deciding_policy'}
|
72
|
},
|
73
|
'error_missing': {
|
74
|
**mapping_page_info,
|
75
|
'error': {'haketilo_error_type': 'missing', 'id': 'some-missing-res'}
|
76
|
},
|
77
|
'error_circular': {
|
78
|
**mapping_page_info,
|
79
|
'error': {'haketilo_error_type': 'circular', 'id': 'some-circular-res'}
|
80
|
},
|
81
|
'error_db': {
|
82
|
**mapping_page_info,
|
83
|
'error': {'haketilo_error_type': 'db'}
|
84
|
},
|
85
|
'error_other': {
|
86
|
**mapping_page_info,
|
87
|
'error': {'haketilo_error_type': 'other'}
|
88
|
}
|
89
|
}
|
90
|
|
91
|
tab_mock_js = '''
|
92
|
;
|
93
|
const mocked_page_info = (%s)[/#mock_page_info-(.*)$/.exec(document.URL)[1]];
|
94
|
browser.tabs.sendMessage = async function(tab_id, msg) {
|
95
|
const this_tab_id = (await browser.tabs.getCurrent()).id;
|
96
|
if (tab_id !== this_tab_id)
|
97
|
throw `not current tab id (${tab_id} instead of ${this_tab_id})`;
|
98
|
|
99
|
if (msg[0] === "page_info") {
|
100
|
return mocked_page_info;
|
101
|
} else if (msg[0] === "repo_query") {
|
102
|
const response = await fetch(msg[1]);
|
103
|
if (!response)
|
104
|
return {error: "Something happened :o"};
|
105
|
|
106
|
const result = {ok: response.ok, status: response.status};
|
107
|
try {
|
108
|
result.json = await response.json();
|
109
|
} catch(e) {
|
110
|
result.error_json = "" + e;
|
111
|
}
|
112
|
return result;
|
113
|
} else {
|
114
|
throw `bad sendMessage message type: '${msg[0]}'`;
|
115
|
}
|
116
|
}
|
117
|
|
118
|
const old_tabs_query = browser.tabs.query;
|
119
|
browser.tabs.query = async function(query) {
|
120
|
const tabs = await old_tabs_query(query);
|
121
|
tabs.forEach(t => t.url = mocked_page_info.url);
|
122
|
return tabs;
|
123
|
}
|
124
|
''' % json.dumps(mocked_page_infos)
|
125
|
|
126
|
popup_ext_data = {
|
127
|
'background_script': broker_js,
|
128
|
'extra_html': ExtraHTML(
|
129
|
'html/popup.html',
|
130
|
{
|
131
|
'common/browser.js': tab_mock_js,
|
132
|
'common/indexeddb.js': '; set_repo("https://hydril.la/");'
|
133
|
},
|
134
|
wrap_into_htmldoc=False
|
135
|
),
|
136
|
'navigate_to': 'html/popup.html'
|
137
|
}
|
138
|
|
139
|
@pytest.mark.ext_data(popup_ext_data)
|
140
|
@pytest.mark.usefixtures('webextension')
|
141
|
@pytest.mark.parametrize('page_info_key', ['', *mocked_page_infos.keys()])
|
142
|
def test_popup_display(driver, execute_in_page, page_info_key):
|
143
|
"""
|
144
|
Test popup viewing while on a page. Test parametrized with different
|
145
|
possible values of page_info object passed in message from the content
|
146
|
script.
|
147
|
"""
|
148
|
reload_with_target(driver, f'mock_page_info-{page_info_key}')
|
149
|
|
150
|
def get_nodes_by_id(driver):
|
151
|
by_id = driver.execute_script(
|
152
|
'''
|
153
|
const nodes = [...document.querySelectorAll("[id]")];
|
154
|
const reductor = (ob, node) => Object.assign(ob, {[node.id]: node});
|
155
|
return nodes.reduce(reductor, {});
|
156
|
''');
|
157
|
return by_id if by_id and 'repo_query_container' in by_id else None
|
158
|
|
159
|
by_id = WebDriverWait(driver, 10).until(get_nodes_by_id)
|
160
|
|
161
|
if page_info_key == '':
|
162
|
error_msg = 'Page info not avaialable. Try reloading the page.'
|
163
|
error_msg_shown = lambda d: by_id['loading_info'].text == error_msg
|
164
|
WebDriverWait(driver, 10).until(error_msg_shown)
|
165
|
return
|
166
|
|
167
|
WebDriverWait(driver, 10).until(lambda d: by_id['info_form'].is_displayed())
|
168
|
assert (page_info_key == 'privileged') == \
|
169
|
by_id['privileged_page_info'].is_displayed()
|
170
|
assert (page_info_key == 'privileged') ^ \
|
171
|
by_id['unprivileged_page_info'].is_displayed()
|
172
|
assert by_id['page_url'].text == mocked_page_infos[page_info_key]['url']
|
173
|
assert not by_id['repo_query_container'].is_displayed()
|
174
|
|
175
|
if 'allow' in page_info_key:
|
176
|
assert by_id['scripts_blocked'].text.lower() == 'no'
|
177
|
elif page_info_key != 'privileged':
|
178
|
assert by_id['scripts_blocked'].text.lower() == 'yes'
|
179
|
|
180
|
payload_text = by_id['injected_payload'].text
|
181
|
if page_info_key == 'mapping':
|
182
|
assert payload_text == 'res1'
|
183
|
elif page_info_key == 'error_missing':
|
184
|
assert payload_text == \
|
185
|
"None (error: resource with id 'some-missing-res' missing from the database)"
|
186
|
elif page_info_key == 'error_circular':
|
187
|
assert payload_text == \
|
188
|
"None (error: circular dependency of resource with id 'some-circular-res' on itself)"
|
189
|
elif page_info_key == 'error_db':
|
190
|
assert payload_text == \
|
191
|
'None (error: failure reading Haketilo internal database)'
|
192
|
elif page_info_key == 'error_other':
|
193
|
assert payload_text == \
|
194
|
'None (error: unknown failure occured)'
|
195
|
elif page_info_key != 'privileged':
|
196
|
assert payload_text == 'None'
|
197
|
|
198
|
mapping_text = by_id['mapping_used'].text
|
199
|
|
200
|
if page_info_key == 'error_deciding_policy':
|
201
|
assert mapping_text == 'None (error occured when determining policy)'
|
202
|
elif page_info_key == 'mapping' or page_info_key.startswith('error'):
|
203
|
assert mapping_text == 'm1'
|
204
|
|
205
|
if 'allowed' in page_info_key:
|
206
|
assert 'None (scripts allowed by' in mapping_text
|
207
|
elif 'blocked' in page_info_key:
|
208
|
assert 'None (scripts blocked by' in mapping_text
|
209
|
|
210
|
if 'rule' in page_info_key:
|
211
|
assert 'by a rule)' in mapping_text
|
212
|
elif 'default' in page_info_key:
|
213
|
assert 'by default policy)' in mapping_text
|
214
|
|
215
|
@pytest.mark.ext_data(popup_ext_data)
|
216
|
@pytest.mark.usefixtures('webextension')
|
217
|
def test_popup_repo_query(driver, execute_in_page):
|
218
|
"""
|
219
|
Test opening and closing the repo query view in popup.
|
220
|
"""
|
221
|
reload_with_target(driver, f'mock_page_info-blocked_rule')
|
222
|
|
223
|
driver.implicitly_wait(10)
|
224
|
search_but = driver.find_element_by_id("search_resources_but")
|
225
|
driver.implicitly_wait(0)
|
226
|
|
227
|
# For unknown reasons waiting for search_but.is_displayed() to return True
|
228
|
# does not guarantee the button will be interactable afterwards under newer
|
229
|
# browsers. Hence, this workaround.
|
230
|
def click_search_but(driver):
|
231
|
try:
|
232
|
search_but.click()
|
233
|
return True
|
234
|
except ElementNotInteractableException:
|
235
|
pass
|
236
|
|
237
|
WebDriverWait(driver, 10).until(click_search_but)
|
238
|
|
239
|
containers = dict([(name, driver.find_element_by_id(f'{name}_container'))
|
240
|
for name in ('page_info', 'repo_query')])
|
241
|
assert not containers['page_info'].is_displayed()
|
242
|
assert containers['repo_query'].is_displayed()
|
243
|
shown = lambda d: 'https://hydril.la/' in containers['repo_query'].text
|
244
|
WebDriverWait(driver, 10).until(shown)
|
245
|
|
246
|
# Click the "Show results" button.
|
247
|
selector = '.repo_query_buttons > button:first-child'
|
248
|
driver.find_element_by_css_selector(selector).click()
|
249
|
shown = lambda d: 'MAPPING_A' in containers['repo_query'].text
|
250
|
WebDriverWait(driver, 10).until(shown)
|
251
|
|
252
|
# Click the "Cancel" button
|
253
|
selector = '.repo_query_bottom_buttons > button'
|
254
|
driver.find_element_by_css_selector(selector).click()
|
255
|
assert containers['page_info'].is_displayed()
|
256
|
assert not containers['repo_query'].is_displayed()
|
257
|
|
258
|
@pytest.mark.ext_data(popup_ext_data)
|
259
|
@pytest.mark.usefixtures('webextension')
|
260
|
def test_popup_settings_opening(driver, execute_in_page):
|
261
|
"""
|
262
|
Test opening the settings page from popup through button click.
|
263
|
"""
|
264
|
driver.find_element_by_id("settings_but").click()
|
265
|
|
266
|
first_handle = driver.current_window_handle
|
267
|
WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == 2)
|
268
|
new_handle = [h for h in driver.window_handles if h != first_handle][0]
|
269
|
|
270
|
driver.switch_to.window(new_handle)
|
271
|
driver.implicitly_wait(10)
|
272
|
assert "Extension's options page for testing" in \
|
273
|
driver.find_element_by_tag_name("h1").text
|