Revision 7218849a
Added by koszko over 1 year ago
| test/world_wide_library.py | ||
|---|---|---|
| 31 | 31 |
from pathlib import Path |
| 32 | 32 |
from shutil import rmtree |
| 33 | 33 |
from threading import Lock |
| 34 |
from uuid import uuid4 |
|
| 34 | 35 |
import json |
| 35 | 36 |
|
| 36 | 37 |
from .misc_constants import here |
| 38 |
from .unit.utils import * # sample repo data |
|
| 39 |
|
|
| 40 |
# TODO: instead of having the entire catalog defined here, make it possible to |
|
| 41 |
# add catalog items from within individual test files. |
|
| 37 | 42 |
|
| 38 | 43 |
served_scripts = {}
|
| 39 | 44 |
served_scripts_lock = Lock() |
| ... | ... | |
| 100 | 105 |
json.dumps({'counter': request_counter})
|
| 101 | 106 |
) |
| 102 | 107 |
|
| 108 |
# Mock a Hydrilla repository. |
|
| 109 |
|
|
| 110 |
# Mock files in the repository. |
|
| 111 |
sample_contents = [f'Mi povas manĝi vitron, ĝi ne damaĝas min {i}'
|
|
| 112 |
for i in range(9)] |
|
| 113 |
sample_hashes = [sha256(c.encode()).digest().hex() for c in sample_contents] |
|
| 114 |
|
|
| 115 |
file_url = lambda hashed: f'https://hydril.la/file/sha256-{hashed}'
|
|
| 116 |
file_handler = lambda contents: lambda c, g, p: (200, {}, contents)
|
|
| 117 |
|
|
| 118 |
sample_files_catalog = dict([(file_url(h), file_handler(c)) |
|
| 119 |
for h, c in zip(sample_hashes, sample_contents)]) |
|
| 120 |
|
|
| 121 |
# Mock resources and mappings in the repository. |
|
| 122 |
sample_resource_templates = [] |
|
| 123 |
|
|
| 124 |
for deps in [(0, 1, 2, 3), (3, 4, 5, 6), (6, 7, 8, 9)]: |
|
| 125 |
letters = [chr(ord('a') + i) for i in deps]
|
|
| 126 |
sample_resource_templates.append({
|
|
| 127 |
'id_suffix': ''.join(letters), |
|
| 128 |
'files_count': deps[0], |
|
| 129 |
'dependencies': [f'resource_{l}' for l in letters]
|
|
| 130 |
}) |
|
| 131 |
|
|
| 132 |
suffixes = [srt['id_suffix'] for srt in sample_resource_templates] |
|
| 133 |
sample_resource_templates.append({
|
|
| 134 |
'id_suffix': '-'.join(suffixes), |
|
| 135 |
'files_count': 2, |
|
| 136 |
'dependencies': [f'resource_{suffix}' for suffix in suffixes]
|
|
| 137 |
}) |
|
| 138 |
|
|
| 139 |
for i in range(10): |
|
| 140 |
sample_resource_templates.append({
|
|
| 141 |
'id_suffix': chr(ord('a') + i),
|
|
| 142 |
'files_count': i, |
|
| 143 |
'dependencies': [] |
|
| 144 |
}) |
|
| 145 |
|
|
| 146 |
sample_resources_catalog = {}
|
|
| 147 |
sample_mappings_catalog = {}
|
|
| 148 |
|
|
| 149 |
for srt in sample_resource_templates: |
|
| 150 |
resource = make_sample_resource() |
|
| 151 |
resource['api_schema_version'] = [1] |
|
| 152 |
resource['api_schema_revision'] = 1 |
|
| 153 |
resource['identifier'] = f'resource_{srt["id_suffix"]}'
|
|
| 154 |
resource['long_name'] = resource['identifier'].upper() |
|
| 155 |
resource['uuid'] = str(uuid4()) |
|
| 156 |
resource['dependencies'] = srt['dependencies'] |
|
| 157 |
resource['source_copyright'] = [] |
|
| 158 |
resource['scripts'] = [] |
|
| 159 |
for i in range(srt['files_count']): |
|
| 160 |
file_ref = {'file': f'file_{i}', 'sha256': sample_hashes[i]}
|
|
| 161 |
resource[('source_copyright', 'scripts')[i & 1]].append(file_ref)
|
|
| 162 |
|
|
| 163 |
# Keeping it simple - just make one corresponding mapping for each resource. |
|
| 164 |
payloads = {'https://example.com/*': {'identifier': resource['identifier']}}
|
|
| 165 |
|
|
| 166 |
mapping = make_sample_mapping() |
|
| 167 |
mapping['api_schema_version'] = [1] |
|
| 168 |
mapping['api_schema_revision'] = 1 |
|
| 169 |
mapping['identifier'] = f'mapping_{srt["id_suffix"]}'
|
|
| 170 |
mapping['long_name'] = mapping['identifier'].upper() |
|
| 171 |
mapping['uuid'] = str(uuid4()) |
|
| 172 |
mapping['source_copyright'] = resource['source_copyright'] |
|
| 173 |
mapping['payloads'] = payloads |
|
| 174 |
|
|
| 175 |
make_handler = lambda txt: lambda c, g, p: (200, {}, txt)
|
|
| 176 |
|
|
| 177 |
for item, catalog in [ |
|
| 178 |
(resource, sample_resources_catalog), |
|
| 179 |
(mapping, sample_mappings_catalog) |
|
| 180 |
]: |
|
| 181 |
fmt = f'https://hydril.la/{item["type"]}/{item["identifier"]}%s.json'
|
|
| 182 |
# Make 2 versions of each item so that we can test updates. |
|
| 183 |
for i in range(2): |
|
| 184 |
for fmt_arg in ('', '/' + item_version_string(item)):
|
|
| 185 |
catalog[fmt % fmt_arg] = make_handler(json.dumps(item)) |
|
| 186 |
item['version'][-1] += 1 |
|
| 187 |
|
|
| 103 | 188 |
catalog = {
|
| 104 | 189 |
'http://gotmyowndoma.in': |
| 105 | 190 |
(302, {'location': 'http://gotmyowndoma.in/index.html'}, None),
|
| ... | ... | |
| 144 | 229 |
'https://site.with.paylo.ad/': |
| 145 | 230 |
(302, {'location': 'https://site.with.paylo.ad/index.html'}, None),
|
| 146 | 231 |
'https://site.with.paylo.ad/index.html': |
| 147 |
(200, {}, here / 'data' / 'pages' / 'gotmyowndomain_https.html')
|
|
| 232 |
(200, {}, here / 'data' / 'pages' / 'gotmyowndomain_https.html'),
|
|
| 233 |
|
|
| 234 |
**sample_files_catalog, |
|
| 235 |
**sample_resources_catalog, |
|
| 236 |
**sample_mappings_catalog |
|
| 148 | 237 |
} |
Also available in: Unified diff
add a mapping/resources installation dialog