"""Tests for External Connections creation-time validation (audit finding F-11)."""
import pytest

from src.core.exceptions import ValidationError
from src.domain.external_connections.schemas import CreateConnectionRequest
from src.domain.external_connections.service import _validate_connection_fields


def _sql_request(conn_type: str) -> CreateConnectionRequest:
    return CreateConnectionRequest(
        name="my-db",
        type=conn_type,
        host="db.example.com",
        port=5432,
        database="appdb",
        username="user",
        password="secret",
    )


def test_mysql_connection_rejected_at_creation_time():
    with pytest.raises(ValidationError, match="not yet supported"):
        _validate_connection_fields(_sql_request("mysql"))


def test_postgres_connection_still_accepted():
    # Should not raise.
    _validate_connection_fields(_sql_request("postgres"))


def test_postgres_connection_still_requires_fields():
    body = _sql_request("postgres")
    body.password = ""
    with pytest.raises(ValidationError, match="requires"):
        _validate_connection_fields(body)
