From 282b56f0df099048918a4d94c4b19968f256ca81 Mon Sep 17 00:00:00 2001 From: boxbeam Date: Tue, 16 Apr 2024 14:05:23 -0400 Subject: [PATCH] Add test case --- ee/tabby-db/src/github_repository_provider.rs | 6 +- .../src/schema/github_repository_provider.rs | 11 +- ee/tabby-webserver/src/schema/mod.rs | 5 +- ee/tabby-webserver/src/service/dao.rs | 1 + .../src/service/github_repository_provider.rs | 102 ++++++++++++++++-- 5 files changed, 111 insertions(+), 14 deletions(-) diff --git a/ee/tabby-db/src/github_repository_provider.rs b/ee/tabby-db/src/github_repository_provider.rs index 3efa0bcd2eac..2f7b9603e91e 100644 --- a/ee/tabby-db/src/github_repository_provider.rs +++ b/ee/tabby-db/src/github_repository_provider.rs @@ -112,10 +112,10 @@ impl DbConn { vendor_id: String, name: String, git_url: String, - ) -> Result<()> { - query!("INSERT INTO github_provided_repositories (github_repository_provider_id, vendor_id, name, git_url) VALUES (?, ?, ?, ?)", + ) -> Result { + let res = query!("INSERT INTO github_provided_repositories (github_repository_provider_id, vendor_id, name, git_url) VALUES (?, ?, ?, ?)", github_provider_id, vendor_id, name, git_url).execute(&self.pool).await?; - Ok(()) + Ok(res.last_insert_rowid()) } pub async fn delete_github_provided_repository(&self, id: i64) -> Result<()> { diff --git a/ee/tabby-webserver/src/schema/github_repository_provider.rs b/ee/tabby-webserver/src/schema/github_repository_provider.rs index 68989ddeadcd..0484467562e3 100644 --- a/ee/tabby-webserver/src/schema/github_repository_provider.rs +++ b/ee/tabby-webserver/src/schema/github_repository_provider.rs @@ -11,6 +11,16 @@ pub struct GithubRepositoryProvider { pub id: ID, pub display_name: String, pub application_id: String, + pub access_token: Option, +} + +impl GithubRepositoryProvider { + pub fn strip_access_token(self) -> Self { + Self { + access_token: None, + ..self + } + } } impl NodeType for GithubRepositoryProvider { @@ -60,7 +70,6 @@ impl NodeType for GithubProvidedRepository { pub trait GithubRepositoryProviderService: Send + Sync { async fn get_github_repository_provider(&self, id: ID) -> Result; async fn read_github_repository_provider_secret(&self, id: ID) -> Result; - async fn read_github_repository_provider_access_token(&self, id: ID) -> Result; async fn update_github_repository_provider_access_token( &self, id: ID, diff --git a/ee/tabby-webserver/src/schema/mod.rs b/ee/tabby-webserver/src/schema/mod.rs index 49fbb3bcbb5b..5116e2a5b4a3 100644 --- a/ee/tabby-webserver/src/schema/mod.rs +++ b/ee/tabby-webserver/src/schema/mod.rs @@ -250,7 +250,10 @@ impl Query { .locator .github_repository_provider() .list_github_repository_providers(after, before, first, last) - .await?) + .await? + .into_iter() + .map(|provider| provider.strip_access_token()) + .collect()) }, ) .await diff --git a/ee/tabby-webserver/src/service/dao.rs b/ee/tabby-webserver/src/service/dao.rs index 08542cf39d2b..aa65cb3278c9 100644 --- a/ee/tabby-webserver/src/service/dao.rs +++ b/ee/tabby-webserver/src/service/dao.rs @@ -126,6 +126,7 @@ impl From for GithubRepositoryProvider { display_name: value.display_name, application_id: value.application_id, id: value.id.as_id(), + access_token: value.access_token, } } } diff --git a/ee/tabby-webserver/src/service/github_repository_provider.rs b/ee/tabby-webserver/src/service/github_repository_provider.rs index e99cb4ee90da..7a4bf99176a4 100644 --- a/ee/tabby-webserver/src/service/github_repository_provider.rs +++ b/ee/tabby-webserver/src/service/github_repository_provider.rs @@ -1,4 +1,3 @@ -use anyhow::anyhow; use async_trait::async_trait; use juniper::ID; use tabby_db::DbConn; @@ -34,14 +33,6 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { Ok(provider.secret) } - async fn read_github_repository_provider_access_token(&self, id: ID) -> Result { - let provider = self.db.get_github_provider(id.as_rowid()?).await?; - let Some(access_token) = provider.access_token else { - return Err(anyhow!("Provider has no access token").into()); - }; - Ok(access_token) - } - async fn update_github_repository_provider_access_token( &self, id: ID, @@ -102,3 +93,96 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { Ok(()) } } + +#[cfg(test)] +mod tests { + use crate::service::AsID; + + use super::*; + + #[tokio::test] + async fn test_github_provided_repositories() { + let db = DbConn::new_in_memory().await.unwrap(); + let service = new_github_repository_provider_service(db.clone()); + + let provider_id1 = db + .create_github_provider( + "test_provider1".into(), + "test_id1".into(), + "test_secret".into(), + ) + .await + .unwrap(); + + let provider_id2 = db + .create_github_provider( + "test_provider2".into(), + "test_id2".into(), + "test_secret".into(), + ) + .await + .unwrap(); + + let repo_id1 = db + .create_github_provided_repository( + provider_id1, + "vendor_id1".into(), + "test_repo1".into(), + "https://github.com/test/test1".into(), + ) + .await + .unwrap(); + + let repo_id2 = db + .create_github_provided_repository( + provider_id2, + "vendor_id2".into(), + "test_repo2".into(), + "https://github.com/test/test2".into(), + ) + .await + .unwrap(); + + // Test listing with no filter on providers + let repos = service + .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .await + .unwrap(); + + assert_eq!(repos.len(), 2); + assert_eq!(repos[0].name, "test_repo1"); + assert_eq!(repos[1].name, "test_repo2"); + + // Test listing with a filter on providers + let repos = service + .list_github_provided_repositories_by_provider( + vec![provider_id1.as_id()], + None, + None, + None, + None, + ) + .await + .unwrap(); + + assert_eq!(repos.len(), 1); + assert_eq!(repos[0].name, "test_repo1"); + + // Test deletion and toggling active status + db.delete_github_provided_repository(repo_id1) + .await + .unwrap(); + + db.update_github_provided_repository_active(repo_id2, true) + .await + .unwrap(); + + let repos = service + .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .await + .unwrap(); + + assert_eq!(repos.len(), 1); + assert_eq!(repos[0].active, true); + } +}