From 6f9c1a227da4d418dd887395a69fb05dd5507448 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 12 Jun 2023 15:52:58 +0100 Subject: [PATCH] refactor: API category context tetests --- tests/e2e/contexts/category/contract.rs | 75 ++++++++++++++----------- tests/e2e/contexts/category/steps.rs | 10 ++++ 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/tests/e2e/contexts/category/contract.rs b/tests/e2e/contexts/category/contract.rs index b5682327..43d854c9 100644 --- a/tests/e2e/contexts/category/contract.rs +++ b/tests/e2e/contexts/category/contract.rs @@ -6,17 +6,10 @@ use crate::common::client::Client; use crate::common::contexts::category::fixtures::random_category_name; use crate::common::contexts::category::forms::{AddCategoryForm, DeleteCategoryForm}; use crate::common::contexts::category::responses::{AddedCategoryResponse, ListResponse}; -use crate::e2e::contexts::category::steps::add_category; +use crate::e2e::contexts::category::steps::{add_category, add_random_category}; use crate::e2e::contexts::user::steps::{new_logged_in_admin, new_logged_in_user}; use crate::e2e::environment::TestEnv; -/* todo: - - it should allow adding a new category to authenticated clients - - it should not allow adding a new category with an empty name - - it should allow adding a new category with an optional icon - - ... -*/ - #[tokio::test] async fn it_should_return_an_empty_category_list_when_there_are_no_categories() { let mut env = TestEnv::new(); @@ -34,17 +27,15 @@ async fn it_should_return_a_category_list() { env.start(api::Implementation::ActixWeb).await; let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); - // Add a category - let category_name = random_category_name(); - let response = add_category(&category_name, &env).await; - assert_eq!(response.status, 200); + add_random_category(&env).await; let response = client.get_categories().await; let res: ListResponse = serde_json::from_str(&response.body).unwrap(); // There should be at least the category we added. - // Since this is an E2E test, there might be more categories. + // Since this is an E2E test and it could be run in a shared test env, + // there might be more categories. assert!(res.data.len() > 1); if let Some(content_type) = &response.content_type { assert_eq!(content_type, "application/json"); @@ -114,17 +105,42 @@ async fn it_should_allow_admins_to_add_new_categories() { } #[tokio::test] -async fn it_should_not_allow_adding_duplicated_categories() { +async fn it_should_allow_adding_empty_categories() { + // code-review: this is a bit weird, is it a intended behavior? + let mut env = TestEnv::new(); env.start(api::Implementation::ActixWeb).await; - // Add a category - let random_category_name = random_category_name(); - let response = add_category(&random_category_name, &env).await; + let logged_in_admin = new_logged_in_admin(&env).await; + let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); + + let category_name = String::new(); + + let response = client + .add_category(AddCategoryForm { + name: category_name.to_string(), + icon: None, + }) + .await; + + let res: AddedCategoryResponse = serde_json::from_str(&response.body).unwrap(); + + assert_eq!(res.data, category_name); + if let Some(content_type) = &response.content_type { + assert_eq!(content_type, "application/json"); + } assert_eq!(response.status, 200); +} + +#[tokio::test] +async fn it_should_not_allow_adding_duplicated_categories() { + let mut env = TestEnv::new(); + env.start(api::Implementation::ActixWeb).await; + + let added_category_name = add_random_category(&env).await; // Try to add the same category again - let response = add_category(&random_category_name, &env).await; + let response = add_category(&added_category_name, &env).await; assert_eq!(response.status, 400); } @@ -136,21 +152,18 @@ async fn it_should_allow_admins_to_delete_categories() { let logged_in_admin = new_logged_in_admin(&env).await; let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); - // Add a category - let category_name = random_category_name(); - let response = add_category(&category_name, &env).await; - assert_eq!(response.status, 200); + let added_category_name = add_random_category(&env).await; let response = client .delete_category(DeleteCategoryForm { - name: category_name.to_string(), + name: added_category_name.to_string(), icon: None, }) .await; let res: AddedCategoryResponse = serde_json::from_str(&response.body).unwrap(); - assert_eq!(res.data, category_name); + assert_eq!(res.data, added_category_name); if let Some(content_type) = &response.content_type { assert_eq!(content_type, "application/json"); } @@ -162,17 +175,14 @@ async fn it_should_not_allow_non_admins_to_delete_categories() { let mut env = TestEnv::new(); env.start(api::Implementation::ActixWeb).await; - // Add a category - let category_name = random_category_name(); - let response = add_category(&category_name, &env).await; - assert_eq!(response.status, 200); + let added_category_name = add_random_category(&env).await; let logged_in_non_admin = new_logged_in_user(&env).await; let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_non_admin.token); let response = client .delete_category(DeleteCategoryForm { - name: category_name.to_string(), + name: added_category_name.to_string(), icon: None, }) .await; @@ -186,14 +196,11 @@ async fn it_should_not_allow_guests_to_delete_categories() { env.start(api::Implementation::ActixWeb).await; let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); - // Add a category - let category_name = random_category_name(); - let response = add_category(&category_name, &env).await; - assert_eq!(response.status, 200); + let added_category_name = add_random_category(&env).await; let response = client .delete_category(DeleteCategoryForm { - name: category_name.to_string(), + name: added_category_name.to_string(), icon: None, }) .await; diff --git a/tests/e2e/contexts/category/steps.rs b/tests/e2e/contexts/category/steps.rs index 2150a7a8..321cdddc 100644 --- a/tests/e2e/contexts/category/steps.rs +++ b/tests/e2e/contexts/category/steps.rs @@ -1,9 +1,19 @@ use crate::common::client::Client; +use crate::common::contexts::category::fixtures::random_category_name; use crate::common::contexts::category::forms::AddCategoryForm; +use crate::common::contexts::category::responses::AddedCategoryResponse; use crate::common::responses::TextResponse; use crate::e2e::contexts::user::steps::new_logged_in_admin; use crate::e2e::environment::TestEnv; +/// Add a random category and return its name. +pub async fn add_random_category(env: &TestEnv) -> String { + let category_name = random_category_name(); + let response = add_category(&category_name, env).await; + let res: AddedCategoryResponse = serde_json::from_str(&response.body).unwrap(); + res.data +} + pub async fn add_category(category_name: &str, env: &TestEnv) -> TextResponse { let logged_in_admin = new_logged_in_admin(env).await; let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);