Testing

laravel-rust-mcp-oauth Alpha Laravel for Rust

McpOAuth::testing() creates real clients, grants and tokens in your test database, so your tests go through the same checks as a real assistant.

use laravel_rust_mcp_oauth::McpOAuth;

#[laravel::test]
async fn expired_tokens_are_refused(app: Application) -> laravel::Result<()> {
    let user = User::factory().create().await?;
    let token = McpOAuth::testing().token_for(&user).await?;

    app.post("/mcp").json(tools_list()).token(&token.access).await.assert_ok();

    app.travel(61.minutes());
    app.post("/mcp").json(tools_list()).token(&token.access).await
        .assert_status(401)
        .assert_header_contains("www-authenticate", r#"error="invalid_token""#);
    Ok(())
}

token_for(&user) returns access, refresh, grant_id and client_id, issued through the crate's own code with your configured prefixes.

HelperWhat it gives you
token_for(&user)A client, a grant and a token pair
client(registration, &metadata)A metadata-document or registered client
pending(&client, &authorization, state)A pending approval, to test the approval page; returns its ID
fake_metadata(url, document)A metadata document served from memory instead of the network
assert_revoked(grant_id)Fails unless the grant is revoked

To serve metadata documents from memory for a whole test application:

use laravel_rust_mcp_oauth::fetch::FakeDocuments;

let documents = FakeDocuments::default();
documents.insert("https://assistant.example/client.json", Ok(br#"{ "client_id": "https://assistant.example/client.json", ... }"#.to_vec()));

McpOAuthServiceProvider::<model::Users<User>>::new().with_documents(documents)

A refresh replay

#[laravel::test]
async fn a_replayed_refresh_token_revokes_the_connection(app: Application) -> laravel::Result<()> {
    let user = User::factory().create().await?;
    let token = McpOAuth::testing().token_for(&user).await?;
    let refresh = |t: &str| app.post("/oauth/token").form(&[
        ("grant_type", "refresh_token"), ("refresh_token", t), ("client_id", &token.client_id),
    ]);

    refresh(&token.refresh).await.assert_ok();
    app.travel(3.minutes());
    refresh(&token.refresh).await.assert_status(400).assert_json_path("error", "invalid_grant");

    McpOAuth::testing().assert_revoked(&token.grant_id).await
}