Project

General

Profile

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

haketilo / test / unit / test_item_preview.py @ 4c6a2323

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

    
3
"""
4
Haketilo unit tests - displaying resources and mappings details
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
from selenium.webdriver.support.ui import WebDriverWait
22

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

    
27
@pytest.mark.ext_data({
28
    'extra_html': ExtraHTML('html/item_preview.html', {}),
29
    'navigate_to': 'html/item_preview.html'
30
})
31
@pytest.mark.usefixtures('webextension')
32
def test_resource_preview(driver, execute_in_page):
33
    """
34
    A test case of the resource preview display function.
35
    """
36
    execute_in_page(load_script('html/item_preview.js'))
37

    
38
    sample_resource = make_sample_resource()
39

    
40
    preview_div = execute_in_page(
41
        '''
42
        let preview_object = resource_preview(arguments[0]);
43
        document.body.append(preview_object.main_div);
44
        returnval(preview_object.main_div);
45
        ''',
46
        sample_resource)
47
    text = preview_div.text
48

    
49
    assert '...' not in text
50

    
51
    for string in [
52
            *filter(lambda v: type(v) is str, sample_resource.values()),
53
            *sample_resource['dependencies'],
54
            *[c['file'] for k in ('source_copyright', 'scripts')
55
              for c in sample_resource[k]],
56
            item_version_string(sample_resource, True)
57
    ]:
58
        assert string in text
59

    
60
    sample_resource['identifier'] = 'hellopear'
61
    sample_resource['long_name'] = 'Hello Pear'
62
    sample_resource['description'] = 'greets a pear'
63
    sample_resource['dependencies'] = ['hello-msg'],
64
    for key in ('scripts', 'source_copyright'):
65
        for file_ref in sample_resource[key]:
66
            file_ref['file'] = file_ref['file'].replace('.', '_')
67

    
68
    preview_div = execute_in_page(
69
        '''
70
        returnval(resource_preview(arguments[0], preview_object).main_div);
71
        ''',
72
        sample_resource)
73
    text = preview_div.text
74

    
75
    for string in ['...', 'pple', 'hello-message', 'report.spdx',
76
                   'LICENSES/CC0-1.0.txt', 'hello.js', 'bye.js']:
77
        assert string not in text
78

    
79
    for string in ['hellopear', 'Hello Pear', 'hello-msg', 'greets a pear',
80
                   'report_spdx', 'LICENSES/CC0-1_0_txt', 'hello_js', 'bye_js']:
81
        assert string in text
82

    
83
@pytest.mark.ext_data({
84
    'extra_html': ExtraHTML('html/item_preview.html', {}),
85
    'navigate_to': 'html/item_preview.html'
86
})
87
@pytest.mark.usefixtures('webextension')
88
def test_mapping_preview(driver, execute_in_page):
89
    """
90
    A test case of the mapping preview display function.
91
    """
92
    execute_in_page(load_script('html/item_preview.js'))
93

    
94
    sample_mapping = make_sample_mapping()
95

    
96
    preview_div = execute_in_page(
97
        '''
98
        let preview_object = mapping_preview(arguments[0]);
99
        document.body.append(preview_object.main_div);
100
        returnval(preview_object.main_div);
101
        ''',
102
        sample_mapping)
103
    text = preview_div.text
104

    
105
    assert '...' not in text
106

    
107
    for string in [
108
            *filter(lambda v: type(v) is str, sample_mapping.values()),
109
            *[p['identifier'] for p in sample_mapping['payloads'].values()],
110
            *[c['file'] for c in sample_mapping['source_copyright']],
111
            item_version_string(sample_mapping)
112
    ]:
113
        assert string in text
114

    
115
    sample_mapping['identifier'] = 'example-org-bloated'
116
    sample_mapping['long_name'] = 'Example.org Bloated',
117
    sample_mapping['payloads'] = dict(
118
        [(pat.replace('.org', '.com'), res_id)
119
         for pat, res_id in sample_mapping['payloads'].items()]
120
    )
121
    for file_ref in sample_mapping['source_copyright']:
122
        file_ref['file'] = file_ref['file'].replace('.', '_')
123

    
124
    preview_div = execute_in_page(
125
        '''
126
        returnval(mapping_preview(arguments[0], preview_object).main_div);
127
        ''',
128
        sample_mapping)
129
    text = preview_div.text
130

    
131
    for string in ['...', 'inimal', 'example.org', 'report.spdx',
132
                   'LICENSES/CC0-1.0.txt']:
133
        assert string not in text
134

    
135
    for string in ['example-org-bloated', 'Example.org Bloated', 'example.com',
136
                   'report_spdx', 'LICENSES/CC0-1_0_txt']:
137
        assert string in text
138

    
139
@pytest.mark.ext_data({
140
    'background_script': broker_js,
141
    'extra_html': ExtraHTML('html/item_preview.html', {}),
142
    'navigate_to': 'html/item_preview.html'
143
})
144
@pytest.mark.usefixtures('webextension')
145
def test_file_preview_link(driver, execute_in_page):
146
    """
147
    A test case of <a> links created by preview functions that allow a
148
    referenced file to be previewed.
149
    """
150
    execute_in_page(load_script('html/item_preview.js'))
151
    # Mock dialog
152
    execute_in_page('dialog.error = (...args) => window.error_args = args;')
153

    
154
    sample_data = make_complete_sample_data()
155
    sample_data['mappings'] = {}
156
    execute_in_page('returnval(haketilodb.save_items(arguments[0]));',
157
                    sample_data)
158

    
159
    # Cause the "link" to `bye.js` to be invalid.
160
    sample_resource = make_sample_resource()
161
    sample_resource['scripts'][1]['hash_key'] = 'dummy nonexistent key'
162

    
163
    execute_in_page(
164
        '''
165
        let resource_preview_object =
166
            resource_preview(arguments[0], undefined, "dummy dialog ctx");
167
        document.body.append(resource_preview_object.main_div);
168
        ''',
169
        sample_resource)
170

    
171
    window0 = driver.window_handles[0]
172
    driver.find_element_by_link_text('hello.js').click()
173
    WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) > 1)
174
    window1 = [wh for wh in driver.window_handles if wh != window0][0]
175
    driver.switch_to.window(window1)
176
    assert sample_files['hello.js']['contents'] in driver.page_source
177

    
178
    driver.switch_to.window(window0)
179
    driver.find_element_by_link_text('bye.js').click()
180
    assert driver.execute_script('return window.error_args;') == [
181
        'dummy dialog ctx',
182
        "File missing from Haketilo's internal database :("
183
    ]
(12-12/25)