From b227155962391914afecca397b307b0d33b1e0bc Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Tue, 6 Dec 2022 00:09:31 +0100 Subject: [PATCH] feat: multiple parents test --- core/src/services/parent_service.rs | 91 ++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/core/src/services/parent_service.rs b/core/src/services/parent_service.rs index c08ce2d..f25664d 100644 --- a/core/src/services/parent_service.rs +++ b/core/src/services/parent_service.rs @@ -54,7 +54,43 @@ impl ParentService { #[cfg(test)] mod tests { - use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails}, candidate_details::{tests::APPLICATION_DETAILS, EncryptedParentDetails, EncryptedApplicationDetails}}, services::{candidate_service::CandidateService, application_service::ApplicationService}}; + use std::sync::Mutex; + + use once_cell::sync::Lazy; + + use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails, CandidateDetails}, candidate_details::{tests::APPLICATION_DETAILS, EncryptedParentDetails, EncryptedApplicationDetails}}, services::{candidate_service::CandidateService, application_service::ApplicationService}, crypto}; + + use super::ParentService; + + pub static APPLICATION_DETAILS_TWO_PARENTS: Lazy> = Lazy::new(|| + Mutex::new(ApplicationDetails { + candidate: CandidateDetails { + name: "name".to_string(), + surname: "surname".to_string(), + birthplace: "birthplace".to_string(), + birthdate: chrono::NaiveDate::from_ymd(2000, 1, 1), + address: "address".to_string(), + telephone: "telephone".to_string(), + citizenship: "citizenship".to_string(), + email: "email".to_string(), + sex: "sex".to_string(), + personal_id_number: "personal_id_number".to_string(), + study: "study".to_string(), + }, + parents: vec![ParentDetails { + name: "parent_name".to_string(), + surname: "parent_surname".to_string(), + telephone: "parent_telephone".to_string(), + email: "parent_email".to_string(), + }, + ParentDetails { + name: "parent_name2".to_string(), + surname: "parent_surname2".to_string(), + telephone: "parent_telephone2".to_string(), + email: "parent_email2".to_string(), + }], + }) + ); #[tokio::test] async fn create_parent_test() { @@ -63,4 +99,57 @@ mod tests { super::ParentService::create(&db, 103100).await.unwrap(); super::ParentService::create(&db, 103100).await.unwrap(); } + + #[tokio::test] + async fn add_parent_details_test() { + let db = get_memory_sqlite_connection().await; + let plain_text_password = "test".to_string(); + let (candidate, _parent) = ApplicationService::create_candidate_with_parent( + &db, + 103101, + &plain_text_password, + "".to_string(), + ) + .await + .ok() + .unwrap(); + + ParentService::create(&db, 103101).await.unwrap(); + + let form = APPLICATION_DETAILS_TWO_PARENTS.lock().unwrap().clone(); + + let (candidate, parents) = ApplicationService::add_all_details(&db, candidate.clone(), &form) + .await + .unwrap(); + + let priv_key = crypto::decrypt_password(candidate.private_key.clone(), plain_text_password).await.unwrap(); + let dec_details = EncryptedApplicationDetails::try_from((candidate, parents)) + .unwrap() + .decrypt(priv_key) + .await + .unwrap(); + + assert_eq!(dec_details.candidate.name, form.candidate.name); + assert_eq!(dec_details.candidate.surname, form.candidate.surname); + assert_eq!(dec_details.candidate.birthplace, form.candidate.birthplace); + assert_eq!(dec_details.candidate.birthdate, form.candidate.birthdate); + assert_eq!(dec_details.candidate.address, form.candidate.address); + assert_eq!(dec_details.candidate.telephone, form.candidate.telephone); + assert_eq!(dec_details.candidate.citizenship, form.candidate.citizenship); + assert_eq!(dec_details.candidate.email, form.candidate.email); + assert_eq!(dec_details.candidate.sex, form.candidate.sex); + assert_eq!(dec_details.candidate.personal_id_number, form.candidate.personal_id_number); + assert_eq!(dec_details.candidate.study, form.candidate.study); + + assert_eq!(dec_details.parents.len(), form.parents.len()); + for i in 0..dec_details.parents.len() { + assert_eq!(dec_details.parents[i].name, form.parents[i].name); + assert_eq!(dec_details.parents[i].surname, form.parents[i].surname); + assert_eq!(dec_details.parents[i].telephone, form.parents[i].telephone); + assert_eq!(dec_details.parents[i].email, form.parents[i].email); + } + + + + } } \ No newline at end of file