mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-24 17:11:49 +00:00
Merge pull request #14 from EETagent/candidate_tests
Candidate service tests
This commit is contained in:
commit
0e510d0789
3 changed files with 97 additions and 4 deletions
|
|
@ -5,6 +5,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
portfolio-entity = { path = "../entity" }
|
portfolio-entity = { path = "../entity" }
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
chrono = "0.4.22"
|
chrono = "0.4.22"
|
||||||
|
|
@ -28,7 +29,4 @@ features = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = "1.21.2"
|
tokio = "1.21.2"
|
||||||
|
|
||||||
[features]
|
|
||||||
mock = ["sea-orm/mock"]
|
|
||||||
|
|
@ -8,3 +8,43 @@ impl Query {
|
||||||
Candidate::find_by_id(id).one(db).await
|
Candidate::find_by_id(id).one(db).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use sea_orm::{DbConn, Set, ActiveModelTrait};
|
||||||
|
use entity::candidate;
|
||||||
|
use sea_orm::{Schema, Database, DbBackend, sea_query::TableCreateStatement, ConnectionTrait};
|
||||||
|
|
||||||
|
use crate::Query;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
async fn get_memory_sqlite_connection() -> DbConn {
|
||||||
|
let base_url = "sqlite::memory:";
|
||||||
|
let db: DbConn = Database::connect(base_url).await.unwrap();
|
||||||
|
|
||||||
|
let schema = Schema::new(DbBackend::Sqlite);
|
||||||
|
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
|
||||||
|
db.execute(db.get_database_backend().build(&stmt)).await.unwrap();
|
||||||
|
db
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_find_candidate_by_id() {
|
||||||
|
let db = get_memory_sqlite_connection().await;
|
||||||
|
let candidate = candidate::ActiveModel {
|
||||||
|
application: Set(103158),
|
||||||
|
code: Set("test".to_string()),
|
||||||
|
public_key: Set("test".to_string()),
|
||||||
|
private_key: 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 candidate = Query::find_candidate_by_id(&db, candidate.application).await.unwrap();
|
||||||
|
assert!(candidate.is_some());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -41,3 +41,58 @@ impl CandidateService {
|
||||||
Ok(candidate)
|
Ok(candidate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use entity::candidate;
|
||||||
|
use sea_orm::{DbConn, Database, sea_query::TableCreateStatement, DbBackend, Schema, ConnectionTrait};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
use crate::{crypto, Mutation, services::candidate_service::CandidateService, token};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
async fn get_memory_sqlite_connection() -> DbConn {
|
||||||
|
let base_url = "sqlite::memory:";
|
||||||
|
let db: DbConn = Database::connect(base_url).await.unwrap();
|
||||||
|
|
||||||
|
let schema = Schema::new(DbBackend::Sqlite);
|
||||||
|
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
|
||||||
|
db.execute(db.get_database_backend().build(&stmt)).await.unwrap();
|
||||||
|
db
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_create_candidate() {
|
||||||
|
let db = get_memory_sqlite_connection().await;
|
||||||
|
|
||||||
|
let form = serde_json::from_value(json!({
|
||||||
|
"application": 5555555,
|
||||||
|
})).unwrap();
|
||||||
|
|
||||||
|
let candidate = Mutation::create_candidate(&db, form, &"Tajny_kod".to_string()).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(candidate.application, 5555555);
|
||||||
|
assert_ne!(candidate.code, "Tajny_kod".to_string());
|
||||||
|
assert!(crypto::verify_password("Tajny_kod", &*candidate.code).ok().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_candidate_jwt() {
|
||||||
|
let db = &get_memory_sqlite_connection().await;
|
||||||
|
|
||||||
|
let form = serde_json::from_value(json!({
|
||||||
|
"application": 5555555,
|
||||||
|
})).unwrap();
|
||||||
|
|
||||||
|
let candidate = Mutation::create_candidate(&db, form, &"Tajny_kod".to_string()).await.unwrap();
|
||||||
|
|
||||||
|
let jwt = CandidateService::login(db, 5555555, "Tajny_kod".to_string()).await.ok().unwrap();
|
||||||
|
|
||||||
|
let claims = token::decode_candidate_token(jwt).ok().unwrap().claims;
|
||||||
|
|
||||||
|
assert_eq!(claims.application_id, candidate.application);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue