aboutsummaryrefslogtreecommitdiff
path: root/test/unit/test_payload_create.py
blob: cd08d43058586f24aafe3ea6f267a0da66eb01ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# SPDX-License-Identifier: CC0-1.0

"""
Haketilo unit tests - using a form to create simple site payload
"""

# This file is part of Haketilo
#
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the CC0 1.0 Universal License as published by
# the Creative Commons Corporation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# CC0 1.0 Universal License for more details.

import pytest
import re
from selenium.webdriver.support.ui import WebDriverWait

from ..extension_crafting import ExtraHTML
from ..script_loader import load_script
from .utils import clear_indexeddb, get_db_contents, sample_files

broker_js = lambda: load_script('background/broadcast_broker.js') + ';start();'

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

sample_patterns = '''
http://example.com/***

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

sample_form_data = {
    'identifier':  'someid',
    'long_name':   'Some Name',
    'description': 'blah blah blah',
    'patterns':    sample_patterns,
    'script':      sample_files['hello.js']['contents']
}

def fill_form_with_sample_data(execute_in_page, sample_data_override={},
                               form_ctx='form_ctx'):
    form_data = sample_form_data.copy()
    form_data.update(sample_data_override)
    execute_in_page(
        f'''
        for (const [key, value] of Object.entries(arguments[0]))
            {form_ctx}[key].value = value;
        ''',
        form_data)

@pytest.mark.ext_data({
    'background_script': broker_js,
    'extra_html': ExtraHTML('html/payload_create.html', {}),
    'navigate_to': 'html/payload_create.html'
})
@pytest.mark.usefixtures('webextension')
def test_payload_create(driver, execute_in_page):
    """
    A test case of creating a simple payload using a form.
    """
    clear_indexeddb(execute_in_page)
    execute_in_page(load_script('html/payload_create.js'))

    create_but, main_div = execute_in_page(
        '''
        const form_ctx = payload_create_form();
        document.body.append(form_ctx.main_div);
        returnval([form_ctx.create_but, form_ctx.main_div]);
        ''')

    fill_form_with_sample_data(execute_in_page)

    create_but.click()

    def success_reported(driver):
        return 'Successfully saved payload' in main_div.text

    WebDriverWait(driver, 10).until(success_reported)

    db_contents = get_db_contents(execute_in_page)

    assert uuidv4_re.match(db_contents['resources'][0]['uuid'])
    assert db_contents['resources'] == [{
      	'source_name':      'local-someid',
	'source_copyright': [],
	'type':             'resource',
	'identifier':       'local-someid',
	'long_name':        'Some Name',
	'uuid':             db_contents['resources'][0]['uuid'],
	'version':          [1],
	'description':      'blah blah blah',
	'dependencies':     [],
	'scripts': [{
            'file': 'payload.js',
            'hash_key': sample_files['hello.js']['hash_key']
        }]
    }]

    assert uuidv4_re.match(db_contents['mappings'][0]['uuid'])
    assert db_contents['mappings'] == [{
      	'source_name':      'local-someid',
	'source_copyright': [],
	'type':             'mapping',
	'identifier':       'local-someid',
	'long_name':        'Some Name',
	'uuid':             db_contents['mappings'][0]['uuid'],
	'version':          [1],
	'description':      'blah blah blah',
        'payloads': {
            'http://example.com/***':   {'identifier': 'local-someid'},
            'https://*.example.org/**': {'identifier': 'local-someid'}
        }
    }]