diff --git a/web/src/router.rs b/web/src/router.rs index a2d6c65..1481466 100644 --- a/web/src/router.rs +++ b/web/src/router.rs @@ -38,7 +38,7 @@ pub fn static_routes() -> Router { mod organization_endpoints_tests { use super::*; use entity::organization; - use log::{LevelFilter, *}; + use log::LevelFilter; use sea_orm::{DatabaseBackend, MockDatabase, MockExecResult}; use serde_json::json; use service::{config::Config, logging::Logger}; @@ -153,9 +153,8 @@ mod organization_endpoints_tests { // Purpose: adds multiple Organization instances to a mock DB and tests that calling // the appropriate endpoint deletes instances specified by distinct IDs. #[tokio::test] - async fn delete_organizations_specified_by_id() -> anyhow::Result<()> { - let config = Config::default(); - let mut app_state = AppState::new(config); + async fn delete_an_organization_specified_by_id() -> anyhow::Result<()> { + let mut app_state = AppState::new(Config::default()); let organizations = [ vec![organization::Model { @@ -200,8 +199,6 @@ mod organization_endpoints_tests { let url = format!("http://{addr}/organizations/2"); let response = client.delete(url).send().await?.text().await?; - debug!("response: {:?}", response); - let organization_model2 = &organizations[0][0]; assert_eq!( @@ -237,8 +234,7 @@ mod organization_endpoints_tests { // the post endpoint supplying the appropriate instance as a JSON payload. #[tokio::test] async fn create_new_organizations_successfully() -> anyhow::Result<()> { - let config = Config::default(); - let mut app_state = AppState::new(config); + let mut app_state = AppState::new(Config::default()); let organizations = [ vec![organization::Model { @@ -313,4 +309,62 @@ mod organization_endpoints_tests { Ok(()) } + + // Purpose: adds multiple Organization instances to a mock DB and tests that calling + // the appropriate endpoint updates an instance specified by an ID. + #[tokio::test] + async fn update_an_organization_specified_by_id() -> anyhow::Result<()> { + let mut app_state = AppState::new(Config::default()); + + let organizations = [ + vec![organization::Model { + id: 2, + name: "Organization Two".to_owned(), + }], + vec![organization::Model { + id: 2, + name: "Updated Organization Two".to_owned(), + }], + ]; + + let exec_results = [MockExecResult { + last_insert_id: 2, + rows_affected: 1, + }]; + + let db = MockDatabase::new(DatabaseBackend::Postgres) + .append_query_results(organizations.clone()) + .append_exec_results(exec_results) + .into_connection(); + + app_state.set_db_conn(db); + let router = define_routes(app_state); + + let listener = TcpListener::bind("0.0.0.0:0".parse::()?).await?; + let addr = listener.local_addr()?; + + tokio::spawn(async move { + axum::serve(listener, router).await.unwrap(); + }); + + let client = reqwest::Client::new(); + + let updated_organization_model2 = organization::Model { + id: 2, + name: "Updated Organization Two".to_owned(), + }; + + let url = format!("http://{addr}/organizations/2"); + let response = client + .put(url) + .json(&updated_organization_model2) + .send() + .await? + .text() + .await?; + + assert_eq!(response, json!(updated_organization_model2).to_string()); + + Ok(()) + } }