Project

General

Profile

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

haketilo / test / haketilo_test / unit / test_item_preview.py @ 5aef54c8

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
    uuid = sample_resource['uuid']
41

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

    
51
    assert '...' not in text
52
    assert 'not set' not in text
53

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

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

    
72
    preview_div = execute_in_page(
73
        '''
74
        returnval(resource_preview(arguments[0], preview_object).main_div);
75
        ''',
76
        sample_resource)
77
    text = preview_div.text
78

    
79
    for string in [uuid, '...', 'pple', 'hello-message', 'report.spdx',
80
                   'LICENSES/CC0-1.0.txt', 'hello.js', 'bye.js']:
81
        assert string not in text
82

    
83
    for string in ['hellopear', 'Hello Pear', 'hello-msg', 'greets a pear',
84
                   'report_spdx', 'LICENSES/CC0-1_0_txt', 'hello_js', 'bye_js',
85
                   'not set']:
86
        assert string in text
87

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

    
99
    sample_mapping = make_sample_mapping()
100
    uuid = sample_mapping['uuid']
101

    
102
    preview_div = execute_in_page(
103
        '''
104
        let preview_object = mapping_preview(arguments[0]);
105
        document.body.append(preview_object.main_div);
106
        returnval(preview_object.main_div);
107
        ''',
108
        sample_mapping)
109
    text = preview_div.text
110

    
111
    assert '...' not in text
112
    assert 'not set' not in text
113

    
114
    for string in [
115
            *filter(lambda v: type(v) is str, sample_mapping.values()),
116
            *[p['identifier'] for p in sample_mapping['payloads'].values()],
117
            *[c['file'] for c in sample_mapping['source_copyright']],
118
            item_version_string(sample_mapping), uuid
119
    ]:
120
        assert string in text
121

    
122
    del sample_mapping['uuid']
123
    sample_mapping['identifier'] = 'example-org-bloated'
124
    sample_mapping['long_name'] = 'Example.org Bloated',
125
    sample_mapping['payloads'] = dict(
126
        [(pat.replace('.org', '.com'), res_id)
127
         for pat, res_id in sample_mapping['payloads'].items()]
128
    )
129
    for file_ref in sample_mapping['source_copyright']:
130
        file_ref['file'] = file_ref['file'].replace('.', '_')
131

    
132
    preview_div = execute_in_page(
133
        '''
134
        returnval(mapping_preview(arguments[0], preview_object).main_div);
135
        ''',
136
        sample_mapping)
137
    text = preview_div.text
138

    
139
    for string in [uuid, '...', 'inimal', 'example.org', 'report.spdx',
140
                   'LICENSES/CC0-1.0.txt']:
141
        assert string not in text
142

    
143
    for string in ['example-org-bloated', 'Example.org Bloated', 'example.com',
144
                   'report_spdx', 'LICENSES/CC0-1_0_txt', 'not set']:
145
        assert string in text
146

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

    
163
    sample_data = make_complete_sample_data()
164
    sample_data['mapping'] = {}
165
    execute_in_page('returnval(haketilodb.save_items(arguments[0]));',
166
                    sample_data)
167

    
168
    # Cause the "link" to `bye.js` to be invalid.
169
    sample_resource = make_sample_resource()
170
    sample_resource['scripts'][1]['sha256'] = 'dummy nonexistent hash'
171

    
172
    execute_in_page(
173
        '''
174
        let resource_preview_object = resource_preview(arguments[0], undefined);
175
        document.body.append(resource_preview_object.main_div);
176
        ''',
177
        sample_resource)
178

    
179
    window0 = driver.window_handles[0]
180
    driver.find_element_by_link_text('hello.js').click()
181

    
182
    def blob_url_navigated(driver):
183
        if len(driver.window_handles) < 2:
184
            return
185
        window1 = [wh for wh in driver.window_handles if wh != window0][0]
186
        driver.switch_to.window(window1)
187
        try:
188
            return driver.current_url.startswith('blob')
189
        except NoSuchWindowException:
190
            pass
191

    
192
    WebDriverWait(driver, 10).until(blob_url_navigated)
193

    
194
    assert sample_files['hello.js']['contents'].strip() \
195
        in driver.find_element_by_tag_name("pre").text
196

    
197
    driver.close()
198
    driver.switch_to.window(window0)
199

    
200
    driver.find_element_by_link_text('bye.js').click()
201

    
202
    def get_error_span(driver):
203
        if len(driver.window_handles) < 2:
204
            return
205
        window1 = [wh for wh in driver.window_handles if wh != window0][0]
206
        driver.switch_to.window(window1)
207
        try:
208
            return driver.find_element_by_id('error_msg')
209
        except NoSuchWindowException:
210
            pass
211

    
212
    error_span = WebDriverWait(driver, 10).until(get_error_span)
213
    assert error_span.is_displayed()
214
    assert "Couldn't find file in Haketilo's internal database :(" \
215
        in error_span.text
(12-12/25)