captcha-sdk 0.0.1

Unified server-side CAPTCHA verification for Rust
Documentation
use captcha_sdk::{FailureReason, ProviderId, VerificationResult};

#[test]
fn creates_a_normalized_invalid_result() {
    let result = VerificationResult::invalid(
        ProviderId::CLOUDFLARE_TURNSTILE,
        vec![FailureReason::ActionMismatch],
    );

    assert!(!result.valid);
    assert_eq!(result.provider, ProviderId::CLOUDFLARE_TURNSTILE);
    assert_eq!(result.reasons, vec![FailureReason::ActionMismatch]);
    assert!(result.metadata.is_empty());
}

#[test]
fn normalized_result_round_trips_through_json() {
    let result = VerificationResult::valid(ProviderId::HCAPTCHA_STANDARD);
    let json = serde_json::to_string(&result).expect("result should serialize");
    let decoded: VerificationResult =
        serde_json::from_str(&json).expect("result should deserialize");

    assert_eq!(decoded, result);
    assert!(json.contains("\"provider\":\"hcaptcha.standard\""));
}

#[cfg(feature = "all-providers")]
#[test]
fn provider_configs_redact_secrets_from_debug_output() {
    use captcha_sdk::providers::{
        altcha::AltchaConfig, captchafox::CaptchaFoxConfig, friendlycaptcha::FriendlyCaptchaConfig,
        hcaptcha::HcaptchaConfig, mtcaptcha::MtCaptchaConfig, procaptcha::ProcaptchaConfig,
        recaptcha::RecaptchaConfig, turnstile::TurnstileConfig,
    };

    for debug in [
        format!("{:?}", TurnstileConfig::new("turnstile-secret")),
        format!("{:?}", HcaptchaConfig::new("hcaptcha-secret")),
        format!("{:?}", RecaptchaConfig::new("recaptcha-secret")),
        format!("{:?}", FriendlyCaptchaConfig::new("friendlycaptcha-secret")),
        format!("{:?}", CaptchaFoxConfig::new("captchafox-secret")),
        format!("{:?}", MtCaptchaConfig::new("mtcaptcha-secret")),
        format!("{:?}", ProcaptchaConfig::new("procaptcha-secret")),
        format!("{:?}", AltchaConfig::new("altcha-secret")),
    ] {
        assert!(debug.contains("[REDACTED]"));
        assert!(!debug.contains("-secret"));
    }
}

#[cfg(feature = "all-providers")]
#[test]
fn provider_configs_use_documented_defaults() {
    use captcha_sdk::providers::{
        captchafox::{CaptchaFoxConfig, DEFAULT_ENDPOINT as CAPTCHAFOX_ENDPOINT},
        friendlycaptcha::{DEFAULT_ENDPOINT as FRIENDLY_ENDPOINT, FriendlyCaptchaConfig},
        hcaptcha::{DEFAULT_ENDPOINT as HCAPTCHA_ENDPOINT, HcaptchaConfig},
        mtcaptcha::{DEFAULT_ENDPOINT as MTCAPTCHA_ENDPOINT, MtCaptchaConfig},
        procaptcha::{DEFAULT_ENDPOINT as PROCAPTCHA_ENDPOINT, ProcaptchaConfig},
        recaptcha::{DEFAULT_ENDPOINT as RECAPTCHA_ENDPOINT, RecaptchaConfig},
        turnstile::{DEFAULT_ENDPOINT as TURNSTILE_ENDPOINT, TurnstileConfig},
    };

    assert_eq!(TurnstileConfig::new("key").endpoint(), TURNSTILE_ENDPOINT);
    assert_eq!(HcaptchaConfig::new("key").endpoint(), HCAPTCHA_ENDPOINT);
    assert_eq!(RecaptchaConfig::new("key").endpoint(), RECAPTCHA_ENDPOINT);
    assert_eq!(
        FriendlyCaptchaConfig::new("key").endpoint(),
        FRIENDLY_ENDPOINT
    );
    assert_eq!(CaptchaFoxConfig::new("key").endpoint(), CAPTCHAFOX_ENDPOINT);
    assert_eq!(MtCaptchaConfig::new("key").endpoint(), MTCAPTCHA_ENDPOINT);
    assert_eq!(ProcaptchaConfig::new("key").endpoint(), PROCAPTCHA_ENDPOINT);
}