diff options
-rw-r--r-- | tests/ci_test.rs | 201 |
1 files changed, 89 insertions, 112 deletions
diff --git a/tests/ci_test.rs b/tests/ci_test.rs index 9685edc..bca0444 100644 --- a/tests/ci_test.rs +++ b/tests/ci_test.rs @@ -1,48 +1,25 @@ -use eyre::{ensure, eyre, WrapErr}; use forgejo_api::{structs::*, Forgejo}; -#[tokio::test] -async fn ci() -> eyre::Result<()> { - let url = url::Url::parse(&std::env::var("FORGEJO_API_CI_INSTANCE_URL")?)?; - let token = std::env::var("FORGEJO_API_CI_TOKEN")?; - let api = Forgejo::new(forgejo_api::Auth::Token(&token), url)?; - - let mut results = Vec::new(); - - results.push(user(&api).await.wrap_err("user error")); - results.push(repo(&api).await.wrap_err("repo error")); - results.push(admin(&api).await.wrap_err("admin error")); - - let mut errors = 0; - for report in results.into_iter().filter_map(Result::err) { - errors += 1; - for (i, err) in report.chain().enumerate() { - println!("{i}. {err}"); - if let Some(err) = err.downcast_ref::<forgejo_api::ForgejoError>() { - if let forgejo_api::ForgejoError::BadStructure(_, body) = err { - println!("BODY: {body}"); - } - } - } - } - if errors > 0 { - eyre::bail!("test failed"); - } - - Ok(()) +fn get_api() -> Forgejo { + let url = url::Url::parse(&std::env::var("FORGEJO_API_CI_INSTANCE_URL").unwrap()).unwrap(); + let token = std::env::var("FORGEJO_API_CI_TOKEN").unwrap(); + Forgejo::new(forgejo_api::Auth::Token(&token), url).unwrap() } -async fn user(api: &Forgejo) -> eyre::Result<()> { - let myself = api.user_get_current().await?; - ensure!(myself.is_admin.unwrap(), "user should be admin"); - ensure!( - myself.login.as_ref().unwrap() == "TestingAdmin", +#[tokio::test] +async fn user() { + let api = get_api(); + + let myself = api.user_get_current().await.unwrap(); + assert!(myself.is_admin.unwrap(), "user should be admin"); + assert_eq!( + myself.login.as_ref().unwrap(), "TestingAdmin", "user should be named \"TestingAdmin\"" ); - let myself_indirect = api.user_get("TestingAdmin").await?; - ensure!( - myself == myself_indirect, + let myself_indirect = api.user_get("TestingAdmin").await.unwrap(); + assert_eq!( + myself, myself_indirect, "result of `myself` does not match result of `get_user`" ); @@ -50,17 +27,17 @@ async fn user(api: &Forgejo) -> eyre::Result<()> { page: None, limit: None, }; - let following = api.user_list_following("TestingAdmin", query).await?; - ensure!(following == Vec::new(), "following list not empty"); + let following = api.user_list_following("TestingAdmin", query).await.unwrap(); + assert_eq!(following, Vec::new(), "following list not empty"); let query = UserListFollowersQuery { page: None, limit: None, }; - let followers = api.user_list_followers("TestingAdmin", query).await?; - ensure!(followers == Vec::new(), "follower list not empty"); + let followers = api.user_list_followers("TestingAdmin", query).await.unwrap(); + assert_eq!(followers, Vec::new(), "follower list not empty"); - let url = url::Url::parse(&std::env::var("FORGEJO_API_CI_INSTANCE_URL")?)?; + let url = url::Url::parse(&std::env::var("FORGEJO_API_CI_INSTANCE_URL").unwrap()).unwrap(); let password_api = Forgejo::new( forgejo_api::Auth::Password { username: "TestingAdmin", @@ -69,18 +46,19 @@ async fn user(api: &Forgejo) -> eyre::Result<()> { }, url, ) - .wrap_err("failed to log in using username and password")?; + .expect("failed to log in using username and password"); - ensure!( - api.user_get_current().await? == password_api.user_get_current().await?, + assert!( + api.user_get_current().await.unwrap() == password_api.user_get_current().await.unwrap(), "users not equal comparing token-auth and pass-auth" ); - - Ok(()) } -async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { - tokio::fs::create_dir("/test_repo").await?; +#[tokio::test] +async fn repo() { + let api = get_api(); + + tokio::fs::create_dir("/test_repo").await.unwrap(); let git = || { let mut cmd = std::process::Command::new("git"); cmd.current_dir("/test_repo"); @@ -88,17 +66,17 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { }; let _ = git() .args(["config", "--global", "init.defaultBranch", "main"]) - .status()?; - let _ = git().args(["init"]).status()?; + .status().unwrap(); + let _ = git().args(["init"]).status().unwrap(); let _ = git() .args(["config", "user.name", "TestingAdmin"]) - .status()?; + .status().unwrap(); let _ = git() .args(["config", "user.email", "admin@noreply.example.org"]) - .status()?; - tokio::fs::write("/test_repo/README.md", "# Test\nThis is a test repo").await?; - let _ = git().args(["add", "."]).status()?; - let _ = git().args(["commit", "-m", "initial commit"]).status()?; + .status().unwrap(); + tokio::fs::write("/test_repo/README.md", "# Test\nThis is a test repo").await.unwrap(); + let _ = git().args(["add", "."]).status().unwrap(); + let _ = git().args(["commit", "-m", "initial commit"]).status().unwrap(); let repo_opt = CreateRepoOption { auto_init: Some(false), @@ -113,16 +91,16 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { template: Some(false), trust_model: Some("default".into()), }; - let remote_repo = api.create_current_user_repo(repo_opt).await?; - ensure!( + let remote_repo = api.create_current_user_repo(repo_opt).await.unwrap(); + assert!( remote_repo.has_pull_requests.unwrap(), "repo does not accept pull requests" ); - ensure!( + assert!( remote_repo.owner.as_ref().unwrap().login.as_ref().unwrap() == "TestingAdmin", "repo owner is not \"TestingAdmin\"" ); - ensure!( + assert!( remote_repo.name.as_ref().unwrap() == "test", "repo owner is not \"test\"" ); @@ -133,18 +111,18 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { remote_url.set_password(Some("password")).unwrap(); let _ = git() .args(["remote", "add", "origin", remote_url.as_str()]) - .status()?; - let _ = git().args(["push", "-u", "origin", "main"]).status()?; + .status().unwrap(); + let _ = git().args(["push", "-u", "origin", "main"]).status().unwrap(); - let _ = git().args(["switch", "-c", "test"]).status()?; + let _ = git().args(["switch", "-c", "test"]).status().unwrap(); tokio::fs::write( "/test_repo/example.rs", "fn add_one(x: u32) -> u32 { x + 1 }", ) - .await?; - let _ = git().args(["add", "."]).status()?; - let _ = git().args(["commit", "-m", "egg"]).status()?; - let _ = git().args(["push", "-u", "origin", "test"]).status()?; + .await.unwrap(); + let _ = git().args(["add", "."]).status().unwrap(); + let _ = git().args(["commit", "-m", "egg"]).status().unwrap(); + let _ = git().args(["push", "-u", "origin", "test"]).status().unwrap(); let pr_opt = CreatePullRequestOption { assignee: None, @@ -160,13 +138,13 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let pr = api .repo_create_pull_request("TestingAdmin", "test", pr_opt) .await - .wrap_err("couldn't create pr")?; + .expect("couldn't create pr"); tokio::time::sleep(std::time::Duration::from_secs(3)).await; let is_merged = api .repo_pull_request_is_merged("TestingAdmin", "test", pr.number.unwrap()) .await .is_ok(); - ensure!(!is_merged, "pr should not yet be merged"); + assert!(!is_merged, "pr should not yet be merged"); let merge_opt = MergePullRequestOption { r#do: "merge".into(), merge_commit_id: None, @@ -179,14 +157,14 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { }; api.repo_merge_pull_request("TestingAdmin", "test", pr.number.unwrap(), merge_opt) .await - .wrap_err_with(|| eyre!("couldn't merge pr {}", pr.number.unwrap()))?; + .expect("couldn't merge pr"); let is_merged = api .repo_pull_request_is_merged("TestingAdmin", "test", pr.number.unwrap()) .await .is_ok(); - ensure!(is_merged, "pr should be merged"); - let _ = git().args(["fetch"]).status()?; - let _ = git().args(["pull"]).status()?; + assert!(is_merged, "pr should be merged"); + let _ = git().args(["fetch"]).status().unwrap(); + let _ = git().args(["pull"]).status().unwrap(); let query = RepoListReleasesQuery { draft: None, @@ -195,10 +173,10 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { page: None, limit: None, }; - ensure!( + assert!( api.repo_list_releases("TestingAdmin", "test", query) .await - .wrap_err("releases list not found")? + .unwrap() .is_empty(), "there should be no releases yet" ); @@ -210,7 +188,7 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { }; api.repo_create_tag("TestingAdmin", "test", tag_opt) .await - .wrap_err("failed to create tag")?; + .expect("failed to create tag"); let release_opt = CreateReleaseOption { body: Some("This is a release!".into()), @@ -223,7 +201,7 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let release = api .repo_create_release("TestingAdmin", "test", release_opt) .await - .wrap_err("failed to create release")?; + .expect("failed to create release"); let edit_release = EditReleaseOption { body: None, draft: Some(false), @@ -234,17 +212,17 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { }; api.repo_edit_release("TestingAdmin", "test", release.id.unwrap(), edit_release) .await - .wrap_err("failed to edit release")?; + .expect("failed to edit release"); let release_by_tag = api .repo_get_release_by_tag("TestingAdmin", "test", "v1.0") .await - .wrap_err("failed to find release")?; + .expect("failed to find release"); let release_latest = api .repo_get_latest_release("TestingAdmin", "test") .await - .wrap_err("failed to find latest release")?; - ensure!(release_by_tag == release_latest, "releases not equal"); + .expect("failed to find latest release"); + assert!(release_by_tag == release_latest, "releases not equal"); let attachment = api .repo_create_release_attachment( @@ -257,8 +235,8 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { }, ) .await - .wrap_err("failed to create release attachment")?; - ensure!( + .expect("failed to create release attachment"); + assert!( &*api .download_release_attachment( "TestingAdmin", @@ -266,7 +244,7 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { release.id.unwrap(), attachment.id.unwrap() ) - .await? + .await.unwrap() == b"This is a file!", "couldn't download attachment" ); @@ -287,20 +265,21 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { attachment.id.unwrap(), ) .await - .wrap_err("failed to deleted attachment")?; + .expect("failed to deleted attachment"); api.repo_delete_release("TestingAdmin", "test", release.id.unwrap()) .await - .wrap_err("failed to delete release")?; + .expect("failed to delete release"); api.repo_delete_tag("TestingAdmin", "test", "v1.0") .await - .wrap_err("failed to delete release")?; - - Ok(()) + .expect("failed to delete release"); } -async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { +#[tokio::test] +async fn admin() { + let api = get_api(); + let user_opt = CreateUserOption { created_at: None, email: "user@noreply.example.org".into(), @@ -317,7 +296,7 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let _ = api .admin_create_user(user_opt) .await - .wrap_err("failed to create user")?; + .expect("failed to create user"); let query = AdminSearchUsersQuery { source_id: None, @@ -328,8 +307,8 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let users = api .admin_search_users(query) .await - .wrap_err("failed to search users")?; - ensure!( + .expect("failed to search users"); + assert!( users .iter() .find(|u| u.login.as_ref().unwrap() == "Pipis") @@ -343,8 +322,8 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let users = api .admin_get_all_emails(query) .await - .wrap_err("failed to search emails")?; - ensure!( + .expect("failed to search emails"); + assert!( users .iter() .find(|u| u.email.as_ref().unwrap() == "user@noreply.example.org") @@ -365,13 +344,13 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let _ = api .admin_create_org("Pipis", org_opt) .await - .wrap_err("failed to create org")?; + .expect("failed to create org"); let query = AdminGetAllOrgsQuery { page: None, limit: None, }; - ensure!( - !api.admin_get_all_orgs(query).await?.is_empty(), + assert!( + !api.admin_get_all_orgs(query).await.unwrap().is_empty(), "org list empty" ); @@ -383,23 +362,23 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let key = api .admin_create_public_key("Pipis", key_opt) .await - .wrap_err("failed to create key")?; + .expect("failed to create key"); api.admin_delete_user_public_key("Pipis", key.id.unwrap()) .await - .wrap_err("failed to delete key")?; + .expect("failed to delete key"); let rename_opt = RenameUserOption { new_username: "Bepis".into(), }; api.admin_rename_user("Pipis", rename_opt) .await - .wrap_err("failed to rename user")?; + .expect("failed to rename user"); let query = AdminDeleteUserQuery { purge: Some(true) }; api.admin_delete_user("Bepis", query) .await - .wrap_err("failed to delete user")?; + .expect("failed to delete user"); let query = AdminDeleteUserQuery { purge: Some(true) }; - ensure!( + assert!( api.admin_delete_user("Ghost", query).await.is_err(), "deleting fake user should fail" ); @@ -411,17 +390,17 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let crons = api .admin_cron_list(query) .await - .wrap_err("failed to get crons list")?; + .expect("failed to get crons list"); api.admin_cron_run( &crons .get(0) - .ok_or_else(|| eyre!("no crons"))? + .expect("no crons") .name .as_ref() .unwrap(), ) .await - .wrap_err("failed to run cron")?; + .expect("failed to run cron"); let hook_opt = CreateHookOption { active: None, @@ -443,7 +422,7 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let hook = api .admin_create_hook(hook_opt) .await - .wrap_err("failed to create hook")?; + .expect("failed to create hook"); let edit_hook = EditHookOption { active: Some(true), authorization_header: None, @@ -453,10 +432,8 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { }; api.admin_edit_hook(hook.id.unwrap(), edit_hook) .await - .wrap_err("failed to edit hook")?; + .expect("failed to edit hook"); api.admin_delete_hook(hook.id.unwrap()) .await - .wrap_err("failed to delete hook")?; - - Ok(()) + .expect("failed to delete hook"); } |