Crate bitwarden_test

Crate bitwarden_test 

Source
Expand description

§Bitwarden Test

This crate should only be used in tests and should not be included in production builds.

Contains test utilities for Bitwarden.

§Play Framework

The Play framework provides scene-based E2E testing with automatic test isolation and cleanup. Using the server side SeederApi to generate data.

§How It Works

  1. Each Play instance generates a unique play_id (UUID)
  2. All HTTP requests include an x-play-id header for server-side test isolation
  3. Any db entry created through a request associated with an x-play-id (users, etc.) are saved as associated with that play_id
  4. When the test closure completes, associated data is automatically cleaned up

§Usage

Use the #[play_test] macro for the most ergonomic experience:

use bitwarden_test::play::{play_test, Play, SingleUserArgs, SingleUserScene};

#[play_test]
async fn test_example(play: Play) {
    // Create a test user via the seeder API
    let args = SingleUserArgs {
        email: "[email protected]".to_string(),
        verified: true,
        ..Default::default()
    };
    let scene = play.scene::<SingleUserScene>(&args).await.unwrap();

    // Access user data from the scene result
    let user_id = &scene.result().user_id;
    let api_key = &scene.result().api_key;

    // Use mangled values for test isolation
    let email = scene.get_mangled("[email protected]");

    // Use credentials for testing...

    // Cleanup happens automatically when the test completes
}

Or use the builder pattern directly for more control:

use bitwarden_test::play::{Play, PlayConfig, SingleUserArgs, SingleUserScene};

#[tokio::test]
async fn test_example() {
    Play::builder()
        .config(PlayConfig::new("https://api", "https://identity", "http://seeder"))
        .run(|play| async move {
            let args = SingleUserArgs {
                email: "[email protected]".to_string(),
                verified: true,
                ..Default::default()
            };
            let scene = play.scene::<SingleUserScene>(&args).await.unwrap();

            // Use credentials for testing...
        })
        .await;
}

§Environment Variables

VariableDefaultDescription
BITWARDEN_API_URLhttps://localhost:8080/apiBase API URL
BITWARDEN_IDENTITY_URLhttps://localhost:8080/identityIdentity server URL
BITWARDEN_SEEDER_URLhttp://localhost:5047Seeder API URL

§Running E2E Tests

E2E tests require a running Bitwarden server with the seeder API enabled:

cargo test -p bw --features integration

Modules§

api 🔒
play
Play test framework for E2E testing
repository 🔒

Structs§

MemoryRepository
A simple in-memory repository implementation. The data is only stored in memory and will not persist beyond the lifetime of the repository instance.

Functions§

start_api_mock
Helper for testing the Bitwarden API using wiremock.