Skip to content

Closes #182#306

Open
p3ris0n wants to merge 2 commits into
benelabs:mainfrom
p3ris0n:feat/p3ris0n-crucible1
Open

Closes #182#306
p3ris0n wants to merge 2 commits into
benelabs:mainfrom
p3ris0n:feat/p3ris0n-crucible1

Conversation

@p3ris0n
Copy link
Copy Markdown

@p3ris0n p3ris0n commented May 27, 2026

Closes #182

Description
This PR implements a robust, production-ready API Test Client within backend/src/test_utils/client.rs. Designed specifically for our Axum, SQLx (PostgreSQL), and Redis stack, this test client simplifies integration testing by mocking HTTP requests, handling session/auth states, and ensuring clean database transactions across test suites.
Changes Made
Core Test Client (backend/src/test_utils/client.rs):
Implemented an isolated TestClient struct that wraps our Axum router using tower::ServiceExt.
Added helper methods for common HTTP verbs (get, post, put, delete).
Integrated automatic header injection (e.g., Bearer tokens for authenticated routes).
State & Dependency Mocking:
Added setups for isolated PostgreSQL transactions that roll back automatically after each test run using SQLx.
Integrated a localized Redis pool/mock client connection handling to prevent test state pollution.
Observability:
Configured tracing-subscriber initialization within the test environment for easier debugging during test failures.
Documentation:
Updated backend/README.md with clear examples of how to spin up and use the new TestClient in integration tests.
Testing Strategy
Integration Tests: Added full-suite validation ensuring the TestClient correctly hits Axum endpoints, processes data via SQLx, and interacts with Redis without leaks.
Run the suite using:
Bash
cd backend
cargo test --lib test_utils::client
Checklist
[x] Feature is fully implemented and verified locally.
[x] Code compiles cleanly without any clippy or compiler warnings.
[x] All unit and integration tests pass successfully.
[x] Documentation added/updated in backend/README.md and inline via Rustdoc.
[x] No breaking changes introduced to existing APIs.
Example Usage (Added to README)
Rust
#[tokio::test]
async fn test_example_endpoint() {
// Initialize test database, redis, and app state
let client = TestClient::new().await;

// Send a mock POST request
let response = client.post("/api/v1/resource")
    .json(&json!({ "name": "Crucible Test" }))
    .send()
    .await;

assert_eq!(response.status(), StatusCode::CREATED);

}

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 27, 2026

@p3ris0n Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] 7.4 Implement API Test Client

1 participant