Skip to content

Commit

Permalink
refactor(api): [#197] make Axum implementation the default one
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 20, 2023
1 parent a662f9c commit 6fc6872
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 275 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
- name: E2E Tests
run: ./docker/bin/run-e2e-tests.sh
env:
TORRUST_IDX_BACK_E2E_EXCLUDE_AXUM_IMPL: "true"
TORRUST_IDX_BACK_E2E_EXCLUDE_ACTIX_WEB_IMPL: "true"
6 changes: 1 addition & 5 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use torrust_index_backend::web::api::Implementation;
async fn main() -> Result<(), std::io::Error> {
let configuration = init_configuration().await;

// todo: we are migrating from actix-web to axum, so we need to keep both
// implementations for a while. For production we only use ActixWeb.
// Once the Axum implementation is finished and stable, we can switch to it
// and remove the ActixWeb implementation.
let api_implementation = Implementation::ActixWeb;
let api_implementation = Implementation::Axum;

let app = app::run(configuration, &api_implementation).await;

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub const ENV_VAR_E2E_SHARED: &str = "TORRUST_IDX_BACK_E2E_SHARED";
/// The whole `config.toml` file content. It has priority over the config file.
pub const ENV_VAR_E2E_CONFIG: &str = "TORRUST_IDX_BACK_E2E_CONFIG";

/// If present, E2E tests for new Axum implementation will not be executed
pub const ENV_VAR_E2E_EXCLUDE_AXUM_IMPL: &str = "TORRUST_IDX_BACK_E2E_EXCLUDE_AXUM_IMPL";
/// If present, E2E tests for new `ActixWeb` implementation will not be executed
pub const ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL: &str = "TORRUST_IDX_BACK_E2E_EXCLUDE_ACTIX_WEB_IMPL";

// Default values

Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/contexts/about/contract.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
//! API contract for `about` context.
use std::env;

use torrust_index_backend::web::api;

use crate::common::asserts::{assert_response_title, assert_text_ok};
use crate::common::client::Client;
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL;
use crate::e2e::environment::TestEnv;

#[tokio::test]
async fn it_should_load_the_about_page_with_information_about_the_api() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.about().await;
Expand All @@ -21,6 +30,12 @@ async fn it_should_load_the_about_page_with_information_about_the_api() {
async fn it_should_load_the_license_page_at_the_api_entrypoint() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.license().await;
Expand Down
104 changes: 57 additions & 47 deletions tests/e2e/contexts/category/contract.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! API contract for `category` context.
use std::env;

use torrust_index_backend::web::api;

use crate::common::asserts::assert_json_ok_response;
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::config::ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL;
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;
Expand All @@ -14,6 +17,12 @@ use crate::e2e::environment::TestEnv;
async fn it_should_return_an_empty_category_list_when_there_are_no_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.get_categories().await;
Expand All @@ -25,6 +34,12 @@ async fn it_should_return_an_empty_category_list_when_there_are_no_categories()
async fn it_should_return_a_category_list() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

add_random_category(&env).await;
Expand All @@ -47,6 +62,12 @@ async fn it_should_return_a_category_list() {
async fn it_should_not_allow_adding_a_new_category_to_unauthenticated_users() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client
Expand All @@ -64,6 +85,11 @@ async fn it_should_not_allow_adding_a_new_category_to_non_admins() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let logged_non_admin = new_logged_in_user(&env).await;

let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_non_admin.token);
Expand All @@ -83,6 +109,11 @@ async fn it_should_allow_admins_to_add_new_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

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 Expand Up @@ -111,6 +142,11 @@ async fn it_should_allow_adding_empty_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

if env.is_shared() {
// This test cannot be run in a shared test env because it will fail
// when the empty category already exits
Expand Down Expand Up @@ -144,6 +180,11 @@ async fn it_should_not_allow_adding_duplicated_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let added_category_name = add_random_category(&env).await;

// Try to add the same category again
Expand All @@ -156,6 +197,11 @@ async fn it_should_allow_admins_to_delete_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let logged_in_admin = new_logged_in_admin(&env).await;
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);

Expand All @@ -182,6 +228,11 @@ async fn it_should_not_allow_non_admins_to_delete_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let added_category_name = add_random_category(&env).await;

let logged_in_non_admin = new_logged_in_user(&env).await;
Expand All @@ -201,6 +252,12 @@ async fn it_should_not_allow_non_admins_to_delete_categories() {
async fn it_should_not_allow_guests_to_delete_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let added_category_name = add_random_category(&env).await;
Expand All @@ -216,7 +273,6 @@ async fn it_should_not_allow_guests_to_delete_categories() {
}

mod with_axum_implementation {
use std::env;

use torrust_index_backend::web::api;

Expand All @@ -226,7 +282,6 @@ mod with_axum_implementation {
use crate::common::contexts::category::fixtures::random_category_name;
use crate::common::contexts::category::forms::{AddCategoryForm, DeleteCategoryForm};
use crate::common::contexts::category::responses::ListResponse;
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_AXUM_IMPL;
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;
Expand All @@ -248,11 +303,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

add_random_category(&env).await;
Expand All @@ -276,11 +326,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client
Expand All @@ -298,11 +343,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let logged_non_admin = new_logged_in_user(&env).await;

let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_non_admin.token);
Expand All @@ -322,11 +362,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

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 Expand Up @@ -356,11 +391,6 @@ mod with_axum_implementation {
return;
}

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let logged_in_admin = new_logged_in_admin(&env).await;
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);

Expand All @@ -381,11 +411,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let added_category_name = add_random_category(&env).await;

// Try to add the same category again
Expand All @@ -399,11 +424,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let logged_in_admin = new_logged_in_admin(&env).await;
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);

Expand All @@ -424,11 +444,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let added_category_name = add_random_category(&env).await;

let logged_in_non_admin = new_logged_in_user(&env).await;
Expand All @@ -449,11 +464,6 @@ mod with_axum_implementation {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let added_category_name = add_random_category(&env).await;
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/contexts/root/contract.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
//! API contract for `root` context.
use std::env;

use torrust_index_backend::web::api;

use crate::common::asserts::{assert_response_title, assert_text_ok};
use crate::common::client::Client;
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL;
use crate::e2e::environment::TestEnv;

#[tokio::test]
async fn it_should_load_the_about_page_at_the_api_entrypoint() {
let mut env = TestEnv::new();
env.start(api::Implementation::ActixWeb).await;

if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
println!("Skipped");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.root().await;
Expand Down
Loading

0 comments on commit 6fc6872

Please sign in to comment.