Skip to content

Commit

Permalink
refactor: API category context tetests
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 14, 2023
1 parent 03dba5c commit 6f9c1a2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
75 changes: 41 additions & 34 deletions tests/e2e/contexts/category/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}

Expand All @@ -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");
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/contexts/category/steps.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit 6f9c1a2

Please sign in to comment.