mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-06 11:30:44 +00:00
feat: session tests
This commit is contained in:
parent
1ba4c68602
commit
fa74688ca3
1 changed files with 48 additions and 2 deletions
|
|
@ -7,7 +7,7 @@ use crate::{crypto::{self, hash_sha256}, Query, token::{generate_candidate_token
|
||||||
pub struct CandidateService;
|
pub struct CandidateService;
|
||||||
|
|
||||||
impl CandidateService {
|
impl CandidateService {
|
||||||
#[deprecated(note = "Use login instead")]
|
#[deprecated(note = "Use session login instead")]
|
||||||
pub async fn login(db: &DatabaseConnection, id: i32, password: String) -> Result<String, ServiceError> {
|
pub async fn login(db: &DatabaseConnection, id: i32, password: String) -> Result<String, ServiceError> {
|
||||||
let candidate = match Query::find_candidate_by_id(db, id).await {
|
let candidate = match Query::find_candidate_by_id(db, id).await {
|
||||||
Ok(candidate) => match candidate {
|
Ok(candidate) => match candidate {
|
||||||
|
|
@ -110,19 +110,23 @@ impl CandidateService {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use entity::candidate;
|
use entity::candidate;
|
||||||
use sea_orm::{DbConn, Database, sea_query::TableCreateStatement, DbBackend, Schema, ConnectionTrait};
|
use sea_orm::{DbConn, Database, sea_query::TableCreateStatement, DbBackend, Schema, ConnectionTrait, prelude::Uuid};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use crate::{crypto, Mutation, services::candidate_service::CandidateService, token};
|
use crate::{crypto, Mutation, services::candidate_service::CandidateService, token};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
async fn get_memory_sqlite_connection() -> DbConn {
|
async fn get_memory_sqlite_connection() -> DbConn {
|
||||||
|
use entity::session;
|
||||||
|
|
||||||
let base_url = "sqlite::memory:";
|
let base_url = "sqlite::memory:";
|
||||||
let db: DbConn = Database::connect(base_url).await.unwrap();
|
let db: DbConn = Database::connect(base_url).await.unwrap();
|
||||||
|
|
||||||
let schema = Schema::new(DbBackend::Sqlite);
|
let schema = Schema::new(DbBackend::Sqlite);
|
||||||
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
|
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
|
||||||
|
let stmt2: TableCreateStatement = schema.create_table_from_entity(session::Entity);
|
||||||
db.execute(db.get_database_backend().build(&stmt)).await.unwrap();
|
db.execute(db.get_database_backend().build(&stmt)).await.unwrap();
|
||||||
|
db.execute(db.get_database_backend().build(&stmt2)).await.unwrap();
|
||||||
db
|
db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,4 +162,46 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(claims.application_id, candidate.application);
|
assert_eq!(claims.application_id, candidate.application);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_candidate_session_correct_password() {
|
||||||
|
let db = &get_memory_sqlite_connection().await;
|
||||||
|
|
||||||
|
let form = serde_json::from_value(json!({
|
||||||
|
"application": 5555555,
|
||||||
|
})).unwrap();
|
||||||
|
|
||||||
|
Mutation::create_candidate(&db, form, &"Tajny_kod".to_string()).await.unwrap();
|
||||||
|
|
||||||
|
// correct password
|
||||||
|
let session = CandidateService::get_session(
|
||||||
|
db,
|
||||||
|
5555555,
|
||||||
|
"Tajny_kod".to_string()
|
||||||
|
)
|
||||||
|
.await.ok().unwrap();
|
||||||
|
// println!("{}", session.err().unwrap().1);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
CandidateService::auth_user_session(db, Uuid::parse_str(&session).unwrap())
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_candidate_session_incorrect_password() {
|
||||||
|
let db = &get_memory_sqlite_connection().await;
|
||||||
|
|
||||||
|
let form = serde_json::from_value(json!({
|
||||||
|
"application": 5555555,
|
||||||
|
})).unwrap();
|
||||||
|
|
||||||
|
let candidate_form = Mutation::create_candidate(&db, form, &"Tajny_kod".to_string()).await.unwrap();
|
||||||
|
|
||||||
|
// incorrect password
|
||||||
|
assert!(
|
||||||
|
CandidateService::get_session(db, candidate_form.application, "Spatny_kod".to_string()).await.is_err()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue