summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-06-15 17:33:07 +0200
committerWojtek Kosior <koszko@koszko.org>2022-06-15 17:33:07 +0200
commitff759b50e5aadc3c973724021ec9fca3759f9639 (patch)
tree7c57f0894591a6ea27c366f9858bc0ef509bad4f /tests
parentae96024993f111bcb1e48824f5f63be63ef38b1f (diff)
downloadhaketilo-hydrilla-ff759b50e5aadc3c973724021ec9fca3759f9639.tar.gz
haketilo-hydrilla-ff759b50e5aadc3c973724021ec9fca3759f9639.zip
New upstream version 1.1~beta1upstream/1.1_beta1debian-upstream
Diffstat (limited to 'tests')
-rw-r--r--tests/helpers.py51
-rw-r--r--tests/source-package-example/index.json154
-rw-r--r--tests/test_server.py154
3 files changed, 238 insertions, 121 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
new file mode 100644
index 0000000..df474b0
--- /dev/null
+++ b/tests/helpers.py
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: CC0-1.0
+
+# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
+#
+# Available under the terms of Creative Commons Zero v1.0 Universal.
+
+import re
+
+variable_word_re = re.compile(r'^<(.+)>$')
+
+def process_command(command, expected_command):
+ """Validate the command line and extract its variable parts (if any)."""
+ assert len(command) == len(expected_command)
+
+ extracted = {}
+ for word, expected_word in zip(command, expected_command):
+ match = variable_word_re.match(expected_word)
+ if match:
+ extracted[match.group(1)] = word
+ else:
+ assert word == expected_word
+
+ return extracted
+
+def run_missing_executable(command, **kwargs):
+ """
+ Instead of running a command, raise FileNotFoundError as if its executable
+ was missing.
+ """
+ raise FileNotFoundError('dummy')
+
+class MockedCompletedProcess:
+ """
+ Object with some fields similar to those of subprocess.CompletedProcess.
+ """
+ def __init__(self, args, returncode=0,
+ stdout='some output', stderr='some error output',
+ text_output=True):
+ """
+ Initialize MockedCompletedProcess. Convert strings to bytes if needed.
+ """
+ self.args = args
+ self.returncode = returncode
+
+ if type(stdout) is str and not text_output:
+ stdout = stdout.encode()
+ if type(stderr) is str and not text_output:
+ stderr = stderr.encode()
+
+ self.stdout = stdout
+ self.stderr = stderr
diff --git a/tests/source-package-example/index.json b/tests/source-package-example/index.json
index 7162dd7..9aa6e70 100644
--- a/tests/source-package-example/index.json
+++ b/tests/source-package-example/index.json
@@ -3,9 +3,9 @@
// Copyright (C) 2021, 2022 Wojtek Kosior <koszko@koszko.org>
// Available under the terms of Creative Commons Zero v1.0 Universal.
-// This is an example index.json file describing Hydrilla site content. As you
-// can see, for storing site content information Hydrilla utilizes JSON with an
-// additional extension in the form of '//' comments support.
+// This is an example index.json file describing Hydrilla packages. As you can
+// see, for storing this information Hydrilla utilizes JSON with an additional
+// extension in the form of '//' comments support.
// An index.json file conveys definitions of site resources and pattern->payload
// mappings. The definitions may reference files under index.json's containing
@@ -62,18 +62,15 @@
// will also belong here once they get implemented.
"definitions": [
{
- // Value of "type" can currently be one of: "resource" and
- // "mapping". The one we have here, "resource", defines a list
- // of injectable scripts that can be used as a payload or as a
- // dependency of another "resource". In the future CSS style sheets
- // and WASM modules will also be composite parts of a "resource" as
- // scripts are now.
- "type": "resource",
+ // Value of "type" can currently be one of: "resource", "mapping"
+ // and "mapping_and_resource" for a combined definition. The one we
+ // have here, "mapping", associates resources with pages on which
+ // they are to be used.
+ "type": "mapping",
- // Used when referring to this resource in "dependencies" list of
- // another resource or in "payload" field of a mapping. Should
- // be consize and can only use a restricted set of characters. It
- // has to match: [-0-9a-z]+
+ // Used when referring to this mapping in "required_mappings" list
+ // of another item. Should be consize and can only use a restricted
+ // set of characters. It has to match: [-0-9a-z]+
"identifier": "helloapple",
// "long_name" should be used to specify a user-friendly alternative
@@ -96,26 +93,80 @@
// Different versions (e.g. 1.0 and 1.3) of the same resource can be
// defined in separate index.json files. This makes it easy to
// accidently cause an identifier clash. To help detect it, we allow
- // each resource to have a UUID associated with it. Attempt to
- // define multiple resources with the same identifier and different
- // UUIDs will result in an error being reported. Defining multiple
- // resources with different identifiers and the same UUID is
+ // each item to have a UUID associated with it. Attempt to define
+ // multiple mapping with the same identifier and different UUIDs
+ // will result in an error being reported. Defining multiple
+ // mappings with different identifiers and the same UUID is
// disallowed for now (it may be later permitted if we consider it
// good for some use-case).
- // As of package source schema version 1.0, UUIDs are optional and
+ // As of package source schema version 2.0, UUIDs are optional and
// can be omitted.
+ "uuid": "54d23bba-472e-42f5-9194-eaa24c0e3ee7",
+
+ // Thanks to the "version" field (and "revision" field in case of
+ // "resource" or "mapping_and_resource", clients will know they have
+ // to update certain item after a new version has appeared in the
+ // repository. If multiple definitions of the same version of given
+ // item are provided to Hydrilla server, an error is generated.
+ // "version" differs from its counterpart in resource in that it has
+ // no accompanying revision number. For combined definitions with
+ // "mapping_and_resource" as type, the value of "revision" is
+ // appended as the last component of the resulting mapping's
+ // version. If type is simply "mapping", revision number is ignored.
+ "version": [2021, 11, 10],
+
+ // A short, meaningful description of what the mapping does.
+ "description": "causes apple to get greeted on Hydrillabugs issue tracker",
+
+ // If needed, a "comment" field can be added to provide some
+ // additional information.
+ // "comment": "this resource something something",
+
+ // The "payloads" object specifies which payloads are to be applied
+ // to which URLs.
+ "payloads": {
+ // Each key should be a valid Haketilo URL pattern.
+ "https://hydrillabugs.koszko.org/***": {
+ // Should be the name of an existing resource. The resource
+ // may, but doesn't have to, be defined in the same
+ // index.json file.
+ "identifier": "helloapple"
+ },
+ // More associations may follow.
+ "https://hachettebugs.koszko.org/***": {
+ "identifier": "helloapple"
+ }
+ }
+ }, {
+ // A "resource" item defines a list of injectable scripts that can
+ // be used as a payload or as a dependency of another "resource". In
+ // the future CSS style sheets and WASM modules will also be
+ // composite parts of a "resource" as scripts are now.
+ "type": "resource",
+
+ // Has similar function to mapping's identifier. Used when
+ // referring to this resource in "dependencies" list of another
+ // resource or in "payload" field of a mapping. Should be consize
+ // and can only use a restricted set of characters. It has to match:
+ // [-0-9a-z]+
+ // It can be the same as some mapping identifier (those are
+ // different entities and are treated separately).
+ "identifier": "helloapple",
+
+ // "long name" and "uuid" have the same meaning as in the case of
+ // resources and "uuid" is also optional. UUIDs of a resource and a
+ // mapping can technically be the same but it is recommended to
+ // avoid even this kind of repetition.
+ "long_name": "Hello Apple",
"uuid": "a6754dcb-58d8-4b7a-a245-24fd7ad4cd68",
// Version should match the upstream version of the resource (e.g. a
- // version of javascript library). Revision number starts as 1 for
+ // version of JavaScript library). Revision number starts as 1 for
// each new resource version and gets incremented by 1 each time a
- // modification to the packaging of this version is done. Hydrilla
- // will allow multiple definitions of the same resource to load, as
- // long as their versions differ. Thanks to the "version" and
- // "revision" fields, clients will know they have to update certain
- // resource after it has been updated. If multiple definitions of
- // the same version of given resource are provided, an error is
- // generated (even if those definitions differ by revision number).
+ // modification to the packaging of this version is done.
+ // If multiple definitions of the same version of given resource are
+ // provided to Hydrilla server, an error is generated (even if those
+ // definitions differ by revision number).
"version": [2021, 11, 10],
"revision": 1,
@@ -123,16 +174,15 @@
// what it does.
"description": "greets an apple",
- // If needed, a "comment" field can be added to provide some
- // additional information.
- // "comment": "this resource something something",
+ // A comment, if necessary.
+ // "comment": "blah blah because bleh"
// Resource's "dependencies" array shall contain names of other
// resources that (in case of scripts at least) should get evaluated
// on a page before this resource's own scripts.
"dependencies": [{"identifier": "hello-message"}],
- // Array of javascript files that belong to this resource.
+ // Array of JavaScript files that belong to this resource.
"scripts": [
{"file": "hello.js"},
{"file": "bye.js"}
@@ -148,48 +198,6 @@
// If "dependencies" is empty, it can also be omitted.
// "dependencies": [],
"scripts": [{"file": "message.js"}]
- }, {
- "type": "mapping",
-
- // Has similar function to resource's identifier. Should be consize
- // and can only use a restricted set of characters. It has to match:
- // [-0-9a-z]+
- // It can be the same as some resource identifier (those are
- // different entities and are treated separately).
- "identifier": "helloapple",
-
- // "long name" and "uuid" have the same meaning as in the case of
- // resources and "uuid" is also optional. UUIDs of a resource and a
- // mapping can technically be the same but it is recommended to
- // avoid even this kind of repetition.
- "long_name": "Hello Apple",
- "uuid": "54d23bba-472e-42f5-9194-eaa24c0e3ee7",
-
- // "version" differs from its counterpart in resource in that it has
- // no accompanying revision number.
- "version": [2021, 11, 10],
-
- // A short, meaningful description of what the mapping does.
- "description": "causes apple to get greeted on Hydrillabugs issue tracker",
-
- // A comment, if necessary.
- // "comment": "blah blah because bleh"
-
- // The "payloads" object specifies which payloads are to be applied
- // to which URLs.
- "payloads": {
- // Each key should be a valid Haketilo URL pattern.
- "https://hydrillabugs.koszko.org/***": {
- // Should be the name of an existing resource. The resource
- // may, but doesn't have to, be defined in the same
- // index.json file.
- "identifier": "helloapple"
- },
- // More associations may follow.
- "https://hachettebugs.koszko.org/***": {
- "identifier": "helloapple"
- }
- }
}
],
// We can also list additional files to include in the produced source
diff --git a/tests/test_server.py b/tests/test_server.py
index 0820d5c..65ae8ce 100644
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -31,6 +31,7 @@ import pytest
import sys
import shutil
import json
+import functools as ft
from pathlib import Path
from hashlib import sha256
@@ -42,9 +43,10 @@ from markupsafe import escape
from werkzeug import Response
from hydrilla import util as hydrilla_util
-from hydrilla.builder import Build
-from hydrilla.server import config, _version
-from hydrilla.server.serve import HydrillaApp
+from hydrilla.builder import build
+from hydrilla.server import config, _version, serve
+
+from .helpers import *
here = Path(__file__).resolve().parent
config_path = here / 'config.json'
@@ -55,13 +57,41 @@ expected_generated_by = {
'version': _version.version
}
-SetupMod = Optional[Callable['Setup', None]]
+SetupMod = Optional[Callable[['Setup'], None]]
source_files = (
'index.json', 'hello.js', 'bye.js', 'message.js', 'README.txt',
'README.txt.license', '.reuse/dep5', 'LICENSES/CC0-1.0.txt'
)
+def run_reuse(command, **kwargs):
+ """
+ Instead of running a 'reuse' command, check if 'mock_reuse_missing' file
+ exists under root directory. If yes, raise FileNotFoundError as if 'reuse'
+ command was missing. If not, check if 'README.txt.license' file exists
+ in the requested directory and return zero if it does.
+ """
+ expected = ['reuse', '--root', '<root>',
+ 'lint' if 'lint' in command else 'spdx']
+
+ root_path = Path(process_command(command, expected)['root'])
+
+ if (root_path / 'mock_reuse_missing').exists():
+ raise FileNotFoundError('dummy')
+
+ is_reuse_compliant = (root_path / 'README.txt.license').exists()
+
+ return MockedCompletedProcess(command, 1 - is_reuse_compliant,
+ stdout=f'dummy {expected[-1]} output',
+ text_output=kwargs.get('text'))
+
+@pytest.fixture
+def mock_reuse(mock_subprocess_run):
+ """
+ Mock the REUSE command when executed through subprocess.run() from serve.py.
+ """
+ mock_subprocess_run(build, run_reuse)
+
class Setup:
"""
Facilitate preparing test malcontent directory, Hydrilla config file and the
@@ -95,8 +125,8 @@ class Setup:
if self._modify_before_build:
self._modify_before_build(self)
- build = Build(self.source_dir, self.index_json)
- build.write_package_files(self.malcontent_dir)
+ build_obj = build.Build(self.source_dir, self.index_json)
+ build_obj.write_package_files(self.malcontent_dir)
if self._modify_after_build:
self._modify_after_build(self)
@@ -114,48 +144,64 @@ class Setup:
Provide app client that serves the objects from built sample package.
"""
if self._client is None:
- app = HydrillaApp(self.config(), flask_config={'TESTING': True})
+ app = serve.HydrillaApp(self.config(),
+ flask_config={'TESTING': True})
self._client = app.test_client()
return self._client
-def remove_all_uuids(setup: Setup) -> None:
- """Modify sample packages before build to contain no (optional) UUIDs"""
- index_json = (setup.source_dir / 'index.json').read_text()
- index_json = json.loads(hydrilla_util.strip_json_comments(index_json))
+def index_json_modification(modify_index_json):
+ """Decorator for function that modifies index.json before build."""
+ def handle_index_json(setup):
+ """Modify index.json before build."""
+ index_path = setup.source_dir / 'index.json'
+ index_json, _ = hydrilla_util.load_instance_from_file(index_path)
+
+ index_json = modify_index_json(index_json) or index_json
+
+ index_json = f'''
+ // SPDX-License-Identifier: CC0-1.0
+ // Copyright (C) 2021, 2022 Wojtek Kosior
+ {json.dumps(index_json)}
+ '''
+
+ index_path.write_text(index_json)
+ return handle_index_json
+
+@index_json_modification
+def remove_all_uuids(index_json):
+ """Modify sample packages to contain no (optional) UUIDs"""
for definition in index_json['definitions']:
del definition['uuid']
- index_json = ("// SPDX-License-Identifier: CC0-1.0\n" +
- "// Copyright (C) 2021, 2022 Wojtek Kosior\n" +
- json.dumps(index_json))
+@index_json_modification
+def bump_schema_v2(index_json) -> None:
+ """Modify sample packages to use version 2 of Hydrilla JSON schemas."""
+ for definition in index_json['definitions']:
+ definition['min_haketilo_version'] = [1, 1]
- (setup.source_dir / 'index.json').write_text(index_json)
+ if definition['identifier'] == 'helloapple' and \
+ definition['type'] == 'resource':
+ definition['required_mappings'] = {'identifier': 'helloapple'}
-default_setup = Setup()
-uuidless_setup = Setup(modify_before_build=remove_all_uuids)
+default_setup = lambda: Setup()
+uuidless_setup = lambda: Setup(modify_before_build=remove_all_uuids)
+schema_v2_setup = lambda: Setup(modify_before_build=bump_schema_v2)
-def def_get(url: str) -> Response:
- """Convenience wrapper for def_get()"""
- return default_setup.client().get(url)
+setup_makers = [default_setup, uuidless_setup, schema_v2_setup]
-def test_project_url() -> None:
- """Fetch index.html and verify project URL from config is present there."""
- response = def_get('/')
- assert b'html' in response.data
- project_url = default_setup.config()['hydrilla_project_url']
- assert escape(project_url).encode() in response.data
-
-@pytest.mark.parametrize('setup', [default_setup, uuidless_setup])
+@pytest.mark.usefixtures('mock_reuse')
+@pytest.mark.parametrize('setup_maker', setup_makers)
@pytest.mark.parametrize('item_type', ['resource', 'mapping'])
-def test_get_newest(setup: Setup, item_type: str) -> None:
+def test_get_newest(setup_maker, item_type: str) -> None:
"""
Verify that
GET '/{item_type}/{item_identifier}.json'
returns proper definition that is also served at:
GET '/{item_type}/{item_identifier}/{item_version}'
"""
+ setup = setup_maker()
response = setup.client().get(f'/{item_type}/helloapple.json')
assert response.status_code == 200
definition = json.loads(response.data.decode())
@@ -166,46 +212,58 @@ def test_get_newest(setup: Setup, item_type: str) -> None:
assert response.status_code == 200
assert definition == json.loads(response.data.decode())
- assert ('uuid' in definition) == (setup is not uuidless_setup)
+ assert ('uuid' in definition) == (setup_maker is not uuidless_setup)
hydrilla_util.validator_for(f'api_{item_type}_description-1.0.1.schema.json')\
.validate(definition)
+@pytest.fixture
+def setup(mock_reuse):
+ """Prepare server test environment in the default way."""
+ return default_setup()
+
+def test_project_url(setup) -> None:
+ """Fetch index.html and verify project URL from config is present there."""
+ response = setup.client().get('/')
+ assert b'html' in response.data
+ project_url = setup.config()['hydrilla_project_url']
+ assert escape(project_url).encode() in response.data
+
@pytest.mark.parametrize('item_type', ['resource', 'mapping'])
-def test_get_nonexistent(item_type: str) -> None:
+def test_get_nonexistent(setup, item_type: str) -> None:
"""
Verify that attempts to GET a JSON definition of a nonexistent item or item
version result in 404.
"""
- response = def_get(f'/{item_type}/nonexistentapple.json')
+ response = setup.client().get(f'/{item_type}/nonexistentapple.json')
assert response.status_code == 404
- response = def_get(f'/{item_type}/helloapple/1.2.3.999')
+ response = setup.client().get(f'/{item_type}/helloapple/1.2.3.999')
assert response.status_code == 404
@pytest.mark.parametrize('item_type', ['resource', 'mapping'])
-def test_file_refs(item_type: str) -> None:
+def test_file_refs(setup, item_type: str) -> None:
"""
Verify that files referenced by definitions are accessible under their
proper URLs and that their hashes match.
"""
- response = def_get(f'/{item_type}/helloapple/2021.11.10')
+ response = setup.client().get(f'/{item_type}/helloapple/2021.11.10')
assert response.status_code == 200
definition = json.loads(response.data.decode())
for file_ref in [*definition.get('scripts', []),
*definition['source_copyright']]:
hash_sum = file_ref["sha256"]
- response = def_get(f'/file/sha256/{hash_sum}')
+ response = setup.client().get(f'/file/sha256/{hash_sum}')
assert response.status_code == 200
assert sha256(response.data).digest().hex() == hash_sum
-def test_empty_query() -> None:
+def test_empty_query(setup) -> None:
"""
Verify that querying mappings for URL gives an empty list when there're no
mathes.
"""
- response = def_get(f'/query?url=https://nonexiste.nt/example')
+ response = setup.client().get(f'/query?url=https://nonexiste.nt/example')
assert response.status_code == 200
response_object = json.loads(response.data.decode())
@@ -219,12 +277,12 @@ def test_empty_query() -> None:
hydrilla_util.validator_for('api_query_result-1.0.1.schema.json')\
.validate(response_object)
-def test_query() -> None:
+def test_query(setup) -> None:
"""
Verify that querying mappings for URL gives a list with reference(s) the the
matching mapping(s).
"""
- response = def_get(f'/query?url=https://hydrillabugs.koszko.org/')
+ response = setup.client().get(f'/query?url=https://hydrillabugs.koszko.org/')
assert response.status_code == 200
response_object = json.loads(response.data.decode())
@@ -239,12 +297,12 @@ def test_query() -> None:
'generated_by': expected_generated_by
}
- hydrilla_util.validator_for('api_query_result-1.0.1.schema.json')\
+ hydrilla_util.validator_for('api_query_result-1.schema.json')\
.validate(response_object)
-def test_source() -> None:
+def test_source(setup) -> None:
"""Verify source descriptions are properly served."""
- response = def_get(f'/source/hello.json')
+ response = setup.client().get(f'/source/hello.json')
assert response.status_code == 200
description = json.loads(response.data.decode())
@@ -254,18 +312,18 @@ def test_source() -> None:
['hello-message', 'helloapple', 'helloapple']
zipfile_hash = description['source_archives']['zip']['sha256']
- response = def_get(f'/source/hello.zip')
+ response = setup.client().get(f'/source/hello.zip')
assert sha256(response.data).digest().hex() == zipfile_hash
- hydrilla_util.validator_for('api_source_description-1.0.1.schema.json')\
+ hydrilla_util.validator_for('api_source_description-1.schema.json')\
.validate(description)
-def test_missing_source() -> None:
+def test_missing_source(setup) -> None:
"""Verify requests for nonexistent sources result in 404."""
- response = def_get(f'/source/nonexistent.json')
+ response = setup.client().get(f'/source/nonexistent.json')
assert response.status_code == 404
- response = def_get(f'/source/nonexistent.zip')
+ response = setup.client().get(f'/source/nonexistent.zip')
assert response.status_code == 404
def test_normalize_version():