refactor: tests code cleanup

This commit is contained in:
Sebastian Pravda 2023-01-15 19:01:07 +01:00
parent 71428a72d5
commit b730c5def5
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
9 changed files with 57 additions and 83 deletions

View file

@ -94,7 +94,7 @@ pub async fn create_candidate(
let plain_text_password = random_12_char_string();
let application = ApplicationService::create(&private_key, &db, form.application_id, &plain_text_password, form.personal_id_number.clone())
ApplicationService::create(&private_key, &db, form.application_id, &plain_text_password, form.personal_id_number.clone())
.await
.map_err(to_custom_error)?;

View file

@ -42,22 +42,13 @@ pub mod tests {
.await
.unwrap();
let application = ApplicationService::create(
ApplicationService::create(
&"".to_string(),
db,
APPLICATION_ID,
&CANDIDATE_PASSWORD.to_string(),
PERSONAL_ID_NUMBER.to_string())
.await.unwrap();
/* ApplicationService::create_candidate_with_parent(
db,
application,
&CANDIDATE_PASSWORD.to_string(),
PERSONAL_ID_NUMBER.to_string(),
)
.await
.unwrap(); */
}
pub fn test_client() -> &'static Mutex<Client> {

View file

@ -1,6 +1,6 @@
use crate::{Mutation, models::candidate_details::{EncryptedCandidateDetails}};
use ::entity::{candidate::{self}, application};
use ::entity::candidate;
use log::{info, warn};
use sea_orm::*;

View file

@ -1,11 +1,9 @@
use chrono::NaiveDate;
use entity::{application, candidate};
use sea_orm::FromQueryResult;
use serde::{Deserialize, Serialize};
use crate::{
database::query::candidate::CandidateResult, error::ServiceError,
services::portfolio_service::SubmissionProgress,
error::ServiceError,
};
use super::candidate_details::{EncryptedString, EncryptedCandidateDetails};

View file

@ -104,15 +104,35 @@ impl AuthenticableTrait for AdminService {
}
#[cfg(test)]
mod admin_tests {
use chrono::Local;
pub mod admin_tests {
use chrono::{Local, Utc};
use entity::admin;
use sea_orm::{Set, ActiveModelTrait};
use crate::{utils::db::get_memory_sqlite_connection, error::ServiceError};
use super::*;
pub async fn create_admin(db: &DbConn) -> admin::Model {
let password = "admin".to_string();
let (pubkey, priv_key) = crypto::create_identity();
let enc_priv_key = crypto::encrypt_password(priv_key, password).await.unwrap();
let admin = admin::ActiveModel {
name: Set("admin".to_string()),
public_key: Set(pubkey),
private_key: Set(enc_priv_key),
password: Set("admin".to_string()),
created_at: Set(Utc::now().naive_utc()),
updated_at: Set(Utc::now().naive_utc()),
..Default::default()
}
.insert(db)
.await
.unwrap();
admin
}
#[tokio::test]
async fn test_admin_login() -> Result<(), ServiceError> {

View file

@ -3,9 +3,9 @@ use chrono::Duration;
use entity::{candidate, parent, application, session};
use sea_orm::{DbConn, prelude::Uuid, IntoActiveModel};
use crate::{error::ServiceError, Query, utils::db::get_recipients, models::candidate_details::{EncryptedApplicationDetails}, models::{candidate::{ApplicationDetails, CreateCandidateResponse}, candidate_details::EncryptedString, auth::AuthenticableTrait, application::ApplicationResponse}, Mutation, crypto::{hash_password, self}};
use crate::{error::ServiceError, Query, utils::db::get_recipients, models::candidate_details::EncryptedApplicationDetails, models::{candidate::{ApplicationDetails, CreateCandidateResponse}, candidate_details::EncryptedString, auth::AuthenticableTrait, application::ApplicationResponse}, Mutation, crypto::{hash_password, self}};
use super::{parent_service::ParentService, candidate_service::CandidateService, session_service::SessionService, portfolio_service::PortfolioService};
use super::{parent_service::ParentService, candidate_service::CandidateService, session_service::SessionService};
const FIELD_OF_STUDY_PREFIXES: [&str; 3] = ["101", "102", "103"];
@ -419,10 +419,11 @@ impl AuthenticableTrait for ApplicationService {
}
}
mod tests {
use crate::{utils::db::get_memory_sqlite_connection, services::{application_service::ApplicationService}, crypto};
#[cfg(test)]
mod application_tests {
use crate::{services::{application_service::ApplicationService, candidate_service::tests::put_user_data}, utils::db::get_memory_sqlite_connection, crypto, models::auth::AuthenticableTrait};
use crate::services::admin_service::admin_tests::create_admin;
const APPLICATION_ID: i32 = 103151;
#[tokio::test]
async fn test_application_id_validation() {
assert!(ApplicationService::is_application_id_valid(101_101));
@ -435,29 +436,28 @@ mod tests {
}
// TODO
/* #[tokio::test]
#[tokio::test]
async fn test_password_reset() {
let db = get_memory_sqlite_connection().await;
let admin = create_admin(&db).await;
let (candidate, _parent) = put_user_data(&db).await;
let (application, _, _) = put_user_data(&db).await;
let private_key = crypto::decrypt_password(admin.private_key, "admin".to_string()).await.unwrap();
assert!(
CandidateService::login(&db, candidate.application, "test".to_string(), "127.0.0.1".to_string()).await.is_ok()
ApplicationService::login(&db, application.id, "test".to_string(), "127.0.0.1".to_string()).await.is_ok()
);
let new_password = CandidateService::reset_password(private_key, &db, candidate.application).await.unwrap().password;
let new_password = ApplicationService::reset_password(private_key, &db, application.id).await.unwrap().password;
assert!(
CandidateService::login(&db, candidate.application, "test".to_string(), "127.0.0.1".to_string()).await.is_err()
ApplicationService::login(&db, application.id, "test".to_string(), "127.0.0.1".to_string()).await.is_err()
);
assert!(
CandidateService::login(&db, candidate.application, new_password, "127.0.0.1".to_string()).await.is_ok()
ApplicationService::login(&db, application.id, new_password, "127.0.0.1".to_string()).await.is_ok()
);
} */
}
#[tokio::test]
async fn test_encrypt_decrypt_private_key_with_passphrase() {

View file

@ -63,11 +63,12 @@ pub mod tests {
use sea_orm::DbConn;
use crate::models::candidate_details::tests::assert_all_application_details;
use crate::services::admin_service::admin_tests::create_admin;
use crate::utils::db::get_memory_sqlite_connection;
use crate::{crypto};
use crate::models::candidate_details::EncryptedApplicationDetails;
use entity::{application, candidate, parent, admin};
use entity::{application, candidate, parent};
use crate::services::application_service::ApplicationService;
@ -87,34 +88,9 @@ pub mod tests {
assert_eq!(candidates.len(), 1);
}
#[cfg(test)]
async fn create_admin(db: &DbConn) -> admin::Model {
use chrono::Utc;
use sea_orm::{Set, ActiveModelTrait};
let password = "admin".to_string();
let (pubkey, priv_key) = crypto::create_identity();
let enc_priv_key = crypto::encrypt_password(priv_key, password).await.unwrap();
let admin = admin::ActiveModel {
name: Set("admin".to_string()),
public_key: Set(pubkey),
private_key: Set(enc_priv_key),
password: Set("admin".to_string()),
created_at: Set(Utc::now().naive_utc()),
updated_at: Set(Utc::now().naive_utc()),
..Default::default()
}
.insert(db)
.await
.unwrap();
admin
}
#[cfg(test)]
pub async fn put_user_data(db: &DbConn) -> (application::Model, candidate::Model, Vec<parent::Model>) {
use crate::models::candidate_details::tests::APPLICATION_DETAILS;
use crate::{models::candidate_details::tests::APPLICATION_DETAILS, services::parent_service::ParentService};
let plain_text_password = "test".to_string();
let application = ApplicationService::create(
@ -126,6 +102,7 @@ pub mod tests {
).await.unwrap();
let candidate= ApplicationService::find_related_candidate(db, &application).await.unwrap();
ParentService::create(db, candidate.id).await.unwrap();
let form = APPLICATION_DETAILS.lock().unwrap().clone();

View file

@ -54,7 +54,7 @@ mod tests {
use once_cell::sync::Lazy;
use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails, CandidateDetails}}, services::{candidate_service::CandidateService}};
use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails, CandidateDetails}, candidate_details::EncryptedApplicationDetails}, services::{candidate_service::{CandidateService, tests::put_user_data}, application_service::ApplicationService, parent_service::ParentService}, crypto};
pub static APPLICATION_DETAILS_TWO_PARENTS: Lazy<Mutex<ApplicationDetails>> = Lazy::new(||
Mutex::new(ApplicationDetails {
@ -95,26 +95,18 @@ mod tests {
super::ParentService::create(&db, candidate.id).await.unwrap();
}
/* #[tokio::test]
#[tokio::test]
async fn add_parent_details_test() {
let db = get_memory_sqlite_connection().await;
let plain_text_password = "test".to_string();
let application = ApplicationService::create(&db, 103151, &plain_text_password, "0000001111".to_string()).await.unwrap();
let (application, candidate, _parent) = ApplicationService::create_candidate_with_parent(
&db,
application,
&plain_text_password,
"".to_string(),
)
.await
.ok()
.unwrap();
// let application = ApplicationService::create(&"".to_string(), &db, 103100, &plain_text_password, "".to_string()).await.unwrap();
let (application, candidate, _) = put_user_data(&db).await;
ParentService::create(&db, 103101).await.unwrap();
ParentService::create(&db, candidate.id).await.unwrap();
let form = APPLICATION_DETAILS_TWO_PARENTS.lock().unwrap().clone();
let (candidate, parents) = ApplicationService::add_all_details(&db, &application.public_key, candidate, &form)
let (candidate, parents) = ApplicationService::add_all_details(&db, &application, candidate, &form)
.await
.unwrap();
@ -134,8 +126,7 @@ mod tests {
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.candidate.personal_id_number, "0000001111".to_string());
assert_eq!(dec_details.parents.len(), form.parents.len());
for i in 0..dec_details.parents.len() {
@ -144,8 +135,5 @@ mod tests {
assert_eq!(dec_details.parents[i].telephone, form.parents[i].telephone);
assert_eq!(dec_details.parents[i].email, form.parents[i].email);
}
} */
}
}

View file

@ -614,7 +614,7 @@ mod tests {
#[serial]
async fn test_add_portfolio() {
let db = get_memory_sqlite_connection().await;
let (application, candidate, _) = put_user_data(&db).await;
let (_, candidate, _) = put_user_data(&db).await;
let (temp_dir, application_dir, _) = create_data_store_temp_dir(candidate.id).await;
@ -633,7 +633,7 @@ mod tests {
#[serial]
async fn test_delete_portfolio() {
let db = get_memory_sqlite_connection().await;
let (application, candidate, _) = put_user_data(&db).await;
let (_, candidate, _) = put_user_data(&db).await;
let (temp_dir, application_dir, _) = create_data_store_temp_dir(candidate.id).await;
@ -657,7 +657,7 @@ mod tests {
async fn test_is_portfolio_submitted() {
let db = get_memory_sqlite_connection().await;
let (application, candidate, _) = put_user_data(&db).await;
let (_, candidate, _) = put_user_data(&db).await;
let (temp_dir, _, _) = create_data_store_temp_dir(candidate.id).await;
PortfolioService::add_cover_letter_to_cache(candidate.id, vec![0]).await.unwrap();