dmvs 0.1.1

Async high-level client for selected Czech ČÚZK DMVS SOAP services
Documentation
//! Separately authorized, read-only R24a smoke check for one explicit DMVS environment.
//!
//! Select `DMVS_ENVIRONMENT=integration` or `DMVS_ENVIRONMENT=production` and run only
//! after authorization for that environment:
//! `DMVS_RUN_LIVE_TESTS=1 DMVS_ENVIRONMENT=integration cargo test --test live -- --ignored --test-threads=1`.
//! INTEG credentials may come from gitignored `.env.integ`; production credentials may
//! come from gitignored `.env`. The selector and opt-in must be process environment
//! variables. No credential, subject identifier, or response content is printed.

use std::io;

use dmvs::{Client, Environment, ProcessingState, SubjectId, TlsIdentity};

fn required(name: &str) -> Result<String, io::Error> {
    std::env::var(name).map_err(|_| {
        io::Error::new(
            io::ErrorKind::NotFound,
            format!("required live-test variable {name} is missing or not Unicode"),
        )
    })
}

fn enabled() -> Result<bool, io::Error> {
    match std::env::var("DMVS_RUN_LIVE_TESTS") {
        Ok(value) if value == "1" => Ok(true),
        Ok(_) => Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "DMVS_RUN_LIVE_TESTS must be exactly 1 when set",
        )),
        Err(std::env::VarError::NotPresent) => Ok(false),
        Err(std::env::VarError::NotUnicode(_)) => Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "DMVS_RUN_LIVE_TESTS is not Unicode",
        )),
    }
}

fn selected_environment() -> Result<(Environment, &'static str), io::Error> {
    match required("DMVS_ENVIRONMENT")?.as_str() {
        "integration" => Ok((Environment::Integration, ".env.integ")),
        "production" => Ok((Environment::Production, ".env")),
        _ => Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "DMVS_ENVIRONMENT must be exactly `integration` or `production`",
        )),
    }
}

fn live_client(environment: Environment) -> Result<Client, Box<dyn std::error::Error>> {
    let subject_id = SubjectId::new(required("DMVS_SUBJECT")?)?;
    Ok(Client::builder(environment)
        .identity(TlsIdentity::from_pem(
            required("DMVS_CERTIFICATE_CHAIN")?,
            required("DMVS_PRIVATE_KEY")?,
        ))
        .subject_id(subject_id)
        .build()?)
}

#[tokio::test]
#[ignore = "contacts the explicitly selected official DMVS environment with a client certificate"]
async fn reads_jvf_versions() -> Result<(), Box<dyn std::error::Error>> {
    if !enabled()? {
        return Ok(());
    }
    let (environment, env_file) = selected_environment()?;
    dotenvy::from_path(env_file).ok();
    let response = live_client(environment)?.list_jvf_versions().await?;
    assert_eq!(response.metadata.state, ProcessingState::Ok);
    Ok(())
}