diff --git a/migration/src/m20231012_094242_create_query_table.rs b/migration/src/m20231012_094242_create_query_table.rs index 419b730..d6d22f9 100644 --- a/migration/src/m20231012_094242_create_query_table.rs +++ b/migration/src/m20231012_094242_create_query_table.rs @@ -22,7 +22,12 @@ impl MigrationTrait for Migration { ) .col(ColumnDef::new(Query::String).string().not_null()) .col(ColumnDef::new(Query::Result).json()) - .col(ColumnDef::new(Query::Outdated).boolean().not_null().default(true)) + .col( + ColumnDef::new(Query::Outdated) + .boolean() + .not_null() + .default(true), + ) .col(ColumnDef::new(Query::ModelId).integer().not_null()) .foreign_key( ForeignKey::create() @@ -49,5 +54,5 @@ enum Query { String, Result, ModelId, - Outdated + Outdated, } diff --git a/src/api/ecdar_api.rs b/src/api/ecdar_api.rs index af82917..4921cf7 100644 --- a/src/api/ecdar_api.rs +++ b/src/api/ecdar_api.rs @@ -82,7 +82,6 @@ async fn handle_session( session.access_token = access_token.clone(); session.refresh_token = refresh_token.clone(); - session.updated_at = Local::now().naive_local(); match session_context.update(session).await { Ok(_) => (), diff --git a/src/database/session_context.rs b/src/database/session_context.rs index 09b6b97..01dd9dc 100644 --- a/src/database/session_context.rs +++ b/src/database/session_context.rs @@ -1,4 +1,4 @@ -use chrono::Utc; +use chrono::{Local, Utc}; use sea_orm::prelude::async_trait::async_trait; use sea_orm::ActiveValue::{Set, Unchanged}; use sea_orm::{ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter}; @@ -118,7 +118,7 @@ impl EntityContextTrait for SessionContext { refresh_token: Set(entity.refresh_token), access_token: Set(entity.access_token), user_id: Unchanged(entity.user_id), - updated_at: Set(Default::default()), + updated_at: Set(Local::now().naive_local()), } .update(&self.db_context.get_connection()) .await diff --git a/src/tests/api/ecdar_api.rs b/src/tests/api/ecdar_api.rs index 2c0ec91..75166d5 100644 --- a/src/tests/api/ecdar_api.rs +++ b/src/tests/api/ecdar_api.rs @@ -12,7 +12,6 @@ mod ecdar_api { use std::str::FromStr; use crate::tests::api::helpers::get_reset_concrete_ecdar_api; - use chrono::Local; use tonic::{metadata, Request}; #[tokio::test] @@ -292,7 +291,7 @@ mod ecdar_api { id: Default::default(), refresh_token: "test_refresh_token".to_string(), access_token: "test_access_token".to_string(), - updated_at: Local::now().naive_local(), + updated_at: Default::default(), user_id: 1, }) .await @@ -318,12 +317,15 @@ mod ecdar_api { }; let updated_session = api.session_context.get_by_id(1).await.unwrap().unwrap(); - assert!(dbg!(updated_session != old_session)); - assert!(dbg!(updated_session.refresh_token) == dbg!(expected_session.refresh_token)); - assert!(dbg!(updated_session.access_token) == dbg!(expected_session.access_token)); - assert!(dbg!(updated_session.updated_at) > dbg!(old_session.updated_at)); - assert!(dbg!(updated_session.user_id) == dbg!(expected_session.user_id)); - assert!(dbg!(updated_session.id) == dbg!(expected_session.id)); + assert_ne!(updated_session, old_session); + assert_eq!( + updated_session.refresh_token, + expected_session.refresh_token + ); + assert_eq!(updated_session.access_token, expected_session.access_token); + assert!(updated_session.updated_at > old_session.updated_at); + assert_eq!(updated_session.user_id, expected_session.user_id); + assert_eq!(updated_session.id, expected_session.id); } #[tokio::test] @@ -394,7 +396,7 @@ mod ecdar_api { api.user_context.create(user.clone()).await.unwrap(); - assert!(dbg!(handle_session( + assert!(handle_session( api.session_context.clone(), &get_auth_token_request, is_new_session, @@ -403,6 +405,6 @@ mod ecdar_api { user.id.to_string(), ) .await - .is_err())); + .is_err()); } } diff --git a/src/tests/database/session_context.rs b/src/tests/database/session_context.rs index b878cb0..70b6bff 100644 --- a/src/tests/database/session_context.rs +++ b/src/tests/database/session_context.rs @@ -10,6 +10,7 @@ mod database_tests { to_active_models, }; + use crate::database::session_context::SessionContextTrait; use chrono::{Duration, Utc}; async fn seed_db() -> (SessionContext, session::Model, user::Model, model::Model) { @@ -187,9 +188,9 @@ mod database_tests { .unwrap(); //A session has nothing to update - let new_session = session::Model { ..session }; + let mut new_session = session::Model { ..session }; - let updated_session = session_context.update(new_session.clone()).await.unwrap(); + let mut updated_session = session_context.update(new_session.clone()).await.unwrap(); let fetched_session = session::Entity::find_by_id(updated_session.id) .one(&session_context.db_context.get_connection()) @@ -197,6 +198,9 @@ mod database_tests { .unwrap() .unwrap(); + new_session.updated_at = fetched_session.updated_at; + updated_session.updated_at = fetched_session.updated_at; + assert_eq!(new_session, updated_session); assert_eq!(updated_session, fetched_session); } @@ -219,8 +223,8 @@ mod database_tests { } #[tokio::test] - async fn update_does_not_modify_updated_at_test() { - let (session_context, session, _, _) = seed_db().await; + async fn update_does_modifies_updated_at_automatically_test() { + let (session_context, mut session, _, _) = seed_db().await; session::Entity::insert(session.clone().into_active_model()) .exec(&session_context.db_context.get_connection()) .await @@ -235,12 +239,16 @@ mod database_tests { .await .unwrap(); + assert!(session.updated_at < res.updated_at); + + session.updated_at = res.updated_at; + assert_eq!(session, res); } #[tokio::test] async fn update_does_not_modify_user_id_test() { - let (session_context, session, _, _) = seed_db().await; + let (session_context, mut session, _, _) = seed_db().await; session::Entity::insert(session.clone().into_active_model()) .exec(&session_context.db_context.get_connection()) .await @@ -255,6 +263,8 @@ mod database_tests { .await .unwrap(); + session.updated_at = res.updated_at; + assert_eq!(session, res); } @@ -331,4 +341,24 @@ mod database_tests { DbErr::RecordNotFound(_) )); } + + #[tokio::test] + async fn get_by_refresh_token_test() { + let (session_context, session, _, _) = seed_db().await; + + session::Entity::insert(session.clone().into_active_model()) + .exec(&session_context.db_context.get_connection()) + .await + .unwrap(); + + let fetched_session = session_context + .get_by_refresh_token(session.refresh_token.clone()) + .await + .unwrap(); + + assert_eq!( + fetched_session.unwrap().refresh_token, + session.refresh_token + ); + } } diff --git a/src/tests/database/user_context.rs b/src/tests/database/user_context.rs index 646a175..4d28c53 100644 --- a/src/tests/database/user_context.rs +++ b/src/tests/database/user_context.rs @@ -1,5 +1,6 @@ #[cfg(test)] mod database_tests { + use crate::database::user_context::UserContextTrait; use crate::tests::database::helpers::*; use crate::{ database::{ @@ -500,4 +501,37 @@ mod database_tests { DbErr::RecordNotFound(_) )); } + + #[tokio::test] + async fn get_by_username_test() { + let (user_context, user) = seed_db().await; + + user::Entity::insert(user.clone().into_active_model()) + .exec(&user_context.db_context.get_connection()) + .await + .unwrap(); + + // Fetches the user created using the 'get_by_username' function + let fetched_user = user_context + .get_by_username(user.username.clone()) + .await + .unwrap(); + + // Assert if the fetched user is the same as the created user + assert_eq!(fetched_user.unwrap().username, user.username); + } + + #[tokio::test] + async fn get_by_email_test() { + let (user_context, user) = seed_db().await; + + user::Entity::insert(user.clone().into_active_model()) + .exec(&user_context.db_context.get_connection()) + .await + .unwrap(); + + let fetched_user = user_context.get_by_email(user.email.clone()).await.unwrap(); + + assert_eq!(fetched_user.unwrap().email, user.email); + } }