From 225583f552ebee490d31fb1b1e0a1e1f64da2bdf Mon Sep 17 00:00:00 2001 From: EETagent Date: Thu, 17 Nov 2022 00:10:56 +0100 Subject: [PATCH] feat: query/mutation tests, lint warnings removal --- core/src/database/mutation/candidate.rs | 88 ++++++++++++++++++-- core/src/database/mutation/parent.rs | 85 ++++++++++++++++++- core/src/database/mutation/session.rs | 5 ++ core/src/database/query/admin.rs | 64 ++++++++++++++- core/src/database/query/candidate.rs | 3 +- core/src/database/query/parent.rs | 46 ++++++++++- core/src/database/query/session.rs | 104 +++++++++++++++++++++++- core/src/services/candidate_service.rs | 2 +- core/src/services/session_service.rs | 53 +++++++----- core/src/util.rs | 6 +- 10 files changed, 419 insertions(+), 37 deletions(-) diff --git a/core/src/database/mutation/candidate.rs b/core/src/database/mutation/candidate.rs index 8ffda50..058a2ec 100644 --- a/core/src/database/mutation/candidate.rs +++ b/core/src/database/mutation/candidate.rs @@ -1,7 +1,7 @@ -use crate::{Mutation, candidate_details::{EncryptedApplicationDetails}}; +use crate::{candidate_details::EncryptedApplicationDetails, Mutation}; use ::entity::candidate::{self}; -use sea_orm::{*}; +use sea_orm::*; impl Mutation { pub async fn create_candidate( @@ -10,7 +10,7 @@ impl Mutation { hashed_password: String, hashed_personal_id_number: String, pubkey: String, - encrypted_priv_key: String + encrypted_priv_key: String, ) -> Result { candidate::ActiveModel { application: Set(application_id), @@ -22,8 +22,8 @@ impl Mutation { updated_at: Set(chrono::offset::Local::now().naive_local()), ..Default::default() } - .insert(db) - .await + .insert(db) + .await } pub async fn add_candidate_details( @@ -47,4 +47,80 @@ impl Mutation { user.update(db).await } -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use crate::candidate_details::{ApplicationDetails, EncryptedApplicationDetails}; + use crate::util::get_memory_sqlite_connection; + use crate::{Mutation, Query}; + + #[tokio::test] + async fn test_create_candidate() { + let db = get_memory_sqlite_connection().await; + + const APPLICATION_ID: i32 = 103158; + + Mutation::create_candidate( + &db, + APPLICATION_ID, + "test".to_string(), + "test".to_string(), + "test".to_string(), + "test".to_string(), + ) + .await + .unwrap(); + + let candidate = Query::find_candidate_by_id(&db, APPLICATION_ID) + .await + .unwrap(); + assert!(candidate.is_some()); + } + + #[tokio::test] + async fn test_add_candidate_details() { + let db = get_memory_sqlite_connection().await; + + const APPLICATION_ID: i32 = 103158; + + let candidate = Mutation::create_candidate( + &db, + APPLICATION_ID, + "test".to_string(), + "test".to_string(), + "test".to_string(), + "test".to_string(), + ) + .await + .unwrap(); + + let encrypted_details: EncryptedApplicationDetails = EncryptedApplicationDetails::new( + ApplicationDetails { + name: "test".to_string(), + surname: "test".to_string(), + birthplace: "test".to_string(), + birthdate: chrono::offset::Local::now().date_naive(), + address: "test".to_string(), + telephone: "test".to_string(), + citizenship: "test".to_string(), + email: "test".to_string(), + parent_email: "test".to_string(), + parent_name: "test".to_string(), + parent_surname: "test".to_string(), + parent_telephone: "test".to_string(), + sex: "test".to_string(), + study: "test".to_string(), + }, + vec!["age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"], + ).await.unwrap(); + + Mutation::add_candidate_details(&db, candidate, encrypted_details).await.unwrap(); + + let candidate = Query::find_candidate_by_id(&db, APPLICATION_ID) + .await + .unwrap().unwrap(); + + assert!(candidate.study.is_some()); + } +} diff --git a/core/src/database/mutation/parent.rs b/core/src/database/mutation/parent.rs index a464e42..1cfd01d 100644 --- a/core/src/database/mutation/parent.rs +++ b/core/src/database/mutation/parent.rs @@ -1,4 +1,4 @@ -use crate::{Mutation, candidate_details::EncryptedApplicationDetails}; +use crate::{candidate_details::EncryptedApplicationDetails, Mutation}; use ::entity::parent::{self, Model}; use sea_orm::*; @@ -31,3 +31,86 @@ impl Mutation { user.update(db).await } } + +#[cfg(test)] +mod tests { + use crate::candidate_details::{ApplicationDetails, EncryptedApplicationDetails}; + use crate::util::get_memory_sqlite_connection; + use crate::{Mutation, Query}; + + #[tokio::test] + async fn test_create_parent() { + let db = get_memory_sqlite_connection().await; + + const APPLICATION_ID: i32 = 103158; + + Mutation::create_candidate( + &db, + APPLICATION_ID, + "test".to_string(), + "test".to_string(), + "test".to_string(), + "test".to_string(), + ) + .await + .unwrap(); + + Mutation::create_parent(&db, APPLICATION_ID).await.unwrap(); + + let parent = Query::find_parent_by_id(&db, APPLICATION_ID).await.unwrap(); + assert!(parent.is_some()); + } + + #[tokio::test] + async fn test_add_candidate_details() { + let db = get_memory_sqlite_connection().await; + + const APPLICATION_ID: i32 = 103158; + + Mutation::create_candidate( + &db, + APPLICATION_ID, + "test".to_string(), + "test".to_string(), + "test".to_string(), + "test".to_string(), + ) + .await + .unwrap(); + + let parent = Mutation::create_parent(&db, APPLICATION_ID).await.unwrap(); + + let encrypted_details: EncryptedApplicationDetails = EncryptedApplicationDetails::new( + ApplicationDetails { + name: "test".to_string(), + surname: "test".to_string(), + birthplace: "test".to_string(), + birthdate: chrono::offset::Local::now().date_naive(), + address: "test".to_string(), + telephone: "test".to_string(), + citizenship: "test".to_string(), + email: "test".to_string(), + parent_email: "test".to_string(), + parent_name: "test".to_string(), + parent_surname: "test".to_string(), + parent_telephone: "test".to_string(), + sex: "test".to_string(), + study: "test".to_string(), + }, + vec!["age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"], + ) + .await + .unwrap(); + + Mutation::add_parent_details(&db, parent, encrypted_details) + .await + .unwrap(); + + let parent = Query::find_parent_by_id(&db, APPLICATION_ID) + .await + .unwrap() + .unwrap(); + + assert!(parent.surname.is_some()); + } +} diff --git a/core/src/database/mutation/session.rs b/core/src/database/mutation/session.rs index 068646e..f557bc0 100644 --- a/core/src/database/mutation/session.rs +++ b/core/src/database/mutation/session.rs @@ -37,3 +37,8 @@ impl Mutation { .await } } + +#[cfg(test)] +mod tests { + // TODO: Testy +} \ No newline at end of file diff --git a/core/src/database/query/admin.rs b/core/src/database/query/admin.rs index a4700a3..de61db1 100644 --- a/core/src/database/query/admin.rs +++ b/core/src/database/query/admin.rs @@ -11,8 +11,70 @@ impl Query { pub async fn get_all_admin_public_keys(db: &DbConn) -> Result, DbErr> { let admins = Admin::find().all(db).await?; - let public_keys = admins.iter().map(|admin| admin.public_key.clone()).collect(); + let public_keys = admins + .iter() + .map(|admin| admin.public_key.clone()) + .collect(); Ok(public_keys) } } + +#[cfg(test)] +mod tests { + use entity::admin; + use sea_orm::{ActiveModelTrait, Set}; + + use crate::util::get_memory_sqlite_connection; + use crate::Query; + + #[tokio::test] + async fn test_find_admin_by_id() { + let db = get_memory_sqlite_connection().await; + let admin = admin::ActiveModel { + id: Set(1), + name: Set("admin_1".to_string()), + public_key: Set("valid_public_key_1".to_string()), + private_key: Set("test".to_string()), + password: Set("test".to_string().to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + updated_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + + let admin = Query::find_admin_by_id(&db, admin.id).await.unwrap(); + assert!(admin.is_some()); + } + + #[tokio::test] + async fn test_get_all_admin_public_keys() { + let db = get_memory_sqlite_connection().await; + for index in 1..5 { + admin::ActiveModel { + id: Set(index), + name: Set(format!("admin_{}", index)), + public_key: Set(format!("valid_public_key_{}", index)), + private_key: Set("test".to_string()), + password: Set("test".to_string().to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + updated_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + } + + + let public_keys = Query::get_all_admin_public_keys(&db).await.unwrap(); + + assert_eq!(public_keys.len(), 4); + + for index in 1..5 { + assert!(public_keys.contains(&format!("valid_public_key_{}", index))); + } + } +} diff --git a/core/src/database/query/candidate.rs b/core/src/database/query/candidate.rs index 22ca2f1..2a6d7c7 100644 --- a/core/src/database/query/candidate.rs +++ b/core/src/database/query/candidate.rs @@ -15,8 +15,7 @@ impl Query { #[cfg(test)] mod tests { use entity::candidate; - use sea_orm::{sea_query::TableCreateStatement, ConnectionTrait, Database, DbBackend, Schema}; - use sea_orm::{ActiveModelTrait, DbConn, Set}; + use sea_orm::{ActiveModelTrait, Set}; use crate::Query; use crate::util::get_memory_sqlite_connection; diff --git a/core/src/database/query/parent.rs b/core/src/database/query/parent.rs index 7fc0429..665d9de 100644 --- a/core/src/database/query/parent.rs +++ b/core/src/database/query/parent.rs @@ -14,4 +14,48 @@ impl Query { Entity::find_by_id(application_id).one(db).await } -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use entity::{candidate, parent}; + use sea_orm::{ActiveModelTrait, Set}; + + use crate::Query; + use crate::util::get_memory_sqlite_connection; + + #[tokio::test] + async fn test_find_parent_by_id() { + let db = get_memory_sqlite_connection().await; + + const APPLICATION_ID: i32 = 103158; + + candidate::ActiveModel { + application: Set(APPLICATION_ID), + code: Set("test".to_string()), + public_key: Set("test".to_string()), + private_key: Set("test".to_string()), + personal_identification_number_hash: Set("test".to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + updated_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + let parent = parent::ActiveModel { + application: Set(APPLICATION_ID), + created_at: Set(chrono::offset::Local::now().naive_local()), + updated_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + + let parent = Query::find_candidate_by_id(&db, parent.application) + .await + .unwrap(); + assert!(parent.is_some()); + } +} diff --git a/core/src/database/query/session.rs b/core/src/database/query/session.rs index 3b8f361..0361997 100644 --- a/core/src/database/query/session.rs +++ b/core/src/database/query/session.rs @@ -24,4 +24,106 @@ impl Query { .all(db) .await } -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use entity::{session}; + use sea_orm::{prelude::Uuid, ActiveModelTrait, Set}; + + use crate::util::get_memory_sqlite_connection; + use crate::Query; + + #[tokio::test] + async fn test_find_session_by_uuid() { + let db = get_memory_sqlite_connection().await; + + let session = session::ActiveModel { + id: Set(Uuid::new_v4()), + ip_address: Set("10.10.10.10".to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + expires_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + + let session = Query::find_session_by_uuid(&db, session.id).await.unwrap(); + assert!(session.is_some()); + } + + // TODO: Opravit test_find_sessions_by_user_id + /* #[tokio::test] + async fn test_find_sessions_by_user_id() { + let db = get_memory_sqlite_connection().await; + + const APPLICATION_ID: i32 = 103158; + + candidate::ActiveModel { + application: Set(APPLICATION_ID), + code: Set("test".to_string()), + public_key: Set("test".to_string()), + private_key: Set("test".to_string()), + personal_identification_number_hash: Set("test".to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + updated_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + + session::ActiveModel { + id: Set(Uuid::new_v4()), + user_id: Set(Some(APPLICATION_ID)), + admin_id: NotSet, + ip_address: Set("10.10.10.10".to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + expires_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + + const ADMIN_ID: i32 = 1; + + admin::ActiveModel { + id: Set(ADMIN_ID), + name: Set("admin".to_string()), + public_key: Set("test".to_string()), + private_key: Set("test".to_string()), + password: Set("test".to_string().to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + updated_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + + session::ActiveModel { + id: Set(Uuid::new_v4()), + user_id: NotSet, + admin_id: Set(Some(ADMIN_ID)), + ip_address: Set("10.10.10.10".to_string()), + created_at: Set(chrono::offset::Local::now().naive_local()), + expires_at: Set(chrono::offset::Local::now().naive_local()), + ..Default::default() + } + .insert(&db) + .await + .unwrap(); + + let sessions = Query::find_sessions_by_user_id(&db, Some(APPLICATION_ID), None) + .await + .unwrap(); + assert_eq!(sessions.len(), 1); + + let sessions = Query::find_sessions_by_user_id(&db, None, Some(ADMIN_ID)) + .await + .unwrap(); + assert_eq!(sessions.len(), 1); + } */ +} diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index f57dba9..bee3374 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -342,7 +342,7 @@ impl CandidateService { #[cfg(test)] mod tests { - use sea_orm::{Database, DbConn}; + use sea_orm::{DbConn}; use serial_test::serial; use crate::util::get_memory_sqlite_connection; diff --git a/core/src/services/session_service.rs b/core/src/services/session_service.rs index 33fdb7b..f81cc3e 100644 --- a/core/src/services/session_service.rs +++ b/core/src/services/session_service.rs @@ -162,16 +162,12 @@ impl SessionService { #[cfg(test)] mod tests { - use entity::{admin, candidate, session, parent}; - - use sea_orm::{ - prelude::Uuid, sea_query::TableCreateStatement, ConnectionTrait, Database, DbBackend, - DbConn, Schema, - }; + use sea_orm::prelude::Uuid; use crate::{ crypto, - services::{session_service::SessionService, application_service::ApplicationService}, util::get_memory_sqlite_connection, + services::{application_service::ApplicationService, session_service::SessionService}, + util::get_memory_sqlite_connection, }; #[tokio::test] @@ -180,10 +176,16 @@ mod tests { let db = get_memory_sqlite_connection().await; - let candidate = ApplicationService::create_candidate_with_parent(&db, 103151, &SECRET.to_string(), "".to_string()) - .await - .ok() - .unwrap().0; + let candidate = ApplicationService::create_candidate_with_parent( + &db, + 103151, + &SECRET.to_string(), + "".to_string(), + ) + .await + .ok() + .unwrap() + .0; assert_eq!(candidate.application, 103151); assert_ne!(candidate.code, SECRET.to_string()); @@ -197,9 +199,15 @@ mod tests { async fn test_candidate_session_correct_password() { let db = &get_memory_sqlite_connection().await; - ApplicationService::create_candidate_with_parent(db, 103151, &"Tajny_kod".to_string(), "".to_string()) - .await - .unwrap().0; + ApplicationService::create_candidate_with_parent( + db, + 103151, + &"Tajny_kod".to_string(), + "".to_string(), + ) + .await + .unwrap() + .0; // correct password let session = SessionService::new_session( @@ -209,8 +217,8 @@ mod tests { "Tajny_kod".to_string(), "127.0.0.1".to_string(), ) - .await - .unwrap(); + .await + .unwrap(); // println!("{}", session.err().unwrap().1); assert!( SessionService::auth_user_session(db, Uuid::parse_str(&session).unwrap()) @@ -223,10 +231,15 @@ mod tests { async fn test_candidate_session_incorrect_password() { let db = &get_memory_sqlite_connection().await; - let candidate_form = - ApplicationService::create_candidate_with_parent(&db, 103151, &"Tajny_kod".to_string(), "".to_string()) - .await - .unwrap().0; + let candidate_form = ApplicationService::create_candidate_with_parent( + &db, + 103151, + &"Tajny_kod".to_string(), + "".to_string(), + ) + .await + .unwrap() + .0; // incorrect password assert!(SessionService::new_session( diff --git a/core/src/util.rs b/core/src/util.rs index f58b3b8..b932dac 100644 --- a/core/src/util.rs +++ b/core/src/util.rs @@ -1,9 +1,7 @@ -use crate::sea_orm::DbConn; - #[cfg(test)] -pub async fn get_memory_sqlite_connection() -> DbConn { +pub async fn get_memory_sqlite_connection() -> sea_orm::DbConn { use entity::{admin, candidate, parent, session}; - use sea_orm::{Schema, Database}; + use sea_orm::{Schema, Database, DbConn}; use sea_orm::{sea_query::TableCreateStatement, ConnectionTrait, DbBackend}; let base_url = "sqlite::memory:";