Skip to content

Commit

Permalink
feat(#173): 409
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Oct 3, 2024
1 parent 22448a3 commit 3aeb8b9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions server/src/handlers/register_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use log::info;
pub async fn register_user(
State(config): State<ServerConfig>,
Json(payload): Json<User>,
) -> Result<StatusCode, String> {
) -> Result<StatusCode, (StatusCode, String)> {
let mut newcomer = User::new(payload.login.clone());
let fakehub = &config.fakehub;
let github = fakehub.main();
Expand All @@ -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),
)),
}
}

Expand All @@ -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};

Expand Down Expand Up @@ -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"),
}
}
}

0 comments on commit 3aeb8b9

Please sign in to comment.