Skip to content

Commit

Permalink
Add update_modifies_session_id_test
Browse files Browse the repository at this point in the history
  • Loading branch information
sabotack committed Nov 29, 2023
1 parent 18fc93f commit 554136c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
38 changes: 25 additions & 13 deletions src/tests/database/in_use_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,29 +179,37 @@ 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())
.exec(&in_use_context.db_context.get_connection())
.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())
Expand All @@ -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]
Expand Down

0 comments on commit 554136c

Please sign in to comment.