use serde_json::json; use types::organization::Organization; use uuid::Uuid; use crate::common::{Client, get_server}; mod common; #[tokio::test] async fn add_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); let organization = Organization { uuid: organization_uuid, name: "Test Organization".to_string(), }; let response = client .add_organization(json!( { "uuid": organization_uuid, "name": "Test Organization", } )) .await; response.assert_status_success(); response.assert_json(&organization); let response = client.get_organizations().await; response.assert_status_success(); let organizations: Vec = response.json(); assert_eq!(organizations.len(), 1); assert_eq!(organizations.first().unwrap(), &organization); } #[tokio::test] async fn add_organization_known_uuid() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); client .add_organization_json(json!( { "uuid": organization_uuid, "name": "Test Organization", } )) .await; let response = client .add_organization(json!( { "uuid": organization_uuid, "name": "Test Organization", } )) .await; response.assert_status_bad_request(); } #[tokio::test] async fn add_invalid_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); let response = client .add_organization(json!( { "uuid": organization_uuid, } )) .await; response.assert_status_unprocessable_entity(); } #[tokio::test] async fn leave_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); client .add_organization_json(json!( { "uuid": organization_uuid, "name": "Test Organization", } )) .await; let response = client .leave_organization(json!( { "uuid": organization_uuid, } )) .await; response.assert_status_success(); let organizations = client.get_organizations_json().await; assert_eq!(organizations.len(), 0); } #[tokio::test] async fn leave_unknown_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); let response = client .leave_organization(json!( { "uuid": organization_uuid, } )) .await; response.assert_status_bad_request(); } #[tokio::test] async fn join_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); client .add_organization_json(json!( { "uuid": organization_uuid, "name": "Test Organization", } )) .await; client .leave_organization(json!( { "uuid": organization_uuid, } )) .await .assert_status_success(); let response = client .join_organization(json!( { "uuid": organization_uuid, } )) .await; response.assert_status_success(); let organizations = client.get_organizations_json().await; assert_eq!(organizations.len(), 1); } #[tokio::test] async fn join_unknown_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); let response = client .join_organization(json!( { "uuid": organization_uuid, } )) .await; response.assert_status_bad_request(); } #[tokio::test] async fn join_already_joined_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); client .add_organization_json(json!( { "uuid": organization_uuid, "name": "Test Organization", } )) .await; client .leave_organization(json!( { "uuid": organization_uuid, } )) .await .assert_status_success(); client .join_organization(json!( { "uuid": organization_uuid, } )) .await .assert_status_success(); let response = client .join_organization(json!( { "uuid": organization_uuid, } )) .await; response.assert_status_bad_request(); } #[tokio::test] async fn leave_already_leaved_organization() { let server = get_server().await; let client = Client::new(&server); let organization_uuid = Uuid::new_v4(); client .add_organization_json(json!( { "uuid": organization_uuid, "name": "Test Organization", } )) .await; client .leave_organization(json!( { "uuid": organization_uuid, } )) .await .assert_status_success(); let response = client .leave_organization(json!( { "uuid": organization_uuid, } )) .await; response.assert_status_bad_request(); }