Skip to content

Commit

Permalink
Adds a blackbox endpoint test to update an existing Organization inst…
Browse files Browse the repository at this point in the history
…ance.
  • Loading branch information
jhodapp committed Dec 30, 2023
1 parent a49059d commit 543dbe9
Showing 1 changed file with 62 additions and 8 deletions.
70 changes: 62 additions & 8 deletions web/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<SocketAddr>()?).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(())
}
}

0 comments on commit 543dbe9

Please sign in to comment.