/* * Copyright (C) 2021 Aravinth Manivannan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ use std::borrow::Cow; use actix_identity::Identity; use actix_web::{web, HttpResponse, Responder}; use serde::{Deserialize, Serialize}; use sqlx::types::time::OffsetDateTime; use uuid::Uuid; use super::{get_admin_check_login, get_uuid}; use crate::api::v1::bench::Bench; use crate::errors::*; use crate::AppData; pub mod routes { use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct Campaign { pub add: &'static str, pub delete: &'static str, // pub get_feedback: &'static str, pub list: &'static str, pub results: &'static str, } impl Campaign { pub const fn new() -> Campaign { let add = "/admin/api/v1/campaign/add"; let delete = "/admin/api/v1/campaign/{uuid}/delete"; // let get_feedback = "/api/v1/campaign/{uuid}/feedback"; let list = "/admin/api/v1/campaign/list"; let results = "/admin/api/v1/campaign/{uuid}/results"; Campaign { add, delete, list, results, } } // pub fn get_benches_route(&self, campaign_id: &str) -> String { // self.get_feedback.replace("{uuid}", &campaign_id) // } pub fn get_delete_route(&self, campaign_id: &str) -> String { self.delete.replace("{uuid}", campaign_id) } pub fn get_results_route(&self, campaign_id: &str) -> String { self.results.replace("{uuid}", campaign_id) } } } pub mod runners { use futures::try_join; use crate::api::v1::bench::Bench; use super::*; pub async fn add_runner( username: &str, payload: &mut AddCapmaign, data: &AppData, ) -> ServiceResult { let mut uuid; let now = OffsetDateTime::now_utc(); payload.difficulties.sort_unstable(); loop { uuid = get_uuid(); let res = sqlx::query!( " INSERT INTO survey_campaigns ( user_id, ID, name, difficulties, created_at ) VALUES( (SELECT id FROM survey_admins WHERE name = $1), $2, $3, $4, $5 );", username, &uuid, &payload.name, &payload.difficulties, &now ) .execute(&data.db) .await; if res.is_ok() { break; } else if let Err(sqlx::Error::Database(err)) = res { if err.code() == Some(Cow::from("23505")) && err.message().contains("survey_admins_id_key") { continue; } else { return Err(sqlx::Error::Database(err).into()); } } } Ok(uuid) } pub async fn list_campaign_runner( username: &str, data: &AppData, ) -> ServiceResult> { struct ListCampaign { name: String, id: Uuid, } let mut campaigns = sqlx::query_as!( ListCampaign, "SELECT name, id FROM survey_campaigns WHERE user_id = ( SELECT ID FROM survey_admins WHERE name = $1 )", username ) .fetch_all(&data.db) .await?; let mut list_resp = Vec::with_capacity(campaigns.len()); campaigns.drain(0..).for_each(|c| { list_resp.push(ListCampaignResp { name: c.name, uuid: c.id.to_string(), }); }); Ok(list_resp) } #[derive(Debug)] struct InternalSurveyResp { id: i32, user_id: Uuid, threads: Option, device_user_provided: String, device_software_recognised: String, } #[derive(Debug)] struct InnerU { created_at: OffsetDateTime, id: Uuid, } impl From for SurveyUser { fn from(u: InnerU) -> Self { Self { id: u.id, created_at: u.created_at.unix_timestamp(), } } } pub async fn get_results( username: &str, uuid: &Uuid, data: &AppData, page: usize, limit: usize, ) -> ServiceResult> { // let uuid = Uuid::parse_str(uuid).map_err(|_| ServiceError::NotAnId)?; let mut db_responses = sqlx::query_as!( InternalSurveyResp, "SELECT ID, device_software_recognised, threads, user_id, device_user_provided FROM survey_responses WHERE campaign_id = ( SELECT ID FROM survey_campaigns WHERE ID = $1 AND user_id = (SELECT ID FROM survey_admins WHERE name = $2) ) LIMIT $3 OFFSET $4 ", uuid, username, limit as i32, page as i32, ) .fetch_all(&data.db) .await?; let mut responses = Vec::with_capacity(db_responses.len()); println!("responses {:?}", db_responses); for r in db_responses.drain(0..) { let benches_fut = sqlx::query_as!( Bench, "SELECT duration, difficulty FROM survey_benches WHERE resp_id = $1 ", r.id, ) .fetch_all(&data.db); let user_fut = sqlx::query_as!( InnerU, "SELECT created_at, ID FROM survey_users WHERE ID = $1 ", r.user_id, ) .fetch_one(&data.db); let (benches, user) = try_join!(benches_fut, user_fut)?; let user = user.into(); responses.push(SurveyResponse { benches, user, device_user_provided: r.device_user_provided, device_software_recognised: r.device_software_recognised, id: r.id as usize, threads: r.threads.map(|t| t as usize), }) } Ok(responses) } pub async fn delete( uuid: &Uuid, username: &str, data: &AppData, ) -> ServiceResult<()> { sqlx::query!( "DELETE FROM survey_campaigns WHERE user_id = ( SELECT ID FROM survey_admins WHERE name = $1 ) AND id = ($2)", username, uuid ) .execute(&data.db) .await?; Ok(()) } } #[actix_web_codegen_const_routes::post( path = "crate::V1_API_ROUTES.admin.campaign.delete", wrap = "get_admin_check_login()" )] pub async fn delete( id: Identity, data: AppData, path: web::Path, ) -> ServiceResult { let username = id.identity().unwrap(); let path = path.into_inner(); let uuid = Uuid::parse_str(&path).map_err(|_| ServiceError::NotAnId)?; runners::delete(&uuid, &username, &data).await?; Ok(HttpResponse::Ok()) } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct SurveyResponse { pub user: SurveyUser, pub device_user_provided: String, pub device_software_recognised: String, pub id: usize, pub threads: Option, pub benches: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SurveyUser { pub created_at: i64, // OffsetDateTime, pub id: Uuid, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ListCampaignResp { pub name: String, pub uuid: String, } #[actix_web_codegen_const_routes::post( path = "crate::V1_API_ROUTES.admin.campaign.list", wrap = "get_admin_check_login()" )] pub async fn list_campaign( id: Identity, data: AppData, ) -> ServiceResult { let username = id.identity().unwrap(); let list_resp = runners::list_campaign_runner(&username, &data).await?; Ok(HttpResponse::Ok().json(list_resp)) } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct AddCapmaign { pub name: String, pub difficulties: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct AddCapmaignResp { pub campaign_id: String, } pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(add); cfg.service(delete); cfg.service(list_campaign); cfg.service(get_campaign_resutls); } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct ResultsPage { pub page: Option, } #[actix_web_codegen_const_routes::get( path = "crate::V1_API_ROUTES.admin.campaign.results", wrap = "get_admin_check_login()" )] pub async fn get_campaign_resutls( id: Identity, query: web::Query, path: web::Path, data: AppData, ) -> ServiceResult { let username = id.identity().unwrap(); let page = query.page.unwrap_or(0); let results = runners::get_results(&username, &path, &data, page, 50).await?; Ok(HttpResponse::Ok().json(results)) } #[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.admin.campaign.add")] async fn add( payload: web::Json, data: AppData, id: Identity, ) -> ServiceResult { let username = id.identity().unwrap(); let mut payload = payload.into_inner(); let campaign_id = runners::add_runner(&username, &mut payload, &data).await?; let resp = AddCapmaignResp { campaign_id: campaign_id.to_string(), }; Ok(HttpResponse::Ok().json(resp)) } #[cfg(test)] mod tests { use crate::api::v1::bench::Submission; use crate::errors::*; use crate::tests::*; use crate::*; use actix_auth_middleware::GetLoginRoute; use actix_web::{http::header, test}; #[actix_rt::test] async fn test_bench_register_works() { let data = get_test_data().await; 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); let redirect_to = Some("foo"); let signin_resp = test::call_service( &app, test::TestRequest::get() .uri(&V1_API_ROUTES.benches.get_login_route(redirect_to)) .to_request(), ) .await; assert_eq!(signin_resp.status(), StatusCode::FOUND); let headers = signin_resp.headers(); assert_eq!( headers.get(header::LOCATION).unwrap(), redirect_to.as_ref().unwrap() ) } #[actix_rt::test] async fn test_add_campaign() { const NAME: &str = "testadminuser"; const EMAIL: &str = "testuserupda@testadminuser.com"; const PASSWORD: &str = "longpassword2"; const DEVICE_USER_PROVIDED: &str = "foo"; const DEVICE_SOFTWARE_RECOGNISED: &str = "Foobar.v2"; const THREADS: i32 = 4; { let data = get_test_data().await; delete_user(NAME, &data).await; } let (data, _creds, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let survey = get_survey_user(data.clone()).await; let survey_cookie = get_cookie!(survey); let app = get_app!(data).await; let campaign = create_new_campaign(NAME, data.clone(), cookies.clone()).await; let campaign_config = get_campaign_config(&campaign, data.clone(), survey_cookie.clone()).await; assert_eq!(DIFFICULTIES.to_vec(), campaign_config.difficulties); let submit_payload = Submission { device_user_provided: DEVICE_USER_PROVIDED.into(), device_software_recognised: DEVICE_SOFTWARE_RECOGNISED.into(), threads: THREADS, benches: BENCHES.clone(), }; let _proof = submit_bench(&submit_payload, &campaign, survey_cookie, data.clone()).await; let list = list_campaings(data.clone(), cookies.clone()).await; assert!(list.iter().any(|c| c.name == NAME)); let responses = super::runners::get_results( NAME, &uuid::Uuid::parse_str(&campaign.campaign_id).unwrap(), &AppData::new(data.clone()), 0, 50, ) .await .unwrap(); assert_eq!(responses.len(), 1); assert_eq!(responses[0].threads, Some(THREADS as usize)); let mut l = responses[0].benches.clone(); l.sort_by(|a, b| a.difficulty.cmp(&b.difficulty)); let mut r = BENCHES.clone(); r.sort_by(|a, b| a.difficulty.cmp(&b.difficulty)); assert_eq!(l, r); assert_eq!( responses[0].device_software_recognised, DEVICE_SOFTWARE_RECOGNISED ); assert_eq!(responses[0].device_user_provided, DEVICE_USER_PROVIDED); let results_resp = get_request!( &app, &V1_API_ROUTES .admin .campaign .get_results_route(&campaign.campaign_id), cookies.clone() ); assert_eq!(results_resp.status(), StatusCode::OK); let res: Vec = test::read_body_json(results_resp).await; assert_eq!(responses, res); bad_post_req_test_witout_payload( NAME, PASSWORD, &V1_API_ROUTES.admin.campaign.delete.replace("{uuid}", NAME), ServiceError::NotAnId, ) .await; delete_campaign(&campaign, data.clone(), cookies.clone()).await; let list = list_campaings(data.clone(), cookies.clone()).await; assert!(!list.iter().any(|c| c.name == NAME)); } }