Misc testing improvements (#805)

* made dummy data more consistent; not an option

* fixed variable dropping issue crashing actix (?)

* removed scopes specific tests, removed schedule tests

* team routes use api

* removed printlns, fmt clippy prepare
This commit is contained in:
Wyatt Verchere
2023-12-20 11:46:53 -08:00
committed by GitHub
parent d59c522f7f
commit 60c535e861
39 changed files with 1775 additions and 1436 deletions

View File

@@ -2,7 +2,6 @@ use std::collections::HashMap;
use actix_http::StatusCode;
use actix_web::test;
use chrono::{Duration, Utc};
use common::api_v3::ApiV3;
use common::database::*;
use common::dummy_data::DUMMY_CATEGORIES;
@@ -20,25 +19,29 @@ use serde_json::json;
use crate::common::api_common::models::CommonItemType;
use crate::common::api_common::request_data::ProjectCreationRequestData;
use crate::common::api_common::{ApiProject, ApiTeams, ApiVersion, AppendsOptionalPat};
use crate::common::dummy_data::{DummyImage, TestFile};
use crate::common::api_common::{ApiProject, ApiTeams, ApiVersion};
use crate::common::dummy_data::{DummyImage, DummyProjectAlpha, DummyProjectBeta, TestFile};
mod common;
#[actix_rt::test]
async fn test_get_project() {
// Test setup and dummy data
with_test_environment_all(None, |test_env| async move {
let alpha_project_id = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let beta_project_id = &test_env.dummy.as_ref().unwrap().project_beta.project_id;
let alpha_project_slug = &test_env.dummy.as_ref().unwrap().project_alpha.project_slug;
let alpha_version_id = &test_env.dummy.as_ref().unwrap().project_alpha.version_id;
let DummyProjectAlpha {
project_id: alpha_project_id,
project_slug: alpha_project_slug,
version_id: alpha_version_id,
..
} = &test_env.dummy.project_alpha;
let DummyProjectBeta {
project_id: beta_project_id,
..
} = &test_env.dummy.project_beta;
let api = &test_env.api;
// Perform request on dummy data
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{alpha_project_id}"))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let resp = api.get_project(alpha_project_id, USER_USER_PAT).await;
let status = resp.status();
let body: serde_json::Value = test::read_body_json(resp).await;
@@ -71,11 +74,7 @@ async fn test_get_project() {
assert_eq!(cached_project["inner"]["slug"], json!(alpha_project_slug));
// Make the request again, this time it should be cached
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{alpha_project_id}"))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let resp = api.get_project(alpha_project_id, USER_USER_PAT).await;
let status = resp.status();
assert_eq!(status, 200);
@@ -84,21 +83,11 @@ async fn test_get_project() {
assert_eq!(body["slug"], json!(alpha_project_slug));
// Request should fail on non-existent project
let req = test::TestRequest::get()
.uri("/v3/project/nonexistent")
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let resp = api.get_project("nonexistent", USER_USER_PAT).await;
assert_eq!(resp.status(), 404);
// Similarly, request should fail on non-authorized user, on a yet-to-be-approved or hidden project, with a 404 (hiding the existence of the project)
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{beta_project_id}"))
.append_pat(ENEMY_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let resp = api.get_project(beta_project_id, ENEMY_USER_PAT).await;
assert_eq!(resp.status(), 404);
})
.await;
@@ -290,8 +279,8 @@ pub async fn test_patch_project() {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
let alpha_project_slug = &test_env.dummy.as_ref().unwrap().project_alpha.project_slug;
let beta_project_slug = &test_env.dummy.as_ref().unwrap().project_beta.project_slug;
let alpha_project_slug = &test_env.dummy.project_alpha.project_slug;
let beta_project_slug = &test_env.dummy.project_beta.project_slug;
// First, we do some patch requests that should fail.
// Failure because the user is not authorized.
@@ -472,7 +461,7 @@ pub async fn test_patch_v3() {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
let alpha_project_slug = &test_env.dummy.as_ref().unwrap().project_alpha.project_slug;
let alpha_project_slug = &test_env.dummy.project_alpha.project_slug;
// Sucessful request to patch many fields.
let resp = api
@@ -503,8 +492,8 @@ pub async fn test_patch_v3() {
pub async fn test_bulk_edit_categories() {
with_test_environment_all(None, |test_env| async move {
let api = &test_env.api;
let alpha_project_id: &str = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let beta_project_id: &str = &test_env.dummy.as_ref().unwrap().project_beta.project_id;
let alpha_project_id: &str = &test_env.dummy.project_alpha.project_id;
let beta_project_id: &str = &test_env.dummy.project_beta.project_id;
let resp = api
.edit_project_bulk(
@@ -544,8 +533,8 @@ pub async fn test_bulk_edit_categories() {
pub async fn test_bulk_edit_links() {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
let alpha_project_id: &str = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let beta_project_id: &str = &test_env.dummy.as_ref().unwrap().project_beta.project_id;
let alpha_project_id: &str = &test_env.dummy.project_alpha.project_id;
let beta_project_id: &str = &test_env.dummy.project_beta.project_id;
// Sets links for issue, source, wiki, and patreon for all projects
// The first loop, sets issue, the second, clears it for all projects.
@@ -595,8 +584,8 @@ pub async fn test_bulk_edit_links() {
async fn delete_project_with_report() {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
let alpha_project_id: &str = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let beta_project_id: &str = &test_env.dummy.as_ref().unwrap().project_beta.project_id;
let alpha_project_id: &str = &test_env.dummy.project_alpha.project_id;
let beta_project_id: &str = &test_env.dummy.project_beta.project_id;
// Create a report for the project
let resp = api
@@ -682,8 +671,8 @@ async fn delete_project_with_report() {
#[actix_rt::test]
async fn permissions_patch_project_v3() {
with_test_environment(Some(8), |test_env: TestEnvironment<ApiV3>| async move {
let alpha_project_id = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let alpha_team_id = &test_env.dummy.as_ref().unwrap().project_alpha.team_id;
let alpha_project_id = &test_env.dummy.project_alpha.project_id;
let alpha_team_id = &test_env.dummy.project_alpha.team_id;
let api = &test_env.api;
// TODO: This should be a separate test from v3
@@ -797,47 +786,69 @@ async fn permissions_patch_project_v3() {
.await;
}
// TODO: Project scheduling has been temporarily disabled, so this test is disabled as well
// #[actix_rt::test]
// async fn permissions_schedule() {
// with_test_environment(None, |test_env : TestEnvironment<ApiV3>| async move {
// let DummyProjectAlpha {
// project_id: alpha_project_id,
// team_id: alpha_team_id,
// ..
// } = &test_env.dummy.project_alpha;
// let DummyProjectBeta {
// project_id: beta_project_id,
// version_id: beta_version_id,
// team_id: beta_team_id,
// ..
// } = &test_env.dummy.project_beta;
// let edit_details = ProjectPermissions::EDIT_DETAILS;
// let api = &test_env.api;
// // Approve beta version as private so we can schedule it
// let resp = api
// .edit_version(
// beta_version_id,
// json!({
// "status": "unlisted"
// }),
// MOD_USER_PAT,
// )
// .await;
// assert_eq!(resp.status(), 204);
// // Schedule version
// let req_gen = |ctx: PermissionsTestContext| async move {
// api.schedule_version(
// beta_version_id,
// "archived",
// Utc::now() + Duration::days(1),
// ctx.test_pat.as_deref(),
// )
// .await
// };
// PermissionsTest::new(&test_env)
// .with_existing_project(beta_project_id, beta_team_id)
// .with_user(FRIEND_USER_ID, FRIEND_USER_PAT, true)
// .simple_project_permissions_test(edit_details, req_gen)
// .await
// .unwrap();
// }).await
// }
// Not covered by PATCH /project
#[actix_rt::test]
async fn permissions_edit_details() {
with_test_environment_all(None, |test_env| async move {
let alpha_project_id = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let alpha_team_id = &test_env.dummy.as_ref().unwrap().project_alpha.team_id;
let beta_project_id = &test_env.dummy.as_ref().unwrap().project_beta.project_id;
let beta_team_id = &test_env.dummy.as_ref().unwrap().project_beta.team_id;
let beta_version_id = &test_env.dummy.as_ref().unwrap().project_beta.version_id;
let DummyProjectAlpha {
project_id: alpha_project_id,
team_id: alpha_team_id,
..
} = &test_env.dummy.project_alpha;
let edit_details = ProjectPermissions::EDIT_DETAILS;
let api = &test_env.api;
// Approve beta version as private so we can schedule it
let req = test::TestRequest::patch()
.uri(&format!("/v3/version/{beta_version_id}"))
.append_pat(MOD_USER_PAT)
.set_json(json!({
"status": "unlisted"
}))
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 204);
// Schedule version
let req_gen = |ctx: PermissionsTestContext| async move {
api.schedule_project(
beta_version_id,
"archived",
Utc::now() + Duration::days(1),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(beta_project_id, beta_team_id)
.with_user(FRIEND_USER_ID, FRIEND_USER_PAT, true)
.simple_project_permissions_test(edit_details, req_gen)
.await
.unwrap();
// Icon edit
// Uses alpha project to delete this icon
let req_gen = |ctx: PermissionsTestContext| async move {
@@ -889,11 +900,7 @@ async fn permissions_edit_details() {
.await
.unwrap();
// Get project, as we need the gallery image url
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{alpha_project_id}"))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let resp = api.get_project(alpha_project_id, USER_USER_PAT).await;
let project: serde_json::Value = test::read_body_json(resp).await;
let gallery_url = project["gallery"][0]["url"].as_str().unwrap();
@@ -940,10 +947,10 @@ async fn permissions_edit_details() {
#[actix_rt::test]
async fn permissions_upload_version() {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let alpha_project_id = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let alpha_version_id = &test_env.dummy.as_ref().unwrap().project_alpha.version_id;
let alpha_team_id = &test_env.dummy.as_ref().unwrap().project_alpha.team_id;
let alpha_file_hash = &test_env.dummy.as_ref().unwrap().project_alpha.file_hash;
let alpha_project_id = &test_env.dummy.project_alpha.project_id;
let alpha_version_id = &test_env.dummy.project_alpha.version_id;
let alpha_team_id = &test_env.dummy.project_alpha.team_id;
let alpha_file_hash = &test_env.dummy.project_alpha.file_hash;
let api = &test_env.api;
@@ -1038,8 +1045,8 @@ async fn permissions_upload_version() {
async fn permissions_manage_invites() {
// Add member, remove member, edit member
with_test_environment_all(None, |test_env| async move {
let alpha_project_id = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
let alpha_team_id = &test_env.dummy.as_ref().unwrap().project_alpha.team_id;
let alpha_project_id = &test_env.dummy.project_alpha.project_id;
let alpha_team_id = &test_env.dummy.project_alpha.team_id;
let api = &test_env.api;
let manage_invites = ProjectPermissions::MANAGE_INVITES;
@@ -1096,22 +1103,19 @@ async fn permissions_manage_invites() {
.unwrap();
// re-add member for testing
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{}/members", alpha_team_id))
.append_pat(ADMIN_USER_PAT)
.set_json(json!({
"user_id": MOD_USER_ID,
}))
.to_request();
let resp = test_env.call(req).await;
let resp = api
.add_user_to_team(
alpha_team_id,
MOD_USER_ID,
Some(ProjectPermissions::empty()),
None,
ADMIN_USER_PAT,
)
.await;
assert_eq!(resp.status(), 204);
// Accept invite
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{}/join", alpha_team_id))
.append_pat(MOD_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let resp = api.join_team(alpha_team_id, MOD_USER_PAT).await;
assert_eq!(resp.status(), 204);
// remove existing member (requires remove_member)