From 554136c22d3888774c2f7dcaa60d3a6846d69986 Mon Sep 17 00:00:00 2001 From: sabotack Date: Wed, 29 Nov 2023 10:33:14 +0100 Subject: [PATCH] Add update_modifies_session_id_test --- src/api/ecdar_api.rs | 3 ++- src/tests/database/in_use_context.rs | 38 ++++++++++++++++++---------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/api/ecdar_api.rs b/src/api/ecdar_api.rs index ea074ab..ad8fe3c 100644 --- a/src/api/ecdar_api.rs +++ b/src/api/ecdar_api.rs @@ -200,7 +200,8 @@ impl EcdarApi for ConcreteEcdarApi { match self.contexts.in_use_context.get_by_id(model.id).await { Ok(Some(in_use)) => { // Check if in_use latest activity is older than the max allowed - if in_use.latest_activity > (Utc::now().naive_utc() - Duration::minutes(IN_USE_DURATION_MINUTES)) + if in_use.latest_activity + > (Utc::now().naive_utc() - Duration::minutes(IN_USE_DURATION_MINUTES)) && in_use.session_id != session.id { return Err(Status::failed_precondition( diff --git a/src/tests/database/in_use_context.rs b/src/tests/database/in_use_context.rs index dee9275..e3da4aa 100644 --- a/src/tests/database/in_use_context.rs +++ b/src/tests/database/in_use_context.rs @@ -179,7 +179,7 @@ mod database_tests { } #[tokio::test] - async fn update_does_not_modify_model_id_test() { + async fn update_modifies_session_id_test() { let (in_use_context, in_use, _, _, _) = seed_db().await; in_use::Entity::insert(in_use.clone().into_active_model()) @@ -187,21 +187,29 @@ mod database_tests { .await .unwrap(); - let updated_in_use = in_use::Model { - model_id: in_use.model_id + 1, - ..in_use.clone() + let mut session2 = create_sessions(1, in_use.session_id)[0].clone(); + session2.id = in_use.session_id + 1; + session2.refresh_token = "new_refresh_token".to_string(); + session2.access_token = "new_access_token".to_string(); + + session::Entity::insert(session2.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); + + let new_in_use = in_use::Model { + session_id: in_use.session_id + 1, + ..in_use }; - let updated_in_use = in_use_context.update(updated_in_use.clone()).await; + let updated_in_use = in_use_context.update(new_in_use.clone()).await.unwrap(); - assert!(matches!( - updated_in_use.unwrap_err(), - DbErr::RecordNotUpdated - )); + assert_ne!(in_use, updated_in_use); + assert_ne!(in_use, new_in_use); } #[tokio::test] - async fn update_does_not_modify_session_id_test() { + async fn update_does_not_modify_model_id_test() { let (in_use_context, in_use, _, _, _) = seed_db().await; in_use::Entity::insert(in_use.clone().into_active_model()) @@ -210,12 +218,16 @@ mod database_tests { .unwrap(); let updated_in_use = in_use::Model { - session_id: in_use.session_id + 1, + model_id: in_use.model_id + 1, ..in_use.clone() }; - let updated_in_use = in_use_context.update(updated_in_use.clone()).await.unwrap(); - assert_eq!(in_use, updated_in_use); + let updated_in_use = in_use_context.update(updated_in_use.clone()).await; + + assert!(matches!( + updated_in_use.unwrap_err(), + DbErr::RecordNotUpdated + )); } #[tokio::test]