Skip to content

Commit 49c63ff

Browse files
committed
editoast: views: split a few tests in smaller ones
Signed-off-by: Loup Federico <16464925+Sh099078@users.noreply.github.com>
1 parent 6e56d92 commit 49c63ff

2 files changed

Lines changed: 41 additions & 24 deletions

File tree

editoast/src/views/infra/mod.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,30 +1011,39 @@ pub mod tests {
10111011
}
10121012

10131013
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
1014-
async fn infra_list_filters_authorized_infras() {
1014+
async fn infra_list_user_only_sees_its_related_infras() {
10151015
let app = test_app!().build();
10161016
let db_pool = app.db_pool();
1017-
let infra = create_small_infra(&mut db_pool.get_ok()).await;
1018-
let infra_no_grant = create_small_infra(&mut db_pool.get_ok()).await;
1019-
1020-
// Regular user with the correct roles should see only the infra he is associated with:
1017+
let infra_1 = create_small_infra(&mut db_pool.get_ok()).await;
1018+
let infra_2 = create_small_infra(&mut db_pool.get_ok()).await;
1019+
let _infra_no_grant = create_small_infra(&mut db_pool.get_ok()).await;
10211020
let user = app
10221021
.user("user_identity", "user_name")
1023-
.with_infra_grant(infra.id, InfraGrant::Reader)
1022+
.with_infra_grant(infra_1.id, InfraGrant::Reader)
1023+
.with_infra_grant(infra_2.id, InfraGrant::Reader)
10241024
.create()
10251025
.await;
10261026
let response: InfraListResponse = app
10271027
.get("/infra/")
10281028
.by_user(user.as_ref())
10291029
.await
1030-
.assert_status(StatusCode::OK)
1030+
.assert_status_ok()
10311031
.json();
10321032
assert_eq!(
1033-
response.results.iter().map(|infra| infra.id).collect_vec(),
1034-
vec![infra.id]
1033+
response
1034+
.results
1035+
.iter()
1036+
.map(|infra| infra.id)
1037+
.collect::<Vec<_>>(),
1038+
vec![infra_1.id, infra_2.id]
10351039
);
1040+
}
10361041

1037-
// An admin should see all the infras:
1042+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
1043+
async fn infra_list_admin_can_see_unrelated_infra() {
1044+
let app = test_app!().build();
1045+
let db_pool = app.db_pool();
1046+
let infra_no_grant = create_small_infra(&mut db_pool.get_ok()).await;
10381047
let admin = app
10391048
.user("admin", "admin")
10401049
.with_roles([Role::Admin])
@@ -1044,11 +1053,15 @@ pub mod tests {
10441053
.get("/infra/")
10451054
.by_user(admin.as_ref())
10461055
.await
1047-
.assert_status(StatusCode::OK)
1056+
.assert_status_ok()
10481057
.json();
10491058
assert_eq!(
1050-
response.results.iter().map(|infra| infra.id).collect_vec(),
1051-
vec![infra.id, infra_no_grant.id]
1059+
response
1060+
.results
1061+
.iter()
1062+
.map(|infra| infra.id)
1063+
.collect::<Vec<_>>(),
1064+
vec![infra_no_grant.id]
10521065
);
10531066
}
10541067

editoast/src/views/rolling_stock/light.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,17 +369,16 @@ mod tests {
369369
}
370370

371371
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
372-
async fn rolling_stock_list_filters_authorized_rolling_stocks() {
372+
async fn rolling_stock_list_user_only_sees_its_related_rolling_stocks() {
373373
let app = test_app!().build();
374374
let db_pool = app.db_pool();
375-
let rolling_stock_grant =
376-
create_fast_rolling_stock(&mut db_pool.get_ok(), "fast_rolling_stock_grant").await;
377-
let rolling_stock_no_grant =
378-
create_fast_rolling_stock(&mut db_pool.get_ok(), "fast_rolling_stock_no_grant").await;
379-
// Regular user with the correct roles should see only the rolling stock he is associated with:
375+
let rs_1 = create_fast_rolling_stock(&mut db_pool.get_ok(), "rs_1").await;
376+
let rs_2 = create_fast_rolling_stock(&mut db_pool.get_ok(), "rs_2").await;
377+
let _rs_no_grant = create_fast_rolling_stock(&mut db_pool.get_ok(), "rs_no_grant").await;
380378
let user = app
381379
.user("user_identity", "user_name")
382-
.with_rolling_stock_grant(rolling_stock_grant.id, RollingStockGrant::Reader)
380+
.with_rolling_stock_grant(rs_1.id, RollingStockGrant::Reader)
381+
.with_rolling_stock_grant(rs_2.id, RollingStockGrant::Reader)
383382
.create()
384383
.await;
385384
let response: LightRollingStockWithLiveriesCountList = app
@@ -394,10 +393,15 @@ mod tests {
394393
.iter()
395394
.map(|rolling_stock| rolling_stock.rolling_stock.id)
396395
.collect::<Vec<_>>(),
397-
vec![rolling_stock_grant.id]
396+
vec![rs_1.id, rs_2.id]
398397
);
399-
// TODO: separate in two tests (admins_can_list_all_rolling_stocks and user_can_list_their_rolling_stocks)
400-
// An admin should see all the rolling stocks:
398+
}
399+
400+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
401+
async fn rolling_stock_list_admin_can_see_unrelated_rolling_stock() {
402+
let app = test_app!().build();
403+
let db_pool = app.db_pool();
404+
let rs_no_grant = create_fast_rolling_stock(&mut db_pool.get_ok(), "rs_no_grant").await;
401405
let admin = app
402406
.user("admin", "admin")
403407
.with_roles([Role::Admin])
@@ -415,7 +419,7 @@ mod tests {
415419
.iter()
416420
.map(|rolling_stock| rolling_stock.rolling_stock.id)
417421
.collect::<Vec<_>>(),
418-
vec![rolling_stock_grant.id, rolling_stock_no_grant.id]
422+
vec![rs_no_grant.id]
419423
);
420424
}
421425

0 commit comments

Comments
 (0)