refactor: personal_id_number in ApplicationDetails

This commit is contained in:
Sebastian Pravda 2022-11-27 10:53:40 +01:00
parent 0a400e0b38
commit 6499c8ce96
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
5 changed files with 117 additions and 128 deletions

View file

@ -359,6 +359,7 @@ mod tests {
\"citizenship\": \"Czech Republic\",
\"email\": \"magor@magor.cz\",
\"sex\": \"MALE\",
\"personal_id_number\": \"0000000000\",
\"study\": \"KB\",
\"parent_name\": \"maminka\",
\"parent_surname\": \"chad\",

View file

@ -71,6 +71,7 @@ pub struct EncryptedApplicationDetails {
pub citizenship: EncryptedString,
pub email: EncryptedString,
pub sex: EncryptedString,
pub personal_id_number: EncryptedString,
pub study: String,
// Parent
@ -96,6 +97,7 @@ impl EncryptedApplicationDetails {
EncryptedString::new(&form.citizenship, &recipients),
EncryptedString::new(&form.email, &recipients),
EncryptedString::new(&form.sex, &recipients),
EncryptedString::new(&form.personal_id_number, &recipients),
EncryptedString::new(&form.parent_name, &recipients),
EncryptedString::new(&form.parent_surname, &recipients),
@ -113,30 +115,32 @@ impl EncryptedApplicationDetails {
citizenship: d.6,
email: d.7,
sex: d.8,
personal_id_number: d.9,
study: form.study,
parent_name: d.9,
parent_surname: d.10,
parent_telephone: d.11,
parent_email: d.12,
parent_name: d.10,
parent_surname: d.11,
parent_telephone: d.12,
parent_email: d.13,
})
}
pub async fn decrypt(self, priv_key: String) -> Result<ApplicationDetails, ServiceError> {
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.parent_name.decrypt(&priv_key),
self.parent_surname.decrypt(&priv_key),
self.parent_telephone.decrypt(&priv_key),
self.parent_email.decrypt(&priv_key),
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.personal_id_number.decrypt(&priv_key),// 9
self.parent_name.decrypt(&priv_key), // 10
self.parent_surname.decrypt(&priv_key), // 11
self.parent_telephone.decrypt(&priv_key), // 12
self.parent_email.decrypt(&priv_key), // 13
)?;
Ok(ApplicationDetails {
@ -149,12 +153,13 @@ impl EncryptedApplicationDetails {
citizenship: d.6,
email: d.7,
sex: d.8,
personal_id_number: d.9,
study: self.study,
parent_name: d.9,
parent_surname: d.10,
parent_telephone: d.11,
parent_email: d.12,
parent_name: d.10,
parent_surname: d.11,
parent_telephone: d.12,
parent_email: d.13,
})
}
}
@ -175,6 +180,7 @@ impl TryFrom<(candidate::Model, parent::Model)> for EncryptedApplicationDetails
citizenship: EncryptedString::try_from(candidate.citizenship)?,
email: EncryptedString::try_from(candidate.email)?,
sex: EncryptedString::try_from(candidate.sex)?,
personal_id_number: EncryptedString::try_from(candidate.personal_identification_number)?,
study: candidate.study.ok_or(ServiceError::CandidateDetailsNotSet)?,
parent_name: EncryptedString::try_from(parent.name)?,
@ -201,6 +207,7 @@ impl TryFrom<CandidateWithParent> for EncryptedApplicationDetails {
citizenship: EncryptedString::try_from(cp.citizenship)?,
email: EncryptedString::try_from(cp.email)?,
sex: EncryptedString::try_from(cp.sex)?,
personal_id_number: EncryptedString::try_from(cp.personal_identification_number)?,
study: cp.study.ok_or(ServiceError::CandidateDetailsNotSet)?,
parent_name: EncryptedString::try_from(cp.parent_name)?,
@ -213,7 +220,7 @@ impl TryFrom<CandidateWithParent> for EncryptedApplicationDetails {
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct ApplicationDetails {
// Candidate
pub name: String,
@ -226,7 +233,7 @@ pub struct ApplicationDetails {
pub email: String,
pub sex: String,
pub study: String,
pub personal_id_number: String,
// Parent
pub parent_name: String,
pub parent_surname: String,
@ -235,33 +242,61 @@ pub struct ApplicationDetails {
}
#[cfg(test)]
mod tests {
pub mod tests {
use std::sync::Mutex;
use chrono::NaiveDate;
use once_cell::sync::Lazy;
use crate::crypto;
use super::{ApplicationDetails, EncryptedApplicationDetails, EncryptedString};
pub static APPLICATION_DETAILS: Lazy<Mutex<ApplicationDetails>> = Lazy::new(||
Mutex::new(ApplicationDetails {
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(),
parent_email: "parent_email".to_string(),
parent_name: "parent_name".to_string(),
parent_surname: "parent_surname".to_string(),
parent_telephone: "parent_telephone".to_string()
})
);
pub fn assert_all_application_details(details: &ApplicationDetails) {
assert_eq!(details.name, "name");
assert_eq!(details.surname, "surname");
assert_eq!(details.birthplace, "birthplace");
assert_eq!(details.birthdate, chrono::NaiveDate::from_ymd(2000, 1, 1));
assert_eq!(details.address, "address");
assert_eq!(details.telephone, "telephone");
assert_eq!(details.citizenship, "citizenship");
assert_eq!(details.email, "email");
assert_eq!(details.sex, "sex");
assert_eq!(details.study, "study");
assert_eq!(details.personal_id_number, "personal_id_number");
assert_eq!(details.parent_name, "parent_name");
assert_eq!(details.parent_surname, "parent_surname");
assert_eq!(details.parent_telephone, "parent_telephone");
assert_eq!(details.parent_email, "parent_email");
}
#[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(),
},
APPLICATION_DETAILS.lock().unwrap().clone(),
vec![PUBLIC_KEY],
)
.await
@ -271,19 +306,19 @@ mod tests {
crypto::decrypt_password_with_private_key(&encrypted_details.name.0, PRIVATE_KEY)
.await
.unwrap(),
"test"
"name"
);
assert_eq!(
crypto::decrypt_password_with_private_key(&encrypted_details.email.0, PRIVATE_KEY)
.await
.unwrap(),
"test"
"email"
);
assert_eq!(
crypto::decrypt_password_with_private_key(&encrypted_details.sex.0, PRIVATE_KEY)
.await
.unwrap(),
"test"
"sex"
);
}
@ -292,23 +327,9 @@ mod tests {
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(),
},
APPLICATION_DETAILS.lock().unwrap().clone(),
vec![PUBLIC_KEY],
)
.await
@ -319,11 +340,32 @@ mod tests {
.await
.unwrap();
assert_eq!(application_details.name, "test");
assert_eq!(application_details.email, "test");
assert_eq!(application_details.sex, "test");
assert_all_application_details(&application_details);
}
// TODO
/* #[tokio::test]
async fn test_encrypted_application_details_from_candidate_parent() {
const PUBLIC_KEY: &str = "age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5";
const PRIVATE_KEY: &str =
"AGE-SECRET-KEY-14QG24502DMUUQDT2SPMX2YXPSES0X8UD6NT0PCTDAT6RH8V5Q3GQGSRXPS";
const birthdate: NaiveDate = chrono::offset::Local::now().date_naive();
let encrypted_details = EncryptedApplicationDetails::try_from(
,
vec![PUBLIC_KEY],
)
.await
.unwrap();
let application_details = encrypted_details
.decrypt(PRIVATE_KEY.to_string())
.await
.unwrap();
assert_all_application_details(&application_details);
} */
#[tokio::test]
async fn test_encrypted_string_new() {
const PUBLIC_KEY: &str = "age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5";

View file

@ -56,6 +56,7 @@ impl Mutation {
user.citizenship = Set(Some(enc_details.citizenship.into()));
user.email = Set(Some(enc_details.email.into()));
user.sex = Set(Some(enc_details.sex.into()));
user.personal_identification_number = Set(Some(enc_details.personal_id_number.into()));
user.study = Set(Some(enc_details.study.into()));
user.updated_at = Set(chrono::offset::Local::now().naive_local());
@ -66,7 +67,8 @@ impl Mutation {
#[cfg(test)]
mod tests {
use crate::candidate_details::{ApplicationDetails, EncryptedApplicationDetails};
use crate::candidate_details::tests::APPLICATION_DETAILS;
use crate::candidate_details::{EncryptedApplicationDetails};
use crate::util::get_memory_sqlite_connection;
use crate::{Mutation, Query};
@ -111,22 +113,7 @@ mod tests {
.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(),
},
APPLICATION_DETAILS.lock().unwrap().clone(),
vec!["age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"],
).await.unwrap();

View file

@ -34,7 +34,8 @@ impl Mutation {
#[cfg(test)]
mod tests {
use crate::candidate_details::{ApplicationDetails, EncryptedApplicationDetails};
use crate::candidate_details::tests::APPLICATION_DETAILS;
use crate::candidate_details::{EncryptedApplicationDetails};
use crate::util::get_memory_sqlite_connection;
use crate::{Mutation, Query};
@ -81,22 +82,7 @@ mod tests {
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(),
},
APPLICATION_DETAILS.lock().unwrap().clone(),
vec!["age1u889gp407hsz309wn09kxx9anl6uns30m27lfwnctfyq9tq4qpus8tzmq5"],
)
.await

View file

@ -247,14 +247,13 @@ impl CandidateService {
pub mod tests {
use sea_orm::{DbConn};
use crate::candidate_details::tests::assert_all_application_details;
use crate::util::get_memory_sqlite_connection;
use crate::{crypto, services::candidate_service::CandidateService, Mutation};
use super::EncryptedApplicationDetails;
use chrono::NaiveDate;
use entity::{candidate, parent, admin};
use crate::candidate_details::{ApplicationDetails};
use crate::services::application_service::ApplicationService;
const APPLICATION_ID: i32 = 103151;
@ -368,6 +367,8 @@ pub mod tests {
#[cfg(test)]
pub async fn put_user_data(db: &DbConn) -> (candidate::Model, parent::Model) {
use crate::candidate_details::tests::APPLICATION_DETAILS;
let plain_text_password = "test".to_string();
let (candidate, _parent) = ApplicationService::create_candidate_with_parent(
&db,
@ -379,22 +380,7 @@ pub mod tests {
.ok()
.unwrap();
let form = ApplicationDetails {
name: "name".to_string(),
surname: "surname".to_string(),
birthplace: "birthplace".to_string(),
birthdate: 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(),
study: "KB".to_string(),
parent_name: "parent_name".to_string(),
parent_surname: "parent_surname".to_string(),
parent_telephone: "parent_telephone".to_string(),
parent_email: "parent_email".to_string(),
};
let form = APPLICATION_DETAILS.lock().unwrap().clone();
ApplicationService::add_all_details(&db, candidate.application, form)
.await
@ -423,19 +409,6 @@ pub mod tests {
.unwrap();
let dec_details = enc_details.decrypt(dec_priv_key).await.ok().unwrap();
assert_eq!(dec_details.name, "name");
assert_eq!(dec_details.surname, "surname");
assert_eq!(dec_details.birthplace, "birthplace");
assert_eq!(dec_details.birthdate, NaiveDate::from_ymd(2000, 1, 1));
assert_eq!(dec_details.address, "address");
assert_eq!(dec_details.telephone, "telephone");
assert_eq!(dec_details.citizenship, "citizenship");
assert_eq!(dec_details.email, "email");
assert_eq!(dec_details.sex, "sex");
assert_eq!(dec_details.study, "KB");
assert_eq!(dec_details.parent_name, "parent_name");
assert_eq!(dec_details.parent_surname, "parent_surname");
assert_eq!(dec_details.parent_telephone, "parent_telephone");
assert_eq!(dec_details.parent_email, "parent_email");
assert_all_application_details(&dec_details);
}
}