From 9c7670a334ad4eb0362e1a23367b32d0b0aa51d1 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 14 Oct 2021 17:13:09 +0530 Subject: [PATCH] about and bench pages --- build.rs | 11 +--- src/api/v1/admin/auth.rs | 6 +- src/api/v1/admin/tests/protected.rs | 2 +- src/pages/mod.rs | 4 +- src/pages/panel/campaigns/about.rs | 54 ++++++++++++++++ src/pages/panel/campaigns/bench.rs | 50 +++++++++++++++ src/pages/panel/campaigns/delete.rs | 4 +- src/pages/panel/campaigns/get.rs | 61 ------------------ src/pages/panel/campaigns/mod.rs | 87 +++++++++++++++++++++++-- src/pages/panel/mod.rs | 24 ++----- templates/bench.html | 95 ++++++++++++++++++++++++++++ templates/index.html | 2 +- templates/panel/campaigns/index.html | 2 +- 13 files changed, 299 insertions(+), 103 deletions(-) create mode 100644 src/pages/panel/campaigns/about.rs create mode 100644 src/pages/panel/campaigns/bench.rs delete mode 100644 src/pages/panel/campaigns/get.rs create mode 100644 templates/bench.html diff --git a/build.rs b/build.rs index af9b4f7..2326206 100644 --- a/build.rs +++ b/build.rs @@ -24,17 +24,10 @@ fn main() { let output = Command::new("git") .args(&["rev-parse", "HEAD"]) .output() - .unwrap(); + .expect("error in git command, is git installed?"); let git_hash = String::from_utf8(output.stdout).unwrap(); println!("cargo:rustc-env=GIT_HASH={}", git_hash); - // let yml = include_str!("./openapi.yaml"); - // let api_json: serde_json::Value = serde_yaml::from_str(yml).unwrap(); - // println!( - // "cargo:rustc-env=OPEN_API_DOCS={}", - // serde_json::to_string(&api_json).unwrap() - // ); - let now = OffsetDateTime::now_utc().format("%y-%m-%d"); println!("cargo:rustc-env=COMPILED_DATE={}", &now); @@ -53,7 +46,7 @@ fn cache_bust() { // mime::TEXT_CSS, // ]; - println!("cargo:rerun-if-changed=static/cache"); + //println!("cargo:rerun-if-changed=static/cache"); let no_hash = vec![NoHashCategory::FileExtentions(vec!["wasm"])]; let config = BusterBuilder::default() diff --git a/src/api/v1/admin/auth.rs b/src/api/v1/admin/auth.rs index bfd7be7..6c1052c 100644 --- a/src/api/v1/admin/auth.rs +++ b/src/api/v1/admin/auth.rs @@ -50,9 +50,9 @@ pub mod routes { impl Auth { pub const fn new() -> Auth { - let login = "/api/v1/admin/signin"; - let logout = "/logout"; - let register = "/api/v1/admin/signup"; + let login = "/admin/api/v1/signin"; + let logout = "/admin/logout"; + let register = "/admin/api/v1/signup"; Auth { logout, login, diff --git a/src/api/v1/admin/tests/protected.rs b/src/api/v1/admin/tests/protected.rs index 98abf5c..21c027f 100644 --- a/src/api/v1/admin/tests/protected.rs +++ b/src/api/v1/admin/tests/protected.rs @@ -29,7 +29,7 @@ async fn protected_routes_work() { const PASSWORD: &str = "longpassword2"; const EMAIL: &str = "testuser119@a.com2"; - let get_protected_urls = ["/logout"]; + let get_protected_urls = [V1_API_ROUTES.admin.auth.logout]; { let data = Data::new().await; diff --git a/src/pages/mod.rs b/src/pages/mod.rs index da08c11..370dced 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -22,7 +22,7 @@ mod panel; pub mod routes; //mod sitemap; -pub const NAME: &str = "mCaptcha"; +pub const NAME: &str = "Kaizen"; pub fn services(cfg: &mut ServiceConfig) { auth::services(cfg); @@ -92,7 +92,7 @@ mod tests { ) .await; - if url == &PAGES.home { + if url == PAGES.home { assert_eq!(authenticated_resp.status(), StatusCode::FOUND); let headers = authenticated_resp.headers(); assert_eq!( diff --git a/src/pages/panel/campaigns/about.rs b/src/pages/panel/campaigns/about.rs new file mode 100644 index 0000000..a54966a --- /dev/null +++ b/src/pages/panel/campaigns/about.rs @@ -0,0 +1,54 @@ +/* + * 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::str::FromStr; + +use actix_web::{web, HttpResponse, Responder}; +use my_codegen::get; +use sailfish::TemplateOnce; +use uuid::Uuid; + +use crate::errors::*; +use crate::PAGES; + +#[derive(TemplateOnce)] +#[template(path = "index.html")] +struct Intro<'a> { + uuid: &'a str, +} + +const PAGE: &str = "Survey"; + +impl<'a> Intro<'a> { + pub fn new(uuid: &'a str) -> Self { + Self { uuid } + } +} + +#[get(path = "PAGES.panel.campaigns.about")] +pub async fn about(path: web::Path) -> PageResult { + let path = path.into_inner(); + + match Uuid::from_str(&path) { + Err(_) => Err(PageError::PageDoesntExist), + Ok(_) => { + let page = Intro::new(&path).render_once().unwrap(); + Ok(HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(page)) + } + } +} diff --git a/src/pages/panel/campaigns/bench.rs b/src/pages/panel/campaigns/bench.rs new file mode 100644 index 0000000..318fa11 --- /dev/null +++ b/src/pages/panel/campaigns/bench.rs @@ -0,0 +1,50 @@ +/* + * 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::str::FromStr; + +use actix_web::{web, HttpResponse, Responder}; +use lazy_static::lazy_static; +use my_codegen::get; +use sailfish::TemplateOnce; +use uuid::Uuid; + +use crate::errors::*; +use crate::PAGES; + +#[derive(TemplateOnce, Default)] +#[template(path = "bench.html")] +struct Bench; +const PAGE: &str = "Survey"; + +lazy_static! { + static ref BENCH: String = Bench::default().render_once().unwrap(); +} + +#[get( + path = "PAGES.panel.campaigns.bench", + wrap = "crate::pages::get_page_check_login()" +)] +pub async fn bench(path: web::Path) -> PageResult { + let path = path.into_inner(); + + match Uuid::from_str(&path) { + Err(_) => Err(PageError::PageDoesntExist), + Ok(_) => Ok(HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(&*BENCH)), + } +} diff --git a/src/pages/panel/campaigns/delete.rs b/src/pages/panel/campaigns/delete.rs index deaf050..5ebddf8 100644 --- a/src/pages/panel/campaigns/delete.rs +++ b/src/pages/panel/campaigns/delete.rs @@ -108,8 +108,8 @@ pub async fn delete_campaign_submit( let status = e.status_code(); let heading = status.canonical_reason().unwrap_or("Error"); - let form_route = crate::V1_API_ROUTES.admin.campaign.get_delete_route(&path); - let title = get_title(&username, &uuid, &data).await?; + let form_route = crate::V1_API_ROUTES.admin.campaign.get_delete_route(path); + let title = get_title(username, uuid, data).await?; let mut ctx = SudoPage::new(&form_route, &title); let err = format!("{}", e); ctx.set_err(heading, &err); diff --git a/src/pages/panel/campaigns/get.rs b/src/pages/panel/campaigns/get.rs deleted file mode 100644 index 20cb51c..0000000 --- a/src/pages/panel/campaigns/get.rs +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 actix_identity::Identity; -//use actix_web::{web, HttpResponse, Responder}; -//use my_codegen::get; -//use sailfish::TemplateOnce; -// -//use crate::api::v1::admin::campaigns::{runners, GetFeedbackResp}; -//use crate::AppData; -//use crate::PAGES; -// -//#[derive(TemplateOnce)] -//#[template(path = "panel/campaigns/get/index.html")] -//struct ViewFeedback<'a> { -// campaign: GetFeedbackResp, -// uuid: &'a str, -//} -// -//const PAGE: &str = "New Campaign"; -// -//impl<'a> ViewFeedback<'a> { -// pub fn new(campaign: GetFeedbackResp, uuid: &'a str) -> Self { -// Self { campaign, uuid } -// } -//} -// -//#[get( -// path = "PAGES.panel.campaigns.get_feedback", -// wrap = "crate::pages::get_page_check_login()" -//)] -//pub async fn get_feedback( -// id: Identity, -// data: AppData, -// path: web::Path, -//) -> impl Responder { -// let username = id.identity().unwrap(); -// let path = path.into_inner(); -// let feedback_resp = runners::get_feedback(&username, &path, &data) -// .await -// .unwrap(); -// let page = ViewFeedback::new(feedback_resp, &path) -// .render_once() -// .unwrap(); -// HttpResponse::Ok() -// .content_type("text/html; charset=utf-8") -// .body(page) -//} diff --git a/src/pages/panel/campaigns/mod.rs b/src/pages/panel/campaigns/mod.rs index a5a18eb..aac65c3 100644 --- a/src/pages/panel/campaigns/mod.rs +++ b/src/pages/panel/campaigns/mod.rs @@ -24,15 +24,17 @@ use crate::api::v1::admin::campaigns::{ use crate::AppData; use crate::PAGES; +pub mod about; +pub mod bench; pub mod delete; -pub mod get; pub mod new; pub mod routes { pub struct Campaigns { pub home: &'static str, pub new: &'static str, - pub get_feedback: &'static str, + pub about: &'static str, + pub bench: &'static str, pub delete: &'static str, } impl Campaigns { @@ -40,7 +42,8 @@ pub mod routes { Campaigns { home: "/admin/campaigns", new: "/admin/campaigns/new", - get_feedback: "/admin/campaigns/{uuid}/feedback", + about: "/survey/campaigns/{uuid}/about", + bench: "/survey/campaigns/{uuid}/bench", delete: "/admin/campaigns/{uuid}/delete", } } @@ -49,8 +52,12 @@ pub mod routes { self.delete.replace("{uuid}", campaign_id) } - pub fn get_feedback_route(&self, campaign_id: &str) -> String { - self.get_feedback.replace("{uuid}", campaign_id) + pub fn get_bench_route(&self, campaign_id: &str) -> String { + self.bench.replace("{uuid}", campaign_id) + } + + pub fn get_about_route(&self, campaign_id: &str) -> String { + self.about.replace("{uuid}", campaign_id) } pub const fn get_sitemap() -> [&'static str; 2] { @@ -64,7 +71,8 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(home); cfg.service(new::new_campaign); cfg.service(new::new_campaign_submit); - // cfg.service(get::get_feedback); + cfg.service(about::about); + cfg.service(bench::bench); cfg.service(delete::delete_campaign); cfg.service(delete::delete_campaign_submit); } @@ -96,3 +104,70 @@ pub async fn home(data: AppData, id: Identity) -> impl Responder { .content_type("text/html; charset=utf-8") .body(&page) } + +#[cfg(test)] +mod tests { + use actix_web::http::StatusCode; + use actix_web::test; + + use crate::tests::*; + use crate::*; + + #[actix_rt::test] + async fn survey_pages_work() { + const NAME: &str = "surveyuserpages"; + const PASSWORD: &str = "longpassword"; + const EMAIL: &str = "templateuser@surveyuserpages.com"; + const CAMPAIGN_NAME: &str = "delcappageusercamaping"; + + let data = Data::new().await; + { + delete_user(NAME, &data).await; + } + + let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let cookies = get_cookie!(signin_resp); + + let campaign = + create_new_campaign(CAMPAIGN_NAME, data.clone(), cookies.clone()).await; + + let app = get_app!(data).await; + + let protected_urls = + vec![PAGES.panel.campaigns.get_bench_route(&campaign.campaign_id)]; + + let public_urls = + vec![PAGES.panel.campaigns.get_about_route(&campaign.campaign_id)]; + + for url in public_urls.iter() { + let resp = + test::call_service(&app, test::TestRequest::get().uri(url).to_request()) + .await; + if resp.status() != StatusCode::OK { + println!("Probably error url: {}", url); + } + assert_eq!(resp.status(), StatusCode::OK); + } + + for url in protected_urls.iter() { + let resp = + test::call_service(&app, test::TestRequest::get().uri(url).to_request()) + .await; + if resp.status() != StatusCode::FOUND { + println!("Probably error url: {}", url); + } + assert_eq!(resp.status(), StatusCode::FOUND); + + let authenticated_resp = test::call_service( + &app, + test::TestRequest::get() + .uri(url) + .cookie(cookies.clone()) + .to_request(), + ) + .await; + + assert_eq!(authenticated_resp.status(), StatusCode::OK); + } + } +} diff --git a/src/pages/panel/mod.rs b/src/pages/panel/mod.rs index 47d9e23..cb99281 100644 --- a/src/pages/panel/mod.rs +++ b/src/pages/panel/mod.rs @@ -13,10 +13,10 @@ * * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ -use actix_web::{HttpResponse, Responder}; -use lazy_static::lazy_static; +use actix_web::{http, HttpResponse, Responder}; use my_codegen::get; -use sailfish::TemplateOnce; + +use super::get_page_check_login; use crate::PAGES; @@ -50,19 +50,9 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) { campaigns::services(cfg); } -#[derive(TemplateOnce, Default)] -#[template(path = "index.html")] -struct HomePage; - -const PAGE: &str = "Survey"; - -lazy_static! { - static ref INDEX: String = HomePage::default().render_once().unwrap(); -} - -#[get(path = "PAGES.panel.home")] +#[get(path = "PAGES.panel.home", wrap = "get_page_check_login()")] pub async fn home() -> impl Responder { - HttpResponse::Ok() - .content_type("text/html; charset=utf-8") - .body(&*INDEX) + HttpResponse::Found() + .insert_header((http::header::LOCATION, PAGES.panel.campaigns.home)) + .finish() } diff --git a/templates/bench.html b/templates/bench.html new file mode 100644 index 0000000..eaa6e10 --- /dev/null +++ b/templates/bench.html @@ -0,0 +1,95 @@ +<. include!("./components/base/top.html"); .> + +
+
+

Click to Start Benchmark

+ + +
+ + + +
+

Running benchmark

+ +
+ +
+ + + + + + + + + +
Difficulty factorDuration(ms)
+
+
+ <. include!("./components/footer/index.html"); .> + + + +<. include!("./components/base/bottom.html"); .> diff --git a/templates/index.html b/templates/index.html index b2877a1..c6153f6 100644 --- a/templates/index.html +++ b/templates/index.html @@ -77,7 +77,7 @@ Get started diff --git a/templates/panel/campaigns/index.html b/templates/panel/campaigns/index.html index 19e292a..44ea033 100644 --- a/templates/panel/campaigns/index.html +++ b/templates/panel/campaigns/index.html @@ -19,7 +19,7 @@ <. for campaign in data.iter() { .> - <. let route = crate::PAGES.panel.campaigns.get_feedback_route(&campaign.uuid); .> + <. let route = crate::PAGES.panel.campaigns.get_about_route(&campaign.uuid); .>