Skip to content

Commit

Permalink
58 refactor reveaal and user endpoint tests (#61)
Browse files Browse the repository at this point in the history
* New test workflow

* Refactored test module paths

* Small clippy fixes
  • Loading branch information
williamwoldum authored Nov 27, 2023
1 parent fb4c7b5 commit dc7a10e
Show file tree
Hide file tree
Showing 18 changed files with 2,267 additions and 2,269 deletions.
41 changes: 35 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,48 @@ on:

env:
CARGO_TERM_COLOR: always
TEST_DATABASE_URL: "sqlite::memory:"

jobs:
test:
# Label of the container job
container-job:
# Containers must run in Linux based operating systems
runs-on: ubuntu-latest
# Docker Hub image that `container-job` executes in
container: rust

# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
ports:
- 5432:5432
# Provide the password and db name for postgres
env:
POSTGRES_PASSWORD: 1234
POSTGRES_DB: ecdar_api
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Check out repository code
uses: actions/checkout@v4
with:
submodules: true
- name: Install Protoc
submodules: 'true'

- name: Install dependencies
uses: arduino/setup-protoc@v2

- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Run tests
run: cargo test --verbose
env:
TEST_DATABASE_URL: "postgresql://postgres:1234@postgres:5432/ecdar_api"
run: cargo test -- --test-threads=1
8 changes: 2 additions & 6 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,10 @@ impl EcdarBackend for ConcreteEcdarApi {
}
}

#[cfg(test)]
#[path = "../tests/api/ecdar_api.rs"]
mod tests;

#[cfg(test)]
#[path = "../tests/api/user_logic.rs"]
mod user_logic;
mod user_logic_tests;

#[cfg(test)]
#[path = "../tests/api/session_logic.rs"]
mod session_logic;
mod session_logic_tests;
2 changes: 1 addition & 1 deletion src/database/access_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ impl EntityContextTrait<access::Model> for AccessContext {
}
#[cfg(test)]
#[path = "../tests/database/access_context.rs"]
mod tests;
mod access_context_tests;
2 changes: 1 addition & 1 deletion src/database/in_use_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ impl EntityContextTrait<in_use::Model> for InUseContext {

#[cfg(test)]
#[path = "../tests/database/in_use_context.rs"]
mod tests;
mod in_use_context_tests;
2 changes: 1 addition & 1 deletion src/database/model_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ impl EntityContextTrait<model::Model> for ModelContext {

#[cfg(test)]
#[path = "../tests/database/model_context.rs"]
mod tests;
mod model_context_tests;
2 changes: 1 addition & 1 deletion src/database/query_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ impl EntityContextTrait<query::Model> for QueryContext {

#[cfg(test)]
#[path = "../tests/database/query_context.rs"]
mod tests;
mod query_context_tests;
2 changes: 1 addition & 1 deletion src/database/session_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ impl EntityContextTrait<session::Model> for SessionContext {

#[cfg(test)]
#[path = "../tests/database/session_context.rs"]
mod tests;
mod session_context_tests;
2 changes: 1 addition & 1 deletion src/database/user_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ impl EntityContextTrait<user::Model> for UserContext {
}
#[cfg(test)]
#[path = "../tests/database/user_context.rs"]
mod tests;
mod user_context_tests;
1 change: 0 additions & 1 deletion src/tests/api/ecdar_api.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/tests/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub mod helpers;
mod session_logic;
mod user_logic;
267 changes: 132 additions & 135 deletions src/tests/api/session_logic.rs
Original file line number Diff line number Diff line change
@@ -1,136 +1,133 @@
#[cfg(test)]
mod session_logic {
use crate::api::ecdar_api::handle_session;
use crate::api::server::server::GetAuthTokenRequest;
use crate::entities::session;
use crate::tests::api::helpers::get_mock_services;
use mockall::predicate;
use sea_orm::DbErr;
use std::str::FromStr;
use std::sync::Arc;
use tonic::{metadata, Code, Request};

#[tokio::test]
async fn handle_session_updated_session_contains_correct_fields_returns_ok() {
let mut mock_services = get_mock_services();

let old_session = session::Model {
id: 1,
refresh_token: "old_refresh_token".to_string(),
access_token: "old_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

let new_session = session::Model {
id: 1,
refresh_token: "new_refresh_token".to_string(),
access_token: "new_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

mock_services
.session_context_mock
.expect_get_by_refresh_token()
.with(predicate::eq("old_refresh_token".to_string()))
.returning(move |_| Ok(Some(old_session.clone())));

mock_services
.session_context_mock
.expect_update()
.with(predicate::eq(new_session.clone()))
.returning(move |_| Ok(new_session.clone()));

let mut get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: None,
});

get_auth_token_request.metadata_mut().insert(
"authorization",
metadata::MetadataValue::from_str("Bearer old_refresh_token").unwrap(),
);

let res = handle_session(
Arc::new(mock_services.session_context_mock),
&get_auth_token_request,
false,
"new_access_token".to_string(),
"new_refresh_token".to_string(),
"1".to_string(),
)
.await;

assert!(res.is_ok());
}

#[tokio::test]
async fn handle_session_no_session_exists_creates_session_returns_ok() {
let mut mock_services = get_mock_services();

let session = session::Model {
id: Default::default(),
refresh_token: "new_refresh_token".to_string(),
access_token: "new_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

mock_services
.session_context_mock
.expect_create()
.with(predicate::eq(session.clone()))
.returning(move |_| Ok(session.clone()));

let get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: None,
});

let res = handle_session(
Arc::new(mock_services.session_context_mock),
&get_auth_token_request,
true,
"new_access_token".to_string(),
"new_refresh_token".to_string(),
"1".to_string(),
)
.await;

assert!(res.is_ok());
}

#[tokio::test]
async fn handle_session_no_session_exists_creates_session_returns_err() {
let mut mock_services = get_mock_services();

let session = session::Model {
id: Default::default(),
refresh_token: "new_refresh_token".to_string(),
access_token: "new_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

mock_services
.session_context_mock
.expect_create()
.with(predicate::eq(session.clone()))
.returning(move |_| Err(DbErr::RecordNotInserted));

let get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: None,
});

let res = handle_session(
Arc::new(mock_services.session_context_mock),
&get_auth_token_request,
true,
"new_access_token".to_string(),
"new_refresh_token".to_string(),
"1".to_string(),
)
.await;

assert_eq!(res.unwrap_err().code(), Code::Internal);
}
use crate::api::ecdar_api::handle_session;
use crate::api::server::server::GetAuthTokenRequest;
use crate::entities::session;
use crate::tests::api::helpers::get_mock_services;
use mockall::predicate;
use sea_orm::DbErr;
use std::str::FromStr;
use std::sync::Arc;
use tonic::{metadata, Code, Request};

#[tokio::test]
async fn handle_session_updated_session_contains_correct_fields_returns_ok() {
let mut mock_services = get_mock_services();

let old_session = session::Model {
id: 1,
refresh_token: "old_refresh_token".to_string(),
access_token: "old_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

let new_session = session::Model {
id: 1,
refresh_token: "new_refresh_token".to_string(),
access_token: "new_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

mock_services
.session_context_mock
.expect_get_by_refresh_token()
.with(predicate::eq("old_refresh_token".to_string()))
.returning(move |_| Ok(Some(old_session.clone())));

mock_services
.session_context_mock
.expect_update()
.with(predicate::eq(new_session.clone()))
.returning(move |_| Ok(new_session.clone()));

let mut get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: None,
});

get_auth_token_request.metadata_mut().insert(
"authorization",
metadata::MetadataValue::from_str("Bearer old_refresh_token").unwrap(),
);

let res = handle_session(
Arc::new(mock_services.session_context_mock),
&get_auth_token_request,
false,
"new_access_token".to_string(),
"new_refresh_token".to_string(),
"1".to_string(),
)
.await;

assert!(res.is_ok());
}

#[tokio::test]
async fn handle_session_no_session_exists_creates_session_returns_ok() {
let mut mock_services = get_mock_services();

let session = session::Model {
id: Default::default(),
refresh_token: "new_refresh_token".to_string(),
access_token: "new_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

mock_services
.session_context_mock
.expect_create()
.with(predicate::eq(session.clone()))
.returning(move |_| Ok(session.clone()));

let get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: None,
});

let res = handle_session(
Arc::new(mock_services.session_context_mock),
&get_auth_token_request,
true,
"new_access_token".to_string(),
"new_refresh_token".to_string(),
"1".to_string(),
)
.await;

assert!(res.is_ok());
}

#[tokio::test]
async fn handle_session_no_session_exists_creates_session_returns_err() {
let mut mock_services = get_mock_services();

let session = session::Model {
id: Default::default(),
refresh_token: "new_refresh_token".to_string(),
access_token: "new_access_token".to_string(),
updated_at: Default::default(),
user_id: 1,
};

mock_services
.session_context_mock
.expect_create()
.with(predicate::eq(session.clone()))
.returning(move |_| Err(DbErr::RecordNotInserted));

let get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: None,
});

let res = handle_session(
Arc::new(mock_services.session_context_mock),
&get_auth_token_request,
true,
"new_access_token".to_string(),
"new_refresh_token".to_string(),
"1".to_string(),
)
.await;

assert_eq!(res.unwrap_err().code(), Code::Internal);
}
Loading

0 comments on commit dc7a10e

Please sign in to comment.