Project

General

Profile

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

haketilo / test / unit / test_item_preview.py @ 92fc67cf

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
from selenium.common.exceptions import NoSuchWindowException
23

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

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

    
39
    sample_resource = make_sample_resource()
40

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

    
50
    assert '...' not in text
51

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

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

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

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

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

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

    
95
    sample_mapping = make_sample_mapping()
96

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

    
106
    assert '...' not in text
107

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

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

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

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

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

    
140
@pytest.mark.ext_data({
141
    'background_script': broker_js,
142
    'extra_html': [
143
        ExtraHTML('html/item_preview.html', {}),
144
        ExtraHTML('html/file_preview.html', {}, wrap_into_htmldoc=False)
145
    ],
146
    'navigate_to': 'html/item_preview.html'
147
})
148
@pytest.mark.usefixtures('webextension')
149
def test_file_preview_link(driver, execute_in_page):
150
    """
151
    A test case of <a> links created by preview functions that allow a
152
    referenced file to be previewed.
153
    """
154
    execute_in_page(load_script('html/item_preview.js'))
155

    
156
    sample_data = make_complete_sample_data()
157
    sample_data['mapping'] = {}
158
    execute_in_page('returnval(haketilodb.save_items(arguments[0]));',
159
                    sample_data)
160

    
161
    # Cause the "link" to `bye.js` to be invalid.
162
    sample_resource = make_sample_resource()
163
    sample_resource['scripts'][1]['sha256'] = 'dummy nonexistent hash'
164

    
165
    execute_in_page(
166
        '''
167
        let resource_preview_object = resource_preview(arguments[0], undefined);
168
        document.body.append(resource_preview_object.main_div);
169
        ''',
170
        sample_resource)
171

    
172
    window0 = driver.window_handles[0]
173
    driver.find_element_by_link_text('hello.js').click()
174

    
175
    def blob_url_navigated(driver):
176
        if len(driver.window_handles) < 2:
177
            return
178
        window1 = [wh for wh in driver.window_handles if wh != window0][0]
179
        driver.switch_to.window(window1)
180
        try:
181
            return driver.current_url.startswith('blob')
182
        except NoSuchWindowException:
183
            pass
184

    
185
    WebDriverWait(driver, 10).until(blob_url_navigated)
186

    
187
    assert sample_files['hello.js']['contents'].strip() \
188
        in driver.find_element_by_tag_name("pre").text
189

    
190
    driver.close()
191
    driver.switch_to.window(window0)
192

    
193
    driver.find_element_by_link_text('bye.js').click()
194

    
195
    def get_error_span(driver):
196
        if len(driver.window_handles) < 2:
197
            return
198
        window1 = [wh for wh in driver.window_handles if wh != window0][0]
199
        driver.switch_to.window(window1)
200
        try:
201
            return driver.find_element_by_id('error_msg')
202
        except NoSuchWindowException:
203
            pass
204

    
205
    error_span = WebDriverWait(driver, 10).until(get_error_span)
206
    assert error_span.is_displayed()
207
    assert "Couldn't find file in Haketilo's internal database :(" \
208
        in error_span.text
(12-12/25)