1 |
42fe4405
|
Wojtek Kosior
|
# 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 |
|
|
|
24 |
|
|
from ..extension_crafting import ExtraHTML
|
25 |
|
|
from ..script_loader import load_script
|
26 |
|
|
from .utils import *
|
27 |
|
|
|
28 |
|
|
unprivileged_page_info = {
|
29 |
|
|
'url': 'https://example_a.com/something',
|
30 |
|
|
'allow': False
|
31 |
|
|
}
|
32 |
|
|
|
33 |
ea9df6c7
|
Wojtek Kosior
|
mapping_page_info = {
|
34 |
|
|
**unprivileged_page_info,
|
35 |
|
|
'mapping': 'm1',
|
36 |
|
|
'payload': {'identifier': 'res1'}
|
37 |
|
|
}
|
38 |
|
|
|
39 |
42fe4405
|
Wojtek Kosior
|
mocked_page_infos = {
|
40 |
|
|
'privileged': {
|
41 |
|
|
'url': 'moz-extension://<some-id>/file.html',
|
42 |
|
|
'privileged': True
|
43 |
|
|
},
|
44 |
|
|
'blocked_default': unprivileged_page_info,
|
45 |
|
|
'allowed_default': {
|
46 |
|
|
**unprivileged_page_info,
|
47 |
|
|
'allow': True
|
48 |
|
|
},
|
49 |
|
|
'blocked_rule': {
|
50 |
|
|
**unprivileged_page_info,
|
51 |
|
|
'mapping': '~allow'
|
52 |
|
|
},
|
53 |
|
|
'allowed_rule': {
|
54 |
|
|
**unprivileged_page_info,
|
55 |
|
|
'allow': True,
|
56 |
|
|
'mapping': '~allow'
|
57 |
|
|
},
|
58 |
ea9df6c7
|
Wojtek Kosior
|
'mapping': mapping_page_info,
|
59 |
|
|
'error_deciding_policy': {
|
60 |
|
|
**mapping_page_info,
|
61 |
|
|
'error': {'haketilo_error_type': 'deciding_policy'}
|
62 |
9d825eaa
|
Wojtek Kosior
|
},
|
63 |
ea9df6c7
|
Wojtek Kosior
|
'error_missing': {
|
64 |
|
|
**mapping_page_info,
|
65 |
|
|
'error': {'haketilo_error_type': 'missing', 'id': 'some-missing-res'}
|
66 |
|
|
},
|
67 |
|
|
'error_circular': {
|
68 |
|
|
**mapping_page_info,
|
69 |
|
|
'error': {'haketilo_error_type': 'circular', 'id': 'some-circular-res'}
|
70 |
|
|
},
|
71 |
|
|
'error_db': {
|
72 |
|
|
**mapping_page_info,
|
73 |
|
|
'error': {'haketilo_error_type': 'db'}
|
74 |
|
|
},
|
75 |
|
|
'error_other': {
|
76 |
|
|
**mapping_page_info,
|
77 |
|
|
'error': {'haketilo_error_type': 'other'}
|
78 |
42fe4405
|
Wojtek Kosior
|
}
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
tab_mock_js = '''
|
82 |
|
|
;
|
83 |
|
|
const mocked_page_info = (%s)[/#mock_page_info-(.*)$/.exec(document.URL)[1]];
|
84 |
|
|
browser.tabs.sendMessage = async function(tab_id, msg) {
|
85 |
|
|
const this_tab_id = (await browser.tabs.getCurrent()).id;
|
86 |
|
|
if (tab_id !== this_tab_id)
|
87 |
|
|
throw `not current tab id (${tab_id} instead of ${this_tab_id})`;
|
88 |
|
|
|
89 |
|
|
if (msg[0] === "page_info") {
|
90 |
|
|
return mocked_page_info;
|
91 |
|
|
} else if (msg[0] === "repo_query") {
|
92 |
|
|
const response = await fetch(msg[1]);
|
93 |
|
|
if (!response)
|
94 |
|
|
return {error: "Something happened :o"};
|
95 |
|
|
|
96 |
|
|
const result = {ok: response.ok, status: response.status};
|
97 |
|
|
try {
|
98 |
|
|
result.json = await response.json();
|
99 |
|
|
} catch(e) {
|
100 |
|
|
result.error_json = "" + e;
|
101 |
|
|
}
|
102 |
|
|
return result;
|
103 |
|
|
} else {
|
104 |
|
|
throw `bad sendMessage message type: '${msg[0]}'`;
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
const old_tabs_query = browser.tabs.query;
|
109 |
|
|
browser.tabs.query = async function(query) {
|
110 |
|
|
const tabs = await old_tabs_query(query);
|
111 |
|
|
tabs.forEach(t => t.url = mocked_page_info.url);
|
112 |
|
|
return tabs;
|
113 |
|
|
}
|
114 |
|
|
''' % json.dumps(mocked_page_infos)
|
115 |
|
|
|
116 |
|
|
popup_ext_data = {
|
117 |
|
|
'background_script': broker_js,
|
118 |
|
|
'extra_html': ExtraHTML(
|
119 |
|
|
'html/popup.html',
|
120 |
|
|
{
|
121 |
|
|
'common/browser.js': tab_mock_js,
|
122 |
|
|
'common/indexeddb.js': '; set_repo("https://hydril.la/");'
|
123 |
|
|
},
|
124 |
|
|
wrap_into_htmldoc=False
|
125 |
|
|
),
|
126 |
|
|
'navigate_to': 'html/popup.html'
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
@pytest.mark.ext_data(popup_ext_data)
|
130 |
|
|
@pytest.mark.usefixtures('webextension')
|
131 |
|
|
@pytest.mark.parametrize('page_info_key', ['', *mocked_page_infos.keys()])
|
132 |
|
|
def test_popup_display(driver, execute_in_page, page_info_key):
|
133 |
|
|
"""
|
134 |
|
|
Test popup viewing while on a page. Test parametrized with different
|
135 |
|
|
possible values of page_info object passed in message from the content
|
136 |
|
|
script.
|
137 |
|
|
"""
|
138 |
26e4800d
|
Wojtek Kosior
|
initial_url = driver.current_url
|
139 |
|
|
driver.get('about:blank')
|
140 |
|
|
driver.get(f'{initial_url}#mock_page_info-{page_info_key}')
|
141 |
42fe4405
|
Wojtek Kosior
|
|
142 |
26e4800d
|
Wojtek Kosior
|
by_id = driver.execute_script(
|
143 |
|
|
'''
|
144 |
|
|
const nodes = [...document.querySelectorAll("[id]")];
|
145 |
|
|
const reductor = (ob, node) => Object.assign(ob, {[node.id]: node});
|
146 |
|
|
return nodes.reduce(reductor, {});
|
147 |
|
|
''')
|
148 |
42fe4405
|
Wojtek Kosior
|
|
149 |
|
|
if page_info_key == '':
|
150 |
|
|
error_msg = 'Page info not avaialable. Try reloading the page.'
|
151 |
|
|
error_msg_shown = lambda d: by_id['loading_info'].text == error_msg
|
152 |
|
|
WebDriverWait(driver, 10).until(error_msg_shown)
|
153 |
|
|
return
|
154 |
|
|
|
155 |
|
|
WebDriverWait(driver, 10).until(lambda d: by_id['info_form'].is_displayed())
|
156 |
|
|
assert (page_info_key == 'privileged') == \
|
157 |
|
|
by_id['privileged_page_info'].is_displayed()
|
158 |
|
|
assert (page_info_key == 'privileged') ^ \
|
159 |
|
|
by_id['unprivileged_page_info'].is_displayed()
|
160 |
|
|
assert by_id['page_url'].text == mocked_page_infos[page_info_key]['url']
|
161 |
|
|
assert not by_id['repo_query_container'].is_displayed()
|
162 |
|
|
|
163 |
ea9df6c7
|
Wojtek Kosior
|
if 'allow' in page_info_key:
|
164 |
42fe4405
|
Wojtek Kosior
|
assert by_id['scripts_blocked'].text.lower() == 'no'
|
165 |
ea9df6c7
|
Wojtek Kosior
|
elif page_info_key != 'privileged':
|
166 |
|
|
assert by_id['scripts_blocked'].text.lower() == 'yes'
|
167 |
42fe4405
|
Wojtek Kosior
|
|
168 |
ea9df6c7
|
Wojtek Kosior
|
payload_text = by_id['injected_payload'].text
|
169 |
42fe4405
|
Wojtek Kosior
|
if page_info_key == 'mapping':
|
170 |
ea9df6c7
|
Wojtek Kosior
|
assert payload_text == 'res1'
|
171 |
|
|
elif page_info_key == 'error_missing':
|
172 |
|
|
assert payload_text == \
|
173 |
|
|
"None (error: resource with id 'some-missing-res' missing from the database)"
|
174 |
|
|
elif page_info_key == 'error_circular':
|
175 |
|
|
assert payload_text == \
|
176 |
|
|
"None (error: circular dependency of resource with id 'some-circular-res' on itself)"
|
177 |
|
|
elif page_info_key == 'error_db':
|
178 |
|
|
assert payload_text == \
|
179 |
|
|
'None (error: failure reading Haketilo internal database)'
|
180 |
|
|
elif page_info_key == 'error_other':
|
181 |
|
|
assert payload_text == \
|
182 |
|
|
'None (error: unknown failure occured)'
|
183 |
42fe4405
|
Wojtek Kosior
|
elif page_info_key != 'privileged':
|
184 |
ea9df6c7
|
Wojtek Kosior
|
assert payload_text == 'None'
|
185 |
42fe4405
|
Wojtek Kosior
|
|
186 |
|
|
mapping_text = by_id['mapping_used'].text
|
187 |
ea9df6c7
|
Wojtek Kosior
|
|
188 |
|
|
if page_info_key == 'error_deciding_policy':
|
189 |
|
|
assert mapping_text == 'None (error occured when determining policy)'
|
190 |
|
|
elif page_info_key == 'mapping' or page_info_key.startswith('error'):
|
191 |
42fe4405
|
Wojtek Kosior
|
assert mapping_text == 'm1'
|
192 |
|
|
|
193 |
|
|
if 'allowed' in page_info_key:
|
194 |
ea9df6c7
|
Wojtek Kosior
|
assert 'None (scripts allowed by' in mapping_text
|
195 |
42fe4405
|
Wojtek Kosior
|
elif 'blocked' in page_info_key:
|
196 |
ea9df6c7
|
Wojtek Kosior
|
assert 'None (scripts blocked by' in mapping_text
|
197 |
42fe4405
|
Wojtek Kosior
|
|
198 |
|
|
if 'rule' in page_info_key:
|
199 |
ea9df6c7
|
Wojtek Kosior
|
assert 'by a rule)' in mapping_text
|
200 |
42fe4405
|
Wojtek Kosior
|
elif 'default' in page_info_key:
|
201 |
ea9df6c7
|
Wojtek Kosior
|
assert 'by default policy)' in mapping_text
|
202 |
9d825eaa
|
Wojtek Kosior
|
|
203 |
42fe4405
|
Wojtek Kosior
|
@pytest.mark.ext_data(popup_ext_data)
|
204 |
|
|
@pytest.mark.usefixtures('webextension')
|
205 |
|
|
def test_popup_repo_query(driver, execute_in_page):
|
206 |
|
|
"""
|
207 |
|
|
Test opening and closing the repo query view in popup.
|
208 |
|
|
"""
|
209 |
26e4800d
|
Wojtek Kosior
|
initial_url = driver.current_url
|
210 |
|
|
driver.get('about:blank')
|
211 |
|
|
driver.get(f'{initial_url}#mock_page_info-blocked_rule')
|
212 |
42fe4405
|
Wojtek Kosior
|
|
213 |
|
|
search_but = driver.find_element_by_id("search_resources_but")
|
214 |
26e4800d
|
Wojtek Kosior
|
WebDriverWait(driver, 10).until(lambda d: search_but.is_displayed())
|
215 |
|
|
search_but.click()
|
216 |
ad69f9c8
|
Wojtek Kosior
|
|
217 |
42fe4405
|
Wojtek Kosior
|
containers = dict([(name, driver.find_element_by_id(f'{name}_container'))
|
218 |
|
|
for name in ('page_info', 'repo_query')])
|
219 |
|
|
assert not containers['page_info'].is_displayed()
|
220 |
|
|
assert containers['repo_query'].is_displayed()
|
221 |
|
|
shown = lambda d: 'https://hydril.la/' in containers['repo_query'].text
|
222 |
|
|
WebDriverWait(driver, 10).until(shown)
|
223 |
|
|
|
224 |
|
|
# Click the "Show results" button.
|
225 |
|
|
selector = '.repo_query_buttons > button:first-child'
|
226 |
|
|
driver.find_element_by_css_selector(selector).click()
|
227 |
57ce414c
|
Wojtek Kosior
|
shown = lambda d: 'MAPPING-A' in containers['repo_query'].text
|
228 |
42fe4405
|
Wojtek Kosior
|
WebDriverWait(driver, 10).until(shown)
|
229 |
|
|
|
230 |
|
|
# Click the "Cancel" button
|
231 |
|
|
selector = '.repo_query_bottom_buttons > button'
|
232 |
|
|
driver.find_element_by_css_selector(selector).click()
|
233 |
|
|
assert containers['page_info'].is_displayed()
|
234 |
|
|
assert not containers['repo_query'].is_displayed()
|
235 |
|
|
|
236 |
|
|
@pytest.mark.ext_data(popup_ext_data)
|
237 |
|
|
@pytest.mark.usefixtures('webextension')
|
238 |
830d22d8
|
Wojtek Kosior
|
# Under Parabola's Iceweasel 75 the settings page's window opened during this
|
239 |
|
|
# test is impossible to close using driver.close() - it raises an exception with
|
240 |
|
|
# message 'closeTab() not supported in iceweasel'. To avoid such error during
|
241 |
|
|
# test cleanup, we use the mark below to tell our driver fixture to span a
|
242 |
|
|
# separate browser instance for this test.
|
243 |
|
|
@pytest.mark.second_driver()
|
244 |
42fe4405
|
Wojtek Kosior
|
def test_popup_settings_opening(driver, execute_in_page):
|
245 |
|
|
"""
|
246 |
|
|
Test opening the settings page from popup through button click.
|
247 |
|
|
"""
|
248 |
|
|
driver.find_element_by_id("settings_but").click()
|
249 |
|
|
|
250 |
|
|
first_handle = driver.current_window_handle
|
251 |
|
|
WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == 2)
|
252 |
|
|
new_handle = [h for h in driver.window_handles if h != first_handle][0]
|
253 |
|
|
|
254 |
|
|
driver.switch_to.window(new_handle)
|
255 |
|
|
driver.implicitly_wait(10)
|
256 |
|
|
assert "Extension's options page for testing" in \
|
257 |
|
|
driver.find_element_by_tag_name("h1").text
|