From dba0834effafeafab4f9b25b2a692bba0cdf3ca2 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 22 Feb 2022 21:57:01 +0530 Subject: [PATCH] feat: register form submission --- src/pages/auth/mod.rs | 2 + src/pages/auth/register.rs | 15 +++++ src/pages/auth/test.rs | 113 +++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 src/pages/auth/test.rs diff --git a/src/pages/auth/mod.rs b/src/pages/auth/mod.rs index 6a8f5fa..33c0a81 100644 --- a/src/pages/auth/mod.rs +++ b/src/pages/auth/mod.rs @@ -21,6 +21,8 @@ pub use super::{context, Footer, PAGES, PAYLOAD_KEY, TEMPLATES}; pub mod login; pub mod register; +#[cfg(test)] +mod test; pub const HOME_BASE: &str = "homebase"; diff --git a/src/pages/auth/register.rs b/src/pages/auth/register.rs index e4df816..5c51f23 100644 --- a/src/pages/auth/register.rs +++ b/src/pages/auth/register.rs @@ -66,6 +66,21 @@ pub async fn get_register(data: AppData) -> impl Responder { pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(get_register); + cfg.service(register_submit); +} + +#[my_codegen::post(path = "PAGES.auth.register")] +pub async fn register_submit( + payload: web::Form, + data: AppData, + db: crate::DB, +) -> PageResult { + data.register(&(**db), &payload) + .await + .map_err(|e| PageError::new(Register::new(&data.settings, Some(&payload)), e))?; + Ok(HttpResponse::Found() + .insert_header((http::header::LOCATION, PAGES.auth.login)) + .finish()) } #[cfg(test)] diff --git a/src/pages/auth/test.rs b/src/pages/auth/test.rs new file mode 100644 index 0000000..294e5ef --- /dev/null +++ b/src/pages/auth/test.rs @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2022 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_web::http::header; +use actix_web::http::StatusCode; +use actix_web::test; + +use super::*; + +use crate::data::api::v1::auth::Register; +use crate::data::Data; +use crate::errors::*; +use crate::tests::*; +use crate::*; + +#[actix_rt::test] +async fn postgrest_pages_auth_works() { + let (db, data) = sqlx_postgres::get_data().await; + auth_works(data.clone(), db.clone()).await; + serverside_password_validation_works(data.clone(), db.clone()).await; +} + +#[actix_rt::test] +async fn sqlite_pages_auth_works() { + let (db, data) = sqlx_sqlite::get_data().await; + auth_works(data.clone(), db.clone()).await; + serverside_password_validation_works(data.clone(), db.clone()).await; +} + +async fn auth_works(data: Arc, db: BoxDB) { + const NAME: &str = "testuserform"; + const EMAIL: &str = "testuserform@foo.com"; + const PASSWORD: &str = "longpassword"; + + let _ = data.delete_user(&db, NAME, PASSWORD).await; + let app = get_app!(data, db).await; + + // 1. Register with email == None + let msg = Register { + username: NAME.into(), + password: PASSWORD.into(), + confirm_password: PASSWORD.into(), + email: None, + }; + let resp = test::call_service( + &app, + post_request!(&msg, PAGES.auth.register, FORM).to_request(), + ) + .await; + assert_eq!(resp.status(), StatusCode::FOUND); + let headers = resp.headers(); + assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login); + let _ = data.delete_user(&db, NAME, PASSWORD).await; + + // 1. Register with email + 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, PAGES.auth.register, FORM).to_request(), + ) + .await; + assert_eq!(resp.status(), StatusCode::FOUND); + let headers = resp.headers(); + assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login); + let _ = data.delete_user(&db, NAME, PASSWORD).await; +} + +async fn serverside_password_validation_works(data: Arc, db: BoxDB) { + const NAME: &str = "pagetestuser542"; + const PASSWORD: &str = "longpassword2"; + + let db = &db; + let _ = data.delete_user(db, NAME, PASSWORD).await; + + let app = get_app!(data, db).await; + + // checking to see if server-side password validation (password == password_config) + // works + let register_msg = Register { + username: NAME.into(), + password: PASSWORD.into(), + confirm_password: NAME.into(), + email: None, + }; + let resp = test::call_service( + &app, + post_request!(®ister_msg, PAGES.auth.register, FORM).to_request(), + ) + .await; + assert_eq!( + resp.status(), + ServiceError::PasswordsDontMatch.status_code() + ); +}