changes tests to a macro (#822)

This commit is contained in:
Wyatt Verchere
2024-01-06 11:08:03 -08:00
committed by GitHub
parent 10eed05d87
commit 87862f3e23
32 changed files with 423 additions and 391 deletions

View File

@@ -1,5 +1,6 @@
use crate::assert_status;
use crate::common::api_common::ApiProject;
use crate::common::asserts::assert_status;
use actix_http::StatusCode;
use actix_web::test;
use bytes::Bytes;
@@ -15,7 +16,7 @@ pub async fn error_404_empty() {
// V2 errors should have 404 as blank body, for missing resources
let api = &test_env.api;
let resp = api.get_project("does-not-exist", USER_USER_PAT).await;
assert_status(&resp, StatusCode::NOT_FOUND);
assert_status!(&resp, StatusCode::NOT_FOUND);
let body = test::read_body(resp).await;
let empty_bytes = Bytes::from_static(b"");
assert_eq!(body, empty_bytes);

View File

@@ -1,15 +1,17 @@
use std::sync::Arc;
use crate::common::{
api_common::{ApiProject, ApiVersion, AppendsOptionalPat},
api_v2::{request_data::get_public_project_creation_data_json, ApiV2},
asserts::assert_status,
database::{
generate_random_name, ADMIN_USER_PAT, FRIEND_USER_ID, FRIEND_USER_PAT, USER_USER_PAT,
use crate::{
assert_status,
common::{
api_common::{ApiProject, ApiVersion, AppendsOptionalPat},
api_v2::{request_data::get_public_project_creation_data_json, ApiV2},
database::{
generate_random_name, ADMIN_USER_PAT, FRIEND_USER_ID, FRIEND_USER_PAT, USER_USER_PAT,
},
dummy_data::TestFile,
environment::{with_test_environment, TestEnvironment},
permissions::{PermissionsTest, PermissionsTestContext},
},
dummy_data::TestFile,
environment::{with_test_environment, TestEnvironment},
permissions::{PermissionsTest, PermissionsTestContext},
};
use actix_http::StatusCode;
use actix_web::test;
@@ -134,7 +136,7 @@ async fn test_add_remove_project() {
.set_multipart(vec![json_segment.clone(), file_segment.clone()])
.to_request();
let resp: actix_web::dev::ServiceResponse = test_env.call(req).await;
assert_status(&resp, StatusCode::OK);
assert_status!(&resp, StatusCode::OK);
// Get the project we just made, and confirm that it's correct
let project = api.get_project_deserialized("demo", USER_USER_PAT).await;
@@ -162,7 +164,7 @@ async fn test_add_remove_project() {
.to_request();
let resp = test_env.call(req).await;
assert_status(&resp, StatusCode::BAD_REQUEST);
assert_status!(&resp, StatusCode::BAD_REQUEST);
// Reusing with the same slug and a different file should fail
let req = test::TestRequest::post()
@@ -175,7 +177,7 @@ async fn test_add_remove_project() {
.to_request();
let resp = test_env.call(req).await;
assert_status(&resp, StatusCode::BAD_REQUEST);
assert_status!(&resp, StatusCode::BAD_REQUEST);
// Different slug, different file should succeed
let req = test::TestRequest::post()
@@ -188,7 +190,7 @@ async fn test_add_remove_project() {
.to_request();
let resp = test_env.call(req).await;
assert_status(&resp, StatusCode::OK);
assert_status!(&resp, StatusCode::OK);
// Get
let project = api.get_project_deserialized("demo", USER_USER_PAT).await;
@@ -196,7 +198,7 @@ async fn test_add_remove_project() {
// Remove the project
let resp = test_env.api.remove_project("demo", USER_USER_PAT).await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
// Confirm that the project is gone from the cache
let mut redis_conn = test_env.db.redis_pool.connect().await.unwrap();
@@ -219,7 +221,7 @@ async fn test_add_remove_project() {
// Old slug no longer works
let resp = api.get_project("demo", USER_USER_PAT).await;
assert_status(&resp, StatusCode::NOT_FOUND);
assert_status!(&resp, StatusCode::NOT_FOUND);
})
.await;
}
@@ -343,7 +345,7 @@ pub async fn test_patch_v2() {
USER_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let project = api
.get_project_deserialized(alpha_project_slug, USER_USER_PAT)
@@ -455,7 +457,7 @@ pub async fn test_bulk_edit_links() {
ADMIN_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let alpha_body = api
.get_project_deserialized(alpha_project_id, ADMIN_USER_PAT)
@@ -495,7 +497,7 @@ pub async fn test_bulk_edit_links() {
ADMIN_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let alpha_body = api
.get_project_deserialized(alpha_project_id, ADMIN_USER_PAT)
@@ -567,7 +569,7 @@ pub async fn test_bulk_edit_links() {
ADMIN_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let alpha_body = api
.get_project_deserialized(alpha_project_id, ADMIN_USER_PAT)

View File

@@ -1,8 +1,9 @@
use crate::assert_status;
use crate::common::api_common::Api;
use crate::common::api_common::ApiProject;
use crate::common::api_common::ApiVersion;
use crate::common::api_v2::ApiV2;
use crate::common::asserts::assert_status;
use crate::common::database::*;
use crate::common::dummy_data::TestFile;
use crate::common::dummy_data::DUMMY_CATEGORIES;
@@ -55,7 +56,7 @@ async fn search_projects() {
MOD_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
(project.id.0, id)
}
};
@@ -284,7 +285,7 @@ async fn search_projects() {
// Forcibly reset the search index
let resp = api.reset_search_index().await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
// Test searches
let stream = futures::stream::iter(pairs);

View File

@@ -2,14 +2,17 @@ use actix_http::StatusCode;
use labrinth::models::teams::ProjectPermissions;
use serde_json::json;
use crate::common::{
api_common::ApiTeams,
api_v2::ApiV2,
asserts::assert_status,
database::{
FRIEND_USER_ID, FRIEND_USER_ID_PARSED, FRIEND_USER_PAT, USER_USER_ID_PARSED, USER_USER_PAT,
use crate::{
assert_status,
common::{
api_common::ApiTeams,
api_v2::ApiV2,
database::{
FRIEND_USER_ID, FRIEND_USER_ID_PARSED, FRIEND_USER_PAT, USER_USER_ID_PARSED,
USER_USER_PAT,
},
environment::{with_test_environment, TestEnvironment},
},
environment::{with_test_environment, TestEnvironment},
};
// trasnfer ownership (requires being owner, etc)
@@ -25,35 +28,35 @@ async fn transfer_ownership_v2() {
let resp = api
.transfer_team_ownership(alpha_team_id, FRIEND_USER_ID, USER_USER_PAT)
.await;
assert_status(&resp, StatusCode::BAD_REQUEST);
assert_status!(&resp, StatusCode::BAD_REQUEST);
// first, invite friend
let resp = api
.add_user_to_team(alpha_team_id, FRIEND_USER_ID, None, None, USER_USER_PAT)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
// still cannot set friend as owner (not accepted)
let resp = api
.transfer_team_ownership(alpha_team_id, FRIEND_USER_ID, USER_USER_PAT)
.await;
assert_status(&resp, StatusCode::BAD_REQUEST);
assert_status!(&resp, StatusCode::BAD_REQUEST);
// accept
let resp = api.join_team(alpha_team_id, FRIEND_USER_PAT).await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
// Cannot set ourselves as owner if we are not owner
let resp = api
.transfer_team_ownership(alpha_team_id, FRIEND_USER_ID, FRIEND_USER_PAT)
.await;
assert_status(&resp, StatusCode::UNAUTHORIZED);
assert_status!(&resp, StatusCode::UNAUTHORIZED);
// Can set friend as owner
let resp = api
.transfer_team_ownership(alpha_team_id, FRIEND_USER_ID, USER_USER_PAT)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
// Check
let members = api
@@ -80,7 +83,7 @@ async fn transfer_ownership_v2() {
let resp = api
.remove_from_team(alpha_team_id, FRIEND_USER_ID, USER_USER_PAT)
.await;
assert_status(&resp, StatusCode::UNAUTHORIZED);
assert_status!(&resp, StatusCode::UNAUTHORIZED);
// V2 only- confirm the owner changing the role to member does nothing
let resp = api
@@ -93,7 +96,7 @@ async fn transfer_ownership_v2() {
FRIEND_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let members = api
.get_team_members_deserialized(alpha_team_id, USER_USER_PAT)
.await;

View File

@@ -8,9 +8,10 @@ use labrinth::{
};
use serde_json::json;
use crate::assert_status;
use crate::common::api_common::{ApiProject, ApiVersion};
use crate::common::api_v2::ApiV2;
use crate::common::asserts::assert_status;
use crate::common::dummy_data::{DummyProjectAlpha, DummyProjectBeta};
use crate::common::environment::{with_test_environment, TestEnvironment};
use crate::common::{
@@ -36,7 +37,7 @@ pub async fn test_patch_version() {
ENEMY_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::UNAUTHORIZED);
assert_status!(&resp, StatusCode::UNAUTHORIZED);
// Failure because these are illegal requested statuses for a normal user.
for req in ["unknown", "scheduled"] {
@@ -50,7 +51,7 @@ pub async fn test_patch_version() {
USER_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::BAD_REQUEST);
assert_status!(&resp, StatusCode::BAD_REQUEST);
}
// Sucessful request to patch many fields.
@@ -74,7 +75,7 @@ pub async fn test_patch_version() {
USER_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let version = api
.get_version_deserialized(alpha_version_id, USER_USER_PAT)
@@ -102,7 +103,7 @@ pub async fn test_patch_version() {
USER_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let version = api
.get_version_deserialized(alpha_version_id, USER_USER_PAT)
@@ -119,7 +120,7 @@ pub async fn test_patch_version() {
USER_USER_PAT,
)
.await;
assert_status(&resp, StatusCode::NO_CONTENT);
assert_status!(&resp, StatusCode::NO_CONTENT);
let version = api
.get_version_deserialized(alpha_version_id, USER_USER_PAT)
@@ -262,12 +263,12 @@ async fn version_updates() {
)
.await;
if success {
assert_status(&resp, StatusCode::OK);
assert_status!(&resp, StatusCode::OK);
let body: serde_json::Value = test::read_body_json(resp).await;
let id = body["id"].as_str().unwrap();
assert_eq!(id, &result_id.to_string());
} else {
assert_status(&resp, StatusCode::NOT_FOUND);
assert_status!(&resp, StatusCode::NOT_FOUND);
}
// update_files