From 9292454dd3317161acb899a1edb730392d7944ec Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 3 Jun 2023 16:10:48 -0700 Subject: [PATCH] Add test for duplicate username. --- src/signup.rs | 59 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/src/signup.rs b/src/signup.rs index 02bb93f..dc8d479 100644 --- a/src/signup.rs +++ b/src/signup.rs @@ -233,6 +233,8 @@ pub(crate) async fn create_user( #[cfg(test)] mod test { + use axum::http::StatusCode; + use crate::{ db::get_pool, templates::{CreateUser, CreateUserSuccess}, @@ -248,13 +250,15 @@ mod test { let server = server_with_pool(&pool).await; let body = massage(GOOD_FORM); - let _resp = server + let resp = server .post("/signup") .expect_failure() // 303 is "failure" .bytes(body) .content_type(FORM_CONTENT_TYPE) .await; + assert_eq!(StatusCode::SEE_OTHER, resp.status_code()); + // get the new user from the db let user = User::try_get("test_user", &pool).await; assert!(user.is_ok()); @@ -292,10 +296,8 @@ mod test { // honestly this is basically the whole suite here //-************************************************************************ mod failure { - use axum::http::StatusCode; - use super::*; - use crate::signup::CreateUserError; + use crate::signup::{CreateUserError, CreateUserErrorKind}; // various ways to fuck up signup const PASSWORD_MISMATCH_FORM: &str = @@ -328,8 +330,7 @@ mod test { assert!(user.is_err()); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let expected = - CreateUserError(crate::signup::CreateUserErrorKind::PasswordMismatch).to_string(); + let expected = CreateUserError(CreateUserErrorKind::PasswordMismatch).to_string(); assert_eq!(&expected, body); } @@ -352,8 +353,7 @@ mod test { assert!(user.is_err()); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let expected = - CreateUserError(crate::signup::CreateUserErrorKind::BadPassword).to_string(); + let expected = CreateUserError(CreateUserErrorKind::BadPassword).to_string(); assert_eq!(&expected, body); } @@ -376,8 +376,7 @@ mod test { assert!(user.is_err()); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let expected = - CreateUserError(crate::signup::CreateUserErrorKind::BadPassword).to_string(); + let expected = CreateUserError(CreateUserErrorKind::BadPassword).to_string(); assert_eq!(&expected, body); } @@ -400,8 +399,7 @@ mod test { assert!(user.is_err()); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let expected = - CreateUserError(crate::signup::CreateUserErrorKind::BadUsername).to_string(); + let expected = CreateUserError(CreateUserErrorKind::BadUsername).to_string(); assert_eq!(&expected, body); } @@ -424,8 +422,38 @@ mod test { assert!(user.is_err()); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let expected = - CreateUserError(crate::signup::CreateUserErrorKind::BadUsername).to_string(); + let expected = CreateUserError(CreateUserErrorKind::BadUsername).to_string(); + assert_eq!(&expected, body); + } + + #[tokio::test] + async fn username_duplicate() { + let pool = get_pool().await; + let server = server_with_pool(&pool).await; + let body = massage(GOOD_FORM); + + let _resp = server + .post("/signup") + .expect_failure() // 303 is "failure" + .bytes(body.clone()) + .content_type(FORM_CONTENT_TYPE) + .await; + + // get the new user from the db + let user = User::try_get("test_user", &pool).await; + assert!(user.is_ok()); + + // now try again + let resp = server + .post("/signup") + .expect_success() + .bytes(body) + .content_type(FORM_CONTENT_TYPE) + .await; + + assert_eq!(resp.status_code(), StatusCode::OK); + let expected = CreateUserError(CreateUserErrorKind::AlreadyExists).to_string(); + let body = std::str::from_utf8(resp.bytes()).unwrap(); assert_eq!(&expected, body); } @@ -448,8 +476,7 @@ mod test { assert!(user.is_err()); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let expected = - CreateUserError(crate::signup::CreateUserErrorKind::BadDisplayname).to_string(); + let expected = CreateUserError(CreateUserErrorKind::BadDisplayname).to_string(); assert_eq!(&expected, body); }