diff --git a/src/api/ecdar_api.rs b/src/api/ecdar_api.rs index b05dcd7..ebcca7e 100644 --- a/src/api/ecdar_api.rs +++ b/src/api/ecdar_api.rs @@ -382,7 +382,6 @@ impl EcdarApiAuth for ConcreteEcdarApi { /// Implementation of the EcdarBackend trait, which is used to ensure backwards compatability with the Reveaal engine. #[tonic::async_trait] -#[automock] impl EcdarBackend for ConcreteEcdarApi { async fn get_user_token( &self, diff --git a/src/api/reveaal_context.rs b/src/api/reveaal_context.rs index f0c391b..603c8af 100644 --- a/src/api/reveaal_context.rs +++ b/src/api/reveaal_context.rs @@ -5,6 +5,7 @@ use crate::api::server::server::{ SimulationStepResponse, UserTokenResponse, }; use async_trait::async_trait; +use mockall::automock; use std::env; use tonic::transport::Channel; use tonic::{Request, Response, Status}; diff --git a/src/database/user_context.rs b/src/database/user_context.rs index 1c63b9d..87f7716 100644 --- a/src/database/user_context.rs +++ b/src/database/user_context.rs @@ -1,7 +1,7 @@ use crate::database::database_context::DatabaseContextTrait; use crate::database::entity_context::EntityContextTrait; use crate::entities::user; -use mockall::automock; +use mockall::{automock, mock}; use sea_orm::prelude::async_trait::async_trait; use sea_orm::ActiveValue::{Set, Unchanged}; use sea_orm::{ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter}; diff --git a/src/tests/api/ecdar_api.rs b/src/tests/api/ecdar_api.rs index 937e6a1..901f323 100644 --- a/src/tests/api/ecdar_api.rs +++ b/src/tests/api/ecdar_api.rs @@ -11,16 +11,27 @@ mod ecdar_api { api::server::server::ecdar_api_server::EcdarApi, entities::session::Model as Session, entities::user::Model as User, }; + use mockall::{mock, predicate}; use std::str::FromStr; use std::sync::Arc; + use async_trait::async_trait; + use futures::SinkExt; use crate::api::server::server::ecdar_backend_server::EcdarBackend; - use crate::tests::api::helpers::get_reset_concrete_ecdar_api; + use crate::database::entity_context::EntityContextTrait; + use crate::database::user_context::MockUserContextTrait; + use crate::tests::api::helpers::get_mock_concrete_ecdar_api; use tonic::{metadata, Request}; #[tokio::test] async fn delete_user_nonexistent_user_returns_err() { - let api = get_reset_concrete_ecdar_api().await; + mock! { + C {} + #[async_trait] + impl EntityContextTrait for C { + async fn delete(&self, entity_id: i32) -> Result; + } + } let mut delete_request = Request::new({}); @@ -29,6 +40,13 @@ mod ecdar_api { .metadata_mut() .insert("uid", metadata::MetadataValue::from_str("1").unwrap()); + + let mut mock_user_context = MockC::new(); + + mock_user_context.expect_delete(). + + let api = get_mock_concrete_ecdar_api(Arc::new(mock_user_context)).await; + let delete_response = api.delete_user(delete_request).await; assert!(delete_response.is_err()); @@ -36,7 +54,7 @@ mod ecdar_api { #[tokio::test] async fn delete_user_existing_user_returns_ok() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let _ = api .user_context @@ -63,7 +81,7 @@ mod ecdar_api { #[tokio::test] async fn create_user_nonexistent_user_returns_ok() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let create_user_request = Request::new(CreateUserRequest { email: "anders21@student.aau.dk".to_string(), @@ -77,7 +95,7 @@ mod ecdar_api { #[tokio::test] async fn create_user_nonexistent_user_inserts_user() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let username = "newuser".to_string(); @@ -99,7 +117,7 @@ mod ecdar_api { #[tokio::test] async fn test_create_user_duplicate_email_returns_error() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let _ = api .user_context @@ -123,7 +141,7 @@ mod ecdar_api { #[tokio::test] async fn test_create_user_invalid_email_returns_error() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let create_user_request = Request::new(CreateUserRequest { email: "invalid-email".to_string(), @@ -137,7 +155,7 @@ mod ecdar_api { #[tokio::test] async fn test_create_user_duplicate_username_returns_error() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let _ = api .user_context @@ -161,7 +179,7 @@ mod ecdar_api { #[tokio::test] async fn test_create_user_invalid_username_returns_error() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let create_user_request = Request::new(CreateUserRequest { email: "valid@email.com".to_string(), @@ -175,7 +193,7 @@ mod ecdar_api { #[tokio::test] async fn test_create_user_valid_request_returns_ok() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let create_user_request = Request::new(CreateUserRequest { email: "newuser@example.com".to_string(), @@ -189,7 +207,7 @@ mod ecdar_api { #[tokio::test] async fn update_user_returns_ok() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let user = User { id: 1, @@ -217,7 +235,7 @@ mod ecdar_api { #[tokio::test] async fn update_user_non_existant_user_returns_err() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let mut update_user_request = Request::new(UpdateUserRequest { email: Some("new_test@test".to_string()), @@ -236,7 +254,7 @@ mod ecdar_api { #[tokio::test] async fn update_user_single_field_returns_ok() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let user = User { id: 1, @@ -264,7 +282,7 @@ mod ecdar_api { #[tokio::test] async fn handle_session_updated_session_contains_correct_fields() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let mut get_auth_token_request = Request::new(GetAuthTokenRequest { user_credentials: Some(UserCredentials { @@ -334,7 +352,7 @@ mod ecdar_api { #[tokio::test] async fn handle_session_no_session_exists_creates_session() { - let api = get_reset_concrete_ecdar_api().await; + let api = get_mock_concrete_ecdar_api().await; let mut get_auth_token_request = Request::new(GetAuthTokenRequest { user_credentials: Some(UserCredentials { @@ -375,7 +393,7 @@ mod ecdar_api { #[tokio::test] async fn handle_session_update_non_existing_session_returns_err() { - let api = get_reset_concrete_ecdar_api(Arc::new(MockEcdarBackend)).await; + let api = get_mock_concrete_ecdar_api(Arc::new(MockEcdarBackend)).await; let mut get_auth_token_request = Request::new(GetAuthTokenRequest { user_credentials: Some(UserCredentials { diff --git a/src/tests/api/helpers.rs b/src/tests/api/helpers.rs index e3c47bc..2035edc 100644 --- a/src/tests/api/helpers.rs +++ b/src/tests/api/helpers.rs @@ -1,11 +1,8 @@ #![cfg(test)] use crate::api::ecdar_api::ConcreteEcdarApi; +use crate::api::reveaal_context::MockReveaalContext; use crate::api::server::server::ecdar_backend_server::EcdarBackend; -use crate::api::server::server::{ - QueryRequest, QueryResponse, SimulationStartRequest, SimulationStepRequest, - SimulationStepResponse, UserTokenResponse, -}; use crate::database::access_context::AccessContext; use crate::database::database_context::{ DatabaseContextTrait, PostgresDatabaseContext, SQLiteDatabaseContext, @@ -15,35 +12,32 @@ use crate::database::in_use_context::InUseContext; use crate::database::model_context::ModelContext; use crate::database::query_context::QueryContext; use crate::database::session_context::SessionContext; -use crate::database::user_context::UserContext; -use dotenv::dotenv; +use crate::database::user_context::{MockUserContextTrait, UserContext}; use sea_orm::{ConnectionTrait, Database, DbBackend}; use std::env; use std::sync::Arc; -use tonic::{Request, Response, Status}; -pub async fn get_reset_concrete_ecdar_api( - mock_ecdar_backend: Arc, +pub async fn get_mock_concrete_ecdar_api( + mock_user_context: Arc, ) -> ConcreteEcdarApi { - dotenv().ok(); - - let url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests."); - let db = Database::connect(&url).await.unwrap(); + let url = env::var("DATABASE_URL").expect("Expected DATABASE_URL to be set."); + let db = Database::connect(&url).await?; let db_context: Arc = match db.get_database_backend() { - DbBackend::Sqlite => Arc::new(SQLiteDatabaseContext::new(&url).await.unwrap()), - DbBackend::Postgres => Arc::new(PostgresDatabaseContext::new(&url).await.unwrap()), + DbBackend::Sqlite => Arc::new(SQLiteDatabaseContext::new(&url).await?), + DbBackend::Postgres => Arc::new(PostgresDatabaseContext::new(&url).await?), _ => panic!("Database protocol not supported"), }; - db_context.reset().await.unwrap(); + let user_context = mock_user_context.clone(); let model_context = Arc::new(ModelContext::new(db_context.clone())); - let user_context = Arc::new(UserContext::new(db_context.clone())); let access_context = Arc::new(AccessContext::new(db_context.clone())); let query_context = Arc::new(QueryContext::new(db_context.clone())); let session_context = Arc::new(SessionContext::new(db_context.clone())); let in_use_context = Arc::new(InUseContext::new(db_context.clone())); + let mock_ecdar_backend = Arc::new(MockReveaalContext::new()); + ConcreteEcdarApi::new( model_context, user_context,