pypiserver/tests/test_init.py
Mason Lin ae3dcf2bbd
feat: 🩺 allow customized health check endpoint (#442)
* feat: 🩺 allow customized health check endpoint

Get the liveness endpoint from the environment variable `HEALTH_ENDPOINT` and verify it. If the customized endpoint is invalied, it will fallback to the DEFAULT_HEALTH_ENDPOINT.

* test:  Test customized endpoint feature

* fix: 🚨 fix check

* feat: Use CLI interface to set health endpoint

* style: 💄 fix black format

* Separate 'build app' and 'add routes'

https://github.com/pypiserver/pypiserver/pull/442#discussion_r973771421

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* keep DEFAULTS in config.py

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* style alignment

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* make CLI arg description more clear

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* style: 🎨 style alignment

* refactor:  SRP, add routes after app created, instead of patching in app_from_config

* style: 🎨 format CLI help

* test:  add test_setup_routes_from_config

* fix: 🐛 test name doesn't work as expected because of using the wrong ids generator.

* test: 🧪 add config error cases for health endpoint

* test:  fix health_endpoint_arg tests

* fix:  Do not fallback to default silently, should raise error

* test: 🧪 add test_health_endpoint in test_main

* test:  setup routes in main

* docs: 📝 Update the help command output in the Quickstart

* docs: 🐛 missing space

* docs: 📝 Add 'Custom Health Check Endpoint' to 'Recipes'

* docs: 📝 refine README

* revert:  revert auto isoft

* build: 💚 fix mypy, missing return types

* Update README.rst

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* Update README.rst

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* Update pypiserver/config.py

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* Update README.rst

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

* style: 💄 black format

* Update README.rst

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>

Co-authored-by: Dmitrii Orlov <dmtree.dev@yahoo.com>
2022-11-02 12:32:20 +01:00

123 lines
3.3 KiB
Python

"""
Test module for app initialization
"""
# Standard library imports
import logging
import pathlib
import typing as t
# Third party imports
import pytest
# Local imports
import pypiserver
from pypiserver.config import Config
logger = logging.getLogger(__name__)
TEST_DIR = pathlib.Path(__file__).parent
HTPASS_FILE = TEST_DIR / "../fixtures/htpasswd.a.a"
WELCOME_FILE = TEST_DIR / "sample_msg.html"
# TODO: make these tests meaningful
@pytest.mark.parametrize(
"conf_options",
[
{},
{"root": "~/stable_packages"},
{
"root": "~/unstable_packages",
"authenticated": "upload",
"passwords": str(HTPASS_FILE),
},
# Verify that the strip parser works properly.
{"authenticated": str("upload")},
],
)
def test_paste_app_factory(conf_options: dict) -> None:
"""Test the paste_app_factory method"""
pypiserver.paste_app_factory({}, **conf_options) # type: ignore
def test_app_factory() -> None:
assert pypiserver.app() is not pypiserver.app()
@pytest.mark.parametrize(
"incoming, updated",
(
(
{"authenticated": []},
{"authenticate": []},
),
(
{"passwords": "./foo"},
{"password_file": "./foo"},
),
(
{"root": str(TEST_DIR)},
{"roots": [TEST_DIR.expanduser().resolve()]},
),
(
{"root": [str(TEST_DIR), str(TEST_DIR)]},
{
"roots": [
TEST_DIR.expanduser().resolve(),
TEST_DIR.expanduser().resolve(),
]
},
),
(
{"redirect_to_fallback": False},
{"disable_fallback": True},
),
(
{"server": "auto"},
{"server_method": "auto"},
),
(
{"welcome_file": str(WELCOME_FILE.resolve())},
{"welcome_msg": WELCOME_FILE.read_text()},
),
),
)
def test_backwards_compat_kwargs_conversion(
incoming: t.Dict[str, t.Any], updated: t.Dict[str, t.Any]
) -> None:
"""Test converting legacy kwargs to modern ones."""
assert pypiserver.backwards_compat_kwargs(incoming) == updated
@pytest.mark.parametrize(
"kwargs",
(
{"redirect_to_fallback": False, "disable_fallback": False},
{"disable_fallback": False, "redirect_to_fallback": False},
),
)
def test_backwards_compat_kwargs_duplicate_check(
kwargs: t.Dict[str, t.Any]
) -> None:
"""Duplicate legacy and modern kwargs cause an error."""
with pytest.raises(ValueError) as err:
pypiserver.backwards_compat_kwargs(kwargs)
assert "('redirect_to_fallback', 'disable_fallback')" in str(err.value)
def test_setup_routes_from_config_customized_endpoint() -> None:
_app = pypiserver.setup_routes_from_config(
pypiserver.app(),
Config.default_with_overrides(health_endpoint="/healthz"),
)
assert "/healthz" in (route.rule for route in _app.routes)
def test_setup_routes_from_config_invalid_customized_endpoint() -> None:
with pytest.raises(RuntimeError, match="overlaps with existing routes"):
pypiserver.setup_routes_from_config(
pypiserver.app(),
Config.default_with_overrides(health_endpoint="/simple"),
)