Skip to content

Commit

Permalink
Fix JWT role extraction test by ensuring consistent use of JWT_SECRET…
Browse files Browse the repository at this point in the history
… from environment
  • Loading branch information
Mehrn0ush committed Oct 5, 2024
1 parent a325232 commit 3c9eb1c
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/auth/rbac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod tests {

#[test]
fn test_rbac_check_success() {
dotenv::dotenv().ok(); // Ensure .env is loaded
dotenv::dotenv().ok(); // Load environment variables from .env

// Retrieve the JWT_SECRET from the environment
let jwt_secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set in .env");
Expand All @@ -235,4 +235,43 @@ mod tests {
// Ensure the result is Ok, meaning the role was validated successfully
assert!(result.is_ok(), "Expected Ok, but got: {:?}", result);
}

#[test]
fn test_extract_roles_success() {
dotenv::dotenv().ok(); // Ensure .env is loaded for consistency

// Ensure JWT_SECRET is set in the environment
let jwt_secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set in .env");

// Define the claims
let claims = TestClaims {
sub: "user123".to_string(),
exp: 9999999999,
roles: vec!["admin".to_string(), "user".to_string()],
};

// Generate the test token using the JWT_SECRET from environment
let token = generate_test_token(claims, &jwt_secret);

// Log the generated token for debugging
println!("Generated token: {}", token);

// Extract roles
let result = extract_roles(&token);

// Log the result for debugging
println!("Extract roles result: {:?}", result);

// Assert that the result is OK
assert!(
result.is_ok(),
"Expected result to be Ok, but got: {:?}",
result
);

// Get roles and check if they contain the expected values
let roles = result.unwrap();
assert!(roles.contains(&"admin".to_string()));
assert!(roles.contains(&"user".to_string()));
}
}

0 comments on commit 3c9eb1c

Please sign in to comment.