diff --git a/server/src/handlers/register_user.rs b/server/src/handlers/register_user.rs index 0310557f..3ac4b12e 100644 --- a/server/src/handlers/register_user.rs +++ b/server/src/handlers/register_user.rs @@ -34,7 +34,7 @@ use log::info; pub async fn register_user( State(config): State, Json(payload): Json, -) -> Result { +) -> Result { let mut newcomer = User::new(payload.login.clone()); let fakehub = &config.fakehub; let github = fakehub.main(); @@ -47,7 +47,10 @@ pub async fn register_user( info!("New user is here. Hello @{}", newcomer.login); Ok(StatusCode::CREATED) } - Err(e) => Err(format!("Can't register user @{}: {}", newcomer.login, e)), + Err(e) => Err(( + StatusCode::CONFLICT, + format!("Can't register user @{}: {}", newcomer.login, e), + )), } } @@ -59,6 +62,7 @@ mod tests { use crate::ServerConfig; use anyhow::Result; use axum::extract::State; + use axum::http::StatusCode; use axum::Json; use hamcrest::{equal_to, is, HamcrestMatcher}; @@ -122,4 +126,20 @@ mod tests { .await .expect("Failed to register user"); } + + #[tokio::test] + async fn returns_409_when_user_exists() -> Result<()> { + let server = ServerConfig { + fakehub: FakeHub::default(), + }; + let state = State(server); + let result = register_user(state, Json(User::new(String::from("jeff")))).await; + match result { + Err((status, _)) => { + assert_that!(status, is(equal_to(StatusCode::CONFLICT))); + Ok(()) + } + Ok(_) => panic!("Expected conflict but got success"), + } + } }