mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-24 12:35:31 +00:00
feat: query/mutation tests, lint warnings removal
This commit is contained in:
parent
4ef80e88d3
commit
225583f552
10 changed files with 419 additions and 37 deletions
|
|
@ -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);
|
||||
} */
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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:";
|
||||
|
|
|
|||
Loading…
Reference in a new issue