Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(webserver): add test for selecting primary email of github user #2233

Merged
merged 2 commits into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions ee/tabby-webserver/src/oauth/github.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use anyhow::Result;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use serde::Deserialize;
use tabby_schema::auth::{AuthenticationService, OAuthCredential, OAuthProvider};
Expand Down Expand Up @@ -121,14 +121,7 @@
.await?;

let emails = resp.json::<Vec<GithubUserEmail>>().await?;

for item in &emails {
if item.primary {
return Ok(item.email.clone());
}
}

bail!("No primary email address found");
select_primary_email(emails)

Check warning on line 124 in ee/tabby-webserver/src/oauth/github.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/oauth/github.rs#L124

Added line #L124 was not covered by tests
}

async fn fetch_user_full_name(&self, access_token: &str) -> Result<String> {
Expand Down Expand Up @@ -169,13 +162,56 @@
Ok(url.to_string())
}

fn select_primary_email(emails: Vec<GithubUserEmail>) -> Result<String> {
emails
.into_iter()
.find(|item| item.primary)
.map(|item| item.email)
.ok_or(anyhow!("No primary email address found"))
}

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

#[test]
fn test_create_authorization_url() {
let url = create_authorization_url("client_id").unwrap();
assert_eq!(url, "https://github.com/login/oauth/authorize?client_id=client_id&response_type=code&scope=read%3Auser+user%3Aemail");
}

#[test]
fn test_select_primary_email() {
let emails = vec![
GithubUserEmail {
email: "[email protected]".into(),
primary: false,
verified: true,
visibility: None,
},
GithubUserEmail {
email: "[email protected]".into(),
primary: true,
verified: true,
visibility: None,
},
GithubUserEmail {
email: "[email protected]".into(),
primary: false,
verified: true,
visibility: None,
},
];

assert_eq!(select_primary_email(emails).unwrap(), "[email protected]");

let emails = vec![GithubUserEmail {
email: "[email protected]".into(),
primary: false,
verified: true,
visibility: None,
}];

assert!(select_primary_email(emails).is_err());
}
}
Loading