Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 16, 2024
1 parent bd71d56 commit 282b56f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
6 changes: 3 additions & 3 deletions ee/tabby-db/src/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64> {
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<()> {
Expand Down
11 changes: 10 additions & 1 deletion ee/tabby-webserver/src/schema/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ pub struct GithubRepositoryProvider {
pub id: ID,
pub display_name: String,
pub application_id: String,
pub access_token: Option<String>,
}

impl GithubRepositoryProvider {
pub fn strip_access_token(self) -> Self {
Self {
access_token: None,
..self
}

Check warning on line 22 in ee/tabby-webserver/src/schema/github_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/github_repository_provider.rs#L17-L22

Added lines #L17 - L22 were not covered by tests
}
}

impl NodeType for GithubRepositoryProvider {
Expand Down Expand Up @@ -60,7 +70,6 @@ impl NodeType for GithubProvidedRepository {
pub trait GithubRepositoryProviderService: Send + Sync {
async fn get_github_repository_provider(&self, id: ID) -> Result<GithubRepositoryProvider>;
async fn read_github_repository_provider_secret(&self, id: ID) -> Result<String>;
async fn read_github_repository_provider_access_token(&self, id: ID) -> Result<String>;
async fn update_github_repository_provider_access_token(
&self,
id: ID,
Expand Down
5 changes: 4 additions & 1 deletion ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},

Check warning on line 257 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L253-L257

Added lines #L253 - L257 were not covered by tests
)
.await
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/service/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl From<GithubRepositoryProviderDAO> for GithubRepositoryProvider {
display_name: value.display_name,
application_id: value.application_id,
id: value.id.as_id(),
access_token: value.access_token,
}
}

Check warning on line 131 in ee/tabby-webserver/src/service/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/dao.rs#L129-L131

Added lines #L129 - L131 were not covered by tests
}
Expand Down
102 changes: 93 additions & 9 deletions ee/tabby-webserver/src/service/github_repository_provider.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::anyhow;
use async_trait::async_trait;
use juniper::ID;
use tabby_db::DbConn;
Expand Down Expand Up @@ -34,14 +33,6 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl {
Ok(provider.secret)
}

async fn read_github_repository_provider_access_token(&self, id: ID) -> Result<String> {
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,
Expand Down Expand Up @@ -102,3 +93,96 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl {
Ok(())
}

Check warning on line 94 in ee/tabby-webserver/src/service/github_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/github_repository_provider.rs#L89-L94

Added lines #L89 - L94 were not covered by tests
}

#[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);
}
}

0 comments on commit 282b56f

Please sign in to comment.