feat: finish porting pages to tera. Fix tests

This commit is contained in:
Aravinth Manivannan 2023-01-24 23:31:04 +05:30
parent b874c17362
commit 6b608c6677
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 109 additions and 98 deletions

1
.gitignore vendored
View File

@ -16,3 +16,4 @@ scripts/creds.py
__pycache__/ __pycache__/
*.py[cod] *.py[cod]
*$py.class *$py.class
src/sailfish/

View File

@ -27,6 +27,7 @@ use crate::{GIT_COMMIT_HASH, VERSION};
pub mod auth; pub mod auth;
pub mod errors; pub mod errors;
pub mod panel;
pub mod routes; pub mod routes;
//pub use routes::get_auth_middleware; //pub use routes::get_auth_middleware;
@ -74,6 +75,7 @@ lazy_static! {
errors::register_templates(&mut tera); errors::register_templates(&mut tera);
tera.autoescape_on(vec![".html", ".sql"]); tera.autoescape_on(vec![".html", ".sql"]);
auth::register_templates(&mut tera); auth::register_templates(&mut tera);
panel::register_templates(&mut tera);
tera tera
}; };
} }
@ -146,7 +148,7 @@ impl<'a> Footer<'a> {
pub fn services(cfg: &mut ServiceConfig) { pub fn services(cfg: &mut ServiceConfig) {
auth::services(cfg); auth::services(cfg);
//panel::services(cfg); panel::services(cfg);
} }
pub fn get_page_check_login() -> Authentication<auth::routes::Auth> { pub fn get_page_check_login() -> Authentication<auth::routes::Auth> {
@ -167,7 +169,6 @@ mod terra_tests {
BASE, BASE,
FOOTER, FOOTER,
PANEL_NAV, PANEL_NAV,
// auth::AUTH_BASE,
auth::login::LOGIN, auth::login::LOGIN,
auth::join::REGISTER, auth::join::REGISTER,
errors::ERROR_TEMPLATE, errors::ERROR_TEMPLATE,
@ -182,7 +183,7 @@ mod terra_tests {
#[cfg(test)] #[cfg(test)]
mod http_page_tests { mod http_page_tests {
use actix_web::http::StatusCode; use actix_web::http::{header, StatusCode};
use actix_web::test; use actix_web::test;
use crate::*; use crate::*;
@ -197,97 +198,108 @@ mod http_page_tests {
let app = get_app!(data).await; let app = get_app!(data).await;
for file in [PAGES.auth.login, PAGES.auth.join, PAGES.home].iter() { for file in [PAGES.auth.login, PAGES.auth.join, PAGES.home].iter() {
println!("[*] Testing route: {}", file);
let resp = get_request!(&app, file); let resp = get_request!(&app, file);
assert_eq!(resp.status(), StatusCode::OK); if file != &PAGES.home {
assert_eq!(resp.status(), StatusCode::OK);
} else {
assert_eq!(resp.status(), StatusCode::FOUND);
let headers = resp.headers();
let loc = headers.get(header::LOCATION).unwrap();
let resp = get_request!(&app, loc.to_str().unwrap());
assert_eq!(resp.status(), StatusCode::OK);
}
} }
} }
} }
//#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]
//#[cfg(test)] #[cfg(test)]
//mod tests { mod tests {
// use actix_web::http::{header, StatusCode}; use actix_web::http::{header, StatusCode};
// use actix_web::test; use actix_web::test;
//
// use crate::tests::*; use crate::tests::*;
// use crate::*; use crate::*;
//
// #[actix_rt::test] #[actix_rt::test]
// async fn protected_pages_templates_work() { async fn protected_pages_templates_work() {
// const NAME: &str = "templateuser"; const NAME: &str = "templateuser";
// const PASSWORD: &str = "longpassword"; const PASSWORD: &str = "longpassword";
// const EMAIL: &str = "templateuser@a.com"; const EMAIL: &str = "templateuser@a.com";
// const CAMPAIGN_NAME: &str = "delcappageusercamaping"; const CAMPAIGN_NAME: &str = "delcappageusercamaping";
//
// let settings = Settings::new().unwrap(); let data = get_test_data().await;
// let data = Data::new(settings).await; {
// { delete_user(NAME, &data).await;
// delete_user(NAME, &data).await; }
// }
// let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await;
// let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp);
// let cookies = get_cookie!(signin_resp);
// let campaign =
// let campaign = create_new_campaign(CAMPAIGN_NAME, data.clone(), cookies.clone()).await;
// create_new_campaign(CAMPAIGN_NAME, data.clone(), cookies.clone()).await;
// let app = get_app!(data).await;
// let app = get_app!(data).await;
// let urls = vec![
// let urls = vec![ PAGES.home.to_string(),
// PAGES.home.to_string(), PAGES.panel.campaigns.home.to_string(),
// PAGES.panel.campaigns.home.to_string(), PAGES.panel.campaigns.new.to_string(),
// PAGES.panel.campaigns.new.to_string(), // PAGES.panel.campaigns.get_feedback_route(&campaign.uuid),
// // PAGES.panel.campaigns.get_feedback_route(&campaign.uuid), PAGES
// PAGES .panel
// .panel .campaigns
// .campaigns .get_delete_route(&campaign.campaign_id),
// .get_delete_route(&campaign.campaign_id), ];
// ];
// for url in urls.iter() {
// for url in urls.iter() { let resp =
// let resp = test::call_service(&app, test::TestRequest::get().uri(url).to_request())
// test::call_service(&app, test::TestRequest::get().uri(url).to_request()) .await;
// .await; if resp.status() != StatusCode::FOUND {
// if resp.status() != StatusCode::FOUND { println!("Probably error url: {}", url);
// println!("Probably error url: {}", url); }
// } assert_eq!(resp.status(), StatusCode::FOUND);
// assert_eq!(resp.status(), StatusCode::FOUND);
// let authenticated_resp = test::call_service(
// let authenticated_resp = test::call_service( &app,
// &app, test::TestRequest::get()
// test::TestRequest::get() .uri(url)
// .uri(url) .cookie(cookies.clone())
// .cookie(cookies.clone()) .to_request(),
// .to_request(), )
// ) .await;
// .await;
// if url == PAGES.home {
// if url == PAGES.home { assert_eq!(authenticated_resp.status(), StatusCode::FOUND);
// assert_eq!(authenticated_resp.status(), StatusCode::FOUND); let headers = authenticated_resp.headers();
// let headers = authenticated_resp.headers(); assert_eq!(
// assert_eq!( headers.get(header::LOCATION).unwrap().to_str().unwrap(),
// headers.get(header::LOCATION).unwrap(), PAGES
// &*super::panel::DEFAULT_CAMPAIGN_ABOUT .panel
// ); .campaigns
// } else { .get_about_route(&data.settings.default_campaign)
// assert_eq!(authenticated_resp.status(), StatusCode::OK); );
// } } else {
// } assert_eq!(authenticated_resp.status(), StatusCode::OK);
// }
// delete_user(NAME, &data).await; }
// }
// delete_user(NAME, &data).await;
// #[actix_rt::test] }
// async fn public_pages_tempaltes_work() {
// let app = test::init_service(App::new().configure(crate::pages::services)).await; #[actix_rt::test]
// let urls = vec![PAGES.auth.login, PAGES.auth.join]; //, PAGES.sitemap]; async fn public_pages_tempaltes_work() {
// let data = get_test_data().await;
// for url in urls.iter() { let app = get_app!(data).await;
// let resp = let urls = vec![PAGES.auth.login, PAGES.auth.join]; //, PAGES.sitemap];
// test::call_service(&app, test::TestRequest::get().uri(url).to_request()) for url in urls.iter() {
// .await; let resp =
// test::call_service(&app, test::TestRequest::get().uri(url).to_request())
// assert_eq!(resp.status(), StatusCode::OK); .await;
// }
// } assert_eq!(resp.status(), StatusCode::OK);
//} }
}
}

View File

@ -15,17 +15,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use actix_auth_middleware::GetLoginRoute; use actix_auth_middleware::GetLoginRoute;
use serde::{Deserialize, Serialize};
use super::auth::routes::Auth; use super::auth::routes::Auth;
use super::errors::routes::Errors;
use super::panel::routes::Panel; use super::panel::routes::Panel;
pub const ROUTES: Routes = Routes::new(); pub const PAGES: Routes = Routes::new();
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Routes { pub struct Routes {
pub home: &'static str, pub home: &'static str,
pub auth: Auth, pub auth: Auth,
pub panel: Panel, pub panel: Panel,
pub errors: Errors,
pub about: &'static str, pub about: &'static str,
pub sitemap: &'static str, pub sitemap: &'static str,
pub thanks: &'static str, pub thanks: &'static str,
@ -42,7 +42,6 @@ impl Routes {
auth: Auth::new(), auth: Auth::new(),
panel, panel,
home, home,
errors: Errors::new(),
about: "https://mcaptcha.org/about/", about: "https://mcaptcha.org/about/",
sitemap: "/sitemap.xml", sitemap: "/sitemap.xml",
thanks: "https://mcaptcha.org/thanks", thanks: "https://mcaptcha.org/thanks",
@ -65,7 +64,7 @@ impl GetLoginRoute for Routes {
// uri::Builder::new().path_and_query( // uri::Builder::new().path_and_query(
format!( format!(
"{}?redirect_to={}", "{}?redirect_to={}",
self.auth.join.to_string(), self.auth.join,
urlencoding::encode(redirect_to) urlencoding::encode(redirect_to)
) )
// let mut url: Uri = self.register.parse().unwrap(); // let mut url: Uri = self.register.parse().unwrap();

View File

@ -1,5 +1,4 @@
{% extends 'base' %} {% extends 'base' %}
{% block body %} {% block body %}
<body class="auth__body"> <body class="auth__body">
<main class="auth__container"> <main class="auth__container">