Skip to content

Commit

Permalink
test(webserver&schema): adding more backend test cases for cov (#3215)
Browse files Browse the repository at this point in the history
* to: adding test cases to job.rs

* to: adding test cases for policy.rs

* test(service/thread): add test cases for get_thread and set_persisted functions

remove test comment

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
Sma1lboy and autofix-ci[bot] authored Sep 28, 2024
1 parent 6c18437 commit 28ecaa8
Show file tree
Hide file tree
Showing 3 changed files with 401 additions and 1 deletion.
152 changes: 152 additions & 0 deletions ee/tabby-schema/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,156 @@ mod tests {
assert!(policy1.check_read_source(source_id).await.is_ok());
assert!(policy2.check_read_source(source_id).await.is_ok());
}

#[tokio::test]
async fn test_check_delete_thread_messages() {
let db = DbConn::new_in_memory().await.unwrap();
let user_id1 = testutils::create_user(&db).await;
let user_id2 = testutils::create_user2(&db).await;

let policy1 = AccessPolicy::new(db.clone(), &user_id1.as_id(), false);

assert!(policy1
.check_delete_thread_messages(&user_id1.as_id())
.is_ok());
assert!(policy1
.check_delete_thread_messages(&user_id2.as_id())
.is_err());
}

#[tokio::test]
async fn test_check_update_thread_persistence() {
let db = DbConn::new_in_memory().await.unwrap();
let user_id1 = testutils::create_user(&db).await;
let user_id2 = testutils::create_user2(&db).await;

let policy1 = AccessPolicy::new(db.clone(), &user_id1.as_id(), false);

assert!(policy1
.check_update_thread_persistence(&user_id1.as_id())
.is_ok());
assert!(policy1
.check_update_thread_persistence(&user_id2.as_id())
.is_err());
}

#[tokio::test]
async fn test_check_read_analytic() {
let db = DbConn::new_in_memory().await.unwrap();
let user_id1 = testutils::create_user(&db).await;
let user_id2 = testutils::create_user2(&db).await;

let policy_normal = AccessPolicy::new(db.clone(), &user_id1.as_id(), false);
let policy_admin = AccessPolicy::new(db.clone(), &user_id1.as_id(), true);

assert!(policy_normal
.check_read_analytic(&[user_id1.as_id()])
.is_ok());
assert!(policy_normal
.check_read_analytic(&[user_id2.as_id()])
.is_err());
assert!(policy_normal.check_read_analytic(&[]).is_err());

assert!(policy_admin
.check_read_analytic(&[user_id1.as_id()])
.is_ok());
assert!(policy_admin
.check_read_analytic(&[user_id2.as_id()])
.is_ok());
assert!(policy_admin.check_read_analytic(&[]).is_ok());
}

#[tokio::test]
async fn test_check_upsert_user_group_membership() {
let db = DbConn::new_in_memory().await.unwrap();
let user_id1 = testutils::create_user(&db).await;
let user_id2 = testutils::create_user2(&db).await;
let user_group_id = db.create_user_group("test").await.unwrap();

db.upsert_user_group_membership(user_id1, user_group_id, true)
.await
.unwrap();

let policy_normal = AccessPolicy::new(db.clone(), &user_id2.as_id(), false);
let policy_group_admin = AccessPolicy::new(db.clone(), &user_id1.as_id(), false);
let policy_admin = AccessPolicy::new(db.clone(), &user_id1.as_id(), true);

let input = UpsertUserGroupMembershipInput {
user_id: user_id2.as_id(),
user_group_id: user_group_id.as_id(),
is_group_admin: false,
};

assert!(policy_normal
.check_upsert_user_group_membership(&input)
.await
.is_err());

assert!(policy_group_admin
.check_upsert_user_group_membership(&input)
.await
.is_ok());

let admin_input = UpsertUserGroupMembershipInput {
is_group_admin: true,
user_id: user_id2.as_id(),
user_group_id: user_group_id.as_id(),
};
assert!(policy_group_admin
.check_upsert_user_group_membership(&admin_input)
.await
.is_err());

assert!(policy_admin
.check_upsert_user_group_membership(&input)
.await
.is_ok());
assert!(policy_admin
.check_upsert_user_group_membership(&admin_input)
.await
.is_ok());
}
#[tokio::test]
async fn test_check_delete_user_group_membership() {
let db = DbConn::new_in_memory().await.unwrap();
let user_id1 = testutils::create_user(&db).await;
let user_id2 = testutils::create_user2(&db).await;
let user_group_id = db.create_user_group("test").await.unwrap();

// Make user1 a group admin and user2 a normal member
db.upsert_user_group_membership(user_id1, user_group_id, true)
.await
.unwrap();
db.upsert_user_group_membership(user_id2, user_group_id, false)
.await
.unwrap();

let policy_normal = AccessPolicy::new(db.clone(), &user_id2.as_id(), false);
let policy_group_admin = AccessPolicy::new(db.clone(), &user_id1.as_id(), false);
let policy_admin = AccessPolicy::new(db.clone(), &user_id1.as_id(), true);

assert!(policy_normal
.check_delete_user_group_membership(&user_group_id.as_id(), &user_id2.as_id())
.await
.is_err());

assert!(policy_group_admin
.check_delete_user_group_membership(&user_group_id.as_id(), &user_id2.as_id())
.await
.is_ok());

assert!(policy_group_admin
.check_delete_user_group_membership(&user_group_id.as_id(), &user_id1.as_id())
.await
.is_err());

assert!(policy_admin
.check_delete_user_group_membership(&user_group_id.as_id(), &user_id1.as_id())
.await
.is_ok());
assert!(policy_admin
.check_delete_user_group_membership(&user_group_id.as_id(), &user_id2.as_id())
.await
.is_ok());
}
}
74 changes: 74 additions & 0 deletions ee/tabby-webserver/src/service/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,78 @@ mod tests {
assert_matches!(job2dao.exit_code, Some(-1));
assert!(!job2dao.is_pending())
}

#[tokio::test]
async fn test_list() {
let db = DbConn::new_in_memory().await.unwrap();
let svc = super::create(db.clone()).await;

let job1 = BackgroundJobEvent::WebCrawler(WebCrawlerJob::new(
"s1".into(),
"http://abc.com".into(),
None,
));
let job2 = BackgroundJobEvent::WebCrawler(WebCrawlerJob::new(
"s2".into(),
"http://def.com".into(),
None,
));

let id1 = svc.trigger(job1.to_command()).await.unwrap();
let id2 = svc.trigger(job2.to_command()).await.unwrap();

let ids = Vec::from([id1.clone(), id2.clone()]);
let all_jobs = svc
.list(Some(ids), None, None, None, None, None)
.await
.unwrap();
assert_eq!(all_jobs.len(), 2);

let specific_jobs = svc
.list(Some(vec![id1.clone()]), None, None, None, None, None)
.await
.unwrap();
assert_eq!(specific_jobs.len(), 1);
assert_eq!(specific_jobs[0].id, id1);

let first_job = svc
.list(None, None, None, None, Some(1), None)
.await
.unwrap();
assert_eq!(first_job.len(), 1);
assert_eq!(first_job[0].id, id1);
}

#[tokio::test]
async fn test_compute_stats() {
let db = DbConn::new_in_memory().await.unwrap();
let svc = super::create(db.clone()).await;

let job1 = BackgroundJobEvent::WebCrawler(WebCrawlerJob::new(
"s1".into(),
"http://abc.com".into(),
None,
));
let job2 = BackgroundJobEvent::WebCrawler(WebCrawlerJob::new(
"s2".into(),
"http://edf.com".into(),
None,
));

svc.trigger(job1.to_command()).await.unwrap();
svc.trigger(job2.to_command()).await.unwrap();

let stats = svc.compute_stats(None).await.unwrap();
assert_eq!(stats.pending, 2);
assert_eq!(stats.success, 0);
assert_eq!(stats.failed, 0);

let _ = db.update_job_status(1, 0).await;
let _ = db.update_job_status(2, 1).await;

let updated_stats = svc.compute_stats(None).await.unwrap();
assert_eq!(updated_stats.pending, 0);
assert_eq!(updated_stats.success, 1);
assert_eq!(updated_stats.failed, 1);
}
}
Loading

0 comments on commit 28ecaa8

Please sign in to comment.