Project

General

Profile

Download (7.36 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / unit / test_popup.py @ 9d825eaa

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

    
24
from ..extension_crafting import ExtraHTML
25
from ..script_loader import load_script
26
from .utils import *
27

    
28
def reload_with_target(driver, target):
29
    current_url = driver.execute_script('return location.href')
30
    driver.execute_script(
31
        '''
32
        window.location.href = arguments[0];
33
        window.location.reload();
34
        ''',
35
        f'{current_url}#{target}')
36

    
37
unprivileged_page_info = {
38
    'url': 'https://example_a.com/something',
39
    'allow': False
40
}
41

    
42
mocked_page_infos = {
43
    'privileged': {
44
        'url': 'moz-extension://<some-id>/file.html',
45
        'privileged': True
46
    },
47
    'blocked_default': unprivileged_page_info,
48
    'allowed_default': {
49
        **unprivileged_page_info,
50
        'allow': True
51
    },
52
    'blocked_rule': {
53
        **unprivileged_page_info,
54
        'mapping': '~allow'
55
    },
56
    'allowed_rule': {
57
        **unprivileged_page_info,
58
        'allow': True,
59
        'mapping': '~allow'
60
    },
61
    'mapping': {
62
        **unprivileged_page_info,
63
        'mapping': 'm1',
64
        'payload': {'identifier': 'res1'}
65
    },
66
    'error': {
67
        **unprivileged_page_info,
68
        'error': True
69
    }
70
}
71

    
72
tab_mock_js = '''
73
;
74
const mocked_page_info = (%s)[/#mock_page_info-(.*)$/.exec(document.URL)[1]];
75
browser.tabs.sendMessage = async function(tab_id, msg) {
76
    const this_tab_id = (await browser.tabs.getCurrent()).id;
77
    if (tab_id !== this_tab_id)
78
        throw `not current tab id (${tab_id} instead of ${this_tab_id})`;
79

    
80
    if (msg[0] === "page_info") {
81
        return mocked_page_info;
82
    } else if (msg[0] === "repo_query") {
83
        const response = await fetch(msg[1]);
84
        if (!response)
85
            return {error: "Something happened :o"};
86

    
87
        const result = {ok: response.ok, status: response.status};
88
        try {
89
            result.json = await response.json();
90
        } catch(e) {
91
            result.error_json = "" + e;
92
        }
93
        return result;
94
    } else {
95
        throw `bad sendMessage message type: '${msg[0]}'`;
96
    }
97
}
98

    
99
const old_tabs_query = browser.tabs.query;
100
browser.tabs.query = async function(query) {
101
    const tabs = await old_tabs_query(query);
102
    tabs.forEach(t => t.url = mocked_page_info.url);
103
    return tabs;
104
}
105
''' % json.dumps(mocked_page_infos)
106

    
107
popup_ext_data = {
108
    'background_script': broker_js,
109
    'extra_html': ExtraHTML(
110
        'html/popup.html',
111
        {
112
            'common/browser.js':   tab_mock_js,
113
            'common/indexeddb.js': '; set_repo("https://hydril.la/");'
114
        },
115
        wrap_into_htmldoc=False
116
    ),
117
    'navigate_to': 'html/popup.html'
118
}
119

    
120
@pytest.mark.ext_data(popup_ext_data)
121
@pytest.mark.usefixtures('webextension')
122
@pytest.mark.parametrize('page_info_key', ['', *mocked_page_infos.keys()])
123
def test_popup_display(driver, execute_in_page, page_info_key):
124
    """
125
    Test popup viewing while on a page. Test parametrized with different
126
    possible values of page_info object passed in message from the content
127
    script.
128
    """
129
    reload_with_target(driver, f'mock_page_info-{page_info_key}')
130

    
131
    by_id = driver.execute_script('''
132
    const nodes = [...document.querySelectorAll("[id]")];
133
    return nodes.reduce((ob, node) => Object.assign(ob, {[node.id]: node}), {});
134
    ''');
135

    
136
    if page_info_key == '':
137
        error_msg = 'Page info not avaialable. Try reloading the page.'
138
        error_msg_shown = lambda d: by_id['loading_info'].text == error_msg
139
        WebDriverWait(driver, 10).until(error_msg_shown)
140
        return
141

    
142
    WebDriverWait(driver, 10).until(lambda d: by_id['info_form'].is_displayed())
143
    assert (page_info_key == 'privileged') == \
144
        by_id['privileged_page_info'].is_displayed()
145
    assert (page_info_key == 'privileged') ^ \
146
        by_id['unprivileged_page_info'].is_displayed()
147
    assert by_id['page_url'].text == mocked_page_infos[page_info_key]['url']
148
    assert not by_id['repo_query_container'].is_displayed()
149

    
150
    if 'blocked' in page_info_key or page_info_key in ('mapping', 'error'):
151
        assert by_id['scripts_blocked'].text.lower() == 'yes'
152
    elif 'allowed' in page_info_key:
153
        assert by_id['scripts_blocked'].text.lower() == 'no'
154

    
155
    if page_info_key == 'mapping':
156
        assert by_id['injected_payload'].text == 'res1'
157
    elif page_info_key != 'privileged':
158
        assert by_id['injected_payload'].text == 'None'
159

    
160
    mapping_text = by_id['mapping_used'].text
161
    if page_info_key == 'mapping':
162
        assert mapping_text == 'm1'
163

    
164
    if 'allowed' in page_info_key:
165
        'None (scripts allowed by' in mapping_text
166
    elif 'blocked' in page_info_key:
167
        'None (scripts blocked by' in mapping_text
168

    
169
    if 'rule' in page_info_key:
170
        'by a rule)' in mapping_text
171
    elif 'default' in page_info_key:
172
        'by default_policy)' in mapping_text
173

    
174
    if page_info_key == 'error':
175
        assert mapping_text == 'None (error occured when determining policy)'
176

    
177
@pytest.mark.ext_data(popup_ext_data)
178
@pytest.mark.usefixtures('webextension')
179
def test_popup_repo_query(driver, execute_in_page):
180
    """
181
    Test opening and closing the repo query view in popup.
182
    """
183
    reload_with_target(driver, f'mock_page_info-blocked_rule')
184

    
185
    search_but = driver.find_element_by_id("search_resources_but")
186
    WebDriverWait(driver, 10).until(lambda d: search_but.is_displayed())
187
    search_but.click()
188
    containers = dict([(name, driver.find_element_by_id(f'{name}_container'))
189
                       for name in ('page_info', 'repo_query')])
190
    assert not containers['page_info'].is_displayed()
191
    assert containers['repo_query'].is_displayed()
192
    shown = lambda d: 'https://hydril.la/' in containers['repo_query'].text
193
    WebDriverWait(driver, 10).until(shown)
194

    
195
    # Click the "Show results" button.
196
    selector = '.repo_query_buttons > button:first-child'
197
    driver.find_element_by_css_selector(selector).click()
198
    shown = lambda d: 'MAPPING_A' in containers['repo_query'].text
199
    WebDriverWait(driver, 10).until(shown)
200

    
201
    # Click the "Cancel" button
202
    selector = '.repo_query_bottom_buttons > button'
203
    driver.find_element_by_css_selector(selector).click()
204
    assert containers['page_info'].is_displayed()
205
    assert not containers['repo_query'].is_displayed()
206

    
207
@pytest.mark.ext_data(popup_ext_data)
208
@pytest.mark.usefixtures('webextension')
209
def test_popup_settings_opening(driver, execute_in_page):
210
    """
211
    Test opening the settings page from popup through button click.
212
    """
213
    driver.find_element_by_id("settings_but").click()
214

    
215
    first_handle = driver.current_window_handle
216
    WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == 2)
217
    new_handle = [h for h in driver.window_handles if h != first_handle][0]
218

    
219
    driver.switch_to.window(new_handle)
220
    driver.implicitly_wait(10)
221
    assert "Extension's options page for testing" in \
222
        driver.find_element_by_tag_name("h1").text
(19-19/25)