use axum::extract::{Json, State}; use crate::{ channel::{app, routes}, test::fixtures, }; #[tokio::test] async fn new_channel() { // Set up the environment let app = fixtures::scratch_app().await; let creator = fixtures::login::create(&app).await; // Call the endpoint let name = fixtures::channel::propose(); let request = routes::CreateRequest { name }; let Json(response_channel) = routes::on_create(State(app.clone()), creator, Json(request.clone())) .await .expect("new channel in an empty app"); // Verify the structure of the response assert_eq!(request.name, response_channel.name); // Verify the semantics let channels = app.channels().all().await.expect("always succeeds"); assert!(channels.contains(&response_channel)); } #[tokio::test] async fn duplicate_name() { // Set up the environment let app = fixtures::scratch_app().await; let creator = fixtures::login::create(&app).await; let channel = fixtures::channel::create(&app).await; // Call the endpoint let request = routes::CreateRequest { name: channel.name }; let routes::CreateError(error) = routes::on_create(State(app.clone()), creator, Json(request.clone())) .await .expect_err("duplicate channel name"); // Verify the structure of the response assert!(matches!( error, app::CreateError::DuplicateName(name) if request.name == name )); }