Skip to content

Commit

Permalink
Merged main
Browse files Browse the repository at this point in the history
  • Loading branch information
williamwoldum committed Dec 7, 2023
2 parents 30cb521 + f4b22a7 commit 543abcd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Ecdar-ProtoBuf
Submodule Ecdar-ProtoBuf updated 1 files
+1 −1 services.proto
21 changes: 18 additions & 3 deletions src/controllers/controller_impls/session_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::entities::{session, user};
use crate::services::service_collection::ServiceCollection;
use async_trait::async_trait;
use sea_orm::DbErr;
use tonic::{Request, Response, Status};
use tonic::{Code, Request, Response, Status};

pub struct SessionController {
contexts: ContextCollection,
Expand Down Expand Up @@ -75,8 +75,23 @@ impl SessionController {

#[async_trait]
impl SessionControllerTrait for SessionController {
async fn delete_session(&self, _request: Request<()>) -> Result<Response<()>, Status> {
todo!()
/// Deletes the requester's session, found by their access token.
///
/// Returns the response that is received from Reveaal.
async fn delete_session(&self, request: Request<()>) -> Result<Response<()>, Status> {
let access_token = request
.token_string()
.ok_or(Status::unauthenticated("No access token provided"))?;

match self
.contexts
.session_context
.delete_by_token(TokenType::AccessToken, access_token)
.await
{
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
}

/// This method is used to get a new access and refresh token for a user.
Expand Down
71 changes: 71 additions & 0 deletions src/tests/controllers/session_controller.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use mockall::predicate;
use std::env;
use std::str::FromStr;

Expand Down Expand Up @@ -264,3 +265,73 @@ async fn get_auth_token_from_invalid_token_returns_err() {

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

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

mock_contexts
.session_context_mock
.expect_delete_by_token()
.with(
predicate::eq(TokenType::AccessToken),
predicate::eq("test_token".to_string()),
)
.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(),
})
});

let contexts = disguise_context_mocks(mock_contexts);
let services = disguise_service_mocks(mock_services);
let session_logic = SessionController::new(contexts, services);

let mut request = Request::new(());
request.metadata_mut().insert(
"authorization",
metadata::MetadataValue::from_str("Bearer test_token").unwrap(),
);

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

assert!(res.is_ok());
}

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

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

let contexts = disguise_context_mocks(mock_contexts);
let services = disguise_service_mocks(mock_services);
let session_logic = SessionController::new(contexts, services);

let mut request = Request::new(());
request.metadata_mut().insert(
"authorization",
metadata::MetadataValue::from_str("Bearer test_token").unwrap(),
);

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

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

0 comments on commit 543abcd

Please sign in to comment.