Skip to content

Commit

Permalink
Add tests for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
sabotack committed Nov 14, 2023
1 parent f586ec9 commit c6de60c
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion src/tests/api/auth.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
#[cfg(test)]
mod auth {}
mod auth {
use crate::api::auth;
use std::{env, str::FromStr};
use tonic::{metadata::MetadataValue, Request};

#[tokio::test]
async fn gtfr_bearer_token_trims_token() {
let token = "Bearer 1234567890";
let mut request = Request::new(());
request
.metadata_mut()
.insert("authorization", MetadataValue::from_str(token).unwrap());

let result = auth::get_token_from_request(&request).unwrap();

assert_eq!(result, token.trim_start_matches("Bearer "));
}

#[tokio::test]
async fn gtfr_no_token_returns_err() {
let request = Request::new(());

let result = auth::get_token_from_request(&request);

assert!(result.is_err());
}

#[tokio::test]
async fn create_token_access_returns_token() {
env::set_var("ACCESS_TOKEN_HS512_SECRET", "access_secret");

let uid = "1";
let result = auth::create_token(auth::TokenType::AccessToken, uid);

assert!(result.is_ok());
}

#[tokio::test]
async fn create_token_refresh_returns_token() {
env::set_var("REFRESH_TOKEN_HS512_SECRET", "refresh_secret");

let uid = "1";
let result = auth::create_token(auth::TokenType::RefreshToken, uid);

assert!(result.is_ok());
}

#[tokio::test]
async fn validate_token_valid_access_returns_tokendata() {
env::set_var("ACCESS_TOKEN_HS512_SECRET", "access_secret");

let token = auth::create_token(auth::TokenType::AccessToken, "1").unwrap();
let result = auth::validate_token(token, false);
assert!(result.is_ok());
}

#[tokio::test]
async fn validate_token_valid_refresh_returns_tokendata() {
env::set_var("REFRESH_TOKEN_HS512_SECRET", "refresh_secret");

let token = auth::create_token(auth::TokenType::RefreshToken, "1").unwrap();
let result = auth::validate_token(token, true);
assert!(result.is_ok());
}

#[tokio::test]
async fn validate_token_invalid_returns_err() {
env::set_var("ACCESS_TOKEN_HS512_SECRET", "access_secret");
env::set_var("REFRESH_TOKEN_HS512_SECRET", "refresh_secret");

let result_access = auth::validate_token("invalid_token".to_string(), false);
let result_refresh = auth::validate_token("invalid_token".to_string(), true);
assert!(result_access.is_err() && result_refresh.is_err());
}

#[tokio::test]
async fn validate_token_wrong_signature_returns_err() {
env::set_var("ACCESS_TOKEN_HS512_SECRET", "access_secret");
env::set_var("REFRESH_TOKEN_HS512_SECRET", "refresh_secret");

let token = auth::create_token(auth::TokenType::AccessToken, "1").unwrap();
let result_access = auth::validate_token(token, true);

let token = auth::create_token(auth::TokenType::RefreshToken, "1").unwrap();
let result_refresh = auth::validate_token(token, false);

assert!(result_access.is_err() && result_refresh.is_err());
}
}

0 comments on commit c6de60c

Please sign in to comment.