init commit
This commit is contained in:
31
tests/models/jobs.rs
Normal file
31
tests/models/jobs.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use gabrielkaszewski_rs::app::App;
|
||||
use loco_rs::testing;
|
||||
use serial_test::serial;
|
||||
|
||||
macro_rules! configure_insta {
|
||||
($($expr:expr),*) => {
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
let _guard = settings.bind_to_scope();
|
||||
};
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_model() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
// query your model, e.g.:
|
||||
//
|
||||
// let item = models::posts::Model::find_by_pid(
|
||||
// &boot.app_context.db,
|
||||
// "11111111-1111-1111-1111-111111111111",
|
||||
// )
|
||||
// .await;
|
||||
|
||||
// snapshot the result:
|
||||
// assert_debug_snapshot!(item);
|
||||
}
|
4
tests/models/mod.rs
Normal file
4
tests/models/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
mod users;
|
||||
|
||||
mod skills;
|
||||
mod jobs;
|
31
tests/models/skills.rs
Normal file
31
tests/models/skills.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use gabrielkaszewski_rs::app::App;
|
||||
use loco_rs::testing;
|
||||
use serial_test::serial;
|
||||
|
||||
macro_rules! configure_insta {
|
||||
($($expr:expr),*) => {
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
let _guard = settings.bind_to_scope();
|
||||
};
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_model() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
// query your model, e.g.:
|
||||
//
|
||||
// let item = models::posts::Model::find_by_pid(
|
||||
// &boot.app_context.db,
|
||||
// "11111111-1111-1111-1111-111111111111",
|
||||
// )
|
||||
// .await;
|
||||
|
||||
// snapshot the result:
|
||||
// assert_debug_snapshot!(item);
|
||||
}
|
21
tests/models/snapshots/can_create_with_password@users.snap
Normal file
21
tests/models/snapshots/can_create_with_password@users.snap
Normal file
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: tests/models/users.rs
|
||||
expression: res
|
||||
---
|
||||
Ok(
|
||||
Model {
|
||||
created_at: DATE,
|
||||
updated_at: DATE,
|
||||
id: ID
|
||||
pid: PID,
|
||||
email: "test@framework.com",
|
||||
password: "PASSWORD",
|
||||
api_key: "lo-PID",
|
||||
name: "framework",
|
||||
reset_token: None,
|
||||
reset_sent_at: None,
|
||||
email_verification_token: None,
|
||||
email_verification_sent_at: None,
|
||||
email_verified_at: None,
|
||||
},
|
||||
)
|
7
tests/models/snapshots/can_find_by_email-2@users.snap
Normal file
7
tests/models/snapshots/can_find_by_email-2@users.snap
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
source: tests/models/main.rs
|
||||
expression: non_existing_user_results
|
||||
---
|
||||
Err(
|
||||
EntityNotFound,
|
||||
)
|
7
tests/models/snapshots/can_find_by_email@users-2.snap
Normal file
7
tests/models/snapshots/can_find_by_email@users-2.snap
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
source: tests/models/users.rs
|
||||
expression: non_existing_user_results
|
||||
---
|
||||
Err(
|
||||
EntityNotFound,
|
||||
)
|
21
tests/models/snapshots/can_find_by_email@users.snap
Normal file
21
tests/models/snapshots/can_find_by_email@users.snap
Normal file
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: tests/models/users.rs
|
||||
expression: existing_user
|
||||
---
|
||||
Ok(
|
||||
Model {
|
||||
created_at: 2023-11-12T12:34:56.789+00:00,
|
||||
updated_at: 2023-11-12T12:34:56.789+00:00,
|
||||
id: 1,
|
||||
pid: 11111111-1111-1111-1111-111111111111,
|
||||
email: "user1@example.com",
|
||||
password: "$argon2id$v=19$m=19456,t=2,p=1$ETQBx4rTgNAZhSaeYZKOZg$eYTdH26CRT6nUJtacLDEboP0li6xUwUF/q5nSlQ8uuc",
|
||||
api_key: "lo-95ec80d7-cb60-4b70-9b4b-9ef74cb88758",
|
||||
name: "user1",
|
||||
reset_token: None,
|
||||
reset_sent_at: None,
|
||||
email_verification_token: None,
|
||||
email_verification_sent_at: None,
|
||||
email_verified_at: None,
|
||||
},
|
||||
)
|
7
tests/models/snapshots/can_find_by_pid-2@users.snap
Normal file
7
tests/models/snapshots/can_find_by_pid-2@users.snap
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
source: tests/models/main.rs
|
||||
expression: non_existing_user_results
|
||||
---
|
||||
Err(
|
||||
EntityNotFound,
|
||||
)
|
7
tests/models/snapshots/can_find_by_pid@users-2.snap
Normal file
7
tests/models/snapshots/can_find_by_pid@users-2.snap
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
source: tests/models/users.rs
|
||||
expression: non_existing_user_results
|
||||
---
|
||||
Err(
|
||||
EntityNotFound,
|
||||
)
|
21
tests/models/snapshots/can_find_by_pid@users.snap
Normal file
21
tests/models/snapshots/can_find_by_pid@users.snap
Normal file
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: tests/models/users.rs
|
||||
expression: existing_user
|
||||
---
|
||||
Ok(
|
||||
Model {
|
||||
created_at: 2023-11-12T12:34:56.789+00:00,
|
||||
updated_at: 2023-11-12T12:34:56.789+00:00,
|
||||
id: 1,
|
||||
pid: 11111111-1111-1111-1111-111111111111,
|
||||
email: "user1@example.com",
|
||||
password: "$argon2id$v=19$m=19456,t=2,p=1$ETQBx4rTgNAZhSaeYZKOZg$eYTdH26CRT6nUJtacLDEboP0li6xUwUF/q5nSlQ8uuc",
|
||||
api_key: "lo-95ec80d7-cb60-4b70-9b4b-9ef74cb88758",
|
||||
name: "user1",
|
||||
reset_token: None,
|
||||
reset_sent_at: None,
|
||||
email_verification_token: None,
|
||||
email_verification_sent_at: None,
|
||||
email_verified_at: None,
|
||||
},
|
||||
)
|
9
tests/models/snapshots/can_validate_model@users.snap
Normal file
9
tests/models/snapshots/can_validate_model@users.snap
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
source: tests/models/main.rs
|
||||
expression: res
|
||||
---
|
||||
Err(
|
||||
Custom(
|
||||
"{\"email\":[{\"code\":\"invalid email\",\"message\":null}],\"name\":[{\"code\":\"length\",\"message\":\"Name must be at least 2 characters long.\"}]}",
|
||||
),
|
||||
)
|
@@ -0,0 +1,7 @@
|
||||
---
|
||||
source: tests/models/users.rs
|
||||
expression: new_user
|
||||
---
|
||||
Err(
|
||||
EntityAlreadyExists,
|
||||
)
|
223
tests/models/users.rs
Normal file
223
tests/models/users.rs
Normal file
@@ -0,0 +1,223 @@
|
||||
use insta::assert_debug_snapshot;
|
||||
use loco_rs::{model::ModelError, testing};
|
||||
use gabrielkaszewski_rs::{
|
||||
app::App,
|
||||
models::users::{self, Model, RegisterParams},
|
||||
};
|
||||
use sea_orm::{ActiveModelTrait, ActiveValue, IntoActiveModel};
|
||||
use serial_test::serial;
|
||||
|
||||
macro_rules! configure_insta {
|
||||
($($expr:expr),*) => {
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
settings.set_snapshot_suffix("users");
|
||||
let _guard = settings.bind_to_scope();
|
||||
};
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_can_validate_model() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
|
||||
let res = users::ActiveModel {
|
||||
name: ActiveValue::set("1".to_string()),
|
||||
email: ActiveValue::set("invalid-email".to_string()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&boot.app_context.db)
|
||||
.await;
|
||||
|
||||
assert_debug_snapshot!(res);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn can_create_with_password() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
|
||||
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, ¶ms).await;
|
||||
|
||||
insta::with_settings!({
|
||||
filters => testing::cleanup_user_model()
|
||||
}, {
|
||||
assert_debug_snapshot!(res);
|
||||
});
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn handle_create_with_password_with_duplicate() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
let new_user: Result<Model, ModelError> = Model::create_with_password(
|
||||
&boot.app_context.db,
|
||||
&RegisterParams {
|
||||
email: "user1@example.com".to_string(),
|
||||
password: "1234".to_string(),
|
||||
name: "framework".to_string(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
assert_debug_snapshot!(new_user);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn can_find_by_email() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
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;
|
||||
|
||||
assert_debug_snapshot!(existing_user);
|
||||
assert_debug_snapshot!(non_existing_user_results);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn can_find_by_pid() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn can_verification_token() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(user.email_verification_sent_at.is_none());
|
||||
assert!(user.email_verification_token.is_none());
|
||||
|
||||
assert!(user
|
||||
.into_active_model()
|
||||
.set_email_verification_sent(&boot.app_context.db)
|
||||
.await
|
||||
.is_ok());
|
||||
|
||||
let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(user.email_verification_sent_at.is_some());
|
||||
assert!(user.email_verification_token.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn can_set_forgot_password_sent() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(user.reset_sent_at.is_none());
|
||||
assert!(user.reset_token.is_none());
|
||||
|
||||
assert!(user
|
||||
.into_active_model()
|
||||
.set_forgot_password_sent(&boot.app_context.db)
|
||||
.await
|
||||
.is_ok());
|
||||
|
||||
let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(user.reset_sent_at.is_some());
|
||||
assert!(user.reset_token.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn can_verified() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(user.email_verified_at.is_none());
|
||||
|
||||
assert!(user
|
||||
.into_active_model()
|
||||
.verified(&boot.app_context.db)
|
||||
.await
|
||||
.is_ok());
|
||||
|
||||
let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(user.email_verified_at.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn can_reset_password() {
|
||||
configure_insta!();
|
||||
|
||||
let boot = testing::boot_test::<App>().await.unwrap();
|
||||
testing::seed::<App>(&boot.app_context.db).await.unwrap();
|
||||
|
||||
let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(user.verify_password("12341234"));
|
||||
|
||||
assert!(user
|
||||
.clone()
|
||||
.into_active_model()
|
||||
.reset_password(&boot.app_context.db, "new-password")
|
||||
.await
|
||||
.is_ok());
|
||||
|
||||
assert!(
|
||||
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
|
||||
.await
|
||||
.unwrap()
|
||||
.verify_password("new-password")
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user