Skip to content

Commit

Permalink
Implement unit tests for OIDC provider configuration in oidc_provider…
Browse files Browse the repository at this point in the history
…s.rs

- Added unit tests for  to verify correct behavior:
  - Tests ensure that valid environment variables result in correct OIDC configuration.
  - Tests cover scenarios where environment variables (, , ) are missing, triggering appropriate panics.
- Ensured proper cleanup of environment variables after each test.
  • Loading branch information
Mehrn0ush committed Oct 22, 2024
1 parent 4601de6 commit 76a4fb6
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/core/oidc_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,70 @@ pub fn google_provider_config() -> OIDCProviderConfig {
discovery_url: "https://accounts.google.com/.well-known/openid-configuration".to_string(),
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::env;

#[test]
fn test_google_provider_config_success() {
// Set environment variables
env::set_var("GOOGLE_CLIENT_ID", "test_client_id");
env::set_var("GOOGLE_CLIENT_SECRET", "test_client_secret");
env::set_var("GOOGLE_REDIRECT_URI", "https://example.com/callback");

// Call the google_provider_config function
let config = google_provider_config();

// Assert that the config contains the expected values
assert_eq!(config.client_id, "test_client_id");
assert_eq!(config.client_secret, "test_client_secret");
assert_eq!(config.redirect_uri, "https://example.com/callback");
assert_eq!(
config.discovery_url,
"https://accounts.google.com/.well-known/openid-configuration"
);

// Clean up the environment variables after test
env::remove_var("GOOGLE_CLIENT_ID");
env::remove_var("GOOGLE_CLIENT_SECRET");
env::remove_var("GOOGLE_REDIRECT_URI");
}

#[test]
#[should_panic(expected = "GOOGLE_CLIENT_ID must be set")]
fn test_missing_google_client_id() {
// Unset the GOOGLE_CLIENT_ID to trigger panic
env::remove_var("GOOGLE_CLIENT_ID");
env::set_var("GOOGLE_CLIENT_SECRET", "test_client_secret");
env::set_var("GOOGLE_REDIRECT_URI", "https://example.com/callback");

// This should panic due to missing GOOGLE_CLIENT_ID
google_provider_config();
}

#[test]
#[should_panic(expected = "GOOGLE_CLIENT_SECRET must be set")]
fn test_missing_google_client_secret() {
// Unset the GOOGLE_CLIENT_SECRET to trigger panic
env::set_var("GOOGLE_CLIENT_ID", "test_client_id");
env::remove_var("GOOGLE_CLIENT_SECRET");
env::set_var("GOOGLE_REDIRECT_URI", "https://example.com/callback");

// This should panic due to missing GOOGLE_CLIENT_SECRET
google_provider_config();
}

#[test]
#[should_panic(expected = "GOOGLE_REDIRECT_URI must be set")]
fn test_missing_google_redirect_uri() {
// Unset the GOOGLE_REDIRECT_URI to trigger panic
env::set_var("GOOGLE_CLIENT_ID", "test_client_id");
env::set_var("GOOGLE_CLIENT_SECRET", "test_client_secret");
env::remove_var("GOOGLE_REDIRECT_URI");

// This should panic due to missing GOOGLE_REDIRECT_URI
google_provider_config();
}
}

0 comments on commit 76a4fb6

Please sign in to comment.