mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-24 12:35:31 +00:00
commit
50559ee9a0
12 changed files with 598 additions and 61 deletions
|
|
@ -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<Self, ServiceError> {
|
||||
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<String, ServiceError> {
|
||||
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<Option<String>> for EncryptedString {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Option<NaiveDate>> for EncryptedString { // TODO: take a look at this
|
||||
impl TryFrom<Option<NaiveDate>> for EncryptedString {
|
||||
// TODO: take a look at this
|
||||
type Error = ServiceError;
|
||||
|
||||
fn try_from(d: Option<NaiveDate>) -> Result<Self, Self::Error> {
|
||||
|
|
@ -79,9 +80,12 @@ pub struct EncryptedApplicationDetails {
|
|||
}
|
||||
|
||||
impl EncryptedApplicationDetails {
|
||||
pub async fn new(form: ApplicationDetails, recipients: Vec<&str>) -> Result<EncryptedApplicationDetails, ServiceError> {
|
||||
pub async fn new(
|
||||
form: ApplicationDetails,
|
||||
recipients: Vec<&str>,
|
||||
) -> Result<EncryptedApplicationDetails, ServiceError> {
|
||||
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<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
|
||||
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<Self, Self::Error> {
|
||||
fn try_from(
|
||||
(candidate, parent): (candidate::Model, parent::Model),
|
||||
) -> Result<Self, Self::Error> {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
#[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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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::Model, DbErr> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,3 +37,8 @@ impl Mutation {
|
|||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// TODO: Testy
|
||||
}
|
||||
|
|
@ -11,8 +11,70 @@ impl Query {
|
|||
pub async fn get_all_admin_public_keys(db: &DbConn) -> Result<Vec<String>, 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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -14,4 +14,48 @@ impl Query {
|
|||
|
||||
Entity::find_by_id(application_id).one(db).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,4 +24,106 @@ impl Query {
|
|||
.all(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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);
|
||||
} */
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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:";
|
||||
|
|
@ -27,4 +25,14 @@ pub async fn get_memory_sqlite_connection() -> 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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue