Testing

laravel-rust-captcha Alpha Laravel for Rust

Each test app has its own captcha service and store, so these helpers are safe in parallel tests.

Through the real image route

use laravel::testing::prelude::*;
use laravel_rust_captcha::testing;
use serde_json::json;

#[laravel::test(app = create)]
async fn the_contact_form_accepts_the_captcha(app: Application) {
    app.scope(async || testing::fix_answers("abcde").await.unwrap()).await;

    let image = app.get("/captcha/contact").await;
    image.assert_ok();

    app.post("/contact")
        .with_cookies_from(&image)
        .form(json!({ "name": "Ada", "captcha": "ABCDE" }))
        .await
        .assert_redirect_to("/thanks");
}

fix_answers makes every image the app issues use that answer, until clear_fixed_answer.

Seeding a challenge

testing::seed(&session, "contact", "abcde") creates a session challenge with a known answer without drawing it. testing::seed_keyed("math", "12") returns the key of a keyed challenge. PHP tests did this by writing the session and cache entries by hand.

Turning the captcha off

Set CAPTCHA_DISABLE=true (for example with .with_envs([("CAPTCHA_DISABLE", "true")])). The rules pass and the routes are gone, as in PHP.