From de7ef5ebb2e658b3d62cd984fd9f4702e41b6ef2 Mon Sep 17 00:00:00 2001 From: flo-ride <43076999+flo-ride@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:03:50 +0100 Subject: [PATCH] feat(tests): Add locations tests --- tests/location_tests.rs | 122 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/location_tests.rs diff --git a/tests/location_tests.rs b/tests/location_tests.rs new file mode 100644 index 0000000..b111a4c --- /dev/null +++ b/tests/location_tests.rs @@ -0,0 +1,122 @@ +mod utils; + +use axum::http::StatusCode; +use serde_json::json; +use utils::{containers::keycloak::User, create_basic_session, create_realm_session}; + +use crate::utils::containers::keycloak::{Client, Realm}; + +#[tokio::test(flavor = "multi_thread")] +async fn location_test_1() { + let realm = Realm { + name: "location_test".to_string(), + clients: vec![Client::default()], + users: vec![ + User { + username: "user_1".to_string(), + email: "user_1@example.com".to_string(), + ..Default::default() + }, + User { + username: "user_2".to_string(), + email: "user_2@example.com".to_string(), + ..Default::default() + }, + User { + username: "user_3".to_string(), + email: "user_3@example.com".to_string(), + ..Default::default() + }, + ], + }; + + let (mut server, _ids, _nodes) = create_basic_session(realm.clone()).await; + let cookies = create_realm_session(&mut server, realm.users).await; + + // POST /location new location + let response = server + .post("/location") + .json(&json!({ + "name": "Salle 401", + "category": "room", + })) + .add_cookie(cookies[0].clone()) + .await; + response.assert_status(StatusCode::CREATED); + let new_location_id = response.text(); + + let response = server.get(&format!("/location/{new_location_id}")).await; + response.assert_status_ok(); + response.assert_json_contains(&json!({ + "id": new_location_id, + "name": "Salle 401", + "category": "room", + "disabled": false, + })); + + let response = server.get("/location").await; + response.assert_status_ok(); + response.assert_json_contains(&json!({ + "current_page": 0, + "total_page": 1, + "locations": [ + { + "id": new_location_id, + "name": "Salle 401", + "category": "room", + "disabled": false, + } + ] + })); + + let response = server + .put(&format!("/location/{new_location_id}")) + .json(&json!({ + "name": "Distributeur 201", + "category": "dispenser" + })) + .add_cookie(cookies[0].clone()) + .await; + response.assert_status_ok(); + + let response = server.get(&format!("/location/{new_location_id}")).await; + response.assert_status_ok(); + response.assert_json_contains(&json!({ + "id": new_location_id, + "name": "Distributeur 201", + "category": "dispenser", + "disabled": false, + })); + + let response = server.get("/location").await; + response.assert_status_ok(); + response.assert_json_contains(&json!({ + "current_page": 0, + "total_page": 1, + "locations": [ + { + "id": new_location_id, + "name": "Distributeur 201", + "category": "dispenser", + "disabled": false, + } + ] + })); + + let response = server + .delete(&format!("/location/{new_location_id}")) + .add_cookie(cookies[0].clone()) + .await; + response.assert_status_ok(); + + let response = server.get(&format!("/location/{new_location_id}")).await; + response.assert_status_not_found(); + + let response = server.get("/location").await; + response.assert_status_ok(); + response.assert_json(&json!({ + "current_page": 0, + "total_page": 1, + "locations": [] + })); +}