498 lines
13 KiB
Rust
498 lines
13 KiB
Rust
// Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use std::str::FromStr;
|
|
use std::sync::Arc;
|
|
|
|
use actix_web::cookie::Cookie;
|
|
use actix_web::test;
|
|
use actix_web::{
|
|
body::{BoxBody, EitherBody},
|
|
dev::ServiceResponse,
|
|
error::ResponseError,
|
|
http::StatusCode,
|
|
};
|
|
use mktemp::Temp;
|
|
|
|
use lazy_static::lazy_static;
|
|
use serde::Serialize;
|
|
use sqlx::migrate::MigrateDatabase;
|
|
use sqlx::types::Uuid;
|
|
|
|
use super::*;
|
|
use crate::api::v1::admin::{
|
|
auth::runners::{Login, Register},
|
|
campaigns::{AddCapmaign, AddCapmaignResp, ListCampaignResp},
|
|
};
|
|
use crate::api::v1::bench::{Bench, BenchConfig, Submission, SubmissionProof};
|
|
use crate::data::Data;
|
|
use crate::errors::*;
|
|
use crate::V1_API_ROUTES;
|
|
|
|
pub async fn get_test_data_with_mcaptcha_client(
|
|
) -> (Arc<Data>, crate::mcaptcha::tests::TestClient) {
|
|
let mut settings = Settings::new().unwrap();
|
|
let tmp_dir = Temp::new_dir().unwrap();
|
|
settings.publish.dir = tmp_dir.join("base_path").to_str().unwrap().into();
|
|
settings.allow_registration = true;
|
|
let test_mcaptcha = crate::mcaptcha::tests::TestClient::default();
|
|
let data = Data::new(settings, Box::new(test_mcaptcha.clone())).await;
|
|
db::migrate_db(&data.db).await.unwrap();
|
|
(data, test_mcaptcha)
|
|
}
|
|
|
|
pub async fn get_test_data() -> Arc<Data> {
|
|
let mut settings = Settings::new().unwrap();
|
|
let tmp_dir = Temp::new_dir().unwrap();
|
|
settings.publish.dir = tmp_dir.join("base_path").to_str().unwrap().into();
|
|
settings.allow_registration = true;
|
|
let mut url = url::Url::parse(&settings.database.url).unwrap();
|
|
url.set_path(&crate::api::v1::get_random(16));
|
|
settings.database.url = url.to_string();
|
|
sqlx::Postgres::create_database(&settings.database.url)
|
|
.await
|
|
.unwrap();
|
|
let test_mcaptcha = Box::new(crate::mcaptcha::tests::TestClient::default());
|
|
let data = Data::new(settings, test_mcaptcha).await;
|
|
|
|
db::migrate_db(&data.db).await.unwrap();
|
|
data
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! get_cookie {
|
|
($resp:expr) => {
|
|
$resp.response().cookies().next().unwrap().to_owned()
|
|
};
|
|
}
|
|
|
|
pub async fn delete_user(name: &str, data: &Data) {
|
|
let r = sqlx::query!("DELETE FROM survey_admins WHERE name = ($1)", name,)
|
|
.execute(&data.db)
|
|
.await;
|
|
println!();
|
|
println!();
|
|
println!();
|
|
println!("Deleting user: {:?}", &r);
|
|
}
|
|
|
|
#[allow(dead_code, clippy::upper_case_acronyms)]
|
|
pub struct FORM;
|
|
|
|
#[macro_export]
|
|
macro_rules! post_request {
|
|
($uri:expr) => {
|
|
test::TestRequest::post().uri($uri)
|
|
};
|
|
|
|
($serializable:expr, $uri:expr) => {
|
|
test::TestRequest::post()
|
|
.uri($uri)
|
|
.insert_header((actix_web::http::header::CONTENT_TYPE, "application/json"))
|
|
.set_payload(serde_json::to_string($serializable).unwrap())
|
|
};
|
|
|
|
($serializable:expr, $uri:expr, FORM) => {
|
|
test::TestRequest::post().uri($uri).set_form($serializable)
|
|
};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! get_works {
|
|
($app:expr,$route:expr ) => {
|
|
let list_sitekey_resp =
|
|
test::call_service(&$app, test::TestRequest::get().uri($route).to_request())
|
|
.await;
|
|
assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
|
|
};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! get_request {
|
|
($app:expr,$route:expr ) => {
|
|
test::call_service(&$app, test::TestRequest::get().uri($route).to_request())
|
|
.await
|
|
};
|
|
|
|
($app:expr, $route:expr, $cookies:expr) => {
|
|
test::call_service(
|
|
&$app,
|
|
test::TestRequest::get()
|
|
.uri($route)
|
|
.cookie($cookies)
|
|
.to_request(),
|
|
)
|
|
.await
|
|
};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! get_app {
|
|
("APP", $settings:expr) => {
|
|
actix_web::App::new()
|
|
.app_data($crate::get_json_err())
|
|
.wrap($crate::get_identity_service(&$settings))
|
|
.wrap($crate::get_survey_session(&$settings))
|
|
.wrap(actix_web::middleware::NormalizePath::new(
|
|
actix_web::middleware::TrailingSlash::Trim,
|
|
))
|
|
.service(
|
|
Files::new("/download", &$settings.publish.dir).show_files_listing(),
|
|
)
|
|
.configure($crate::services)
|
|
};
|
|
|
|
// ($settings:expr) => {
|
|
// test::init_service(get_app!("APP"))
|
|
// };
|
|
($data:expr) => {
|
|
actix_web::test::init_service(
|
|
get_app!("APP", $data.settings)
|
|
.app_data(actix_web::web::Data::new($data.clone())),
|
|
)
|
|
};
|
|
}
|
|
|
|
/// register and signin utility
|
|
pub async fn register_and_signin(
|
|
data: &Arc<Data>,
|
|
name: &str,
|
|
email: &str,
|
|
password: &str,
|
|
) -> (Login, ServiceResponse<EitherBody<BoxBody>>) {
|
|
register(data, name, email, password).await;
|
|
signin(data, name, password).await
|
|
}
|
|
|
|
/// register utility
|
|
pub async fn register(data: &Arc<Data>, name: &str, email: &str, password: &str) {
|
|
let app = get_app!(data).await;
|
|
|
|
// 1. Register
|
|
let msg = Register {
|
|
username: name.into(),
|
|
password: password.into(),
|
|
confirm_password: password.into(),
|
|
email: Some(email.into()),
|
|
};
|
|
let resp = test::call_service(
|
|
&app,
|
|
post_request!(&msg, V1_API_ROUTES.admin.auth.register).to_request(),
|
|
)
|
|
.await;
|
|
if resp.status() != StatusCode::OK {
|
|
let resp_err: ErrorToResponse = test::read_body_json(resp).await;
|
|
panic!("{}", resp_err.error);
|
|
} else {
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
}
|
|
}
|
|
|
|
/// signin util
|
|
pub async fn signin(
|
|
data: &Arc<Data>,
|
|
name: &str,
|
|
password: &str,
|
|
) -> (Login, ServiceResponse<EitherBody<BoxBody>>) {
|
|
let app = get_app!(data.clone()).await;
|
|
|
|
// 2. signin
|
|
let creds = Login {
|
|
login: name.into(),
|
|
password: password.into(),
|
|
};
|
|
let signin_resp = test::call_service(
|
|
&app,
|
|
post_request!(&creds, V1_API_ROUTES.admin.auth.login).to_request(),
|
|
)
|
|
.await;
|
|
assert_eq!(signin_resp.status(), StatusCode::OK);
|
|
(creds, signin_resp)
|
|
}
|
|
|
|
/// pub duplicate test
|
|
pub async fn bad_post_req_test<T: Serialize>(
|
|
data: &Arc<Data>,
|
|
name: &str,
|
|
password: &str,
|
|
url: &str,
|
|
payload: &T,
|
|
err: ServiceError,
|
|
) {
|
|
let (_, signin_resp) = signin(data, name, password).await;
|
|
let cookies = get_cookie!(signin_resp);
|
|
let app = get_app!(data).await;
|
|
|
|
let resp = test::call_service(
|
|
&app,
|
|
post_request!(&payload, url)
|
|
.cookie(cookies.clone())
|
|
.to_request(),
|
|
)
|
|
.await;
|
|
assert_eq!(resp.status(), err.status_code());
|
|
let resp_err: ErrorToResponse = test::read_body_json(resp).await;
|
|
//println!("{}", txt.error);
|
|
assert_eq!(resp_err.error, format!("{}", err));
|
|
}
|
|
|
|
/// bad post req test without payload
|
|
pub async fn bad_post_req_test_witout_payload(
|
|
data: &Arc<Data>,
|
|
name: &str,
|
|
password: &str,
|
|
url: &str,
|
|
err: ServiceError,
|
|
) {
|
|
let (_, signin_resp) = signin(data, name, password).await;
|
|
let cookies = get_cookie!(signin_resp);
|
|
let app = get_app!(data).await;
|
|
|
|
let resp = test::call_service(
|
|
&app,
|
|
post_request!(url).cookie(cookies.clone()).to_request(),
|
|
)
|
|
.await;
|
|
assert_eq!(resp.status(), err.status_code());
|
|
let resp_err: ErrorToResponse = test::read_body_json(resp).await;
|
|
//println!("{}", txt.error);
|
|
assert_eq!(resp_err.error, format!("{}", err));
|
|
}
|
|
|
|
pub const DIFFICULTIES: [i32; 5] = [1, 2, 3, 4, 5];
|
|
|
|
pub async fn create_new_campaign(
|
|
campaign_name: &str,
|
|
data: Arc<Data>,
|
|
cookies: Cookie<'_>,
|
|
) -> AddCapmaignResp {
|
|
let new = AddCapmaign {
|
|
name: campaign_name.into(),
|
|
difficulties: DIFFICULTIES.into(),
|
|
};
|
|
|
|
let app = get_app!(data).await;
|
|
let new_resp = test::call_service(
|
|
&app,
|
|
post_request!(&new, crate::V1_API_ROUTES.admin.campaign.add)
|
|
.cookie(cookies)
|
|
.to_request(),
|
|
)
|
|
.await;
|
|
assert_eq!(new_resp.status(), StatusCode::OK);
|
|
let uuid: AddCapmaignResp = test::read_body_json(new_resp).await;
|
|
uuid
|
|
}
|
|
|
|
pub async fn get_survey_user(data: Arc<Data>) -> ServiceResponse<EitherBody<BoxBody>> {
|
|
let app = get_app!(data).await;
|
|
let signin_resp = test::call_service(
|
|
&app,
|
|
test::TestRequest::get()
|
|
.uri(V1_API_ROUTES.benches.register)
|
|
.to_request(),
|
|
)
|
|
.await;
|
|
|
|
assert_eq!(signin_resp.status(), StatusCode::OK);
|
|
signin_resp
|
|
}
|
|
|
|
pub async fn get_campaign_config(
|
|
campaign: &AddCapmaignResp,
|
|
data: Arc<Data>,
|
|
cookies: Cookie<'_>,
|
|
) -> BenchConfig {
|
|
let app = get_app!(data).await;
|
|
let route = V1_API_ROUTES
|
|
.benches
|
|
.fetch_routes(&campaign.campaign_id.to_string());
|
|
let new_resp = test::call_service(
|
|
&app,
|
|
test::TestRequest::get()
|
|
.uri(&route)
|
|
.cookie(cookies)
|
|
.to_request(),
|
|
)
|
|
.await;
|
|
assert_eq!(new_resp.status(), StatusCode::OK);
|
|
test::read_body_json(new_resp).await
|
|
}
|
|
|
|
pub async fn delete_campaign(
|
|
camapign: &AddCapmaignResp,
|
|
data: Arc<Data>,
|
|
cookies: Cookie<'_>,
|
|
) {
|
|
let del_route = V1_API_ROUTES
|
|
.admin
|
|
.campaign
|
|
.get_delete_route(&camapign.campaign_id);
|
|
let app = get_app!(data).await;
|
|
let del_resp =
|
|
test::call_service(&app, post_request!(&del_route).cookie(cookies).to_request())
|
|
.await;
|
|
assert_eq!(del_resp.status(), StatusCode::OK);
|
|
}
|
|
|
|
pub async fn list_campaings(
|
|
data: Arc<Data>,
|
|
cookies: Cookie<'_>,
|
|
) -> Vec<ListCampaignResp> {
|
|
let app = get_app!(data).await;
|
|
let list_resp = test::call_service(
|
|
&app,
|
|
post_request!(crate::V1_API_ROUTES.admin.campaign.list)
|
|
.cookie(cookies)
|
|
.to_request(),
|
|
)
|
|
.await;
|
|
assert_eq!(list_resp.status(), StatusCode::OK);
|
|
test::read_body_json(list_resp).await
|
|
}
|
|
|
|
pub async fn submit_bench(
|
|
payload: &Submission,
|
|
campaign: &AddCapmaignResp,
|
|
cookies: Cookie<'_>,
|
|
data: Arc<Data>,
|
|
) -> SubmissionProof {
|
|
let route = V1_API_ROUTES.benches.submit_route(&campaign.campaign_id);
|
|
let app = get_app!(data).await;
|
|
let add_feedback_resp = test::call_service(
|
|
&app,
|
|
post_request!(&payload, &route).cookie(cookies).to_request(),
|
|
)
|
|
.await;
|
|
if add_feedback_resp.status() != StatusCode::OK {
|
|
let headers = add_feedback_resp.headers();
|
|
println!("{:#?}", headers);
|
|
}
|
|
|
|
assert_eq!(add_feedback_resp.status(), StatusCode::OK);
|
|
|
|
let proof: SubmissionProof = test::read_body_json(add_feedback_resp).await;
|
|
|
|
let survey_user_id = Uuid::from_str(&proof.token).unwrap();
|
|
let proof_uuid = Uuid::from_str(&proof.proof).unwrap();
|
|
|
|
struct Exists {
|
|
exists: Option<bool>,
|
|
}
|
|
let res = sqlx::query_as!(
|
|
Exists,
|
|
"SELECT EXISTS (
|
|
SELECT 1 from survey_responses
|
|
WHERE device_software_recognised = $1
|
|
AND device_user_provided = $2
|
|
AND THREADS = $3
|
|
AND user_id = $4
|
|
);",
|
|
&payload.device_software_recognised,
|
|
&payload.device_user_provided,
|
|
payload.threads,
|
|
&survey_user_id,
|
|
)
|
|
.fetch_one(&data.db)
|
|
.await
|
|
.unwrap();
|
|
assert!(res.exists.as_ref().unwrap());
|
|
|
|
let res = sqlx::query_as!(
|
|
Exists,
|
|
"SELECT EXISTS (
|
|
SELECT 1 from survey_response_tokens
|
|
WHERE id = $1
|
|
);",
|
|
&proof_uuid
|
|
)
|
|
.fetch_one(&data.db)
|
|
.await
|
|
.unwrap();
|
|
assert!(res.exists.as_ref().unwrap());
|
|
|
|
struct ID {
|
|
id: i32,
|
|
}
|
|
|
|
let resp_id = sqlx::query_as!(
|
|
ID,
|
|
"SELECT ID
|
|
FROM survey_responses
|
|
WHERE
|
|
user_id = $1
|
|
AND
|
|
device_software_recognised = $2;",
|
|
&survey_user_id,
|
|
&payload.device_software_recognised
|
|
)
|
|
.fetch_one(&data.db)
|
|
.await
|
|
.unwrap();
|
|
|
|
for bench in payload.benches.iter() {
|
|
let res = sqlx::query_as!(
|
|
Exists,
|
|
"SELECT EXISTS(
|
|
SELECT 1 FROM survey_benches
|
|
WHERE resp_id = $1
|
|
AND difficulty = $2
|
|
AND duration = $3);",
|
|
&resp_id.id,
|
|
&bench.difficulty,
|
|
bench.duration
|
|
)
|
|
.fetch_one(&data.db)
|
|
.await
|
|
.unwrap();
|
|
assert!(res.exists.as_ref().unwrap());
|
|
}
|
|
|
|
proof
|
|
}
|
|
//
|
|
//pub async fn get_feedback(
|
|
// campaign: &CreateResp,
|
|
// data: Arc<Data>,
|
|
// cookies: Cookie<'_>,
|
|
//) -> GetFeedbackResp {
|
|
// let get_feedback_route = V1_API_ROUTES.campaign.get_feedback_route(&campaign.uuid);
|
|
// let app = get_app!(data).await;
|
|
//
|
|
// let get_feedback_resp = test::call_service(
|
|
// &app,
|
|
// post_request!(&get_feedback_route)
|
|
// .cookie(cookies)
|
|
// .to_request(),
|
|
// )
|
|
// .await;
|
|
// assert_eq!(get_feedback_resp.status(), StatusCode::OK);
|
|
// test::read_body_json(get_feedback_resp).await
|
|
//}
|
|
|
|
lazy_static! {
|
|
pub static ref BENCHES: Vec<Bench> = vec![
|
|
Bench {
|
|
difficulty: 1,
|
|
duration: 1.00,
|
|
},
|
|
Bench {
|
|
difficulty: 2,
|
|
duration: 2.00,
|
|
},
|
|
Bench {
|
|
difficulty: 3,
|
|
duration: 3.00,
|
|
},
|
|
Bench {
|
|
difficulty: 4,
|
|
duration: 4.00,
|
|
},
|
|
Bench {
|
|
difficulty: 5,
|
|
duration: 5.00,
|
|
},
|
|
];
|
|
}
|