Project

General

Profile

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

haketilo / test / haketilo_test / unit / test_payload_create.py @ a066af01

1
# SPDX-License-Identifier: CC0-1.0
2

    
3
"""
4
Haketilo unit tests - using a form to create simple site payload
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 re
22
from hashlib import sha256
23

    
24
from selenium.webdriver.support.ui import WebDriverWait
25

    
26
from ..extension_crafting import ExtraHTML
27
from ..script_loader import load_script
28
from .utils import *
29

    
30
uuidv4_re = re.compile(
31
    r'^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$',
32
    re.IGNORECASE
33
)
34

    
35
sample_patterns = '''
36
http://example.com/***
37

    
38
https://*.example.org/**'''
39

    
40
sample_form_data = {
41
    'identifier':  'someid',
42
    'long_name':   'Some Name',
43
    'description': 'blah blah blah',
44
    'patterns':    sample_patterns,
45
    'script':      sample_files['hello.js']['contents']
46
}
47

    
48
resource_schema_id, mapping_schema_id = [
49
    f'https://hydrilla.koszko.org/schemas/api_{t}_description-1.0.1.schema.json'
50
    for t in ('resource', 'mapping')
51
]
52

    
53
def fill_form_with_sample_data(execute_in_page, sample_data_override={},
54
                               form_ctx='form_ctx'):
55
    form_data = sample_form_data.copy()
56
    form_data.update(sample_data_override)
57
    execute_in_page(
58
        f'''
59
        for (const [key, value] of Object.entries(arguments[0]))
60
            {form_ctx}[key].value = value;
61
        ''',
62
        form_data)
63
    return form_data
64

    
65
cleared_form_inputs = {
66
    'identifier':  '',
67
    'long_name':   '',
68
    'description': '',
69
    'patterns':    'https://example.com/***',
70
    'script':      'console.log("Hello, World!");'
71
}
72
def assert_form_contents(execute_in_page, inputs=cleared_form_inputs):
73
    inputs_keys = [*inputs.keys()]
74
    values = execute_in_page(
75
        'returnval(arguments[0].map(i => form_ctx[i].value));',
76
        inputs_keys
77
    )
78
    for key, value in zip(inputs_keys, values):
79
        assert inputs[key] == value
80

    
81
@pytest.mark.ext_data({
82
    'background_script': broker_js,
83
    'extra_html': ExtraHTML('html/payload_create.html', {}),
84
    'navigate_to': 'html/payload_create.html'
85
})
86
@pytest.mark.usefixtures('webextension')
87
def test_payload_create_normal_usage(driver, execute_in_page):
88
    """
89
    A test case of normal usage of simple payload creation form.
90
    """
91
    execute_in_page(load_script('html/payload_create.js'))
92

    
93
    create_but, form_container, dialog_container = execute_in_page(
94
        '''
95
        const form_ctx = payload_create_form();
96
        document.body.append(form_ctx.main_div);
97
        returnval([form_ctx.create_but, form_ctx.form_container,
98
                   form_ctx.dialog_container]);
99
        ''')
100

    
101
    assert patterns_doc_url == \
102
        driver.find_element_by_link_text('URL patterns').get_attribute('href')
103

    
104
    assert form_container.is_displayed()
105
    assert not dialog_container.is_displayed()
106

    
107
    assert_form_contents(execute_in_page)
108

    
109
    form_data = fill_form_with_sample_data(execute_in_page)
110

    
111
    create_but.click()
112

    
113
    assert not form_container.is_displayed()
114
    assert dialog_container.is_displayed()
115

    
116
    def success_reported(driver):
117
        return 'Successfully saved payload' in dialog_container.text
118

    
119
    WebDriverWait(driver, 10).until(success_reported)
120
    execute_in_page('form_ctx.dialog_ctx.ok_but.click();')
121

    
122
    assert form_container.is_displayed()
123
    assert not dialog_container.is_displayed()
124

    
125
    def assert_db_contents():
126
        db_contents = get_db_contents(execute_in_page)
127

    
128
        assert uuidv4_re.match(db_contents['resource'][0]['uuid'])
129

    
130
        localid = f'local-{form_data["identifier"]}'
131
        long_name = form_data['long_name'] or form_data['identifier']
132
        payloads = dict([(pat, {'identifier': localid})
133
                         for pat in form_data['patterns'].split('\n') if pat])
134

    
135
        assert db_contents['resource'] == [{
136
            '$schema':          resource_schema_id,
137
      	    'source_name':      localid,
138
	    'source_copyright': [],
139
	    'type':             'resource',
140
	    'identifier':       localid,
141
	    'uuid':             db_contents['resource'][0]['uuid'],
142
	    'version':          [1],
143
            'revision':         1,
144
	    'description':      form_data['description'],
145
	    'dependencies':     [],
146
            'long_name':        long_name,
147
	    'scripts': [{
148
                'file': 'payload.js',
149
                'sha256': sha256(form_data['script'].encode()).digest().hex()
150
            }]
151
        }]
152

    
153
        assert uuidv4_re.match(db_contents['mapping'][0]['uuid'])
154
        assert db_contents['mapping'] == [{
155
            '$schema':          mapping_schema_id,
156
      	    'source_name':      localid,
157
	    'source_copyright': [],
158
	    'type':             'mapping',
159
	    'identifier':       localid,
160
	    'uuid':             db_contents['mapping'][0]['uuid'],
161
	    'version':          [1],
162
            'description':      form_data['description'],
163
            'long_name':        long_name,
164
            'payloads':         payloads
165
        }]
166

    
167
    assert_db_contents()
168

    
169
    form_data = fill_form_with_sample_data(execute_in_page, {
170
        'long_name':   '',
171
        'description': 'bam bam bam',
172
        'patterns':    'https://new.example.com/***',
173
        'script':      sample_files['bye.js']['contents']
174
    })
175

    
176
    create_but.click()
177

    
178
    for type in ('Resource', 'Mapping'):
179
        def override_asked(driver):
180
            return f"{type} 'local-someid' already exists. Override?" \
181
                in dialog_container.text
182
        WebDriverWait(driver, 10).until(override_asked)
183
        execute_in_page('form_ctx.dialog_ctx.yes_but.click();')
184

    
185
    assert_db_contents()
186

    
187
@pytest.mark.ext_data({
188
    'background_script': broker_js,
189
    'extra_html': ExtraHTML('html/payload_create.html', {}),
190
    'navigate_to': 'html/payload_create.html'
191
})
192
@pytest.mark.usefixtures('webextension')
193
def test_payload_create_errors(driver, execute_in_page):
194
    """
195
    A test case of various error the simple payload form might show.
196
    """
197
    execute_in_page(load_script('html/payload_create.js'))
198

    
199
    create_but, dialog_container = execute_in_page(
200
        '''
201
        const form_ctx = payload_create_form();
202
        document.body.append(form_ctx.main_div);
203
        returnval([form_ctx.create_but, form_ctx.dialog_container]);
204
        ''')
205

    
206
    for data_override, expected_msg in [
207
            ({'identifier': ''},   "The 'identifier' field is required!"),
208
            ({'identifier': ':('}, 'Identifier may only contain '),
209
            ({'script':     ''},  "The 'script' field is required!"),
210
            ({'patterns':   ''},   "The 'URL patterns' field is required!"),
211
            ({'patterns':   ':d'}, "':d' is not a valid URL pattern. See here for more details."),
212
            ({'patterns': '\n'.join(['http://example.com'] * 2)},
213
             "Pattern 'http://example.com' specified multiple times!")
214
    ]:
215
        # Attempt creating the payload
216
        form_data = fill_form_with_sample_data(execute_in_page, data_override)
217
        create_but.click()
218
        # Verify the error message
219
        assert expected_msg in dialog_container.text
220

    
221
        # Verify patterns documentation <a> link.
222
        if expected_msg == {'patterns': ':d'}:
223
            doc_link_elem = driver.find_element_by_link_text('here')
224
            assert doc_link.get_attribute('href') == patterns_doc_url
225

    
226
        # Verify the form was NOT cleared upon failed saving.
227
        execute_in_page('form_ctx.dialog_ctx.ok_but.click();')
228
        assert_form_contents(execute_in_page, form_data)
229

    
230
    # Add a sample item and attempt overriding it.
231
    fill_form_with_sample_data(execute_in_page)
232
    create_but.click()
233
    WebDriverWait(driver, 10).until(lambda _: 'Succes' in dialog_container.text)
234
    execute_in_page('form_ctx.dialog_ctx.ok_but.click();')
235

    
236
    # Verify that denying override leads to saving failure.
237
    form_data = fill_form_with_sample_data(execute_in_page)
238
    create_but.click()
239
    WebDriverWait(driver, 10).until(lambda _: 'Overri' in dialog_container.text)
240
    execute_in_page('form_ctx.dialog_ctx.no_but.click();')
241
    assert 'Failed to save payload :(' in dialog_container.text
242
    execute_in_page('form_ctx.dialog_ctx.ok_but.click();')
243
    assert_form_contents(execute_in_page, form_data)
244

    
245
    # Verify that IndexedDB errors get caught and reported as saving failures.
246
    execute_in_page('haketilodb.get = async () => {throw "someerror";}')
247
    form_data = fill_form_with_sample_data(execute_in_page, {'identifier': 'o'})
248
    create_but.click()
249
    WebDriverWait(driver, 10).until(lambda _: 'Failed' in dialog_container.text)
250
    execute_in_page('form_ctx.dialog_ctx.ok_but.click();')
251
    assert_form_contents(execute_in_page, form_data)
252

    
253
    # Verify that the loading message gets shown during IndexedDB operations.
254
    execute_in_page('haketilodb.get = () => new Promise(cb => null);')
255
    create_but.click()
256
    assert 'Saving payload...' in dialog_container.text
(16-16/25)