# Testing (laravel-rust-mcp-oauth)

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

- Package: laravel-rust-mcp-oauth (Laravel for Rust, alpha), crates: `laravel-rust-mcp-oauth`
- Maintenance: active; Latest release: 0.1.0 (2026-12-05); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-mcp-oauth), [crates.io](https://crates.io/crates/laravel-rust-mcp-oauth/0.1.0), [docs.rs](https://docs.rs/laravel-rust-mcp-oauth/0.1.0), [Changelog](https://oss.bowerbird.si/mcp-oauth/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-mcp-oauth/blob/8b1d066397c9f1a1cf02d444a910a1c65585c159/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-mcp-oauth/blob/8b1d066397c9f1a1cf02d444a910a1c65585c159/SECURITY.md)
- Based on: [Laravel MCP](https://github.com/laravel/mcp): The public flow of Mcp::oauthRoutes(): discovery, client registration, an approval screen in your app and bearer tokens for the MCP server.; [Laravel Passport](https://github.com/laravel/passport): The authorization server Laravel MCP builds on. This crate needs no Passport, supports Client ID Metadata Documents and uses opaque, hashed tokens instead of JWTs.; [MCP authorization specification](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization): The sign-in flow MCP clients expect, with Client ID Metadata Documents preferred over dynamic registration.
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/mcp-oauth/docs/testing/
- Source: https://github.com/bowerbird-si/laravel-rust-mcp-oauth/blob/8b1d066397c9f1a1cf02d444a910a1c65585c159/docs/testing.md

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

```rust
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.

| Helper | What 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:

```rust
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

```rust
#[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
}
```
