Buckets:
| from pathlib import Path | |
| from typing import Any | |
| import httpx | |
| import pytest | |
| import respx | |
| from fastapi import APIRouter | |
| from pydantic import ValidationError | |
| from tiled.adapters.array import ArrayAdapter | |
| from tiled.adapters.mapping import MapAdapter | |
| from tiled.client import Context, from_context | |
| from tiled.config import Config, parse_configs | |
| from tiled.server.app import build_app_from_config | |
| tree = MapAdapter({"example": ArrayAdapter.from_array([1, 2, 3])}) | |
| def test_root(): | |
| "One tree served at top level" | |
| config = { | |
| "trees": [ | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/", | |
| }, | |
| ] | |
| } | |
| with Context.from_app(build_app_from_config(config)) as context: | |
| client = from_context(context) | |
| assert list(client) == ["example"] | |
| def test_single_nested(): | |
| "One tree served nested one layer down" | |
| config = { | |
| "trees": [ | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/b", | |
| }, | |
| ] | |
| } | |
| with Context.from_app(build_app_from_config(config)) as context: | |
| client = from_context(context) | |
| assert list(client) == ["a"] | |
| assert list(client["a"]) == ["b"] | |
| assert list(client["a"]["b"]) == ["example"] | |
| def test_single_deeply_nested(): | |
| "One tree served nested many layers down" | |
| config = { | |
| "trees": [ | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/b/c/d/e", | |
| }, | |
| ] | |
| } | |
| with Context.from_app(build_app_from_config(config)) as context: | |
| client = from_context(context) | |
| assert list(client) == ["a"] | |
| assert list(client["a"]) == ["b"] | |
| assert list(client["a"]["b"]) == ["c"] | |
| assert list(client["a"]["b"]["c"]) == ["d"] | |
| assert list(client["a"]["b"]["c"]["d"]) == ["e"] | |
| assert list(client["a"]["b"]["c"]["d"]["e"]) == ["example"] | |
| def test_many_nested(): | |
| config = { | |
| "trees": [ | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/b", | |
| }, | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/c", | |
| }, | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/d/e", | |
| }, | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/d/f", | |
| }, | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/d/g/h", | |
| }, | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a/d/g/i", | |
| }, | |
| ], | |
| } | |
| with Context.from_app(build_app_from_config(config)) as context: | |
| client = from_context(context) | |
| assert list(client["a"]["b"]) == ["example"] | |
| assert list(client["a"]["c"]) == ["example"] | |
| assert list(client["a"]["d"]["e"]) == ["example"] | |
| assert list(client["a"]["d"]["f"]) == ["example"] | |
| assert list(client["a"]["d"]["g"]["h"]) == ["example"] | |
| assert list(client["a"]["d"]["g"]["i"]) == ["example"] | |
| def test_extra_files(tmpdir): | |
| import yaml | |
| config = {"trees": [{"path": "/", "tree": "tiled.examples.generated_minimal:tree"}]} | |
| with open(tmpdir / "config.yml", "w") as config_file: | |
| yaml.dump(config, config_file) | |
| with open(tmpdir / "README.md", "w") as extra_file: | |
| extra_file.write("# Example") | |
| parse_configs(str(tmpdir)) | |
| def test_multi_file_trees(tmpdir): | |
| "Test that 'trees' can be specified across more than one file, merged." | |
| import yaml | |
| conf1 = {"trees": [{"path": "/a", "tree": "tiled.examples.generated_minimal:tree"}]} | |
| conf2 = {"trees": [{"path": "/b", "tree": "tiled.examples.generated_minimal:tree"}]} | |
| with open(tmpdir / "conf1.yml", "w") as c1: | |
| yaml.dump(conf1, c1) | |
| with open(tmpdir / "conf2.yml", "w") as c2: | |
| yaml.dump(conf2, c2) | |
| config = parse_configs(tmpdir) | |
| assert len(config.trees) == 2 | |
| def test_multi_file_conflict(tmpdir): | |
| "Test that media_types can only be specified in a single config file." | |
| import yaml | |
| conf1 = { | |
| "media_types": {}, | |
| "trees": [{"path": "/a", "tree": "tiled.examples.generated_minimal:tree"}], | |
| } | |
| conf2 = { | |
| "media_types": {}, | |
| "trees": [{"path": "/b", "tree": "tiled.examples.generated_minimal:tree"}], | |
| } | |
| with open(tmpdir / "conf1.yml", "w") as c1: | |
| yaml.dump(conf1, c1) | |
| with open(tmpdir / "conf2.yml", "w") as c2: | |
| yaml.dump(conf2, c2) | |
| with pytest.raises(ValueError, match="Duplicate configuration for {'media_types'}"): | |
| parse_configs(tmpdir) | |
| def test_invalid_config_file_path(tmpdir: Path, path: str, error: type[Exception]): | |
| with pytest.raises(error): | |
| parse_configs(tmpdir / path) | |
| def test_example_configs(path): | |
| config = parse_configs(path) | |
| build_app_from_config(config) | |
| def test_pydantic_config(): | |
| Config.model_validate({"catalog": {"uri": "sqlite:///:memory:"}}) | |
| def test_catalog_adapters_by_mimetype_mapping(): | |
| Config.model_validate( | |
| { | |
| "catalog": { | |
| "uri": "sqlite:///:memory:", | |
| "adapters_by_mimetype": { | |
| "text/csv": "tiled.adapters.csv:CSVAdapter", | |
| }, | |
| } | |
| } | |
| ) | |
| def test_duplicate_auth_providers(): | |
| with pytest.raises(ValidationError, match="provider names must be unique"): | |
| Config.model_validate( | |
| { | |
| "authentication": { | |
| "providers": [ | |
| { | |
| "provider": "one", | |
| "authenticator": "tiled.authenticators:DummyAuthenticator", | |
| }, | |
| { | |
| "provider": "one", | |
| "authenticator": "tiled.authenticators:DictionaryAuthenticator", | |
| }, | |
| ] | |
| } | |
| } | |
| ) | |
| def test_proxied_authenticator_single_instance_required( | |
| well_known_response: dict[str, Any] | |
| ): | |
| respx.get("http://example.com").mock( | |
| side_effect=[ | |
| httpx.Response(httpx.codes.OK, json=well_known_response), | |
| httpx.Response(httpx.codes.OK, json=well_known_response), | |
| ] | |
| ) | |
| with pytest.raises( | |
| ValidationError, | |
| match="Multiple ProxiedOIDCAuthenticator instances are configured.", | |
| ): | |
| Config.model_validate( | |
| { | |
| "trees": [], | |
| "authentication": { | |
| "providers": [ | |
| { | |
| "provider": "one", | |
| "authenticator": "tiled.authenticators:ProxiedOIDCAuthenticator", | |
| "args": { | |
| "audience": "tiled", | |
| "client_id": "tiled", | |
| "client_secret": "secret", | |
| "device_flow_client_id": "tiled-cli", | |
| "well_known_uri": "http://example.com", | |
| }, | |
| }, | |
| { | |
| "provider": "two", | |
| "authenticator": "tiled.authenticators:ProxiedOIDCAuthenticator", | |
| "args": { | |
| "audience": "tiled", | |
| "client_id": "tiled", | |
| "client_secret": "secret", | |
| "device_flow_client_id": "tiled-cli", | |
| "well_known_uri": "http://example.com", | |
| "redirect_on_success": "https://example.com/ui/auth-callback", | |
| }, | |
| }, | |
| ] | |
| }, | |
| } | |
| ) | |
| def test_proxied_authenticator_is_not_used_with_other_authenticators( | |
| well_known_response: dict[str, Any], | |
| ): | |
| respx.get("http://example.com").mock( | |
| return_value=httpx.Response(httpx.codes.OK, json=well_known_response) | |
| ) | |
| with pytest.raises( | |
| ValidationError, | |
| match="ProxiedOIDCAuthenticator must not be configured together with other authentication providers.", | |
| ): | |
| Config.model_validate( | |
| { | |
| "trees": [], | |
| "authentication": { | |
| "providers": [ | |
| { | |
| "provider": "one", | |
| "authenticator": "tiled.authenticators:DummyAuthenticator", | |
| }, | |
| { | |
| "provider": "two", | |
| "authenticator": "tiled.authenticators:ProxiedOIDCAuthenticator", | |
| "args": { | |
| "audience": "tiled", | |
| "client_id": "tiled", | |
| "client_secret": "secret", | |
| "well_known_uri": "http://example.com", | |
| "device_flow_client_id": "tiled-cli", | |
| }, | |
| }, | |
| ] | |
| }, | |
| } | |
| ) | |
| def test_overlapping_trees(paths: tuple[str, ...]): | |
| with pytest.raises(ValidationError, match="cannot be subpaths of each other"): | |
| Config.model_validate( | |
| {"trees": [{"tree": "catalog", "path": path} for path in paths]} | |
| ) | |
| def test_empty_api_key(): | |
| with pytest.raises( | |
| ValidationError, match=r"should match pattern '\[a-zA-Z0-9\]\+'" | |
| ): | |
| Config.model_validate({"authentication": {"single_user_api_key": ""}}) | |
| class Dummy: | |
| "Referenced below in test_tree_given_as_method" | |
| def constructor(): | |
| return tree | |
| def test_tree_given_as_method(): | |
| config = { | |
| "trees": [ | |
| { | |
| "tree": f"{__name__}:Dummy.constructor", | |
| "path": "/", | |
| }, | |
| ] | |
| } | |
| Config.model_validate(config) | |
| # Define custom APIRouters for tests | |
| router_in_tree = APIRouter() | |
| tree.include_routers = [router_in_tree] | |
| router1 = APIRouter() | |
| router2 = APIRouter() | |
| async def tree_endpoint(): | |
| return {"message": "Hello from the tree router"} | |
| async def router1_endpoint(): | |
| return {"message": "Hello from router 1"} | |
| async def router2_endpoint(): | |
| return {"message": "Hello from router 2"} | |
| def test_include_routers(sqlite_or_postgres_uri): | |
| config = { | |
| "trees": [ | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/a", | |
| }, | |
| { | |
| "tree": f"{__name__}:tree", | |
| "path": "/b", | |
| }, | |
| { | |
| "path": "/c", | |
| "tree": "catalog", | |
| "args": { | |
| "uri": sqlite_or_postgres_uri, | |
| "init_if_not_exists": True, | |
| }, | |
| }, | |
| ], | |
| "routers": [f"{__name__}:router1", f"{__name__}:router2"], | |
| } | |
| app = build_app_from_config(config) | |
| with Context.from_app(app) as context: | |
| client = from_context(context) | |
| # Test access to the endpoints defined in the included routers | |
| response_by_endpoint = { | |
| "/custom/tree-endpoint": "Hello from the tree router", | |
| "/custom/router1-endpoint": "Hello from router 1", | |
| "/custom/router2-endpoint": "Hello from router 2", | |
| "/api/v1/tree-endpoint": "Hello from the tree router", | |
| "/api/v1/router1-endpoint": "Hello from router 1", | |
| "/api/v1/router2-endpoint": "Hello from router 2", | |
| } | |
| for endpoint, expected_message in response_by_endpoint.items(): | |
| response = client.context.http_client.get(endpoint) | |
| assert response.status_code == 200 | |
| assert response.json()["message"] == expected_message | |
Xet Storage Details
- Size:
- 12.8 kB
- Xet hash:
- 22325b9a36f9e6e0027511cd1d7cb746d01a4411c789b380f743c2c05a528f9e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.