Files
thoughts/thoughts-backend/utils/src/testing/api/mod.rs
Gabriel Kaszewski 3d73c7f198 feat(auth): implement user registration and login with JWT authentication
- Added `bcrypt`, `jsonwebtoken`, and `once_cell` dependencies to manage password hashing and JWT handling.
- Created `Claims` struct for JWT claims and implemented token generation in the login route.
- Implemented user registration and authentication logic in the `auth` module.
- Updated error handling to include validation errors.
- Created new routes for user registration and login, and integrated them into the main router.
- Added tests for the authentication flow, including registration and login scenarios.
- Updated user model to include a password hash field.
- Refactored user creation logic to include password validation.
- Adjusted feed and user routes to utilize JWT for authentication.
2025-09-06 00:06:30 +02:00

71 lines
1.9 KiB
Rust

use axum::{body::Body, http::Request, response::Response, Router};
use tower::ServiceExt;
pub async fn make_get_request(app: Router, url: &str, user_id: Option<i32>) -> Response {
let mut builder = Request::builder()
.uri(url)
.header("Content-Type", "application/json");
if let Some(user_id) = user_id {
builder = builder.header("x-test-user-id", user_id.to_string());
}
app.oneshot(builder.body(Body::empty()).unwrap())
.await
.unwrap()
}
pub async fn make_post_request(
app: Router,
url: &str,
body: String,
user_id: Option<i32>,
) -> Response {
let mut builder = Request::builder()
.method("POST")
.uri(url)
.header("Content-Type", "application/json");
if let Some(user_id) = user_id {
builder = builder.header("x-test-user-id", user_id.to_string());
}
app.oneshot(builder.body(Body::from(body)).unwrap())
.await
.unwrap()
}
pub async fn make_delete_request(app: Router, url: &str, user_id: Option<i32>) -> Response {
let mut builder = Request::builder()
.method("DELETE")
.uri(url)
.header("Content-Type", "application/json");
if let Some(user_id) = user_id {
builder = builder.header("x-test-user-id", user_id.to_string());
}
app.oneshot(builder.body(Body::empty()).unwrap())
.await
.unwrap()
}
pub async fn make_jwt_request(
app: Router,
url: &str,
method: &str,
body: Option<String>,
token: &str,
) -> Response {
let builder = Request::builder()
.method(method)
.uri(url)
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", token));
let request_body = body.unwrap_or_default();
app.oneshot(builder.body(Body::from(request_body)).unwrap())
.await
.unwrap()
}