from id_configurator.config import Configuration from id_configurator.context import Context from id_configurator.modules.npm import NpmModule CONFIG_WITH_REPO = { "uuid": "12345678-1234-5678-1234-567812345678", "organisation": "InterDiode", "configurations": { "npm": {"repositories": [{"name": "demo", "url": "https://example.invalid/"}]} }, } CONFIG_WITH_TWO_REPOS = { "uuid": "12345678-1234-5678-1234-567812345678", "organisation": "InterDiode", "configurations": { "npm": { "repositories": [ {"name": "demo", "url": "https://example.invalid/"}, {"name": "myorg", "url": "https://other.invalid/"}, ] } }, } EMPTY_CONFIG = { "uuid": "12345678-1234-5678-1234-567812345678", "organisation": "InterDiode", } CONFIG_WITH_CERTIFICATE_ONLY = { "uuid": "12345678-1234-5678-1234-567812345678", "organisation": "InterDiode", "configurations": { "certificates": [{"name": "interdiode-ca", "content": "-----BEGIN...-----"}] }, } def make_config(data: dict) -> Configuration: return Configuration.model_validate(data) def test_is_relevant_false_when_no_repositories(tmp_path) -> None: context = Context(config=make_config(EMPTY_CONFIG), dst=tmp_path / "dst", src=None) assert NpmModule().is_relevant(context) is False def test_is_relevant_true_with_certificate_only(tmp_path) -> None: context = Context( config=make_config(CONFIG_WITH_CERTIFICATE_ONLY), dst=tmp_path / "dst", src=None ) assert NpmModule().is_relevant(context) is True def test_is_relevant_true_with_repository(tmp_path) -> None: context = Context( config=make_config(CONFIG_WITH_REPO), dst=tmp_path / "dst", src=None ) assert NpmModule().is_relevant(context) is True def test_apply_writes_default_registry_and_cafile(tmp_path) -> None: dst = tmp_path / "dst" context = Context(config=make_config(CONFIG_WITH_REPO), dst=dst, src=None) NpmModule().apply(context) npmrc = (dst / "usr/local/etc/npmrc").read_text(encoding="utf-8") assert npmrc == ( "cafile=/etc/ssl/certs/ca-certificates.crt\nregistry=https://example.invalid/\n" ) def test_apply_writes_additional_repositories_scoped(tmp_path) -> None: dst = tmp_path / "dst" context = Context(config=make_config(CONFIG_WITH_TWO_REPOS), dst=dst, src=None) NpmModule().apply(context) npmrc = (dst / "usr/local/etc/npmrc").read_text(encoding="utf-8") assert npmrc == ( "cafile=/etc/ssl/certs/ca-certificates.crt\n" "registry=https://example.invalid/\n" "@myorg:registry=https://other.invalid/\n" ) def test_apply_without_certificates_does_not_write_etc_environment(tmp_path) -> None: dst = tmp_path / "dst" context = Context(config=make_config(CONFIG_WITH_REPO), dst=dst, src=None) NpmModule().apply(context) assert not (dst / "etc/environment").exists() def test_apply_sets_node_extra_ca_certs_when_certificates_configured( tmp_path, ) -> None: dst = tmp_path / "dst" context = Context( config=make_config(CONFIG_WITH_CERTIFICATE_ONLY), dst=dst, src=None ) NpmModule().apply(context) assert not (dst / "usr/local/etc/npmrc").exists() environment = (dst / "etc/environment").read_text(encoding="utf-8") assert environment == "NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt\n" def test_apply_merges_node_extra_ca_certs_into_existing_etc_environment( tmp_path, ) -> None: src = tmp_path / "src" src.mkdir() (src / "etc").mkdir() (src / "etc/environment").write_text( "PATH=/usr/bin\nNODE_EXTRA_CA_CERTS=/old/stale.pem\n", encoding="utf-8" ) dst = tmp_path / "dst" context = Context( config=make_config(CONFIG_WITH_CERTIFICATE_ONLY), dst=dst, src=src ) NpmModule().apply(context) environment = (dst / "etc/environment").read_text(encoding="utf-8") assert environment == ( "PATH=/usr/bin\nNODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt\n" )