Skip to content

Commit

Permalink
Tests for list access info
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorPlatz committed Nov 28, 2023
1 parent f6b41ae commit 6137836
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl EcdarApi for ConcreteEcdarApi {
.ok_or(Status::internal("Could not get uid from request metadata"))?;

Check warning on line 160 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/ecdar_api.rs

let mut access_info_list: Vec<AccessInfo> = Vec::new(); // Initialize the Vec

match self.access_context.get_access_by_uid(uid).await {
Ok(accesses) => {
for access in accesses {
Expand Down
55 changes: 54 additions & 1 deletion src/tests/api/access_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ mod access_logic {
use crate::tests::api::helpers::{get_mock_concrete_ecdar_api, get_mock_services};
use mockall::predicate;
use sea_orm::DbErr;
use tonic::{Code, Request};
use std::str::FromStr;
use tonic::{metadata, Code, Request};

#[tokio::test]
async fn create_invalid_access_returns_err() {
Expand Down Expand Up @@ -172,4 +173,56 @@ mod access_logic {

assert!(res.is_ok());
}

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

let access = access::Model {
id: 1,
role: "Editor".to_string(),
model_id: Default::default(),
user_id: Default::default(),
};

mock_services
.access_context_mock
.expect_get_access_by_uid()
.returning(move |_| Ok(vec![access.clone()]));

let mut list_access_info_request = Request::new(());

list_access_info_request
.metadata_mut()
.insert("uid", metadata::MetadataValue::from_str("1").unwrap());

let api = get_mock_concrete_ecdar_api(mock_services);

let res = api.list_access_info(list_access_info_request).await;

Check warning on line 202 in src/tests/api/access_logic.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/../tests/api/access_logic.rs
assert!(res.is_ok());
}

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

mock_services
.access_context_mock
.expect_get_access_by_uid()
.returning(move |_| Ok(vec![]));

let mut list_access_info_request = Request::new(());

list_access_info_request
.metadata_mut()
.insert("uid", metadata::MetadataValue::from_str("1").unwrap());

Check warning on line 220 in src/tests/api/access_logic.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/../tests/api/access_logic.rs
let api = get_mock_concrete_ecdar_api(mock_services);

let res = api.list_access_info(list_access_info_request).await.unwrap_err();

assert_eq!(res.code(), Code::NotFound);
}

Check warning on line 227 in src/tests/api/access_logic.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/../tests/api/access_logic.rs
}

0 comments on commit 6137836

Please sign in to comment.