Skip to content

Commit

Permalink
Use delete_by_token instead
Browse files Browse the repository at this point in the history
  • Loading branch information
sabotack committed Dec 6, 2023
1 parent a6d9bd3 commit 082e7ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
14 changes: 3 additions & 11 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,20 +844,12 @@ impl EcdarApi for ConcreteEcdarApi {
.token_string()
.ok_or(Status::unauthenticated("No access token provided"))?;

let session = self
match self
.contexts
.session_context
.get_by_token(TokenType::AccessToken, access_token)
.delete_by_token(TokenType::AccessToken, access_token)
.await
.map_err(|err| Status::new(Code::Internal, err.to_string()))?
.ok_or_else(|| {
Status::new(
Code::Unauthenticated,
"No session found with given access token",
)
})?;

match self.contexts.session_context.delete(session.id).await {
{
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand Down
32 changes: 11 additions & 21 deletions src/tests/api/session_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use mockall::predicate;
use sea_orm::DbErr;
use std::str::FromStr;
use std::sync::Arc;
use tonic::{metadata, Code, Request};
use tonic::{metadata, Code, Request, Status};

#[tokio::test]
async fn handle_session_updated_session_contains_correct_fields_returns_ok() {
Expand Down Expand Up @@ -142,33 +142,19 @@ async fn delete_session_returns_ok() {

mock_services
.session_context_mock
.expect_delete()
.with(predicate::eq(1))
.returning(move |_| {
Ok(session::Model {
id: 1,
refresh_token: Default::default(),
access_token: "test_token".to_string(),
updated_at: Default::default(),
user_id: Default::default(),
})
});

mock_services
.session_context_mock
.expect_get_by_token()
.expect_delete_by_token()
.with(
predicate::eq(TokenType::AccessToken),
predicate::eq("test_token".to_string()),
)
.returning(move |_, _| {
Ok(Some(session::Model {
Ok(session::Model {
id: 1,
refresh_token: Default::default(),
access_token: "test_token".to_string(),
updated_at: Default::default(),
user_id: Default::default(),
}))
})
});

let api = get_mock_concrete_ecdar_api(mock_services);
Expand All @@ -190,12 +176,16 @@ async fn delete_session_no_session_returns_err() {

mock_services
.session_context_mock
.expect_get_by_token()
.expect_delete_by_token()
.with(
predicate::eq(TokenType::AccessToken),
predicate::eq("test_token".to_string()),
)
.returning(move |_, _| Ok(None));
.returning(move |_, _| {
Err(DbErr::RecordNotFound(
"No session found with the provided access token".to_string(),
))
});

let api = get_mock_concrete_ecdar_api(mock_services);

Expand All @@ -207,5 +197,5 @@ async fn delete_session_no_session_returns_err() {

let res = api.delete_session(request).await;

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

0 comments on commit 082e7ec

Please sign in to comment.