From 225583f552ebee490d31fb1b1e0a1e1f64da2bdf Mon Sep 17 00:00:00 2001 From: EETagent Date: Thu, 17 Nov 2022 00:10:56 +0100 Subject: [PATCH 1/5] 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:"; From fe1ddf9342747b03b93fbc9c60399802abd3e425 Mon Sep 17 00:00:00 2001 From: EETagent Date: Thu, 17 Nov 2022 12:25:21 +0100 Subject: [PATCH 2/5] feat: sqlite memory connection test --- core/src/util.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/src/util.rs b/core/src/util.rs index b932dac..ab47922 100644 --- a/core/src/util.rs +++ b/core/src/util.rs @@ -25,4 +25,14 @@ pub async fn get_memory_sqlite_connection() -> sea_orm::DbConn { .await .unwrap(); db +} + +#[cfg(test)] +mod tests { + use super::get_memory_sqlite_connection; + + #[tokio::test] + async fn test_get_memory_sqlite_connection() { + get_memory_sqlite_connection().await; + } } \ No newline at end of file From 47e16ab48a2d6025821306e9f70a6c5047dee2d6 Mon Sep 17 00:00:00 2001 From: EETagent Date: Thu, 17 Nov 2022 12:37:44 +0100 Subject: [PATCH 3/5] feat: candidate details tests --- core/src/candidate_details.rs | 177 +++++++++++++++++++++++++++++----- 1 file changed, 153 insertions(+), 24 deletions(-) diff --git a/core/src/candidate_details.rs b/core/src/candidate_details.rs index 2bf1961..dc6350a 100644 --- a/core/src/candidate_details.rs +++ b/core/src/candidate_details.rs @@ -1,8 +1,8 @@ -use chrono::{NaiveDate}; +use chrono::NaiveDate; use entity::{candidate, parent}; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; -use crate::{error::ServiceError, crypto}; +use crate::{crypto, error::ServiceError}; pub const NAIVE_DATE_FMT: &str = "%Y-%m-%d"; @@ -11,16 +11,16 @@ pub struct EncryptedString(String); impl EncryptedString { pub async fn new(s: &str, recipients: &Vec<&str>) -> Result { - match crypto::encrypt_password_with_recipients(&s, &recipients).await{ + match crypto::encrypt_password_with_recipients(&s, &recipients).await { Ok(encrypted) => Ok(Self(encrypted)), - Err(_) => Err(ServiceError::CryptoEncryptFailed), + Err(_) => Err(ServiceError::CryptoEncryptFailed), } } pub async fn decrypt(&self, private_key: &String) -> Result { match crypto::decrypt_password_with_private_key(&self.0, private_key).await { Ok(decrypted) => Ok(decrypted), - Err(_) => Err(ServiceError::CryptoDecryptFailed), + Err(_) => Err(ServiceError::CryptoDecryptFailed), } } @@ -46,7 +46,8 @@ impl TryFrom> for EncryptedString { } } -impl TryFrom> for EncryptedString { // TODO: take a look at this +impl TryFrom> for EncryptedString { + // TODO: take a look at this type Error = ServiceError; fn try_from(d: Option) -> Result { @@ -79,9 +80,12 @@ pub struct EncryptedApplicationDetails { } impl EncryptedApplicationDetails { - pub async fn new(form: ApplicationDetails, recipients: Vec<&str>) -> Result { + pub async fn new( + form: ApplicationDetails, + recipients: Vec<&str>, + ) -> Result { let birthdate_str = form.birthdate.format(NAIVE_DATE_FMT).to_string(); - let d = tokio::try_join!( + let d = tokio::try_join!( EncryptedString::new(&form.name, &recipients), EncryptedString::new(&form.surname, &recipients), EncryptedString::new(&form.birthplace, &recipients), @@ -92,7 +96,6 @@ impl EncryptedApplicationDetails { EncryptedString::new(&form.email, &recipients), EncryptedString::new(&form.sex, &recipients), EncryptedString::new(&form.study, &recipients), - EncryptedString::new(&form.parent_name, &recipients), EncryptedString::new(&form.parent_surname, &recipients), EncryptedString::new(&form.parent_telephone, &recipients), @@ -119,18 +122,17 @@ impl EncryptedApplicationDetails { } pub async fn decrypt(self, priv_key: String) -> Result { - let d = tokio::try_join!( - self.name.decrypt(&priv_key), // 0 - self.surname.decrypt(&priv_key), // 1 - self.birthplace.decrypt(&priv_key), // 2 - self.birthdate.decrypt(&priv_key), // 3 - self.address.decrypt(&priv_key), // 4 - self.telephone.decrypt(&priv_key), // 5 + let d = tokio::try_join!( + self.name.decrypt(&priv_key), // 0 + self.surname.decrypt(&priv_key), // 1 + self.birthplace.decrypt(&priv_key), // 2 + self.birthdate.decrypt(&priv_key), // 3 + self.address.decrypt(&priv_key), // 4 + self.telephone.decrypt(&priv_key), // 5 self.citizenship.decrypt(&priv_key), // 6 - self.email.decrypt(&priv_key), // 7 - self.sex.decrypt(&priv_key), // 8 - self.study.decrypt(&priv_key), // 9 - + self.email.decrypt(&priv_key), // 7 + self.sex.decrypt(&priv_key), // 8 + self.study.decrypt(&priv_key), // 9 self.parent_name.decrypt(&priv_key), self.parent_surname.decrypt(&priv_key), self.parent_telephone.decrypt(&priv_key), @@ -160,7 +162,9 @@ impl EncryptedApplicationDetails { impl TryFrom<(candidate::Model, parent::Model)> for EncryptedApplicationDetails { type Error = ServiceError; - fn try_from((candidate, parent): (candidate::Model, parent::Model)) -> Result { + fn try_from( + (candidate, parent): (candidate::Model, parent::Model), + ) -> Result { Ok(EncryptedApplicationDetails { name: EncryptedString::try_from(candidate.name)?, surname: EncryptedString::try_from(candidate.surname)?, @@ -194,10 +198,135 @@ pub struct ApplicationDetails { pub email: String, pub sex: String, pub study: String, - + // Parent pub parent_name: String, pub parent_surname: String, pub parent_telephone: String, pub parent_email: String, -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use crate::crypto; + + use super::{ApplicationDetails, EncryptedApplicationDetails, EncryptedString}; + + #[tokio::test] + async fn test_encrypted_application_details_new() { + const PUBLIC_KEY: &str = "age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"; + const PRIVATE_KEY: &str = + "AGE-SECRET-KEY-14QG24502DMUUQDT2SPMX2YXPSES0X8UD6NT0PCTDAT6RH8V5Q3GQGSRXPS"; + let encrypted_details = 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![PUBLIC_KEY], + ) + .await + .unwrap(); + + assert_eq!( + crypto::decrypt_password_with_private_key(&encrypted_details.name.0, PRIVATE_KEY) + .await + .unwrap(), + "test" + ); + assert_eq!( + crypto::decrypt_password_with_private_key(&encrypted_details.email.0, PRIVATE_KEY) + .await + .unwrap(), + "test" + ); + assert_eq!( + crypto::decrypt_password_with_private_key(&encrypted_details.sex.0, PRIVATE_KEY) + .await + .unwrap(), + "test" + ); + } + + #[tokio::test] + async fn test_encrypted_application_details_decrypt() { + const PUBLIC_KEY: &str = "age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"; + const PRIVATE_KEY: &str = + "AGE-SECRET-KEY-14QG24502DMUUQDT2SPMX2YXPSES0X8UD6NT0PCTDAT6RH8V5Q3GQGSRXPS"; + let encrypted_details = 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![PUBLIC_KEY], + ) + .await + .unwrap(); + + let application_details = encrypted_details + .decrypt(PRIVATE_KEY.to_string()) + .await + .unwrap(); + + assert_eq!(application_details.name, "test"); + assert_eq!(application_details.email, "test"); + assert_eq!(application_details.sex, "test"); + } + + #[tokio::test] + async fn test_encrypted_string_new() { + const PUBLIC_KEY: &str = "age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"; + const PRIVATE_KEY: &str = + "AGE-SECRET-KEY-14QG24502DMUUQDT2SPMX2YXPSES0X8UD6NT0PCTDAT6RH8V5Q3GQGSRXPS"; + + let encrypted = EncryptedString::new("test", &vec![PUBLIC_KEY]) + .await + .unwrap(); + + assert_eq!( + crypto::decrypt_password_with_private_key(&encrypted.0, PRIVATE_KEY) + .await + .unwrap(), + "test" + ); + } + + #[tokio::test] + async fn test_encrypted_string_decrypt() { + const PUBLIC_KEY: &str = "age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"; + const PRIVATE_KEY: &str = + "AGE-SECRET-KEY-14QG24502DMUUQDT2SPMX2YXPSES0X8UD6NT0PCTDAT6RH8V5Q3GQGSRXPS"; + + let encrypted = EncryptedString::new("test", &vec![PUBLIC_KEY]) + .await + .unwrap(); + + assert_eq!( + encrypted.decrypt(&PRIVATE_KEY.to_string()).await.unwrap(), + "test" + ); + } +} From f6a28aa22620ddd9b772e64860087754832d5f24 Mon Sep 17 00:00:00 2001 From: EETagent Date: Thu, 17 Nov 2022 12:42:20 +0100 Subject: [PATCH 4/5] feat: include custom errors in test coverage --- core/src/error.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/src/error.rs b/core/src/error.rs index 4c8f4f1..206d579 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -94,3 +94,17 @@ impl ServiceError { } } } + +#[cfg(test)] +mod tests { + use super::ServiceError; + + #[test] + fn test_service_error_code() { + let error = ServiceError::CryptoHashFailed; + + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status + assert!(error.code() >= 100); + assert!(error.code() <= 599); + } +} \ No newline at end of file From aa1df5c74e2e78e1320de560542eb823426ed709 Mon Sep 17 00:00:00 2001 From: EETagent Date: Thu, 17 Nov 2022 12:50:19 +0100 Subject: [PATCH 5/5] fix: reset env after test run --- core/src/services/candidate_service.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index bee3374..f4ff3b1 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -482,6 +482,8 @@ mod tests { #[cfg(test)] async fn clear_data_store_temp_dir(temp_dir: PathBuf) { tokio::fs::remove_dir_all(temp_dir).await.unwrap(); + + std::env::remove_var("STORE_PATH"); } #[tokio::test]