Files
id_configurator/tests/test_config.py
T
2026-08-28 16:52:07 +02:00

164 lines
5.3 KiB
Python

import base64
import json
import pytest
from id_configurator.config import (
AptSource,
Configuration,
load_configuration,
parse_configuration,
)
FULL_CONFIG = {
"uuid": "12345678-1234-5678-1234-567812345678",
"organisation": "InterDiode",
"description": "a test configuration",
"configurations": {
"certificates": [{"name": "my-ca", "content": "ZmFrZQ=="}],
"apt": {
"sources": [
{
"name": "interdiode",
"types": ["deb"],
"uris": ["http://example.invalid/debian"],
"suites": ["stable"],
"components": ["main", "contrib"],
"signed-by": "/etc/apt/keyrings/interdiode.asc",
}
],
"keys": [{"name": "interdiode", "content": "ZmFrZQ=="}],
},
"yum": {
"repositories": [
{
"name": "interdiode",
"index-url": "http://example.invalid/yum",
"gpg-url": "http://example.invalid/gpg",
}
]
},
"python": {
"repositories": [
{"name": "interdiode", "index-url": "http://example.invalid/simple"}
]
},
},
}
def test_parse_configuration_from_json() -> None:
configuration = parse_configuration(json.dumps(FULL_CONFIG))
assert isinstance(configuration, Configuration) # nosec B101
assert configuration.organisation == "InterDiode" # nosec B101
assert configuration.configurations.certificates[0].name == "my-ca" # nosec B101
assert (
configuration.configurations.yum.repositories[0].index_url
== "http://example.invalid/yum"
) # nosec B101
assert (
configuration.configurations.yum.repositories[0].gpg_url
== "http://example.invalid/gpg"
) # nosec B101
assert (
configuration.configurations.python.repositories[0].index_url
== "http://example.invalid/simple"
) # nosec B101
assert (
configuration.configurations.apt.sources[0].signed_by
== "/etc/apt/keyrings/interdiode.asc"
) # nosec B101
assert configuration.configurations.apt.keys[0].name == "interdiode" # nosec B101
def test_parse_configuration_from_yaml() -> None:
raw = """
uuid: 12345678-1234-5678-1234-567812345678
organisation: InterDiode
"""
configuration = parse_configuration(raw)
assert configuration.organisation == "InterDiode" # nosec B101
assert configuration.description == "" # nosec B101
assert configuration.configurations.certificates == [] # nosec B101
def test_load_configuration_from_file(tmp_path) -> None:
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps(FULL_CONFIG), encoding="utf-8")
configuration = load_configuration(config_file, None)
assert configuration.organisation == "InterDiode" # nosec B101
def test_load_configuration_from_env() -> None:
encoded = base64.b64encode(json.dumps(FULL_CONFIG).encode("utf-8")).decode("ascii")
configuration = load_configuration(None, encoded)
assert configuration.organisation == "InterDiode" # nosec B101
def test_load_configuration_without_source_raises() -> None:
with pytest.raises(ValueError, match="either --config or --config-env"):
load_configuration(None, None)
def test_apt_source_to_deb822_minimal() -> None:
source = AptSource(
name="interdiode", uris=["http://example.invalid/debian"], suites=["stable"]
)
assert (
source.to_deb822()
== "Types: deb\nURIs: http://example.invalid/debian\nSuites: stable\nEnabled: yes\n"
) # nosec B101
def test_apt_source_to_deb822_full() -> None:
source = AptSource(
name="interdiode",
types=["deb", "deb-src"],
uris=["http://example.invalid/debian"],
suites=["stable"],
components=["main", "contrib"],
architectures=["amd64"],
languages=["en"],
targets=["deb"],
enabled=False,
trusted=True,
**{
"signed-by": "/etc/apt/keyrings/interdiode.asc",
"by-hash": True,
"allow-insecure": False,
"allow-weak": False,
"allow-downgrade-to-insecure": False,
"check-valid-until": True,
"valid-until-min": "10",
"valid-until-max": "1w",
"check-date": True,
"date-max-future": "1d",
},
pdiffs=True,
description="InterDiode APT repository",
)
rendered = source.to_deb822()
assert rendered.splitlines() == [ # nosec B101
"Types: deb deb-src",
"URIs: http://example.invalid/debian",
"Suites: stable",
"Components: main contrib",
"Architectures: amd64",
"Languages: en",
"Targets: deb",
"Signed-By: /etc/apt/keyrings/interdiode.asc",
"Trusted: yes",
"PDiffs: yes",
"By-Hash: yes",
"Allow-Insecure: no",
"Allow-Weak: no",
"Allow-Downgrade-To-Insecure: no",
"Check-Valid-Until: yes",
"Valid-Until-Min: 10",
"Valid-Until-Max: 1w",
"Check-Date: yes",
"Date-Max-Future: 1d",
"Description: InterDiode APT repository",
"Enabled: no",
]