-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from ECDAR-AAU-SW-P5/delete-model-tests
Add delete_model tests
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use std::str::FromStr; | ||
|
||
use mockall::predicate; | ||
use tonic::{metadata, Code, Request}; | ||
|
||
use crate::{ | ||
api::server::server::{ | ||
ecdar_api_server::EcdarApi, Component, ComponentsInfo, DeleteModelRequest, | ||
}, | ||
entities::model, | ||
tests::api::helpers::{get_mock_concrete_ecdar_api, get_mock_services}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn delete_not_owner_returns_err() { | ||
let mut mock_services = get_mock_services(); | ||
|
||
mock_services | ||
.model_context_mock | ||
.expect_get_by_id() | ||
.with(predicate::eq(1)) | ||
.returning(move |_| { | ||
Ok(Some(model::Model { | ||
id: 1, | ||
name: Default::default(), | ||
components_info: Default::default(), | ||
owner_id: 2, | ||
})) | ||
}); | ||
|
||
let mut request = Request::new(DeleteModelRequest { id: 1 }); | ||
|
||
request | ||
.metadata_mut() | ||
.insert("uid", metadata::MetadataValue::from_str("1").unwrap()); | ||
|
||
let api = get_mock_concrete_ecdar_api(mock_services); | ||
|
||
let res = api.delete_model(request).await.unwrap_err(); | ||
|
||
assert_eq!(res.code(), Code::PermissionDenied); | ||
} | ||
|
||
#[tokio::test] | ||
async fn delete_invalid_model_returns_err() { | ||
let mut mock_services = get_mock_services(); | ||
|
||
mock_services | ||
.model_context_mock | ||
.expect_get_by_id() | ||
.with(predicate::eq(2)) | ||
.returning(move |_| Ok(None)); | ||
|
||
let mut request = Request::new(DeleteModelRequest { id: 2 }); | ||
|
||
request | ||
.metadata_mut() | ||
.insert("uid", metadata::MetadataValue::from_str("1").unwrap()); | ||
|
||
let api = get_mock_concrete_ecdar_api(mock_services); | ||
|
||
let res = api.delete_model(request).await.unwrap_err(); | ||
|
||
assert_eq!(res.code(), Code::NotFound); | ||
} | ||
|
||
#[tokio::test] | ||
async fn delete_model_returns_ok() { | ||
let mut mock_services = get_mock_services(); | ||
|
||
mock_services | ||
.model_context_mock | ||
.expect_get_by_id() | ||
.with(predicate::eq(1)) | ||
.returning(move |_| { | ||
Ok(Some(model::Model { | ||
id: 1, | ||
name: Default::default(), | ||
components_info: Default::default(), | ||
owner_id: 1, | ||
})) | ||
}); | ||
|
||
mock_services | ||
.model_context_mock | ||
.expect_delete() | ||
.with(predicate::eq(1)) | ||
.returning(move |_| { | ||
Ok(model::Model { | ||
id: 1, | ||
name: Default::default(), | ||
components_info: Default::default(), | ||
owner_id: 1, | ||
}) | ||
}); | ||
|
||
let mut request = Request::new(DeleteModelRequest { id: 1 }); | ||
|
||
request | ||
.metadata_mut() | ||
.insert("uid", metadata::MetadataValue::from_str("1").unwrap()); | ||
|
||
let api = get_mock_concrete_ecdar_api(mock_services); | ||
|
||
let res = api.delete_model(request).await; | ||
|
||
assert!(res.is_ok()); | ||
} |