aboutsummaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-02-09 17:09:13 +0100
committerWojtek Kosior <koszko@koszko.org>2022-02-09 18:00:16 +0100
commit1c65dd5ca24052ccf9a92939eecd0966c9635c50 (patch)
tree7575157ccd729b4ad79f6c04501023ccb0532a63 /test/unit
parent830d22d8931a4e5f0606d401f24d7cd937f8697e (diff)
downloadbrowser-extension-1c65dd5ca24052ccf9a92939eecd0966c9635c50.tar.gz
browser-extension-1c65dd5ca24052ccf9a92939eecd0966c9635c50.zip
adapt to changes in file path format
From now on we assume Hydrilla serves file contents at 'file/sha256/<hash>' instead of 'file/sha256-<hash>'. With this commit we also stop using the "hash_key" property internally.
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/test_indexeddb.py48
-rw-r--r--test/unit/test_indexeddb_files_server.py6
-rw-r--r--test/unit/test_item_list.py8
-rw-r--r--test/unit/test_item_preview.py2
-rw-r--r--test/unit/test_payload_create.py4
-rw-r--r--test/unit/utils.py19
6 files changed, 50 insertions, 37 deletions
diff --git a/test/unit/test_indexeddb.py b/test/unit/test_indexeddb.py
index 7ce4781..9041116 100644
--- a/test/unit/test_indexeddb.py
+++ b/test/unit/test_indexeddb.py
@@ -6,7 +6,7 @@ Haketilo unit tests - IndexedDB access
# This file is part of Haketilo
#
-# Copyright (C) 2021,2022 Wojtek Kosior <koszko@koszko.org>
+# Copyright (C) 2021, 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
@@ -73,19 +73,19 @@ def test_haketilodb_item_modifications(driver, execute_in_page):
.then(finalize_transaction);
returnval(promise);
}''',
- sample_item, sample_files_by_hash)
+ sample_item, {'sha256': sample_files_by_sha256})
database_contents = get_db_contents(execute_in_page)
assert len(database_contents['files']) == 4
- assert all([sample_files_by_hash[file['hash_key']] == file['contents']
+ assert all([sample_files_by_sha256[file['sha256']] == file['contents']
for file in database_contents['files']])
assert all([len(file) == 2 for file in database_contents['files']])
assert len(database_contents['file_uses']) == 4
assert all([uses['uses'] == 1 for uses in database_contents['file_uses']])
- assert set([uses['hash_key'] for uses in database_contents['file_uses']]) \
- == set([file['hash_key'] for file in database_contents['files']])
+ assert set([uses['sha256'] for uses in database_contents['file_uses']]) \
+ == set([file['sha256'] for file in database_contents['files']])
assert database_contents['mapping'] == []
assert database_contents['resource'] == [sample_item]
@@ -93,8 +93,8 @@ def test_haketilodb_item_modifications(driver, execute_in_page):
# See if trying to add an item without providing all its files ends in an
# exception and aborts the transaction as it should.
sample_item['scripts'].append(sample_file_ref('combined.js'))
- incomplete_files = {**sample_files_by_hash}
- incomplete_files.pop(sample_files['combined.js']['hash_key'])
+ incomplete_files = {**sample_files_by_sha256}
+ incomplete_files.pop(sample_files['combined.js']['sha256'])
exception = execute_in_page(
'''{
const args = arguments;
@@ -112,14 +112,14 @@ def test_haketilodb_item_modifications(driver, execute_in_page):
}
returnval(try_add_item());
}''',
- sample_item, incomplete_files)
+ sample_item, {'sha256': incomplete_files})
previous_database_contents = database_contents
database_contents = get_db_contents(execute_in_page)
assert 'file not present' in exception
for key, val in database_contents.items():
- keyfun = lambda item: item.get('hash_key') or item['identifier']
+ keyfun = lambda item: item.get('sha256') or item['identifier']
assert sorted(previous_database_contents[key], key=keyfun) \
== sorted(val, key=keyfun)
@@ -132,7 +132,7 @@ def test_haketilodb_item_modifications(driver, execute_in_page):
.then(finalize_transaction);
returnval(promise);
}''',
- sample_item, sample_files_by_hash)
+ sample_item, {'sha256': sample_files_by_sha256})
database_contents = get_db_contents(execute_in_page)
@@ -141,14 +141,14 @@ def test_haketilodb_item_modifications(driver, execute_in_page):
sample_files_list = [sample_files[name] for name in names]
uses_list = [1, 2, 1, 1, 1]
- uses = dict([(uses['hash_key'], uses['uses'])
+ uses = dict([(uses['sha256'], uses['uses'])
for uses in database_contents['file_uses']])
- assert uses == dict([(file['hash_key'], nr)
+ assert uses == dict([(file['sha256'], nr)
for file, nr in zip(sample_files_list, uses_list)])
- files = dict([(file['hash_key'], file['contents'])
+ files = dict([(file['sha256'], file['contents'])
for file in database_contents['files']])
- assert files == dict([(file['hash_key'], file['contents'])
+ assert files == dict([(file['sha256'], file['contents'])
for file in sample_files_list])
del database_contents['resource'][0]['source_copyright'][0]['extra_prop']
@@ -174,13 +174,13 @@ def test_haketilodb_item_modifications(driver, execute_in_page):
sample_files_list = [sample_files[name] for name in names]
uses_list = [1, 1]
- uses = dict([(uses['hash_key'], uses['uses'])
+ uses = dict([(uses['sha256'], uses['uses'])
for uses in results[0]['file_uses']])
- assert uses == dict([(file['hash_key'], 1) for file in sample_files_list])
+ assert uses == dict([(file['sha256'], 1) for file in sample_files_list])
- files = dict([(file['hash_key'], file['contents'])
+ files = dict([(file['sha256'], file['contents'])
for file in results[0]['files']])
- assert files == dict([(file['hash_key'], file['contents'])
+ assert files == dict([(file['sha256'], file['contents'])
for file in sample_files_list])
assert results[0]['resource'] == []
@@ -206,7 +206,9 @@ def test_haketilodb_item_modifications(driver, execute_in_page):
'0.1.1': sample_mapping
}
},
- 'files': sample_files_by_hash
+ 'file': {
+ 'sha256': sample_files_by_sha256
+ }
}
clear_indexeddb(execute_in_page)
@@ -350,7 +352,9 @@ def test_haketilodb_track(driver, execute_in_page, wait_elem_text):
'0.1.1': sample_mapping
}
},
- 'files': sample_files_by_hash
+ 'file': {
+ 'sha256': sample_files_by_sha256
+ }
}
driver.switch_to.window(windows[1])
execute_in_page('initial_data = arguments[0];', initial_data)
@@ -423,7 +427,9 @@ def test_haketilodb_track(driver, execute_in_page, wait_elem_text):
'0.1.1': sample_mapping2
}
},
- 'files': sample_files_by_hash
+ 'file': {
+ 'sha256': sample_files_by_sha256
+ }
}
execute_in_page('returnval(save_items(arguments[0]));', sample_data)
execute_in_page('returnval(set_setting("option22", "abc"));')
diff --git a/test/unit/test_indexeddb_files_server.py b/test/unit/test_indexeddb_files_server.py
index ab69d9d..390bbe7 100644
--- a/test/unit/test_indexeddb_files_server.py
+++ b/test/unit/test_indexeddb_files_server.py
@@ -35,7 +35,7 @@ sample_files_list = [(f'file_{n}_{i}', f'contents {n} {i}')
sample_files = dict(sample_files_list)
-sample_files, sample_files_by_hash = make_sample_files(sample_files)
+sample_files, sample_files_by_sha256 = make_sample_files(sample_files)
def make_sample_resource_with_deps(n):
resource = make_sample_resource(with_files=False)
@@ -53,7 +53,9 @@ resources = [make_sample_resource_with_deps(n) for n in range(count)]
sample_data = {
'resources': sample_data_dict(resources),
'mapping': {},
- 'files': sample_files_by_hash
+ 'file': {
+ 'sha256': sample_files_by_sha256
+ }
}
def prepare_test_page(initial_indexeddb_data, execute_in_page):
diff --git a/test/unit/test_item_list.py b/test/unit/test_item_list.py
index 0702129..46691c3 100644
--- a/test/unit/test_item_list.py
+++ b/test/unit/test_item_list.py
@@ -106,7 +106,9 @@ def test_item_list_ordering(driver, execute_in_page, item_type):
sample_data = {
'resources': {},
'mappings': {},
- 'files': sample_files_by_hash
+ 'file': {
+ 'sha256': sample_files_by_sha256
+ }
}
indexes_added = set()
@@ -189,7 +191,9 @@ def test_item_list_displaying(driver, execute_in_page, item_type):
sample_data = {
'resources': {},
'mappings': {},
- 'files': sample_files_by_hash
+ 'file': {
+ 'sha256': sample_files_by_sha256
+ }
}
sample_data[item_type + 's'] = sample_data_dict(items)
diff --git a/test/unit/test_item_preview.py b/test/unit/test_item_preview.py
index 8b2b161..8787d5d 100644
--- a/test/unit/test_item_preview.py
+++ b/test/unit/test_item_preview.py
@@ -160,7 +160,7 @@ def test_file_preview_link(driver, execute_in_page):
# Cause the "link" to `bye.js` to be invalid.
sample_resource = make_sample_resource()
- sample_resource['scripts'][1]['hash_key'] = 'dummy nonexistent key'
+ sample_resource['scripts'][1]['sha256'] = 'dummy nonexistent hash'
execute_in_page(
'''
diff --git a/test/unit/test_payload_create.py b/test/unit/test_payload_create.py
index cee3a9b..9689c37 100644
--- a/test/unit/test_payload_create.py
+++ b/test/unit/test_payload_create.py
@@ -19,6 +19,8 @@ Haketilo unit tests - using a form to create simple site payload
import pytest
import re
+from hashlib import sha256
+
from selenium.webdriver.support.ui import WebDriverWait
from ..extension_crafting import ExtraHTML
@@ -137,7 +139,7 @@ def test_payload_create_normal_usage(driver, execute_in_page):
'long_name': long_name,
'scripts': [{
'file': 'payload.js',
- 'hash_key': make_hash_key(form_data['script'])
+ 'sha256': sha256(form_data['script'].encode()).digest().hex()
}]
}]
diff --git a/test/unit/utils.py b/test/unit/utils.py
index 56880d5..85dee63 100644
--- a/test/unit/utils.py
+++ b/test/unit/utils.py
@@ -33,12 +33,9 @@ from ..script_loader import load_script
patterns_doc_url = \
'https://hydrillabugs.koszko.org/projects/haketilo/wiki/URL_patterns'
-def make_hash_key(file_contents):
- return f'sha256-{sha256(file_contents.encode()).digest().hex()}'
-
def sample_file(contents):
return {
- 'hash_key': make_hash_key(contents),
+ 'sha256': sha256(contents.encode()).digest().hex(),
'contents': contents
}
@@ -51,12 +48,12 @@ def make_sample_files(names_contents):
sample_files = dict([(name, sample_file(contents))
for name, contents in names_contents.items()])
- sample_files_by_hash = dict([[file['hash_key'], file['contents']]
- for file in sample_files.values()])
+ sample_files_by_sha256 = dict([[file['sha256'], file['contents']]
+ for file in sample_files.values()])
- return sample_files, sample_files_by_hash
+ return sample_files, sample_files_by_sha256
-sample_files, sample_files_by_hash = make_sample_files({
+sample_files, sample_files_by_sha256 = make_sample_files({
'report.spdx': '<!-- dummy report -->',
'LICENSES/somelicense.txt': 'Permission is granted...',
'LICENSES/CC0-1.0.txt': 'Dummy Commons...',
@@ -73,7 +70,7 @@ def sample_file_ref(file_name, sample_files_dict=sample_files):
"""
return {
'file': file_name,
- 'hash_key': sample_files_dict[file_name]['hash_key']
+ 'sha256': sample_files_dict[file_name]['sha256']
}
def make_sample_mapping(with_files=True):
@@ -154,7 +151,9 @@ def make_complete_sample_data():
return {
'resources': sample_data_dict([make_sample_resource()]),
'mappings': sample_data_dict([make_sample_mapping()]),
- 'files': sample_files_by_hash
+ 'file': {
+ 'sha256': sample_files_by_sha256
+ }
}
def clear_indexeddb(execute_in_page):