Skip to content

Commit 6fc6872

Browse files
committed
refactor(api): [#197] make Axum implementation the default one
1 parent a662f9c commit 6fc6872

File tree

10 files changed

+317
-275
lines changed

10 files changed

+317
-275
lines changed

.github/workflows/develop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
- name: E2E Tests
3333
run: ./docker/bin/run-e2e-tests.sh
3434
env:
35-
TORRUST_IDX_BACK_E2E_EXCLUDE_AXUM_IMPL: "true"
35+
TORRUST_IDX_BACK_E2E_EXCLUDE_ACTIX_WEB_IMPL: "true"

src/bin/main.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ use torrust_index_backend::web::api::Implementation;
66
async fn main() -> Result<(), std::io::Error> {
77
let configuration = init_configuration().await;
88

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

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

tests/e2e/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub const ENV_VAR_E2E_SHARED: &str = "TORRUST_IDX_BACK_E2E_SHARED";
1414
/// The whole `config.toml` file content. It has priority over the config file.
1515
pub const ENV_VAR_E2E_CONFIG: &str = "TORRUST_IDX_BACK_E2E_CONFIG";
1616

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

2020
// Default values
2121

tests/e2e/contexts/about/contract.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
//! API contract for `about` context.
2+
use std::env;
3+
24
use torrust_index_backend::web::api;
35

46
use crate::common::asserts::{assert_response_title, assert_text_ok};
57
use crate::common::client::Client;
8+
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL;
69
use crate::e2e::environment::TestEnv;
710

811
#[tokio::test]
912
async fn it_should_load_the_about_page_with_information_about_the_api() {
1013
let mut env = TestEnv::new();
1114
env.start(api::Implementation::ActixWeb).await;
15+
16+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
17+
println!("Skipped");
18+
return;
19+
}
20+
1221
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
1322

1423
let response = client.about().await;
@@ -21,6 +30,12 @@ async fn it_should_load_the_about_page_with_information_about_the_api() {
2130
async fn it_should_load_the_license_page_at_the_api_entrypoint() {
2231
let mut env = TestEnv::new();
2332
env.start(api::Implementation::ActixWeb).await;
33+
34+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
35+
println!("Skipped");
36+
return;
37+
}
38+
2439
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
2540

2641
let response = client.license().await;

tests/e2e/contexts/category/contract.rs

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! API contract for `category` context.
2+
use std::env;
3+
24
use torrust_index_backend::web::api;
35

46
use crate::common::asserts::assert_json_ok_response;
57
use crate::common::client::Client;
68
use crate::common::contexts::category::fixtures::random_category_name;
79
use crate::common::contexts::category::forms::{AddCategoryForm, DeleteCategoryForm};
810
use crate::common::contexts::category::responses::{AddedCategoryResponse, ListResponse};
11+
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL;
912
use crate::e2e::contexts::category::steps::{add_category, add_random_category};
1013
use crate::e2e::contexts::user::steps::{new_logged_in_admin, new_logged_in_user};
1114
use crate::e2e::environment::TestEnv;
@@ -14,6 +17,12 @@ use crate::e2e::environment::TestEnv;
1417
async fn it_should_return_an_empty_category_list_when_there_are_no_categories() {
1518
let mut env = TestEnv::new();
1619
env.start(api::Implementation::ActixWeb).await;
20+
21+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
22+
println!("Skipped");
23+
return;
24+
}
25+
1726
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
1827

1928
let response = client.get_categories().await;
@@ -25,6 +34,12 @@ async fn it_should_return_an_empty_category_list_when_there_are_no_categories()
2534
async fn it_should_return_a_category_list() {
2635
let mut env = TestEnv::new();
2736
env.start(api::Implementation::ActixWeb).await;
37+
38+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
39+
println!("Skipped");
40+
return;
41+
}
42+
2843
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
2944

3045
add_random_category(&env).await;
@@ -47,6 +62,12 @@ async fn it_should_return_a_category_list() {
4762
async fn it_should_not_allow_adding_a_new_category_to_unauthenticated_users() {
4863
let mut env = TestEnv::new();
4964
env.start(api::Implementation::ActixWeb).await;
65+
66+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
67+
println!("Skipped");
68+
return;
69+
}
70+
5071
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
5172

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

88+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
89+
println!("Skipped");
90+
return;
91+
}
92+
6793
let logged_non_admin = new_logged_in_user(&env).await;
6894

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

112+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
113+
println!("Skipped");
114+
return;
115+
}
116+
86117
let logged_in_admin = new_logged_in_admin(&env).await;
87118
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
88119

@@ -111,6 +142,11 @@ async fn it_should_allow_adding_empty_categories() {
111142
let mut env = TestEnv::new();
112143
env.start(api::Implementation::ActixWeb).await;
113144

145+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
146+
println!("Skipped");
147+
return;
148+
}
149+
114150
if env.is_shared() {
115151
// This test cannot be run in a shared test env because it will fail
116152
// when the empty category already exits
@@ -144,6 +180,11 @@ async fn it_should_not_allow_adding_duplicated_categories() {
144180
let mut env = TestEnv::new();
145181
env.start(api::Implementation::ActixWeb).await;
146182

183+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
184+
println!("Skipped");
185+
return;
186+
}
187+
147188
let added_category_name = add_random_category(&env).await;
148189

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

200+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
201+
println!("Skipped");
202+
return;
203+
}
204+
159205
let logged_in_admin = new_logged_in_admin(&env).await;
160206
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
161207

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

231+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
232+
println!("Skipped");
233+
return;
234+
}
235+
185236
let added_category_name = add_random_category(&env).await;
186237

187238
let logged_in_non_admin = new_logged_in_user(&env).await;
@@ -201,6 +252,12 @@ async fn it_should_not_allow_non_admins_to_delete_categories() {
201252
async fn it_should_not_allow_guests_to_delete_categories() {
202253
let mut env = TestEnv::new();
203254
env.start(api::Implementation::ActixWeb).await;
255+
256+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
257+
println!("Skipped");
258+
return;
259+
}
260+
204261
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
205262

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

218275
mod with_axum_implementation {
219-
use std::env;
220276

221277
use torrust_index_backend::web::api;
222278

@@ -226,7 +282,6 @@ mod with_axum_implementation {
226282
use crate::common::contexts::category::fixtures::random_category_name;
227283
use crate::common::contexts::category::forms::{AddCategoryForm, DeleteCategoryForm};
228284
use crate::common::contexts::category::responses::ListResponse;
229-
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_AXUM_IMPL;
230285
use crate::e2e::contexts::category::steps::{add_category, add_random_category};
231286
use crate::e2e::contexts::user::steps::{new_logged_in_admin, new_logged_in_user};
232287
use crate::e2e::environment::TestEnv;
@@ -248,11 +303,6 @@ mod with_axum_implementation {
248303
let mut env = TestEnv::new();
249304
env.start(api::Implementation::Axum).await;
250305

251-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
252-
println!("Skipped");
253-
return;
254-
}
255-
256306
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
257307

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

279-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
280-
println!("Skipped");
281-
return;
282-
}
283-
284329
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
285330

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

301-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
302-
println!("Skipped");
303-
return;
304-
}
305-
306346
let logged_non_admin = new_logged_in_user(&env).await;
307347

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

325-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
326-
println!("Skipped");
327-
return;
328-
}
329-
330365
let logged_in_admin = new_logged_in_admin(&env).await;
331366
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
332367

@@ -356,11 +391,6 @@ mod with_axum_implementation {
356391
return;
357392
}
358393

359-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
360-
println!("Skipped");
361-
return;
362-
}
363-
364394
let logged_in_admin = new_logged_in_admin(&env).await;
365395
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
366396

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

384-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
385-
println!("Skipped");
386-
return;
387-
}
388-
389414
let added_category_name = add_random_category(&env).await;
390415

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

402-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
403-
println!("Skipped");
404-
return;
405-
}
406-
407427
let logged_in_admin = new_logged_in_admin(&env).await;
408428
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
409429

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

427-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
428-
println!("Skipped");
429-
return;
430-
}
431-
432447
let added_category_name = add_random_category(&env).await;
433448

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

452-
if env::var(ENV_VAR_E2E_EXCLUDE_AXUM_IMPL).is_ok() {
453-
println!("Skipped");
454-
return;
455-
}
456-
457467
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
458468

459469
let added_category_name = add_random_category(&env).await;

tests/e2e/contexts/root/contract.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
//! API contract for `root` context.
2+
use std::env;
3+
24
use torrust_index_backend::web::api;
35

46
use crate::common::asserts::{assert_response_title, assert_text_ok};
57
use crate::common::client::Client;
8+
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL;
69
use crate::e2e::environment::TestEnv;
710

811
#[tokio::test]
912
async fn it_should_load_the_about_page_at_the_api_entrypoint() {
1013
let mut env = TestEnv::new();
1114
env.start(api::Implementation::ActixWeb).await;
15+
16+
if env::var(ENV_VAR_E2E_EXCLUDE_ACTIX_WEB_IMPL).is_ok() {
17+
println!("Skipped");
18+
return;
19+
}
20+
1221
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
1322

1423
let response = client.root().await;

0 commit comments

Comments
 (0)