about and bench pages

This commit is contained in:
Aravinth Manivannan 2021-10-14 17:13:09 +05:30
parent 7094d55065
commit 9c7670a334
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
13 changed files with 299 additions and 103 deletions

View file

@ -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()

View file

@ -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,

View file

@ -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;

View file

@ -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!(

View file

@ -0,0 +1,54 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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<String>) -> PageResult<impl Responder> {
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))
}
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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<String>) -> PageResult<impl Responder> {
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)),
}
}

View file

@ -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);

View file

@ -1,61 +0,0 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
//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<String>,
//) -> 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)
//}

View file

@ -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);
}
}
}

View file

@ -13,10 +13,10 @@
*
* You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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()
}

95
templates/bench.html Normal file
View file

@ -0,0 +1,95 @@
<. include!("./components/base/top.html"); .>
<body class="survey__body">
<main class="survey__container">
<div id="pre-bench">
<h1>Click to Start Benchmark</h1>
<button id="start" type="submit">Start</button>
</div>
<noscript>
<b>
This is a WASM benchmark, please enable JavScript to run it. We don't
collect any data and
<a href="https://github.com/mCaptcha/benches"
>source code is available here</a
>
</b>
</noscript>
<div id="bench">
<h1 id="status" class="name">Running benchmark</h1>
<div id="device-info"></div>
<div id="counter"></div>
<table>
<thead>
<tr>
<th>Difficulty factor</th>
<th>Duration(ms)</th>
</tr>
</thead>
<tbody id="stats"></tbody>
</table>
</div>
</main>
<. include!("./components/footer/index.html"); .>
</body>
<style>
body {
width: 100%;
display: flex;
flex-direction: column;
padding: 40px 0;
display: hidden;
align-items: center;
}
#start {
width: 100px;
}
#pre-bench,
#bench {
width: 100%;
display: flex;
margin: 40px auto;
align-items: center;
flex-direction: column;
}
#bench {
display: none;
}
#status {
max-width: 80%;
margin: auto;
}
table {
border-collapse: collapse;
margin: 40px auto;
width: 40%;
}
td,
th {
border: 1px solid #999;
padding: 0.5rem;
text-align: left;
}
th {
color: green;
}
#device-info {
margin: auto;
font-size: 1.3rem;
}
</style>
<script src="<.= &*crate::GLUE .>"></script>
<. include!("./components/base/bottom.html"); .>

View file

@ -77,7 +77,7 @@
<a
class="link__btn"
href="<.= crate::V1_API_ROUTES.benches.register .>"
href="<.= crate::PAGES.panel.campaigns.get_bench_route(uuid) .>"
target="_blank"
>Get started</a
>

View file

@ -19,7 +19,7 @@
</thead>
<tbody class="campaign__body">
<. 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); .>
<tr class="campaign__item">
<td>
<a href="<.= &route .>">