"""Test M2M config validation methods.""" import json from pathlib import Path from typing import Any import pytest from m2mconfig.registry import M2MValidationError, validate_config from m2mconfig.schemas import RegistryEntry @pytest.mark.parametrize( "contents, expect_exception, expected_response", [ pytest.param( [ { "name": "pp-m2m-config-test-machine-to-machine", "prod_aws_account": "437795906767", "qa_aws_account": "437795906767", "client_metadata": { "m2m_identity_uuid": "1a6f06cd-cc30-4252-8654-cb93a9843b38" }, } ], False, [ RegistryEntry.model_validate( { "name": "pp-m2m-config-test-machine-to-machine", "prod_aws_account": "437795906767", "qa_aws_account": "437795906767", "client_metadata": { "m2m_identity_uuid": "1a6f06cd-cc30-4252-8654-cb93a9843b38" }, } ), ], id="Should handle well-formed config", ), pytest.param( [ { "name": "pp-m2m-config-test-machine-to-machine", "prod_aws_account": "437795906767", "qa_aws_account": "437795906767", "client_metadata": { "m2m_identity_uuid": "83b63d26-9887-11f0-add1-224337b051bd" }, } ], True, None, id="Should reject non-uuidv4 client_metadata.m2m_identity_uuid", ), pytest.param( [ { "name": "duplicate-machine-to-machine", "prod_aws_account": "437795906767", "qa_aws_account": "437795906767", "client_metadata": { "m2m_identity_uuid": "1a6f06cd-cc30-4252-8654-cb93a9843b38" }, }, { "name": "duplicate-machine-to-machine", "prod_aws_account": "437795906767", "qa_aws_account": "437795906767", "client_metadata": { "m2m_identity_uuid": "39b98aaa-6efd-4162-9f4b-8faddd43402a" }, }, ], True, None, id="Should abort when multiple entries have the same name.", ), pytest.param( [ { "name": "duplicate-machine-to-machine-01", "prod_aws_account": "437795906767", "qa_aws_account": "437795906767", "client_metadata": { "m2m_identity_uuid": "1a6f06cd-cc30-4252-8654-cb93a9843b38" }, }, { "name": "duplicate-machine-to-machine-02", "prod_aws_account": "437795906767", "qa_aws_account": "437795906767", "client_metadata": { "m2m_identity_uuid": "1a6f06cd-cc30-4252-8654-cb93a9843b38" }, }, ], True, None, id="Should abort when multiple entries have the same m2m_identity_uuid.", ), pytest.param( [ { "name": "pp-m2m-config-test-machine-to-machine", } ], True, None, id="Should abort if an entry has missing fields", ), pytest.param( [], True, None, id="Should abort for 0 entries", ), ], ) def test_validate_config( contents: dict[str, Any], expect_exception: bool, expected_response: list[RegistryEntry], tmp_path: Path, ) -> None: """Test config validation.""" d = tmp_path / "m2m_config_test" d.mkdir() p = d / "m2m.json" p.write_text(json.dumps(contents, indent=2)) if expect_exception: with pytest.raises(M2MValidationError): validate_config(m2m_json_path=str(p)) else: response = validate_config(m2m_json_path=str(p)) assert expected_response == response def test_validate_config_invalid_json_contents( tmp_path: Path, ) -> None: """Test config validation.""" d = tmp_path / "m2m_config_test" d.mkdir() p = d / "m2m.json" p.write_text("no.such.json") with pytest.raises(M2MValidationError): validate_config(m2m_json_path=str(p)) def test_validate_config_missing_file(tmp_path: Path) -> None: """Test config validation fails with an invalid file path.""" with pytest.raises(M2MValidationError): validate_config(m2m_json_path="/no/such/dir/no.such.file")