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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# SPDX-License-Identifier: CC0-1.0
"""
Haketilo unit tests - displaying resources and mappings details
"""
# 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
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchWindowException
from ..extension_crafting import ExtraHTML
from ..script_loader import load_script
from .utils import *
@pytest.mark.ext_data({
'extra_html': ExtraHTML('html/item_preview.html', {}),
'navigate_to': 'html/item_preview.html'
})
@pytest.mark.usefixtures('webextension')
def test_resource_preview(driver, execute_in_page):
"""
A test case of the resource preview display function.
"""
execute_in_page(load_script('html/item_preview.js'))
sample_resource = make_sample_resource()
preview_div = execute_in_page(
'''
let preview_object = resource_preview(arguments[0]);
document.body.append(preview_object.main_div);
returnval(preview_object.main_div);
''',
sample_resource)
text = preview_div.text
assert '...' not in text
for string in [
*filter(lambda v: type(v) is str, sample_resource.values()),
*sample_resource['dependencies'],
*[c['file'] for k in ('source_copyright', 'scripts')
for c in sample_resource[k]],
item_version_string(sample_resource, True)
]:
assert string in text
sample_resource['identifier'] = 'hellopear'
sample_resource['long_name'] = 'Hello Pear'
sample_resource['description'] = 'greets a pear'
sample_resource['dependencies'] = ['hello-msg'],
for key in ('scripts', 'source_copyright'):
for file_ref in sample_resource[key]:
file_ref['file'] = file_ref['file'].replace('.', '_')
preview_div = execute_in_page(
'''
returnval(resource_preview(arguments[0], preview_object).main_div);
''',
sample_resource)
text = preview_div.text
for string in ['...', 'pple', 'hello-message', 'report.spdx',
'LICENSES/CC0-1.0.txt', 'hello.js', 'bye.js']:
assert string not in text
for string in ['hellopear', 'Hello Pear', 'hello-msg', 'greets a pear',
'report_spdx', 'LICENSES/CC0-1_0_txt', 'hello_js', 'bye_js']:
assert string in text
@pytest.mark.ext_data({
'extra_html': ExtraHTML('html/item_preview.html', {}),
'navigate_to': 'html/item_preview.html'
})
@pytest.mark.usefixtures('webextension')
def test_mapping_preview(driver, execute_in_page):
"""
A test case of the mapping preview display function.
"""
execute_in_page(load_script('html/item_preview.js'))
sample_mapping = make_sample_mapping()
preview_div = execute_in_page(
'''
let preview_object = mapping_preview(arguments[0]);
document.body.append(preview_object.main_div);
returnval(preview_object.main_div);
''',
sample_mapping)
text = preview_div.text
assert '...' not in text
for string in [
*filter(lambda v: type(v) is str, sample_mapping.values()),
*[p['identifier'] for p in sample_mapping['payloads'].values()],
*[c['file'] for c in sample_mapping['source_copyright']],
item_version_string(sample_mapping)
]:
assert string in text
sample_mapping['identifier'] = 'example-org-bloated'
sample_mapping['long_name'] = 'Example.org Bloated',
sample_mapping['payloads'] = dict(
[(pat.replace('.org', '.com'), res_id)
for pat, res_id in sample_mapping['payloads'].items()]
)
for file_ref in sample_mapping['source_copyright']:
file_ref['file'] = file_ref['file'].replace('.', '_')
preview_div = execute_in_page(
'''
returnval(mapping_preview(arguments[0], preview_object).main_div);
''',
sample_mapping)
text = preview_div.text
for string in ['...', 'inimal', 'example.org', 'report.spdx',
'LICENSES/CC0-1.0.txt']:
assert string not in text
for string in ['example-org-bloated', 'Example.org Bloated', 'example.com',
'report_spdx', 'LICENSES/CC0-1_0_txt']:
assert string in text
@pytest.mark.ext_data({
'background_script': broker_js,
'extra_html': [
ExtraHTML('html/item_preview.html', {}),
ExtraHTML('html/file_preview.html', {}, wrap_into_htmldoc=False)
],
'navigate_to': 'html/item_preview.html'
})
@pytest.mark.usefixtures('webextension')
def test_file_preview_link(driver, execute_in_page):
"""
A test case of <a> links created by preview functions that allow a
referenced file to be previewed.
"""
execute_in_page(load_script('html/item_preview.js'))
sample_data = make_complete_sample_data()
sample_data['mappings'] = {}
execute_in_page('returnval(haketilodb.save_items(arguments[0]));',
sample_data)
# Cause the "link" to `bye.js` to be invalid.
sample_resource = make_sample_resource()
sample_resource['scripts'][1]['hash_key'] = 'dummy nonexistent key'
execute_in_page(
'''
let resource_preview_object = resource_preview(arguments[0], undefined);
document.body.append(resource_preview_object.main_div);
''',
sample_resource)
window0 = driver.window_handles[0]
driver.find_element_by_link_text('hello.js').click()
def blob_url_navigated(driver):
if len(driver.window_handles) < 2:
return
window1 = [wh for wh in driver.window_handles if wh != window0][0]
driver.switch_to.window(window1)
try:
return driver.current_url.startswith('blob')
except NoSuchWindowException:
pass
WebDriverWait(driver, 10).until(blob_url_navigated)
assert sample_files['hello.js']['contents'].strip() \
in driver.find_element_by_tag_name("pre").text
driver.close()
driver.switch_to.window(window0)
driver.find_element_by_link_text('bye.js').click()
def get_error_span(driver):
if len(driver.window_handles) < 2:
return
window1 = [wh for wh in driver.window_handles if wh != window0][0]
driver.switch_to.window(window1)
try:
return driver.find_element_by_id('error_msg')
except NoSuchWindowException:
pass
error_span = WebDriverWait(driver, 10).until(get_error_span)
assert error_span.is_displayed()
assert "Couldn't find file in Haketilo's internal database :(" \
in error_span.text
|