diff options
-rw-r--r-- | src/pydrilla/pydrilla.py | 14 | ||||
-rw-r--r-- | src/test/test_pydrilla.py | 17 |
2 files changed, 30 insertions, 1 deletions
diff --git a/src/pydrilla/pydrilla.py b/src/pydrilla/pydrilla.py index af4ff1c..b0a5974 100644 --- a/src/pydrilla/pydrilla.py +++ b/src/pydrilla/pydrilla.py @@ -24,7 +24,8 @@ # file's license. Although I request that you do not make use this code # in a proprietary program, I am not going to enforce this in court. -from flask import Flask, Blueprint, current_app, url_for, abort, request +from flask import Flask, Blueprint, current_app, url_for, abort, request, \ + redirect from jinja2 import Environment, PackageLoader import re #from hashlib import sha256 @@ -591,6 +592,8 @@ def create_app(config_path=(here / 'config.json'), flask_config={}): raise ValueError(_('config_key_absent_{}').format(key)) app._pydrilla_static_resource_uri = config['static_resource_uri'] + if app._pydrilla_static_resource_uri[-1] != '/': + app._pydrilla_static_resource_uri += '/' app._pydrilla_hydrilla_sources_uri = config['hydrilla_sources_uri'] app._pydrilla_werror = config.get('werror', False) if 'hydrilla_parent' in config: @@ -698,3 +701,12 @@ def query(): url = request.args['url'] return json.dumps(content().query(url)) + +@bp.route('/sources/<string:identifier>/<path:path>') +def get_file(identifier, path): + if identifier not in content().indexes: + abort(404) + + new_uri = f'{current_app._pydrilla_static_resource_uri}{identifier}/{path}' + + return redirect(new_uri, code=301) diff --git a/src/test/test_pydrilla.py b/src/test/test_pydrilla.py index db9a321..22022ae 100644 --- a/src/test/test_pydrilla.py +++ b/src/test/test_pydrilla.py @@ -58,12 +58,14 @@ def test_api_basic(client, development_config): for item_type in ['mapping', 'resource']: response = client.get(f'/{item_type}s/helloapple') + assert response.status_code == 200 definition = json.loads(response.data.decode()) assert definition['type'] == item_type assert definition['source_name'] == 'hello' assert definition['version'] == [2021, 11, 10] response = client.get(f'/{item_type}s/helloapple?ver=2021.11.10.0') + assert response.status_code == 200 assert definition == json.loads(response.data.decode()) response = client.get(f'/{item_type}s/helloapple?ver=2021.11.10.999') @@ -76,12 +78,14 @@ def test_api_basic(client, development_config): assert response.status_code == 404 response = client.get(f'/{item_type}s/helloapple?ver=all') + assert response.status_code == 200 definitions = json.loads(response.data.decode()) assert type(definitions) is list assert all([d['type'] == item_type for d in definitions]) assert any([d['version'] == [2021, 11, 10] for d in definitions]) response = client.get('/licenses/CC0-1.0') + assert response.status_code == 200 definition = json.loads(response.data.decode()) assert definition['type'] == 'license' assert definition['long_name'] == 'Creative Commons Zero v1.0 Universal' @@ -91,6 +95,7 @@ def test_api_basic(client, development_config): assert response.status_code == 404 response = client.get('/sources/hello') + assert response.status_code == 200 definition = json.loads(response.data.decode()) assert definition['source_name'] == 'hello' @@ -100,6 +105,7 @@ def test_api_basic(client, development_config): assert response.status_code == 404 response = client.get('/query?url=https://hachettebugs.koszko.org') + assert response.status_code == 200 definitions = json.loads(response.data.decode()) assert type(definitions) is list assert all([d['type'] == 'mapping' for d in definitions]) @@ -107,9 +113,20 @@ def test_api_basic(client, development_config): for d in definitions for p in d['payloads']]) response = client.get('/query?url=https://random_bad_domain.org/something') + assert response.status_code == 200 definitions = json.loads(response.data.decode()) assert definitions == [] + resource_uri = development_config['static_resource_uri'] + response = client.get('/sources/hello/hello.js') + assert response.status_code == 301 + assert response.location == resource_uri + 'hello/hello.js' + response = client.get('/sources/random-bad-identifier/hello.js') + assert response.status_code == 404 + response = client.get('/sources/hello/random/bad/path') + assert response.status_code == 301 + assert response.location == resource_uri + 'hello/random/bad/path' + def test_normalize_version(): assert pydrilla.normalize_version([4, 5, 3, 0, 0]) == [4, 5, 3] assert pydrilla.normalize_version([1, 0, 5, 0]) == [1, 0, 5] |