Skip to content

Commit

Permalink
test: add unit tests for OAuth providers
Browse files Browse the repository at this point in the history
  • Loading branch information
cmackenzie1 committed Feb 26, 2025
1 parent f652902 commit c917192
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
54 changes: 54 additions & 0 deletions torii-auth-oauth/src/providers/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,57 @@ impl Github {
Ok(token_response)
}
}

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

#[tokio::test]
async fn test_github_get_authorization_url() {
let github = Github::new(
"client_id".to_string(),
"client_secret".to_string(),
"http://localhost:8080/callback".to_string(),
);

let (auth_url, pkce_verifier) = github.get_authorization_url().unwrap();
assert!(auth_url.url.contains("github.com"));
assert!(auth_url.url.contains("client_id=client_id"));
assert!(auth_url.url.contains("scope=read%3Auser+user%3Aemail"));
assert!(!auth_url.csrf_state.is_empty());
assert!(!pkce_verifier.is_empty());
}

#[test]
fn test_github_user_info_deserialization() {
let json = r#"{
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"name": "monalisa octocat",
"email": "octocat@github.com",
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2008-01-14T04:33:35Z"
}"#;

let user_info: GithubUserInfo = serde_json::from_str(json).unwrap();
assert_eq!(user_info.login, "octocat");
assert_eq!(user_info.id, 1);
assert_eq!(user_info.name.as_deref(), Some("monalisa octocat"));
assert_eq!(user_info.email.as_deref(), Some("octocat@github.com"));
}

#[test]
fn test_github_user_email_deserialization() {
let json = r#"{
"email": "octocat@github.com",
"primary": true,
"verified": true
}"#;

let email: GithubUserEmail = serde_json::from_str(json).unwrap();
assert_eq!(email.email, "octocat@github.com");
assert!(email.primary);
assert!(email.verified);
}
}
21 changes: 21 additions & 0 deletions torii-auth-oauth/src/providers/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,24 @@ impl Google {
Ok(token_response)
}
}

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

#[tokio::test]
async fn test_google_get_authorization_url() {
let google = Google::new(
"client_id".to_string(),
"client_secret".to_string(),
"http://localhost:8080/callback".to_string(),
);

let (auth_url, pkce_verifier) = google.get_authorization_url().unwrap();
assert!(auth_url.url.contains("accounts.google.com"));
assert!(auth_url.url.contains("client_id=client_id"));
assert!(auth_url.url.contains("scope=openid+email+profile"));
assert!(!auth_url.csrf_state.is_empty());
assert!(!pkce_verifier.is_empty());
}
}
41 changes: 41 additions & 0 deletions torii-auth-oauth/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,44 @@ pub enum UserInfo {
Google(google::GoogleUserInfo),
Github(github::GithubUserInfo),
}

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

#[test]
fn test_provider_name() {
let google = Provider::google(
"client_id".to_string(),
"client_secret".to_string(),
"http://localhost:8080/callback".to_string(),
);
assert_eq!(google.name(), "google");

let github = Provider::github(
"client_id".to_string(),
"client_secret".to_string(),
"http://localhost:8080/callback".to_string(),
);
assert_eq!(github.name(), "github");
}

#[tokio::test]
async fn test_provider_get_authorization_url() {
let google = Provider::google(
"client_id".to_string(),
"client_secret".to_string(),
"http://localhost:8080/callback".to_string(),
);
let (auth_url, _) = google.get_authorization_url().unwrap();
assert!(auth_url.url().contains("accounts.google.com"));

let github = Provider::github(
"client_id".to_string(),
"client_secret".to_string(),
"http://localhost:8080/callback".to_string(),
);
let (auth_url, _) = github.get_authorization_url().unwrap();
assert!(auth_url.url().contains("github.com"));
}
}

0 comments on commit c917192

Please sign in to comment.