refactor: move admin tests to admin route

This commit is contained in:
EETagent 2022-11-21 17:49:22 +01:00
parent 004125b332
commit cdf75d3eec
3 changed files with 127 additions and 102 deletions

View file

@ -157,3 +157,62 @@ pub async fn get_candidate_portfolio(
Ok(portfolio) Ok(portfolio)
} }
#[cfg(test)]
mod tests {
use rocket::{local::blocking::Client, http::{Cookie, Status}};
use crate::test::tests::{test_client, ADMIN_PASSWORD, ADMIN_ID};
fn admin_login(client: &Client) -> (Cookie, Cookie) {
let response = client
.post("/admin/login")
.body(format!(
"{{
\"admin_id\": {},
\"password\": \"{}\"
}}",
ADMIN_ID, ADMIN_PASSWORD
))
.dispatch();
println!("{:?}", response);
(
response.cookies().get("id").unwrap().to_owned(),
response.cookies().get("key").unwrap().to_owned(),
)
}
fn create_candidate(
client: &Client,
cookies: (Cookie, Cookie),
id: i32,
pid: String,
) -> String {
let response = client
.post("/admin/create")
.body(format!(
"{{
\"application_id\": {},
\"personal_id_number\": \"{}\"
}}",
id, pid
))
.cookie(cookies.0)
.cookie(cookies.1)
.dispatch();
assert_eq!(response.status(), Status::Ok);
response.into_string().unwrap()
}
#[test]
fn test_create_candidate() {
let client = test_client().lock().unwrap();
let cookies = admin_login(&client);
let password = create_candidate(&client, cookies, 1031511, "0".to_string());
assert_eq!(password.len(), 8);
}
}

View file

@ -1,4 +1,4 @@
use std::net::{SocketAddr, IpAddr, Ipv4Addr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use portfolio_core::candidate_details::ApplicationDetails; use portfolio_core::candidate_details::ApplicationDetails;
use portfolio_core::services::application_service::ApplicationService; use portfolio_core::services::application_service::ApplicationService;
@ -85,18 +85,14 @@ pub async fn add_details(
#[post("/get_details")] #[post("/get_details")]
pub async fn get_details( pub async fn get_details(
conn: Connection<'_, Db>, conn: Connection<'_, Db>,
session: CandidateAuth session: CandidateAuth,
) -> Result<Json<ApplicationDetails>, Custom<String>> { ) -> Result<Json<ApplicationDetails>, Custom<String>> {
let db = conn.into_inner(); let db = conn.into_inner();
let private_key = session.get_private_key(); let private_key = session.get_private_key();
let candidate: entity::candidate::Model = session.into(); let candidate: entity::candidate::Model = session.into();
// let handle = tokio::spawn(async move { // let handle = tokio::spawn(async move {
let details = ApplicationService::decrypt_all_details(private_key, let details = ApplicationService::decrypt_all_details(private_key, db, candidate.application)
db,
candidate.application
)
.await .await
.map_err(|e| { .map_err(|e| {
Custom( Custom(
@ -287,10 +283,50 @@ pub async fn download_portfolio(session: CandidateAuth) -> Result<Vec<u8>, Custo
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use portfolio_core::{candidate_details::ApplicationDetails, sea_orm::prelude::Uuid, crypto}; use portfolio_core::{candidate_details::ApplicationDetails, crypto, sea_orm::prelude::Uuid};
use rocket::http::{Status, Cookie}; use rocket::{
http::{Cookie, Status},
local::blocking::Client,
};
use crate::test::tests::{create_candidate, admin_login, test_client, candidate_login, APPLICATION_ID}; use crate::test::tests::{test_client, APPLICATION_ID, CANDIDATE_PASSWORD, ADMIN_PASSWORD, ADMIN_ID};
fn candidate_login(client: &Client) -> (Cookie, Cookie) {
let response = client
.post("/candidate/login")
.body(format!(
"{{
\"application_id\": {},
\"password\": \"{}\"
}}",
APPLICATION_ID, CANDIDATE_PASSWORD
))
.dispatch();
(
response.cookies().get("id").unwrap().to_owned(),
response.cookies().get("key").unwrap().to_owned(),
)
}
fn admin_login(client: &Client) -> (Cookie, Cookie) {
let response = client
.post("/admin/login")
.body(format!(
"{{
\"admin_id\": {},
\"password\": \"{}\"
}}",
ADMIN_ID, ADMIN_PASSWORD
))
.dispatch();
println!("{:?}", response);
(
response.cookies().get("id").unwrap().to_owned(),
response.cookies().get("key").unwrap().to_owned(),
)
}
const CANDIDATE_DETAILS: &'static str = "{ const CANDIDATE_DETAILS: &'static str = "{
\"name\": \"idk\", \"name\": \"idk\",
@ -315,15 +351,6 @@ mod tests {
let _response = candidate_login(&client); let _response = candidate_login(&client);
} }
#[test]
fn test_create_candidate() {
let client = test_client().lock().unwrap();
let cookies = admin_login(&client);
let password = create_candidate(&client, cookies, 1031511, "0".to_string());
assert_eq!(password.len(), 8);
}
#[test] #[test]
fn test_auth_candidate() { fn test_auth_candidate() {
let client = test_client().lock().unwrap(); let client = test_client().lock().unwrap();
@ -362,7 +389,8 @@ mod tests {
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
let details_resp: ApplicationDetails = serde_json::from_str(&response.into_string().unwrap()).unwrap(); let details_resp: ApplicationDetails =
serde_json::from_str(&response.into_string().unwrap()).unwrap();
assert_eq!(details_orig, details_resp); assert_eq!(details_orig, details_resp);
} }

View file

@ -9,7 +9,6 @@ pub mod tests {
services::application_service::ApplicationService, services::application_service::ApplicationService,
}; };
use rocket::{ use rocket::{
http::{Cookie, Status},
local::blocking::Client, local::blocking::Client,
}; };
use std::sync::Mutex; use std::sync::Mutex;
@ -60,65 +59,4 @@ pub mod tests {
Mutex::from(Client::tracked(rocket).expect("valid rocket instance")) Mutex::from(Client::tracked(rocket).expect("valid rocket instance"))
}) })
} }
pub fn candidate_login(client: &Client) -> (Cookie, Cookie) {
let response = client
.post("/candidate/login")
.body(format!(
"{{
\"application_id\": {},
\"password\": \"{}\"
}}",
APPLICATION_ID, CANDIDATE_PASSWORD
))
.dispatch();
(
response.cookies().get("id").unwrap().to_owned(),
response.cookies().get("key").unwrap().to_owned(),
)
}
pub fn admin_login(client: &Client) -> (Cookie, Cookie) {
let response = client
.post("/admin/login")
.body(format!(
"{{
\"admin_id\": {},
\"password\": \"{}\"
}}",
ADMIN_ID, ADMIN_PASSWORD
))
.dispatch();
println!("{:?}", response);
(
response.cookies().get("id").unwrap().to_owned(),
response.cookies().get("key").unwrap().to_owned(),
)
}
pub fn create_candidate(
client: &Client,
cookies: (Cookie, Cookie),
id: i32,
pid: String,
) -> String {
let response = client
.post("/admin/create")
.body(format!(
"{{
\"application_id\": {},
\"personal_id_number\": \"{}\"
}}",
id, pid
))
.cookie(cookies.0)
.cookie(cookies.1)
.dispatch();
assert_eq!(response.status(), Status::Ok);
response.into_string().unwrap()
}
} }