mirror of https://github.com/realaravinth/gitpad
feat: register form submission
parent
2a8df0ebb8
commit
dba0834eff
|
@ -21,6 +21,8 @@ pub use super::{context, Footer, PAGES, PAYLOAD_KEY, TEMPLATES};
|
||||||
|
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test;
|
||||||
|
|
||||||
pub const HOME_BASE: &str = "homebase";
|
pub const HOME_BASE: &str = "homebase";
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,21 @@ pub async fn get_register(data: AppData) -> impl Responder {
|
||||||
|
|
||||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.service(get_register);
|
cfg.service(get_register);
|
||||||
|
cfg.service(register_submit);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[my_codegen::post(path = "PAGES.auth.register")]
|
||||||
|
pub async fn register_submit(
|
||||||
|
payload: web::Form<RegisterPayload>,
|
||||||
|
data: AppData,
|
||||||
|
db: crate::DB,
|
||||||
|
) -> PageResult<impl Responder, Register> {
|
||||||
|
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)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -0,0 +1,113 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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_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<Data>, 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<Data>, 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()
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue