Buckets:

download
raw
21.2 kB
"""Focused regression tests for Bloom runtime configuration."""
from __future__ import annotations
import json
import logging
import re
import tempfile
import tomllib
from pathlib import Path
import pytest
import yaml
from pydantic import ValidationError
from bloom_lims import __version__
from bloom_lims.config import (
DEFAULT_TAPDB_DOMAIN_CODE,
DEFAULT_TAPDB_OWNER_REPO_NAME,
BloomSettings,
StorageSettings,
assert_tapdb_version,
build_default_config_template,
build_effective_config_summary,
expected_conda_env_name,
generate_example_webhook_secret,
get_settings,
get_tapdb_runtime_context,
get_user_config_path,
validate_settings,
)
from tests.support.runtime import (
ensure_test_runtime_environment,
read_pyproject_dependency_spec,
)
def test_storage_temp_dir_uses_system_tempdir():
assert StorageSettings().temp_dir == str(Path(tempfile.gettempdir()) / "bloom")
def test_storage_upload_dir_is_required(monkeypatch, tmp_path: Path):
config_path = tmp_path / "tapdb" / "bloom-local2" / "tapdb-config.yaml"
monkeypatch.delenv("BLOOM_CONFIG_PATH", raising=False)
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / ".config"))
monkeypatch.setenv("BLOOM_TAPDB__CONFIG_PATH", str(config_path))
get_settings.cache_clear()
with pytest.raises(ValidationError, match="storage.upload_dir is required"):
BloomSettings()
def test_expected_conda_env_name_uses_deployment_code(monkeypatch):
monkeypatch.setenv("BLOOM_DEPLOYMENT_CODE", "bringup")
assert expected_conda_env_name() == "BLOOM-bringup"
def test_expected_conda_env_name_requires_explicit_deployment_code(monkeypatch):
monkeypatch.delenv("BLOOM_DEPLOYMENT_CODE", raising=False)
monkeypatch.delenv("DEPLOYMENT_CODE", raising=False)
monkeypatch.delenv("LSMC_DEPLOYMENT_CODE", raising=False)
monkeypatch.setenv("CONDA_DEFAULT_ENV", "BLOOM-smoke")
with pytest.raises(RuntimeError, match="Bloom requires explicit"):
expected_conda_env_name()
def test_user_config_path_prefers_explicit_bloom_config_path(
monkeypatch, tmp_path: Path
):
explicit = tmp_path / "custom" / "bloom-config-omfg33.yaml"
monkeypatch.setenv("BLOOM_CONFIG_PATH", str(explicit))
assert get_user_config_path() == explicit.resolve()
def test_user_config_path_rejects_relative_bloom_config_path(monkeypatch):
monkeypatch.setenv("BLOOM_CONFIG_PATH", "relative/bloom-config.yaml")
with pytest.raises(
RuntimeError, match="BLOOM_CONFIG_PATH must be a full absolute path"
):
get_user_config_path()
def test_explicit_bloom_config_path_overrides_deployment_guess(
monkeypatch, tmp_path: Path
):
explicit_config = tmp_path / "configs" / "bloom-override.yaml"
explicit_config.parent.mkdir(parents=True)
explicit_config.write_text(
"deployment:\n"
" name: explicit-deploy\n"
"storage:\n"
f" upload_dir: {tmp_path / 'uploads'}\n"
"auth:\n"
" jwt_secret: explicit-test-session-secret\n"
" cognito_domain: explicit.auth.us-west-2.amazoncognito.com\n",
encoding="utf-8",
)
monkeypatch.setenv("BLOOM_CONFIG_PATH", str(explicit_config))
monkeypatch.setenv("BLOOM_DEPLOYMENT_CODE", "wrong-deploy")
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "xdg"))
monkeypatch.delenv("BLOOM_AUTH__COGNITO_DOMAIN", raising=False)
get_settings.cache_clear()
settings = BloomSettings()
assert settings.deployment.name == "explicit-deploy"
assert settings.auth.cognito_domain == "explicit.auth.us-west-2.amazoncognito.com"
def test_nested_env_overrides_template_defaults(monkeypatch):
monkeypatch.setenv("BLOOM_AUTH__COGNITO_USER_POOL_ID", "pool-from-env")
monkeypatch.setenv("BLOOM_AUTH__COGNITO_CLIENT_ID", "client-from-env")
monkeypatch.setenv(
"BLOOM_AUTH__COGNITO_DOMAIN", "env-test.auth.us-west-2.amazoncognito.com"
)
monkeypatch.setenv(
"BLOOM_AUTH__COGNITO_REDIRECT_URI", "https://example.test/auth/callback"
)
get_settings.cache_clear()
settings = BloomSettings()
assert settings.auth.cognito_user_pool_id == "pool-from-env"
assert settings.auth.cognito_client_id == "client-from-env"
assert settings.auth.cognito_domain == "env-test.auth.us-west-2.amazoncognito.com"
assert settings.auth.cognito_redirect_uri == "https://example.test/auth/callback"
def test_external_broker_configuration_from_shared_env(monkeypatch, tmp_path: Path):
ca_bundle = tmp_path / "ca-bundle.pem"
ca_bundle.write_text("TEST CA\n", encoding="utf-8")
monkeypatch.setenv("LSMC_AUTH_MODE", "external_broker")
monkeypatch.setenv("LSMC_AUTH_BROKER_SERVICE_ID", "bloom")
monkeypatch.setenv(
"LSMC_AUTH_BROKER_LOGIN_URL", "https://dev.login.lsmc.com/auth/login"
)
monkeypatch.setenv(
"LSMC_AUTH_BROKER_HANDOFF_EXCHANGE_URL",
"https://dev.login.lsmc.com/auth/handoff/consume",
)
monkeypatch.setenv("LSMC_AUTH_BROKER_SERVICE_TOKEN", "test-bloom-service-token")
monkeypatch.setenv(
"LSMC_AUTH_BROKER_CALLBACK_URL",
"https://localhost:8912/auth/lsmc/callback",
)
monkeypatch.setenv(
"LSMC_AUTH_BROKER_LOGOUT_URL", "https://dev.login.lsmc.com/auth/logout"
)
monkeypatch.setenv("LSMC_AUTH_BROKER_CA_BUNDLE", str(ca_bundle))
get_settings.cache_clear()
settings = BloomSettings()
assert settings.auth.mode == "external_broker"
assert settings.auth.external_broker.service_id == "bloom"
assert settings.auth.external_broker.handoff_exchange_url.endswith(
"/auth/handoff/consume"
)
assert settings.auth.external_broker.ca_bundle == str(ca_bundle)
def test_external_broker_mode_requires_explicit_settings():
with pytest.raises(ValidationError, match="external broker auth requires"):
BloomSettings(auth={"mode": "external_broker"})
def test_env_overrides_user_yaml_for_auth_cognito_domain(monkeypatch, tmp_path: Path):
xdg_config_home = tmp_path / ".config"
user_config_dir = xdg_config_home / "bloom-local"
user_config_dir.mkdir(parents=True)
(user_config_dir / "bloom-config-local.yaml").write_text(
"auth:\n cognito_domain: yaml-test.auth.us-west-2.amazoncognito.com\n",
encoding="utf-8",
)
monkeypatch.setenv("XDG_CONFIG_HOME", str(xdg_config_home))
monkeypatch.setenv("BLOOM_DEPLOYMENT_CODE", "local")
monkeypatch.setenv(
"BLOOM_AUTH__COGNITO_DOMAIN", "env-test.auth.us-west-2.amazoncognito.com"
)
get_settings.cache_clear()
settings = BloomSettings()
assert settings.auth.cognito_domain == "env-test.auth.us-west-2.amazoncognito.com"
def test_auth_cognito_domain_rejects_schemeful_input():
with pytest.raises(
ValidationError, match="auth.cognito_domain must be a bare host"
):
BloomSettings(
auth={
"cognito_domain": "https://bloom.auth.us-east-1.amazoncognito.com",
"cognito_user_pool_id": "us-east-1_POOL123",
"cognito_client_id": "bloom-client",
"cognito_region": "us-east-1",
"cognito_redirect_uri": "https://localhost:8912/auth/callback",
"cognito_logout_redirect_uri": "https://localhost:8912/",
}
)
def test_build_default_config_template_injects_fresh_jwt_secret():
first = build_default_config_template().decode("utf-8")
second = build_default_config_template().decode("utf-8")
assert first != second
for template in (first, second):
match = re.search(r'jwt_secret:\s*"([^"]+)"', template)
assert match is not None
assert len(match.group(1)) >= 32
data = yaml.safe_load(template)
auth = data["auth"]
assert auth["mode"] == "cognito"
assert auth["external_broker"] == {
"service_id": "bloom",
"login_url": "",
"handoff_exchange_url": "",
"service_token": "",
"callback_url": "",
"logout_url": "",
"ca_bundle": "",
}
assert auth["cognito_allowed_domains"] == [
"lsmc.com",
"lsmc.bio",
"lsmc.life",
"daylilyinformatics.com",
]
assert (
auth["cognito_default_tenant_id"] == "00000000-0000-0000-0000-000000000000"
)
assert auth["auto_provision_allowed_domains"] == ["lsmc.com"]
assert data["network"]["allowed_hosts"] == []
assert data["tapdb"]["domain_registry_path"] == ""
assert data["tapdb"]["prefix_ownership_registry_path"] == ""
assert data["tapdb"]["config_path"] == ""
assert "daylilyinformatics.bio" not in template
def test_settings_sources_do_not_include_dotenv():
init_settings = object()
env_settings = object()
dotenv_settings = object()
file_secret_settings = object()
sources = BloomSettings.settings_customise_sources(
BloomSettings,
init_settings,
env_settings,
dotenv_settings,
file_secret_settings,
)
assert dotenv_settings not in sources
def test_runtime_context_requires_explicit_tapdb_paths(monkeypatch):
temp_home = Path(tempfile.mkdtemp())
monkeypatch.setenv("HOME", str(temp_home))
monkeypatch.setenv("XDG_CONFIG_HOME", str(temp_home / ".config"))
monkeypatch.delenv("BLOOM_DEPLOYMENT_CODE", raising=False)
monkeypatch.delenv("DEPLOYMENT_CODE", raising=False)
monkeypatch.delenv("LSMC_DEPLOYMENT_CODE", raising=False)
monkeypatch.delenv("BLOOM_TAPDB__CONFIG_PATH", raising=False)
monkeypatch.delenv("BLOOM_TAPDB__DOMAIN_REGISTRY_PATH", raising=False)
monkeypatch.delenv("BLOOM_TAPDB__PREFIX_OWNERSHIP_REGISTRY_PATH", raising=False)
get_settings.cache_clear()
with pytest.raises(RuntimeError, match="Bloom requires explicit"):
get_tapdb_runtime_context(BloomSettings())
@pytest.mark.parametrize(
("field_name", "field_value", "message"),
[
(
"config_path",
"relative/tapdb-config.yaml",
"tapdb.config_path must be a full absolute path",
),
(
"schema_name",
"",
"tapdb.schema_name is required and must be passed explicitly",
),
(
"config_path",
"~/tapdb-config.yaml",
"tapdb.config_path must be a full absolute path",
),
(
"domain_registry_path",
"relative/domain_code_registry.json",
"tapdb registry paths must be full absolute paths",
),
(
"prefix_ownership_registry_path",
"~/prefix_ownership_registry.json",
"tapdb registry paths must be full absolute paths",
),
],
)
def test_tapdb_path_fields_reject_non_absolute_values(
field_name: str,
field_value: str,
message: str,
):
if field_name == "schema_name":
with pytest.raises(RuntimeError, match=message):
get_tapdb_runtime_context(BloomSettings(tapdb={field_name: field_value}))
else:
with pytest.raises(ValidationError, match=message):
BloomSettings(tapdb={field_name: field_value})
def test_strict_app_startup_accepts_synthesized_test_config(
monkeypatch, tmp_path: Path
):
from bloom_lims.app import create_app
monkeypatch.delenv("BLOOM_SKIP_STARTUP_VALIDATION", raising=False)
monkeypatch.setenv(
"BLOOM_TAPDB__CONFIG_PATH", str(tmp_path / "missing-startup-config.yaml")
)
ensure_test_runtime_environment()
get_settings.cache_clear()
app = create_app()
assert app.title == "FastAPI"
def test_create_app_does_not_mount_upload_or_tmp_static_dirs(
monkeypatch, tmp_path: Path
):
from bloom_lims.app import create_app
monkeypatch.delenv("BLOOM_SKIP_STARTUP_VALIDATION", raising=False)
monkeypatch.setenv(
"BLOOM_TAPDB__CONFIG_PATH", str(tmp_path / "missing-startup-config.yaml")
)
ensure_test_runtime_environment()
get_settings.cache_clear()
app = create_app()
mount_paths = {
path for route in app.routes if (path := getattr(route, "path", None))
}
assert "/static" in mount_paths
assert "/templates" in mount_paths
assert "/uploads" not in mount_paths
assert "/tmp" not in mount_paths
def test_generate_example_webhook_secret_is_20_char_alphanumeric():
secret = generate_example_webhook_secret()
assert len(secret) == 20
assert secret.isalnum()
def test_validate_settings_warns_when_atlas_webhook_secret_missing(monkeypatch):
monkeypatch.setenv("BLOOM_ATLAS__WEBHOOK_SECRET", "")
get_settings.cache_clear()
warnings = validate_settings()
assert any(
"Atlas webhook signature secret is not configured" in warning
for warning in warnings
)
assert any("20-character alphanumeric secret" in warning for warning in warnings)
def test_create_app_logs_atlas_webhook_secret_warning(
monkeypatch, tmp_path: Path, caplog
):
from bloom_lims.app import create_app
monkeypatch.delenv("BLOOM_SKIP_STARTUP_VALIDATION", raising=False)
monkeypatch.setenv(
"BLOOM_TAPDB__CONFIG_PATH", str(tmp_path / "missing-startup-config.yaml")
)
monkeypatch.setenv("BLOOM_ATLAS__WEBHOOK_SECRET", "")
ensure_test_runtime_environment()
get_settings.cache_clear()
caplog.set_level(logging.WARNING)
create_app()
assert any(
"Atlas webhook signature secret is not configured" in message
for message in caplog.messages
)
def test_runtime_context_preserves_explicit_tapdb_paths(tmp_path: Path):
config_path = tmp_path / "tapdb" / "bloom" / "tapdb-config.yaml"
domain_registry_path = tmp_path / "domain_code_registry.json"
prefix_registry_path = tmp_path / "prefix_ownership_registry.json"
settings = BloomSettings(
tapdb={
"config_path": str(config_path),
"schema_name": "tapdb_bloom_test",
"domain_registry_path": str(domain_registry_path),
"prefix_ownership_registry_path": str(prefix_registry_path),
}
)
ctx = get_tapdb_runtime_context(settings)
assert ctx.config_path == str(config_path)
assert ctx.schema_name == "tapdb_bloom_test"
assert ctx.domain_registry_path == str(domain_registry_path)
assert ctx.prefix_ownership_registry_path == str(prefix_registry_path)
def test_tapdb_contract_defaults_match_shipped_templates(monkeypatch, tmp_path: Path):
temp_home = Path(tempfile.mkdtemp())
monkeypatch.setenv("HOME", str(temp_home))
monkeypatch.setenv("XDG_CONFIG_HOME", str(temp_home / ".config"))
monkeypatch.setenv("BLOOM_DEPLOYMENT_CODE", "template-check")
monkeypatch.setenv("DEPLOYMENT_CODE", "template-check")
monkeypatch.setenv("LSMC_DEPLOYMENT_CODE", "template-check")
monkeypatch.delenv("BLOOM_CONFIG_PATH", raising=False)
for key in (
"MERIDIAN_DOMAIN_CODE",
"TAPDB_OWNER_REPO",
"TAPDB_DOMAIN_CODE",
"TAPDB_DOMAIN_REGISTRY_PATH",
"TAPDB_PREFIX_OWNERSHIP_REGISTRY_PATH",
"BLOOM_TAPDB__CONFIG_PATH",
"BLOOM_TAPDB__SCHEMA_NAME",
"BLOOM_TAPDB__OWNER_REPO_NAME",
"BLOOM_TAPDB__DOMAIN_CODE",
"BLOOM_TAPDB__DOMAIN_REGISTRY_PATH",
"BLOOM_TAPDB__PREFIX_OWNERSHIP_REGISTRY_PATH",
):
monkeypatch.delenv(key, raising=False)
settings = BloomSettings(storage={"upload_dir": str(tmp_path / "uploads")})
expected_tapdb_spec = read_pyproject_dependency_spec("daylily-tapdb")
assert expected_tapdb_spec == "==9.0.10"
assert assert_tapdb_version()
assert settings.tapdb.owner_repo_name == DEFAULT_TAPDB_OWNER_REPO_NAME
assert settings.tapdb.domain_code == DEFAULT_TAPDB_DOMAIN_CODE
assert settings.tapdb.domain_registry_path == ""
assert settings.tapdb.prefix_ownership_registry_path == ""
assert settings.tapdb.config_path == ""
assert settings.tapdb.schema_name == ""
project_root = Path(__file__).resolve().parents[1]
root_template = (project_root / "config" / "bloom-config-template.yaml").read_text(
encoding="utf-8"
)
packaged_template = (
project_root / "bloom_lims" / "etc" / "bloom-config-template.yaml"
).read_text(encoding="utf-8")
packaged_domain_registry = json.loads(
(project_root / "bloom_lims" / "etc" / "domain_code_registry.json").read_text(
encoding="utf-8"
)
)
packaged_prefix_registry = json.loads(
(
project_root / "bloom_lims" / "etc" / "prefix_ownership_registry.json"
).read_text(encoding="utf-8")
)
pyproject = tomllib.loads(
(project_root / "pyproject.toml").read_text(encoding="utf-8")
)
assert 'owner_repo_name: "bloom"' in root_template
assert 'domain_code: "Z"' in root_template
assert 'domain_registry_path: ""' in root_template
assert 'prefix_ownership_registry_path: ""' in root_template
assert 'config_path: ""' in root_template
assert 'schema_name: ""' in root_template
assert 'physical_database: ""' in root_template
assert 'owner_repo_name: "bloom"' in packaged_template
assert 'domain_code: "Z"' in packaged_template
assert 'domain_registry_path: ""' in packaged_template
assert 'prefix_ownership_registry_path: ""' in packaged_template
assert 'config_path: ""' in packaged_template
assert 'schema_name: ""' in packaged_template
assert 'physical_database: ""' in packaged_template
assert "tapdb.env" not in root_template
assert "tapdb.env" not in packaged_template
assert " env:" not in root_template
assert " env:" not in packaged_template
assert "allowed_hosts: []" in root_template
assert "allowed_hosts: []" in packaged_template
assert pyproject["tool"]["setuptools"]["package-data"]["bloom_lims"] == [
"etc/*.yaml",
"etc/*.json",
]
assert packaged_domain_registry["domains"] == {"Z": {"name": "bloom"}}
assert set(packaged_prefix_registry["ownership"]["Z"]) == {
"ADT",
"BAC",
"BAN",
"BAR",
"BBX",
"BC",
"BCB",
"BCE",
"BCF",
"BCN",
"BCP",
"BCR",
"BCT",
"BCW",
"BD",
"BDA",
"BDC",
"BDN",
"BDP",
"BDQ",
"BDT",
"BDX",
"BDY",
"BEQ",
"BFX",
"BG",
"BGM",
"BGP",
"BGR",
"BGS",
"BGT",
"BGV",
"BGX",
"BHE",
"BN",
"BNA",
"BNB",
"BNC",
"BNF",
"BNG",
"BNK",
"BNP",
"BNQ",
"BNR",
"BNS",
"BNX",
"BR",
"BRC",
"BRM",
"BRN",
"BRP",
"BRT",
"BSJ",
"BWF",
"BWS",
"EDG",
"GSE",
"GVR",
"MSG",
"SYS",
"TPX",
"XRF",
}
assert {
claim["issuer_app_code"]
for claim in packaged_prefix_registry["ownership"]["Z"].values()
} == {"bloom", "daylily-tapdb"}
def test_settings_accept_network_allowed_hosts() -> None:
settings = BloomSettings(
network={"allowed_hosts": ["bloom.dev2.lsmc.life", "54.218.100.68"]}
)
assert settings.network.allowed_hosts == ["bloom.dev2.lsmc.life", "54.218.100.68"]
def test_effective_config_summary_redacts_sensitive_values(monkeypatch, tmp_path: Path):
ensure_test_runtime_environment()
monkeypatch.setenv("BLOOM_UI__SHOW_ENVIRONMENT_CHROME", "false")
monkeypatch.setenv("BLOOM_AUTH__COGNITO_CLIENT_SECRET", "super-secret")
monkeypatch.setenv("BLOOM_AUTH__JWT_SECRET", "jwt-secret")
monkeypatch.setenv("BLOOM_ATLAS__WEBHOOK_SECRET", "atlas-secret")
monkeypatch.setenv("BLOOM_DEPLOYMENT__NAME", "510x2")
monkeypatch.setenv("BLOOM_AWS__REGION", "us-east-1")
monkeypatch.setenv("BLOOM_TAPDB__CONFIG_PATH", str(tmp_path / "tapdb.yaml"))
get_settings.cache_clear()
summary = build_effective_config_summary()
rows = {row["key"]: row["value"] for row in summary["effective_rows"]}
assert summary["build_version"] == __version__
assert summary["show_environment_chrome"] is False
assert summary["deployment_name"] == "510x2"
assert summary["aws_region"] == "us-east-1"
assert summary["user_config_path"]
assert summary["template_config_path"]
assert summary["tapdb_config_path"] == str(tmp_path / "tapdb.yaml")
assert rows["auth.cognito_client_secret"] == "<redacted>"
assert rows["auth.jwt_secret"] == "<redacted>"
assert rows["atlas.webhook_secret"] == "<redacted>"
assert rows["ui.show_environment_chrome"] == "false"
def test_create_app_uses_scm_version(monkeypatch, tmp_path: Path):
from bloom_lims.app import create_app
monkeypatch.delenv("BLOOM_SKIP_STARTUP_VALIDATION", raising=False)
monkeypatch.setenv("BLOOM_TAPDB__CONFIG_PATH", str(tmp_path / "missing.yaml"))
ensure_test_runtime_environment()
get_settings.cache_clear()
app = create_app()
assert app.version == __version__

Xet Storage Details

Size:
21.2 kB
·
Xet hash:
68d008b4d0438fbe8c718a6a11ebbcbf58df53d77fa05fa06a038bc7e898b7ad

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.