recipes/backend/tests/utils/mod.rs
Greg Burri 3626f8a11b Update dependencies and implement email service integration
- Refactor app and email modules to include email service
- Add tests for user sign-up and mock email service
2025-05-02 00:57:32 +02:00

61 lines
1.8 KiB
Rust

use std::error::Error;
use recipes::{app, config, data::db, log};
mod mock_email;
pub async fn common_state() -> Result<app::AppState, Box<dyn Error>> {
let db_connection = db::Connection::new_in_memory().await?;
let config = config::Config::default();
let log = log::Log::new_no_log();
Ok(app::AppState {
config,
db_connection,
log,
email_service: mock_email::MockEmailService::create_service(),
})
}
pub async fn create_user(
db_connection: &db::Connection,
email: &str,
password: &str,
) -> Result<i64, Box<dyn Error>> {
if let db::user::SignUpResult::UserCreatedWaitingForValidation(token) = db_connection
.sign_up(email, password, chrono::Weekday::Mon)
.await?
{
if let db::user::ValidationResult::Ok(_, user_id) = db_connection
.validation(&token, chrono::Duration::hours(1), "", "")
.await?
{
Ok(user_id)
} else {
Err(Box::<dyn Error>::from("Unable to validate user"))
}
} else {
Err(Box::<dyn Error>::from("Unable to sign up"))
}
}
pub async fn sign_in(
db_connection: &db::Connection,
email: &str,
password: &str,
) -> Result<String, Box<dyn Error>> {
match db_connection.sign_in(email, password, "", "").await? {
db::user::SignInResult::Ok(token, _) => Ok(token),
_ => Err(Box::<dyn Error>::from("Unable to sign in")),
}
}
pub async fn create_recipe(
db_connection: &db::Connection,
user_id: i64,
title: &str,
) -> Result<i64, Box<dyn Error>> {
let recipe_id = db_connection.create_recipe(user_id).await?;
db_connection.set_recipe_title(recipe_id, title).await?;
db_connection.set_recipe_is_public(recipe_id, true).await?;
Ok(recipe_id)
}