Skip to content

Commit

Permalink
WIP: Fix session_logic test
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsSR committed Dec 5, 2023
1 parent cd29928 commit 8aa7e93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 106 deletions.
2 changes: 1 addition & 1 deletion src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl TokenType {
/// assert_eq!(token.token_type(), TokenType::AccessToken);
/// assert_eq!(token.to_string(), token.as_str());
/// ```
#[derive(Debug)]
pub struct Token {
token_type: TokenType,
token: String,
Expand Down
114 changes: 9 additions & 105 deletions src/tests/api/session_logic.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,15 @@
use crate::api::auth::TokenType;
use crate::api::ecdar_api::update_session;
use crate::api::server::server::GetAuthTokenRequest;
use crate::api::{auth::TokenType, ecdar_api::handle_session};
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_token()
.with(
predicate::eq(TokenType::RefreshToken),
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() {
async fn update_session_no_session_exists_creates_session_returns_err() {
let mut mock_services = get_mock_services();

let session = session::Model {
Expand All @@ -79,58 +22,19 @@ async fn handle_session_no_session_exists_creates_session_returns_ok() {

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,
};
.expect_get_by_token()
.returning(move |_, _| Ok(None));

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

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

let res = handle_session(
let res = update_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(),
"old_refresh_token".to_string(),
)
.await;

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

0 comments on commit 8aa7e93

Please sign in to comment.