- 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.
51 lines
1.7 KiB
Rust
51 lines
1.7 KiB
Rust
use api::setup_router;
|
|
use app::persistence::user::create_user;
|
|
use axum::Router;
|
|
use http_body_util::BodyExt;
|
|
use models::params::{auth::RegisterParams, user::CreateUserParams};
|
|
use sea_orm::DatabaseConnection;
|
|
use serde_json::{json, Value};
|
|
use utils::testing::{make_post_request, setup_test_db};
|
|
|
|
pub struct TestApp {
|
|
pub router: Router,
|
|
pub db: DatabaseConnection,
|
|
}
|
|
|
|
pub async fn setup() -> TestApp {
|
|
let db = setup_test_db("sqlite::memory:")
|
|
.await
|
|
.expect("Failed to set up test db");
|
|
let router = setup_router(db.clone());
|
|
TestApp { router, db }
|
|
}
|
|
|
|
// Helper to create users for tests
|
|
pub async fn create_test_user(db: &DatabaseConnection, username: &str) {
|
|
let params = CreateUserParams {
|
|
username: username.to_string(),
|
|
password: "password".to_string(),
|
|
};
|
|
create_user(db, params)
|
|
.await
|
|
.expect("Failed to create test user");
|
|
}
|
|
|
|
pub async fn create_user_with_password(db: &DatabaseConnection, username: &str, password: &str) {
|
|
let params = RegisterParams {
|
|
username: username.to_string(),
|
|
password: password.to_string(),
|
|
};
|
|
app::persistence::auth::register_user(db, params)
|
|
.await
|
|
.expect("Failed to create test user with password");
|
|
}
|
|
|
|
pub async fn login_user(router: Router, username: &str, password: &str) -> String {
|
|
let login_body = json!({ "username": username, "password": password }).to_string();
|
|
let response = make_post_request(router, "/auth/login", login_body, None).await;
|
|
let body = response.into_body().collect().await.unwrap().to_bytes();
|
|
let v: Value = serde_json::from_slice(&body).unwrap();
|
|
v["token"].as_str().unwrap().to_string()
|
|
}
|