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(())
}