Add test for duplicate username.

This commit is contained in:
Joe Ardent 2023-06-03 16:10:48 -07:00
parent 8d040ad368
commit 9292454dd3

View file

@ -233,6 +233,8 @@ pub(crate) async fn create_user(
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use axum::http::StatusCode;
use crate::{ use crate::{
db::get_pool, db::get_pool,
templates::{CreateUser, CreateUserSuccess}, templates::{CreateUser, CreateUserSuccess},
@ -248,13 +250,15 @@ mod test {
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(GOOD_FORM); let body = massage(GOOD_FORM);
let _resp = server let resp = server
.post("/signup") .post("/signup")
.expect_failure() // 303 is "failure" .expect_failure() // 303 is "failure"
.bytes(body) .bytes(body)
.content_type(FORM_CONTENT_TYPE) .content_type(FORM_CONTENT_TYPE)
.await; .await;
assert_eq!(StatusCode::SEE_OTHER, resp.status_code());
// get the new user from the db // get the new user from the db
let user = User::try_get("test_user", &pool).await; let user = User::try_get("test_user", &pool).await;
assert!(user.is_ok()); assert!(user.is_ok());
@ -292,10 +296,8 @@ mod test {
// honestly this is basically the whole suite here // honestly this is basically the whole suite here
//-************************************************************************ //-************************************************************************
mod failure { mod failure {
use axum::http::StatusCode;
use super::*; use super::*;
use crate::signup::CreateUserError; use crate::signup::{CreateUserError, CreateUserErrorKind};
// various ways to fuck up signup // various ways to fuck up signup
const PASSWORD_MISMATCH_FORM: &str = const PASSWORD_MISMATCH_FORM: &str =
@ -328,8 +330,7 @@ mod test {
assert!(user.is_err()); assert!(user.is_err());
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let expected = let expected = CreateUserError(CreateUserErrorKind::PasswordMismatch).to_string();
CreateUserError(crate::signup::CreateUserErrorKind::PasswordMismatch).to_string();
assert_eq!(&expected, body); assert_eq!(&expected, body);
} }
@ -352,8 +353,7 @@ mod test {
assert!(user.is_err()); assert!(user.is_err());
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let expected = let expected = CreateUserError(CreateUserErrorKind::BadPassword).to_string();
CreateUserError(crate::signup::CreateUserErrorKind::BadPassword).to_string();
assert_eq!(&expected, body); assert_eq!(&expected, body);
} }
@ -376,8 +376,7 @@ mod test {
assert!(user.is_err()); assert!(user.is_err());
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let expected = let expected = CreateUserError(CreateUserErrorKind::BadPassword).to_string();
CreateUserError(crate::signup::CreateUserErrorKind::BadPassword).to_string();
assert_eq!(&expected, body); assert_eq!(&expected, body);
} }
@ -400,8 +399,7 @@ mod test {
assert!(user.is_err()); assert!(user.is_err());
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let expected = let expected = CreateUserError(CreateUserErrorKind::BadUsername).to_string();
CreateUserError(crate::signup::CreateUserErrorKind::BadUsername).to_string();
assert_eq!(&expected, body); assert_eq!(&expected, body);
} }
@ -424,8 +422,38 @@ mod test {
assert!(user.is_err()); assert!(user.is_err());
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let expected = let expected = CreateUserError(CreateUserErrorKind::BadUsername).to_string();
CreateUserError(crate::signup::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); assert_eq!(&expected, body);
} }
@ -448,8 +476,7 @@ mod test {
assert!(user.is_err()); assert!(user.is_err());
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let expected = let expected = CreateUserError(CreateUserErrorKind::BadDisplayname).to_string();
CreateUserError(crate::signup::CreateUserErrorKind::BadDisplayname).to_string();
assert_eq!(&expected, body); assert_eq!(&expected, body);
} }