Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Dec 16, 2024
1 parent e800614 commit 916b92a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 93 deletions.
8 changes: 4 additions & 4 deletions loco-new/base_template/src/models/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Validator {
pub email: String,
}

impl Validatable for super::_entities::users::ActiveModel {
impl Validatable for ActiveModel {
fn validator(&self) -> Box<dyn Validate> {
Box::new(Validator {
name: self.name.as_ref().to_owned(),
Expand Down Expand Up @@ -58,7 +58,7 @@ impl ActiveModelBehavior for super::_entities::users::ActiveModel {
}

#[async_trait]
impl Authenticable for super::_entities::users::Model {
impl Authenticable for Model {
async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult<Self> {
let user = users::Entity::find()
.filter(
Expand All @@ -76,7 +76,7 @@ impl Authenticable for super::_entities::users::Model {
}
}

impl super::_entities::users::Model {
impl Model {
/// finds a user by the provided email
///
/// # Errors
Expand Down Expand Up @@ -263,7 +263,7 @@ impl super::_entities::users::Model {
}
}

impl super::_entities::users::ActiveModel {
impl ActiveModel {
/// Sets the email verification information for the user and
/// updates it in the database.
///
Expand Down
133 changes: 69 additions & 64 deletions loco-new/base_template/tests/models/users.rs.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::{offset::Local, Duration};
use insta::assert_debug_snapshot;
use loco_rs::{model::ModelError, testing::prelude::*};
use loco_rs::testing::prelude::*;
use {{settings.module_name}}::{
app::App,
models::users::{self, Model, RegisterParams},
Expand All @@ -22,15 +22,15 @@ macro_rules! configure_insta {
async fn test_can_validate_model() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");

let res = users::ActiveModel {
let invalid_user = users::ActiveModel {
name: ActiveValue::set("1".to_string()),
email: ActiveValue::set("invalid-email".to_string()),
..Default::default()
}
.insert(&boot.app_context.db)
.await;
};

let res = invalid_user.insert(&boot.app_context.db).await;

assert_debug_snapshot!(res);
}
Expand All @@ -40,13 +40,14 @@ async fn test_can_validate_model() {
async fn can_create_with_password() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");

let params = RegisterParams {
email: "test@framework.com".to_string(),
password: "1234".to_string(),
name: "framework".to_string(),
};

let res = Model::create_with_password(&boot.app_context.db, &params).await;

insta::with_settings!({
Expand All @@ -55,16 +56,15 @@ async fn can_create_with_password() {
assert_debug_snapshot!(res);
});
}

#[tokio::test]
#[serial]
async fn handle_create_with_password_with_duplicate() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");

let new_user: Result<Model, ModelError> = Model::create_with_password(
let new_user = Model::create_with_password(
&boot.app_context.db,
&RegisterParams {
email: "user1@example.com".to_string(),
Expand All @@ -73,6 +73,7 @@ async fn handle_create_with_password_with_duplicate() {
},
)
.await;

assert_debug_snapshot!(new_user);
}

Expand All @@ -81,12 +82,11 @@ async fn handle_create_with_password_with_duplicate() {
async fn can_find_by_email() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");

let existing_user = Model::find_by_email(&boot.app_context.db, "user1@example.com").await;
let non_existing_user_results =
Model::find_by_email(&boot.app_context.db, "un@existing-email.com").await;
let non_existing_user_results = Model::find_by_email(&boot.app_context.db, "un@existing-email.com").await;

assert_debug_snapshot!(existing_user);
assert_debug_snapshot!(non_existing_user_results);
Expand All @@ -97,13 +97,11 @@ async fn can_find_by_email() {
async fn can_find_by_pid() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");

let existing_user =
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
let non_existing_user_results =
Model::find_by_pid(&boot.app_context.db, "23232323-2323-2323-2323-232323232323").await;
let existing_user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
let non_existing_user_results = Model::find_by_pid(&boot.app_context.db, "23232323-2323-2323-2323-232323232323").await;

assert_debug_snapshot!(existing_user);
assert_debug_snapshot!(non_existing_user_results);
Expand All @@ -114,115 +112,122 @@ async fn can_find_by_pid() {
async fn can_verification_token() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap();
.expect("Failed to find user by PID");

assert!(user.email_verification_sent_at.is_none());
assert!(user.email_verification_token.is_none());
assert!(user.email_verification_sent_at.is_none(), "Expected no email verification sent timestamp");
assert!(user.email_verification_token.is_none(), "Expected no email verification token");

assert!(user
let result = user
.into_active_model()
.set_email_verification_sent(&boot.app_context.db)
.await
.is_ok());
.await;

assert!(result.is_ok(), "Failed to set email verification sent");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap();
.expect("Failed to find user by PID after setting verification sent");

assert!(user.email_verification_sent_at.is_some());
assert!(user.email_verification_token.is_some());
assert!(user.email_verification_sent_at.is_some(), "Expected email verification sent timestamp to be present");
assert!(user.email_verification_token.is_some(), "Expected email verification token to be present");
}


#[tokio::test]
#[serial]
async fn can_set_forgot_password_sent() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap();
.expect("Failed to find user by PID");

assert!(user.reset_sent_at.is_none());
assert!(user.reset_token.is_none());
assert!(user.reset_sent_at.is_none(), "Expected no reset sent timestamp");
assert!(user.reset_token.is_none(), "Expected no reset token");

assert!(user
let result = user
.into_active_model()
.set_forgot_password_sent(&boot.app_context.db)
.await
.is_ok());
.await;

assert!(result.is_ok(), "Failed to set forgot password sent");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap();
.expect("Failed to find user by PID after setting forgot password sent");

assert!(user.reset_sent_at.is_some());
assert!(user.reset_token.is_some());
assert!(user.reset_sent_at.is_some(), "Expected reset sent timestamp to be present");
assert!(user.reset_token.is_some(), "Expected reset token to be present");
}


#[tokio::test]
#[serial]
async fn can_verified() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap();
.expect("Failed to find user by PID");

assert!(user.email_verified_at.is_none());
assert!(user.email_verified_at.is_none(), "Expected email to be unverified");

assert!(user
let result = user
.into_active_model()
.verified(&boot.app_context.db)
.await
.is_ok());
.await;

assert!(result.is_ok(), "Failed to mark email as verified");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap();
.expect("Failed to find user by PID after verification");

assert!(user.email_verified_at.is_some());
assert!(user.email_verified_at.is_some(), "Expected email to be verified");
}


#[tokio::test]
#[serial]
async fn can_reset_password() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap();
.expect("Failed to find user by PID");

assert!(user.verify_password("12341234"));
assert!(user.verify_password("12341234"), "Password verification failed for original password");

assert!(user
let result = user
.clone()
.into_active_model()
.reset_password(&boot.app_context.db, "new-password")
.await;

assert!(result.is_ok(), "Failed to reset password");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.is_ok());
.expect("Failed to find user by PID after password reset");

assert!(
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
.unwrap()
.verify_password("new-password")
);
assert!(user.verify_password("new-password"), "Password verification failed for new password");
}


#[tokio::test]
#[serial]
async fn magic_link() {
Expand Down
Loading

0 comments on commit 916b92a

Please sign in to comment.