Test permissions use api (#784)

* initial push

* fmt; clippy

---------

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Wyatt Verchere
2023-12-11 18:24:49 -08:00
committed by GitHub
parent 27ccd3dfa8
commit 6217523cc8
32 changed files with 1558 additions and 1015 deletions

View File

@@ -8,7 +8,8 @@ use crate::models::projects::{Dependency, FileType, Version, VersionStatus, Vers
use crate::models::v2::projects::LegacyVersion;
use crate::queue::session::AuthQueue;
use crate::routes::{v2_reroute, v3};
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
use actix_web::{delete, get, patch, post, web, HttpRequest, HttpResponse};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use validator::Validate;
@@ -22,6 +23,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.service(version_get)
.service(version_delete)
.service(version_edit)
.service(version_schedule)
.service(super::version_creation::upload_file_to_version),
);
}
@@ -270,3 +272,35 @@ pub async fn version_delete(
.await
.or_else(v2_reroute::flatten_404_error)
}
#[derive(Deserialize)]
pub struct SchedulingData {
pub time: DateTime<Utc>,
pub requested_status: VersionStatus,
}
#[post("{id}/schedule")]
pub async fn version_schedule(
req: HttpRequest,
info: web::Path<(VersionId,)>,
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
scheduling_data: web::Json<SchedulingData>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let scheduling_data = scheduling_data.into_inner();
let scheduling_data = v3::versions::SchedulingData {
time: scheduling_data.time,
requested_status: scheduling_data.requested_status,
};
v3::versions::version_schedule(
req,
info,
pool,
redis,
web::Json(scheduling_data),
session_queue,
)
.await
.or_else(v2_reroute::flatten_404_error)
}

View File

@@ -1,4 +1,3 @@
use actix_web::test;
use chrono::{DateTime, Duration, Utc};
use common::permissions::PermissionsTest;
use common::permissions::PermissionsTestContext;
@@ -166,15 +165,23 @@ pub async fn permissions_analytics_revenue() {
.team_id
.clone();
let api = &test_env.api;
let view_analytics = ProjectPermissions::VIEW_ANALYTICS;
// first, do check with a project
let req_gen = |ctx: &PermissionsTestContext| {
let projects_string = serde_json::to_string(&vec![ctx.project_id]).unwrap();
let projects_string = urlencoding::encode(&projects_string);
test::TestRequest::get().uri(&format!(
"/v3/analytics/revenue?project_ids={projects_string}&resolution_minutes=5",
))
let req_gen = |ctx: PermissionsTestContext| async move {
let project_id = ctx.project_id.unwrap();
let ids_or_slugs = vec![project_id.as_str()];
api.get_analytics_revenue(
ids_or_slugs,
false,
None,
None,
Some(5),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
@@ -197,12 +204,20 @@ pub async fn permissions_analytics_revenue() {
// Now with a version
// Need to use alpha
let req_gen = |_: &PermissionsTestContext| {
let versions_string = serde_json::to_string(&vec![alpha_version_id.clone()]).unwrap();
let versions_string = urlencoding::encode(&versions_string);
test::TestRequest::get().uri(&format!(
"/v3/analytics/revenue?version_ids={versions_string}&resolution_minutes=5",
))
let req_gen = |ctx: PermissionsTestContext| {
let alpha_version_id = alpha_version_id.clone();
async move {
let ids_or_slugs = vec![alpha_version_id.as_str()];
api.get_analytics_revenue(
ids_or_slugs,
true,
None,
None,
Some(5),
ctx.test_pat.as_deref(),
)
.await
}
};
PermissionsTest::new(&test_env)

View File

@@ -10,8 +10,8 @@ use labrinth::models::{
use crate::common::{api_v2::ApiV2, api_v3::ApiV3, dummy_data::TestFile};
use super::{
models::{CommonImageData, CommonProject, CommonVersion},
request_data::ProjectCreationRequestData,
models::{CommonProject, CommonVersion},
request_data::{ImageData, ProjectCreationRequestData},
Api, ApiProject, ApiTags, ApiTeams, ApiVersion,
};
@@ -65,17 +65,21 @@ impl Api for GenericApi {
delegate_api_variant!(
#[async_trait(?Send)]
impl ApiProject for GenericApi {
[add_public_project, (CommonProject, Vec<CommonVersion>), slug: &str, version_jar: Option<TestFile>, modify_json: Option<json_patch::Patch>, pat: &str],
[add_public_project, (CommonProject, Vec<CommonVersion>), slug: &str, version_jar: Option<TestFile>, modify_json: Option<json_patch::Patch>, pat: Option<&str>],
[get_public_project_creation_data_json, serde_json::Value, slug: &str, version_jar: Option<&TestFile>],
[create_project, ServiceResponse, creation_data: ProjectCreationRequestData, pat: &str],
[remove_project, ServiceResponse, project_slug_or_id: &str, pat: &str],
[get_project, ServiceResponse, id_or_slug: &str, pat: &str],
[get_project_deserialized_common, CommonProject, id_or_slug: &str, pat: &str],
[get_user_projects, ServiceResponse, user_id_or_username: &str, pat: &str],
[get_user_projects_deserialized_common, Vec<CommonProject>, user_id_or_username: &str, pat: &str],
[edit_project, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: &str],
[edit_project_bulk, ServiceResponse, ids_or_slugs: &[&str], patch: serde_json::Value, pat: &str],
[edit_project_icon, ServiceResponse, id_or_slug: &str, icon: Option<CommonImageData>, pat: &str],
[create_project, ServiceResponse, creation_data: ProjectCreationRequestData, pat: Option<&str>],
[remove_project, ServiceResponse, project_slug_or_id: &str, pat: Option<&str>],
[get_project, ServiceResponse, id_or_slug: &str, pat: Option<&str>],
[get_project_deserialized_common, CommonProject, id_or_slug: &str, pat: Option<&str>],
[get_user_projects, ServiceResponse, user_id_or_username: &str, pat: Option<&str>],
[get_user_projects_deserialized_common, Vec<CommonProject>, user_id_or_username: &str, pat: Option<&str>],
[edit_project, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: Option<&str>],
[edit_project_bulk, ServiceResponse, ids_or_slugs: &[&str], patch: serde_json::Value, pat: Option<&str>],
[edit_project_icon, ServiceResponse, id_or_slug: &str, icon: Option<ImageData>, pat: Option<&str>],
[schedule_project, ServiceResponse, id_or_slug: &str, requested_status: &str, date : chrono::DateTime<chrono::Utc>, pat: Option<&str>],
[add_gallery_item, ServiceResponse, id_or_slug: &str, image: ImageData, featured: bool, title: Option<String>, description: Option<String>, ordering: Option<i32>, pat: Option<&str>],
[remove_gallery_item, ServiceResponse, id_or_slug: &str, image_url: &str, pat: Option<&str>],
[edit_gallery_item, ServiceResponse, id_or_slug: &str, image_url: &str, patch: HashMap<String, String>, pat: Option<&str>],
}
);
@@ -92,44 +96,47 @@ delegate_api_variant!(
delegate_api_variant!(
#[async_trait(?Send)]
impl ApiTeams for GenericApi {
[get_team_members, ServiceResponse, team_id: &str, pat: &str],
[get_team_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, team_id: &str, pat: &str],
[get_project_members, ServiceResponse, id_or_slug: &str, pat: &str],
[get_project_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_slug: &str, pat: &str],
[get_organization_members, ServiceResponse, id_or_title: &str, pat: &str],
[get_organization_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_title: &str, pat: &str],
[join_team, ServiceResponse, team_id: &str, pat: &str],
[remove_from_team, ServiceResponse, team_id: &str, user_id: &str, pat: &str],
[edit_team_member, ServiceResponse, team_id: &str, user_id: &str, patch: serde_json::Value, pat: &str],
[transfer_team_ownership, ServiceResponse, team_id: &str, user_id: &str, pat: &str],
[get_user_notifications, ServiceResponse, user_id: &str, pat: &str],
[get_user_notifications_deserialized_common, Vec<crate::common::api_common::models::CommonNotification>, user_id: &str, pat: &str],
[mark_notification_read, ServiceResponse, notification_id: &str, pat: &str],
[add_user_to_team, ServiceResponse, team_id: &str, user_id: &str, project_permissions: Option<ProjectPermissions>, organization_permissions: Option<OrganizationPermissions>, pat: &str],
[delete_notification, ServiceResponse, notification_id: &str, pat: &str],
[get_team_members, ServiceResponse, team_id: &str, pat: Option<&str>],
[get_team_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, team_id: &str, pat: Option<&str>],
[get_project_members, ServiceResponse, id_or_slug: &str, pat: Option<&str>],
[get_project_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_slug: &str, pat: Option<&str>],
[get_organization_members, ServiceResponse, id_or_title: &str, pat: Option<&str>],
[get_organization_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_title: &str, pat: Option<&str>],
[join_team, ServiceResponse, team_id: &str, pat: Option<&str>],
[remove_from_team, ServiceResponse, team_id: &str, user_id: &str, pat: Option<&str>],
[edit_team_member, ServiceResponse, team_id: &str, user_id: &str, patch: serde_json::Value, pat: Option<&str>],
[transfer_team_ownership, ServiceResponse, team_id: &str, user_id: &str, pat: Option<&str>],
[get_user_notifications, ServiceResponse, user_id: &str, pat: Option<&str>],
[get_user_notifications_deserialized_common, Vec<crate::common::api_common::models::CommonNotification>, user_id: &str, pat: Option<&str>],
[mark_notification_read, ServiceResponse, notification_id: &str, pat: Option<&str>],
[add_user_to_team, ServiceResponse, team_id: &str, user_id: &str, project_permissions: Option<ProjectPermissions>, organization_permissions: Option<OrganizationPermissions>, pat: Option<&str>],
[delete_notification, ServiceResponse, notification_id: &str, pat: Option<&str>],
}
);
delegate_api_variant!(
#[async_trait(?Send)]
impl ApiVersion for GenericApi {
[add_public_version, ServiceResponse, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: &str],
[add_public_version_deserialized_common, CommonVersion, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: &str],
[get_version, ServiceResponse, id_or_slug: &str, pat: &str],
[get_version_deserialized_common, CommonVersion, id_or_slug: &str, pat: &str],
[get_versions, ServiceResponse, ids_or_slugs: Vec<String>, pat: &str],
[get_versions_deserialized_common, Vec<CommonVersion>, ids_or_slugs: Vec<String>, pat: &str],
[edit_version, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: &str],
[get_version_from_hash, ServiceResponse, id_or_slug: &str, hash: &str, pat: &str],
[get_version_from_hash_deserialized_common, CommonVersion, id_or_slug: &str, hash: &str, pat: &str],
[get_versions_from_hashes, ServiceResponse, hashes: &[&str], algorithm: &str, pat: &str],
[get_versions_from_hashes_deserialized_common, HashMap<String, CommonVersion>, hashes: &[&str], algorithm: &str, pat: &str],
[get_update_from_hash, ServiceResponse, hash: &str, algorithm: &str, loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: &str],
[get_update_from_hash_deserialized_common, CommonVersion, hash: &str, algorithm: &str,loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>,version_types: Option<Vec<String>>, pat: &str],
[update_files, ServiceResponse, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: &str],
[update_files_deserialized_common, HashMap<String, CommonVersion>, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: &str],
[get_project_versions, ServiceResponse, project_id_slug: &str, game_versions: Option<Vec<String>>,loaders: Option<Vec<String>>,featured: Option<bool>, version_type: Option<VersionType>, limit: Option<usize>, offset: Option<usize>,pat: &str],
[get_project_versions_deserialized_common, Vec<CommonVersion>, project_id_slug: &str, game_versions: Option<Vec<String>>, loaders: Option<Vec<String>>,featured: Option<bool>,version_type: Option<VersionType>,limit: Option<usize>,offset: Option<usize>,pat: &str],
[edit_version_ordering, ServiceResponse, version_id: &str,ordering: Option<i32>,pat: &str],
[add_public_version, ServiceResponse, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: Option<&str>],
[add_public_version_deserialized_common, CommonVersion, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: Option<&str>],
[get_version, ServiceResponse, id_or_slug: &str, pat: Option<&str>],
[get_version_deserialized_common, CommonVersion, id_or_slug: &str, pat: Option<&str>],
[get_versions, ServiceResponse, ids_or_slugs: Vec<String>, pat: Option<&str>],
[get_versions_deserialized_common, Vec<CommonVersion>, ids_or_slugs: Vec<String>, pat: Option<&str>],
[edit_version, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: Option<&str>],
[get_version_from_hash, ServiceResponse, id_or_slug: &str, hash: &str, pat: Option<&str>],
[get_version_from_hash_deserialized_common, CommonVersion, id_or_slug: &str, hash: &str, pat: Option<&str>],
[get_versions_from_hashes, ServiceResponse, hashes: &[&str], algorithm: &str, pat: Option<&str>],
[get_versions_from_hashes_deserialized_common, HashMap<String, CommonVersion>, hashes: &[&str], algorithm: &str, pat: Option<&str>],
[get_update_from_hash, ServiceResponse, hash: &str, algorithm: &str, loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: Option<&str>],
[get_update_from_hash_deserialized_common, CommonVersion, hash: &str, algorithm: &str,loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>,version_types: Option<Vec<String>>, pat: Option<&str>],
[update_files, ServiceResponse, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: Option<&str>],
[update_files_deserialized_common, HashMap<String, CommonVersion>, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: Option<&str>],
[get_project_versions, ServiceResponse, project_id_slug: &str, game_versions: Option<Vec<String>>,loaders: Option<Vec<String>>,featured: Option<bool>, version_type: Option<VersionType>, limit: Option<usize>, offset: Option<usize>,pat: Option<&str>],
[get_project_versions_deserialized_common, Vec<CommonVersion>, project_id_slug: &str, game_versions: Option<Vec<String>>, loaders: Option<Vec<String>>,featured: Option<bool>,version_type: Option<VersionType>,limit: Option<usize>,offset: Option<usize>,pat: Option<&str>],
[edit_version_ordering, ServiceResponse, version_id: &str,ordering: Option<i32>,pat: Option<&str>],
[upload_file_to_version, ServiceResponse, version_id: &str, file: &TestFile, pat: Option<&str>],
[remove_version, ServiceResponse, version_id: &str, pat: Option<&str>],
[remove_version_file, ServiceResponse, hash: &str, pat: Option<&str>],
}
);

View File

@@ -1,12 +1,13 @@
use std::collections::HashMap;
use self::models::{
CommonCategoryData, CommonImageData, CommonLoaderData, CommonNotification, CommonProject,
CommonTeamMember, CommonVersion,
CommonCategoryData, CommonLoaderData, CommonNotification, CommonProject, CommonTeamMember,
CommonVersion,
};
use self::request_data::ProjectCreationRequestData;
use self::request_data::{ImageData, ProjectCreationRequestData};
use actix_web::dev::ServiceResponse;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use labrinth::{
models::{
projects::{ProjectId, VersionType},
@@ -38,12 +39,12 @@ pub trait ApiProject {
slug: &str,
version_jar: Option<TestFile>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> (CommonProject, Vec<CommonVersion>);
async fn create_project(
&self,
creation_data: ProjectCreationRequestData,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_public_project_creation_data_json(
&self,
@@ -51,32 +52,71 @@ pub trait ApiProject {
version_jar: Option<&TestFile>,
) -> serde_json::Value;
async fn remove_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_project_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonProject;
async fn get_user_projects(&self, user_id_or_username: &str, pat: &str) -> ServiceResponse;
async fn remove_project(&self, id_or_slug: &str, pat: Option<&str>) -> ServiceResponse;
async fn get_project(&self, id_or_slug: &str, pat: Option<&str>) -> ServiceResponse;
async fn get_project_deserialized_common(
&self,
id_or_slug: &str,
pat: Option<&str>,
) -> CommonProject;
async fn get_user_projects(
&self,
user_id_or_username: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_user_projects_deserialized_common(
&self,
user_id_or_username: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonProject>;
async fn edit_project(
&self,
id_or_slug: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn edit_project_bulk(
&self,
ids_or_slugs: &[&str],
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn edit_project_icon(
&self,
id_or_slug: &str,
icon: Option<CommonImageData>,
pat: &str,
icon: Option<ImageData>,
pat: Option<&str>,
) -> ServiceResponse;
async fn schedule_project(
&self,
id_or_slug: &str,
requested_status: &str,
date: DateTime<Utc>,
pat: Option<&str>,
) -> ServiceResponse;
#[allow(clippy::too_many_arguments)]
async fn add_gallery_item(
&self,
id_or_slug: &str,
image: ImageData,
featured: bool,
title: Option<String>,
description: Option<String>,
ordering: Option<i32>,
pat: Option<&str>,
) -> ServiceResponse;
async fn remove_gallery_item(
&self,
id_or_slug: &str,
url: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn edit_gallery_item(
&self,
id_or_slug: &str,
url: &str,
patch: HashMap<String, String>,
pat: Option<&str>,
) -> ServiceResponse;
}
@@ -90,55 +130,72 @@ pub trait ApiTags {
#[async_trait(?Send)]
pub trait ApiTeams {
async fn get_team_members(&self, team_id: &str, pat: &str) -> ServiceResponse;
async fn get_team_members(&self, team_id: &str, pat: Option<&str>) -> ServiceResponse;
async fn get_team_members_deserialized_common(
&self,
team_id: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember>;
async fn get_project_members(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_project_members(&self, id_or_slug: &str, pat: Option<&str>) -> ServiceResponse;
async fn get_project_members_deserialized_common(
&self,
id_or_slug: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember>;
async fn get_organization_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse;
async fn get_organization_members(
&self,
id_or_title: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_organization_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember>;
async fn join_team(&self, team_id: &str, pat: &str) -> ServiceResponse;
async fn remove_from_team(&self, team_id: &str, user_id: &str, pat: &str) -> ServiceResponse;
async fn join_team(&self, team_id: &str, pat: Option<&str>) -> ServiceResponse;
async fn remove_from_team(
&self,
team_id: &str,
user_id: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn edit_team_member(
&self,
team_id: &str,
user_id: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn transfer_team_ownership(
&self,
team_id: &str,
user_id: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_user_notifications(&self, user_id: &str, pat: &str) -> ServiceResponse;
async fn get_user_notifications(&self, user_id: &str, pat: Option<&str>) -> ServiceResponse;
async fn get_user_notifications_deserialized_common(
&self,
user_id: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonNotification>;
async fn mark_notification_read(&self, notification_id: &str, pat: &str) -> ServiceResponse;
async fn mark_notification_read(
&self,
notification_id: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn add_user_to_team(
&self,
team_id: &str,
user_id: &str,
project_permissions: Option<ProjectPermissions>,
organization_permissions: Option<OrganizationPermissions>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn delete_notification(
&self,
notification_id: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn delete_notification(&self, notification_id: &str, pat: &str) -> ServiceResponse;
}
#[async_trait(?Send)]
@@ -150,7 +207,7 @@ pub trait ApiVersion {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn add_public_version_deserialized_common(
&self,
@@ -159,45 +216,49 @@ pub trait ApiVersion {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion;
async fn get_version(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_version_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonVersion;
async fn get_versions(&self, ids_or_slugs: Vec<String>, pat: &str) -> ServiceResponse;
async fn get_version(&self, id_or_slug: &str, pat: Option<&str>) -> ServiceResponse;
async fn get_version_deserialized_common(
&self,
id_or_slug: &str,
pat: Option<&str>,
) -> CommonVersion;
async fn get_versions(&self, ids_or_slugs: Vec<String>, pat: Option<&str>) -> ServiceResponse;
async fn get_versions_deserialized_common(
&self,
ids_or_slugs: Vec<String>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion>;
async fn edit_version(
&self,
id_or_slug: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_version_from_hash(
&self,
id_or_slug: &str,
hash: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_version_from_hash_deserialized_common(
&self,
id_or_slug: &str,
hash: &str,
pat: &str,
pat: Option<&str>,
) -> CommonVersion;
async fn get_versions_from_hashes(
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_versions_from_hashes_deserialized_common(
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion>;
async fn get_update_from_hash(
&self,
@@ -206,7 +267,7 @@ pub trait ApiVersion {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn get_update_from_hash_deserialized_common(
&self,
@@ -215,7 +276,7 @@ pub trait ApiVersion {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion;
async fn update_files(
&self,
@@ -224,7 +285,7 @@ pub trait ApiVersion {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn update_files_deserialized_common(
&self,
@@ -233,7 +294,7 @@ pub trait ApiVersion {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion>;
#[allow(clippy::too_many_arguments)]
async fn get_project_versions(
@@ -245,7 +306,7 @@ pub trait ApiVersion {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
#[allow(clippy::too_many_arguments)]
async fn get_project_versions_deserialized_common(
@@ -257,12 +318,34 @@ pub trait ApiVersion {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion>;
async fn edit_version_ordering(
&self,
version_id: &str,
ordering: Option<i32>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse;
async fn upload_file_to_version(
&self,
version_id: &str,
file: &TestFile,
pat: Option<&str>,
) -> ServiceResponse;
async fn remove_version(&self, id_or_slug: &str, pat: Option<&str>) -> ServiceResponse;
async fn remove_version_file(&self, hash: &str, pat: Option<&str>) -> ServiceResponse;
}
pub trait AppendsOptionalPat {
fn append_pat(self, pat: Option<&str>) -> Self;
}
// Impl this on all actix_web::test::TestRequest
impl AppendsOptionalPat for actix_web::test::TestRequest {
fn append_pat(self, pat: Option<&str>) -> Self {
if let Some(pat) = pat {
self.append_header(("Authorization", pat))
} else {
self
}
}
}

View File

@@ -74,13 +74,6 @@ pub struct CommonVersion {
pub ordering: Option<i32>,
}
#[derive(Deserialize)]
pub struct CommonImageData {
pub filename: String,
pub extension: String,
pub icon: Vec<u8>,
}
#[derive(Deserialize)]
pub struct CommonLoaderData {
pub icon: String,

View File

@@ -1,8 +1,10 @@
use std::collections::HashMap;
use crate::common::{
api_common::{
models::{CommonImageData, CommonProject, CommonVersion},
request_data::ProjectCreationRequestData,
Api, ApiProject,
models::{CommonProject, CommonVersion},
request_data::{ImageData, ProjectCreationRequestData},
Api, ApiProject, AppendsOptionalPat,
},
dummy_data::TestFile,
};
@@ -13,6 +15,7 @@ use actix_web::{
};
use async_trait::async_trait;
use bytes::Bytes;
use chrono::{DateTime, Utc};
use labrinth::{
models::v2::{projects::LegacyProject, search::LegacySearchResults},
util::actix::AppendsMultipart,
@@ -27,7 +30,11 @@ use super::{
};
impl ApiV2 {
pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: &str) -> LegacyProject {
pub async fn get_project_deserialized(
&self,
id_or_slug: &str,
pat: Option<&str>,
) -> LegacyProject {
let resp = self.get_project(id_or_slug, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
@@ -36,7 +43,7 @@ impl ApiV2 {
pub async fn get_user_projects_deserialized(
&self,
user_id_or_username: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<LegacyProject> {
let resp = self.get_user_projects(user_id_or_username, pat).await;
assert_eq!(resp.status(), 200);
@@ -47,7 +54,7 @@ impl ApiV2 {
&self,
query: Option<&str>,
facets: Option<serde_json::Value>,
pat: &str,
pat: Option<&str>,
) -> LegacySearchResults {
let query_field = if let Some(query) = query {
format!("&query={}", urlencoding::encode(query))
@@ -63,7 +70,7 @@ impl ApiV2 {
let req = test::TestRequest::get()
.uri(&format!("/v2/search?{}{}", query_field, facets_field))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
let status = resp.status();
@@ -79,7 +86,7 @@ impl ApiProject for ApiV2 {
slug: &str,
version_jar: Option<TestFile>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> (CommonProject, Vec<CommonVersion>) {
let creation_data = get_public_project_creation_data(slug, version_jar, modify_json);
@@ -91,7 +98,7 @@ impl ApiProject for ApiV2 {
// Approve as a moderator.
let req = TestRequest::patch()
.uri(&format!("/v2/project/{}", slug))
.append_header(("Authorization", MOD_USER_PAT))
.append_pat(MOD_USER_PAT)
.set_json(json!(
{
"status": "approved"
@@ -106,7 +113,7 @@ impl ApiProject for ApiV2 {
// Get project's versions
let req = TestRequest::get()
.uri(&format!("/v2/project/{}/version", slug))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
let versions: Vec<CommonVersion> = test::read_body_json(resp).await;
@@ -125,35 +132,38 @@ impl ApiProject for ApiV2 {
async fn create_project(
&self,
creation_data: ProjectCreationRequestData,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = TestRequest::post()
.uri("/v2/project")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_multipart(creation_data.segment_data)
.to_request();
self.call(req).await
}
async fn remove_project(&self, project_slug_or_id: &str, pat: &str) -> ServiceResponse {
async fn remove_project(&self, project_slug_or_id: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v2/project/{project_slug_or_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
assert_eq!(resp.status(), 204);
resp
self.call(req).await
}
async fn get_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse {
async fn get_project(&self, id_or_slug: &str, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v2/project/{id_or_slug}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn get_project_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonProject {
async fn get_project_deserialized_common(
&self,
id_or_slug: &str,
pat: Option<&str>,
) -> CommonProject {
let resp = self.get_project(id_or_slug, pat).await;
assert_eq!(resp.status(), 200);
// First, deserialize to the non-common format (to test the response is valid for this api version)
@@ -163,10 +173,14 @@ impl ApiProject for ApiV2 {
serde_json::from_value(value).unwrap()
}
async fn get_user_projects(&self, user_id_or_username: &str, pat: &str) -> ServiceResponse {
async fn get_user_projects(
&self,
user_id_or_username: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v2/user/{}/projects", user_id_or_username))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -174,7 +188,7 @@ impl ApiProject for ApiV2 {
async fn get_user_projects_deserialized_common(
&self,
user_id_or_username: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonProject> {
let resp = self.get_user_projects(user_id_or_username, pat).await;
assert_eq!(resp.status(), 200);
@@ -189,11 +203,11 @@ impl ApiProject for ApiV2 {
&self,
id_or_slug: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v2/project/{id_or_slug}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -204,7 +218,7 @@ impl ApiProject for ApiV2 {
&self,
ids_or_slugs: &[&str],
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let projects_str = ids_or_slugs
.iter()
@@ -216,7 +230,7 @@ impl ApiProject for ApiV2 {
"/v2/projects?ids={encoded}",
encoded = urlencoding::encode(&format!("[{projects_str}]"))
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -226,8 +240,8 @@ impl ApiProject for ApiV2 {
async fn edit_project_icon(
&self,
id_or_slug: &str,
icon: Option<CommonImageData>,
pat: &str,
icon: Option<ImageData>,
pat: Option<&str>,
) -> ServiceResponse {
if let Some(icon) = icon {
// If an icon is provided, upload it
@@ -236,7 +250,7 @@ impl ApiProject for ApiV2 {
"/v2/project/{id_or_slug}/icon?ext={ext}",
ext = icon.extension
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_payload(Bytes::from(icon.icon))
.to_request();
@@ -245,10 +259,110 @@ impl ApiProject for ApiV2 {
// If no icon is provided, delete the icon
let req = test::TestRequest::delete()
.uri(&format!("/v2/project/{id_or_slug}/icon"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
}
async fn schedule_project(
&self,
id_or_slug: &str,
requested_status: &str,
date: DateTime<Utc>,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v2/version/{id_or_slug}/schedule"))
.set_json(json!(
{
"requested_status": requested_status,
"time": date,
}
))
.append_pat(pat)
.to_request();
self.call(req).await
}
#[allow(clippy::too_many_arguments)]
async fn add_gallery_item(
&self,
id_or_slug: &str,
image: ImageData,
featured: bool,
title: Option<String>,
description: Option<String>,
ordering: Option<i32>,
pat: Option<&str>,
) -> ServiceResponse {
let mut url = format!(
"/v2/project/{id_or_slug}/gallery?ext={ext}&featured={featured}",
ext = image.extension,
featured = featured
);
if let Some(title) = title {
url.push_str(&format!("&title={}", title));
}
if let Some(description) = description {
url.push_str(&format!("&description={}", description));
}
if let Some(ordering) = ordering {
url.push_str(&format!("&ordering={}", ordering));
}
let req = test::TestRequest::post()
.uri(&url)
.append_pat(pat)
.set_payload(Bytes::from(image.icon))
.to_request();
self.call(req).await
}
async fn edit_gallery_item(
&self,
id_or_slug: &str,
image_url: &str,
patch: HashMap<String, String>,
pat: Option<&str>,
) -> ServiceResponse {
let mut url = format!(
"/v2/project/{id_or_slug}/gallery?url={image_url}",
image_url = urlencoding::encode(image_url)
);
for (key, value) in patch {
url.push_str(&format!(
"&{key}={value}",
key = key,
value = urlencoding::encode(&value)
));
}
let req = test::TestRequest::patch()
.uri(&url)
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn remove_gallery_item(
&self,
id_or_slug: &str,
url: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!(
"/v2/project/{id_or_slug}/gallery?url={url}",
url = url
))
.append_pat(pat)
.to_request();
self.call(req).await
}
}

View File

@@ -2,8 +2,8 @@
use serde_json::json;
use crate::common::{
api_common::request_data::{ImageData, ProjectCreationRequestData, VersionCreationRequestData},
dummy_data::{DummyImage, TestFile},
api_common::request_data::{ProjectCreationRequestData, VersionCreationRequestData},
dummy_data::TestFile,
};
use labrinth::{
models::projects::ProjectId,
@@ -123,11 +123,3 @@ pub fn get_public_creation_data_multipart(
vec![json_segment]
}
}
pub fn get_icon_data(dummy_icon: DummyImage) -> ImageData {
ImageData {
filename: dummy_icon.filename(),
extension: dummy_icon.extension(),
icon: dummy_icon.bytes(),
}
}

View File

@@ -8,7 +8,7 @@ use labrinth::routes::v2::tags::{CategoryData, GameVersionQueryData, LoaderData}
use crate::common::{
api_common::{
models::{CommonCategoryData, CommonLoaderData},
Api, ApiTags,
Api, ApiTags, AppendsOptionalPat,
},
database::ADMIN_USER_PAT,
};
@@ -21,7 +21,7 @@ impl ApiV2 {
async fn get_side_types(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v2/tag/side_type")
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}
@@ -35,7 +35,7 @@ impl ApiV2 {
pub async fn get_game_versions(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v2/tag/game_version")
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}
@@ -64,7 +64,7 @@ impl ApiTags for ApiV2 {
async fn get_loaders(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v2/tag/loader")
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}
@@ -82,7 +82,7 @@ impl ApiTags for ApiV2 {
async fn get_categories(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v2/tag/category")
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}

View File

@@ -10,7 +10,7 @@ use serde_json::json;
use crate::common::{
api_common::{
models::{CommonNotification, CommonTeamMember},
Api, ApiTeams,
Api, ApiTeams, AppendsOptionalPat,
},
asserts::assert_status,
};
@@ -21,7 +21,7 @@ impl ApiV2 {
pub async fn get_organization_members_deserialized(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<LegacyTeamMember> {
let resp = self.get_organization_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -31,7 +31,7 @@ impl ApiV2 {
pub async fn get_team_members_deserialized(
&self,
team_id: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<LegacyTeamMember> {
let resp = self.get_team_members(team_id, pat).await;
assert_eq!(resp.status(), 200);
@@ -41,7 +41,7 @@ impl ApiV2 {
pub async fn get_user_notifications_deserialized(
&self,
user_id: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<LegacyNotification> {
let resp = self.get_user_notifications(user_id, pat).await;
assert_eq!(resp.status(), 200);
@@ -51,10 +51,10 @@ impl ApiV2 {
#[async_trait(?Send)]
impl ApiTeams for ApiV2 {
async fn get_team_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_team_members(&self, id_or_title: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v2/team/{id_or_title}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -62,7 +62,7 @@ impl ApiTeams for ApiV2 {
async fn get_team_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember> {
let resp = self.get_team_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -72,10 +72,10 @@ impl ApiTeams for ApiV2 {
test::read_body_json(resp).await
}
async fn get_project_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_project_members(&self, id_or_title: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v2/project/{id_or_title}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -83,7 +83,7 @@ impl ApiTeams for ApiV2 {
async fn get_project_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember> {
let resp = self.get_project_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -93,10 +93,14 @@ impl ApiTeams for ApiV2 {
test::read_body_json(resp).await
}
async fn get_organization_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_organization_members(
&self,
id_or_title: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v2/organization/{id_or_title}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -104,7 +108,7 @@ impl ApiTeams for ApiV2 {
async fn get_organization_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember> {
let resp = self.get_organization_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -114,18 +118,23 @@ impl ApiTeams for ApiV2 {
test::read_body_json(resp).await
}
async fn join_team(&self, team_id: &str, pat: &str) -> ServiceResponse {
async fn join_team(&self, team_id: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v2/team/{team_id}/join"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn remove_from_team(&self, team_id: &str, user_id: &str, pat: &str) -> ServiceResponse {
async fn remove_from_team(
&self,
team_id: &str,
user_id: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v2/team/{team_id}/members/{user_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -135,11 +144,11 @@ impl ApiTeams for ApiV2 {
team_id: &str,
user_id: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v2/team/{team_id}/members/{user_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
self.call(req).await
@@ -149,11 +158,11 @@ impl ApiTeams for ApiV2 {
&self,
team_id: &str,
user_id: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v2/team/{team_id}/owner"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"user_id": user_id,
}))
@@ -161,10 +170,10 @@ impl ApiTeams for ApiV2 {
self.call(req).await
}
async fn get_user_notifications(&self, user_id: &str, pat: &str) -> ServiceResponse {
async fn get_user_notifications(&self, user_id: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v2/user/{user_id}/notifications"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -172,7 +181,7 @@ impl ApiTeams for ApiV2 {
async fn get_user_notifications_deserialized_common(
&self,
user_id: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonNotification> {
let resp = self.get_user_notifications(user_id, pat).await;
assert_status(&resp, StatusCode::OK);
@@ -183,10 +192,14 @@ impl ApiTeams for ApiV2 {
serde_json::from_value(value).unwrap()
}
async fn mark_notification_read(&self, notification_id: &str, pat: &str) -> ServiceResponse {
async fn mark_notification_read(
&self,
notification_id: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v2/notification/{notification_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -197,11 +210,11 @@ impl ApiTeams for ApiV2 {
user_id: &str,
project_permissions: Option<ProjectPermissions>,
organization_permissions: Option<OrganizationPermissions>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v2/team/{team_id}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!( {
"user_id": user_id,
"permissions" : project_permissions.map(|p| p.bits()).unwrap_or_default(),
@@ -211,10 +224,14 @@ impl ApiTeams for ApiV2 {
self.call(req).await
}
async fn delete_notification(&self, notification_id: &str, pat: &str) -> ServiceResponse {
async fn delete_notification(
&self,
notification_id: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v2/notification/{notification_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}

View File

@@ -1,12 +1,15 @@
use std::collections::HashMap;
use super::{request_data::get_public_version_creation_data, ApiV2};
use super::{
request_data::{self, get_public_version_creation_data},
ApiV2,
};
use crate::common::{
api_common::{models::CommonVersion, Api, ApiVersion},
api_common::{models::CommonVersion, Api, ApiVersion, AppendsOptionalPat},
asserts::assert_status,
dummy_data::TestFile,
};
use actix_http::{header::AUTHORIZATION, StatusCode};
use actix_http::StatusCode;
use actix_web::{
dev::ServiceResponse,
test::{self, TestRequest},
@@ -28,7 +31,7 @@ pub fn url_encode_json_serialized_vec(elements: &[String]) -> String {
}
impl ApiV2 {
pub async fn get_version_deserialized(&self, id: &str, pat: &str) -> LegacyVersion {
pub async fn get_version_deserialized(&self, id: &str, pat: Option<&str>) -> LegacyVersion {
let resp = self.get_version(id, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
@@ -38,7 +41,7 @@ impl ApiV2 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> LegacyVersion {
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -49,7 +52,7 @@ impl ApiV2 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, LegacyVersion> {
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -60,11 +63,11 @@ impl ApiV2 {
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v2/version_files/update_individual")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"algorithm": algorithm,
"hashes": hashes
@@ -77,7 +80,7 @@ impl ApiV2 {
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, LegacyVersion> {
let resp = self.update_individual_files(algorithm, hashes, pat).await;
assert_eq!(resp.status(), 200);
@@ -94,7 +97,7 @@ impl ApiVersion for ApiV2 {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let creation_data = get_public_version_creation_data(
project_id,
@@ -107,7 +110,7 @@ impl ApiVersion for ApiV2 {
// Add a project.
let req = TestRequest::post()
.uri("/v2/version")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_multipart(creation_data.segment_data)
.to_request();
self.call(req).await
@@ -120,7 +123,7 @@ impl ApiVersion for ApiV2 {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self
.add_public_version(
@@ -140,15 +143,15 @@ impl ApiVersion for ApiV2 {
serde_json::from_value(value).unwrap()
}
async fn get_version(&self, id: &str, pat: &str) -> ServiceResponse {
async fn get_version(&self, id: &str, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v2/version/{id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn get_version_deserialized_common(&self, id: &str, pat: &str) -> CommonVersion {
async fn get_version_deserialized_common(&self, id: &str, pat: Option<&str>) -> CommonVersion {
let resp = self.get_version(id, pat).await;
assert_eq!(resp.status(), 200);
// First, deserialize to the non-common format (to test the response is valid for this api version)
@@ -162,11 +165,11 @@ impl ApiVersion for ApiV2 {
&self,
version_id: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v2/version/{version_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -177,11 +180,11 @@ impl ApiVersion for ApiV2 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v2/version_file/{hash}?algorithm={algorithm}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -190,7 +193,7 @@ impl ApiVersion for ApiV2 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -205,11 +208,11 @@ impl ApiVersion for ApiV2 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = TestRequest::post()
.uri("/v2/version_files")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"hashes": hashes,
"algorithm": algorithm,
@@ -222,7 +225,7 @@ impl ApiVersion for ApiV2 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion> {
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -240,13 +243,13 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!(
"/v2/version_file/{hash}/update?algorithm={algorithm}"
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"loaders": loaders,
"game_versions": game_versions,
@@ -263,7 +266,7 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self
.get_update_from_hash(hash, algorithm, loaders, game_versions, version_types, pat)
@@ -283,11 +286,11 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v2/version_files/update")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"algorithm": algorithm,
"hashes": hashes,
@@ -306,7 +309,7 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion> {
let resp = self
.update_files(
@@ -337,7 +340,7 @@ impl ApiVersion for ApiV2 {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let mut query_string = String::new();
if let Some(game_versions) = game_versions {
@@ -372,7 +375,7 @@ impl ApiVersion for ApiV2 {
"/v2/project/{project_id_slug}/version?{}",
query_string.trim_start_matches('&')
))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -387,7 +390,7 @@ impl ApiVersion for ApiV2 {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion> {
let resp = self
.get_project_versions(
@@ -413,7 +416,7 @@ impl ApiVersion for ApiV2 {
&self,
version_id: &str,
ordering: Option<i32>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let request = test::TestRequest::patch()
.uri(&format!("/v2/version/{version_id}"))
@@ -422,16 +425,16 @@ impl ApiVersion for ApiV2 {
"ordering": ordering
}
))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(request).await
}
async fn get_versions(&self, version_ids: Vec<String>, pat: &str) -> ServiceResponse {
async fn get_versions(&self, version_ids: Vec<String>, pat: Option<&str>) -> ServiceResponse {
let ids = url_encode_json_serialized_vec(&version_ids);
let request = test::TestRequest::get()
.uri(&format!("/v2/versions?ids={}", ids))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(request).await
}
@@ -439,7 +442,7 @@ impl ApiVersion for ApiV2 {
async fn get_versions_deserialized_common(
&self,
version_ids: Vec<String>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion> {
let resp = self.get_versions(version_ids, pat).await;
assert_status(&resp, StatusCode::OK);
@@ -449,4 +452,40 @@ impl ApiVersion for ApiV2 {
let value = serde_json::to_value(v).unwrap();
serde_json::from_value(value).unwrap()
}
async fn upload_file_to_version(
&self,
version_id: &str,
file: &TestFile,
pat: Option<&str>,
) -> ServiceResponse {
let m = request_data::get_public_creation_data_multipart(
&json!({
"file_parts": [file.filename()]
}),
Some(file),
);
let request = test::TestRequest::post()
.uri(&format!("/v2/version/{version_id}/file"))
.append_pat(pat)
.set_multipart(m)
.to_request();
self.call(request).await
}
async fn remove_version(&self, version_id: &str, pat: Option<&str>) -> ServiceResponse {
let request = test::TestRequest::delete()
.uri(&format!("/v2/version/{version_id}"))
.append_pat(pat)
.to_request();
self.call(request).await
}
async fn remove_version_file(&self, hash: &str, pat: Option<&str>) -> ServiceResponse {
let request = test::TestRequest::delete()
.uri(&format!("/v2/version_file/{hash}"))
.append_pat(pat)
.to_request();
self.call(request).await
}
}

View File

@@ -10,7 +10,10 @@ use labrinth::auth::oauth::{
};
use reqwest::header::{AUTHORIZATION, LOCATION};
use crate::common::{api_common::Api, asserts::assert_status};
use crate::common::{
api_common::{Api, AppendsOptionalPat},
asserts::assert_status,
};
use super::ApiV3;
@@ -22,7 +25,7 @@ impl ApiV3 {
scope: Option<&str>,
redirect_uri: Option<&str>,
state: Option<&str>,
user_pat: &str,
user_pat: Option<&str>,
) -> String {
let auth_resp = self
.oauth_authorize(client_id, scope, redirect_uri, state, user_pat)
@@ -42,21 +45,18 @@ impl ApiV3 {
scope: Option<&str>,
redirect_uri: Option<&str>,
state: Option<&str>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let uri = generate_authorize_uri(client_id, scope, redirect_uri, state);
let req = TestRequest::get()
.uri(&uri)
.append_header((AUTHORIZATION, pat))
.to_request();
let req = TestRequest::get().uri(&uri).append_pat(pat).to_request();
self.call(req).await
}
pub async fn oauth_accept(&self, flow: &str, pat: &str) -> ServiceResponse {
pub async fn oauth_accept(&self, flow: &str, pat: Option<&str>) -> ServiceResponse {
self.call(
TestRequest::post()
.uri("/_internal/oauth/accept")
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.set_json(RespondToOAuthClientScopes {
flow: flow.to_string(),
})
@@ -65,11 +65,11 @@ impl ApiV3 {
.await
}
pub async fn oauth_reject(&self, flow: &str, pat: &str) -> ServiceResponse {
pub async fn oauth_reject(&self, flow: &str, pat: Option<&str>) -> ServiceResponse {
self.call(
TestRequest::post()
.uri("/_internal/oauth/reject")
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.set_json(RespondToOAuthClientScopes {
flow: flow.to_string(),
})

View File

@@ -10,10 +10,12 @@ use labrinth::{
},
routes::v3::oauth_clients::OAuthClientEdit,
};
use reqwest::header::AUTHORIZATION;
use serde_json::json;
use crate::common::{api_common::Api, asserts::assert_status};
use crate::common::{
api_common::{Api, AppendsOptionalPat},
asserts::assert_status,
};
use super::ApiV3;
@@ -23,12 +25,12 @@ impl ApiV3 {
name: String,
max_scopes: Scopes,
redirect_uris: Vec<String>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let max_scopes = max_scopes.bits();
let req = TestRequest::post()
.uri("/_internal/oauth/app")
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.set_json(json!({
"name": name,
"max_scopes": max_scopes,
@@ -39,10 +41,14 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_user_oauth_clients(&self, user_id: &str, pat: &str) -> Vec<OAuthClient> {
pub async fn get_user_oauth_clients(
&self,
user_id: &str,
pat: Option<&str>,
) -> Vec<OAuthClient> {
let req = TestRequest::get()
.uri(&format!("/v3/user/{}/oauth_apps", user_id))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
assert_status(&resp, StatusCode::OK);
@@ -50,10 +56,10 @@ impl ApiV3 {
test::read_body_json(resp).await
}
pub async fn get_oauth_client(&self, client_id: String, pat: &str) -> ServiceResponse {
pub async fn get_oauth_client(&self, client_id: String, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/_internal/oauth/app/{}", client_id))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(req).await
@@ -63,7 +69,7 @@ impl ApiV3 {
&self,
client_id: &str,
edit: OAuthClientEdit,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = TestRequest::patch()
.uri(&format!(
@@ -71,36 +77,43 @@ impl ApiV3 {
urlencoding::encode(client_id)
))
.set_json(edit)
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
pub async fn delete_oauth_client(&self, client_id: &str, pat: &str) -> ServiceResponse {
pub async fn delete_oauth_client(&self, client_id: &str, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::delete()
.uri(&format!("/_internal/oauth/app/{}", client_id))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
pub async fn revoke_oauth_authorization(&self, client_id: &str, pat: &str) -> ServiceResponse {
pub async fn revoke_oauth_authorization(
&self,
client_id: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = TestRequest::delete()
.uri(&format!(
"/_internal/oauth/authorizations?client_id={}",
urlencoding::encode(client_id)
))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
pub async fn get_user_oauth_authorizations(&self, pat: &str) -> Vec<OAuthClientAuthorization> {
pub async fn get_user_oauth_authorizations(
&self,
pat: Option<&str>,
) -> Vec<OAuthClientAuthorization> {
let req = TestRequest::get()
.uri("/_internal/oauth/authorizations")
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
assert_status(&resp, StatusCode::OK);

View File

@@ -6,7 +6,7 @@ use bytes::Bytes;
use labrinth::models::{organizations::Organization, v3::projects::Project};
use serde_json::json;
use crate::common::api_common::{request_data::ImageData, Api};
use crate::common::api_common::{request_data::ImageData, Api, AppendsOptionalPat};
use super::ApiV3;
@@ -15,11 +15,11 @@ impl ApiV3 {
&self,
organization_title: &str,
description: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v3/organization")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"name": organization_title,
"description": description,
@@ -28,10 +28,10 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_organization(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
pub async fn get_organization(&self, id_or_title: &str, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/organization/{id_or_title}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -39,17 +39,21 @@ impl ApiV3 {
pub async fn get_organization_deserialized(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Organization {
let resp = self.get_organization(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_organization_projects(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
pub async fn get_organization_projects(
&self,
id_or_title: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/organization/{id_or_title}/projects"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -57,7 +61,7 @@ impl ApiV3 {
pub async fn get_organization_projects_deserialized(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<Project> {
let resp = self.get_organization_projects(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -68,11 +72,11 @@ impl ApiV3 {
&self,
id_or_title: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v3/organization/{id_or_title}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -83,7 +87,7 @@ impl ApiV3 {
&self,
id_or_title: &str,
icon: Option<ImageData>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
if let Some(icon) = icon {
// If an icon is provided, upload it
@@ -92,7 +96,7 @@ impl ApiV3 {
"/v3/organization/{id_or_title}/icon?ext={ext}",
ext = icon.extension
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_payload(Bytes::from(icon.icon))
.to_request();
@@ -101,17 +105,21 @@ impl ApiV3 {
// If no icon is provided, delete the icon
let req = test::TestRequest::delete()
.uri(&format!("/v3/organization/{id_or_title}/icon"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
}
pub async fn delete_organization(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
pub async fn delete_organization(
&self,
id_or_title: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v3/organization/{id_or_title}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
@@ -121,11 +129,11 @@ impl ApiV3 {
&self,
id_or_title: &str,
project_id_or_slug: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v3/organization/{id_or_title}/projects"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"project_id": project_id_or_slug,
}))
@@ -138,13 +146,13 @@ impl ApiV3 {
&self,
id_or_title: &str,
project_id_or_slug: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!(
"/v3/organization/{id_or_title}/projects/{project_id_or_slug}"
))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await

View File

@@ -17,9 +17,9 @@ use serde_json::json;
use crate::common::{
api_common::{
models::{CommonImageData, CommonProject, CommonVersion},
request_data::ProjectCreationRequestData,
Api, ApiProject,
models::{CommonProject, CommonVersion},
request_data::{ImageData, ProjectCreationRequestData},
Api, ApiProject, AppendsOptionalPat,
},
asserts::assert_status,
database::MOD_USER_PAT,
@@ -38,7 +38,7 @@ impl ApiProject for ApiV3 {
slug: &str,
version_jar: Option<TestFile>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> (CommonProject, Vec<CommonVersion>) {
let creation_data = get_public_project_creation_data(slug, version_jar, modify_json);
@@ -50,7 +50,7 @@ impl ApiProject for ApiV3 {
// Approve as a moderator.
let req = TestRequest::patch()
.uri(&format!("/v3/project/{}", slug))
.append_header(("Authorization", MOD_USER_PAT))
.append_pat(MOD_USER_PAT)
.set_json(json!(
{
"status": "approved"
@@ -66,7 +66,7 @@ impl ApiProject for ApiV3 {
// Get project's versions
let req = TestRequest::get()
.uri(&format!("/v3/project/{}/version", slug))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
let versions: Vec<CommonVersion> = test::read_body_json(resp).await;
@@ -85,35 +85,38 @@ impl ApiProject for ApiV3 {
async fn create_project(
&self,
creation_data: ProjectCreationRequestData,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = TestRequest::post()
.uri("/v3/project")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_multipart(creation_data.segment_data)
.to_request();
self.call(req).await
}
async fn remove_project(&self, project_slug_or_id: &str, pat: &str) -> ServiceResponse {
async fn remove_project(&self, project_slug_or_id: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v3/project/{project_slug_or_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
assert_eq!(resp.status(), 204);
resp
self.call(req).await
}
async fn get_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse {
async fn get_project(&self, id_or_slug: &str, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/project/{id_or_slug}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn get_project_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonProject {
async fn get_project_deserialized_common(
&self,
id_or_slug: &str,
pat: Option<&str>,
) -> CommonProject {
let resp = self.get_project(id_or_slug, pat).await;
assert_eq!(resp.status(), 200);
// First, deserialize to the non-common format (to test the response is valid for this api version)
@@ -123,10 +126,14 @@ impl ApiProject for ApiV3 {
serde_json::from_value(value).unwrap()
}
async fn get_user_projects(&self, user_id_or_username: &str, pat: &str) -> ServiceResponse {
async fn get_user_projects(
&self,
user_id_or_username: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/user/{}/projects", user_id_or_username))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -134,7 +141,7 @@ impl ApiProject for ApiV3 {
async fn get_user_projects_deserialized_common(
&self,
user_id_or_username: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonProject> {
let resp = self.get_user_projects(user_id_or_username, pat).await;
assert_eq!(resp.status(), 200);
@@ -149,11 +156,11 @@ impl ApiProject for ApiV3 {
&self,
id_or_slug: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v3/project/{id_or_slug}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -164,7 +171,7 @@ impl ApiProject for ApiV3 {
&self,
ids_or_slugs: &[&str],
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let projects_str = ids_or_slugs
.iter()
@@ -176,7 +183,7 @@ impl ApiProject for ApiV3 {
"/v3/projects?ids={encoded}",
encoded = urlencoding::encode(&format!("[{projects_str}]"))
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -186,8 +193,8 @@ impl ApiProject for ApiV3 {
async fn edit_project_icon(
&self,
id_or_slug: &str,
icon: Option<CommonImageData>,
pat: &str,
icon: Option<ImageData>,
pat: Option<&str>,
) -> ServiceResponse {
if let Some(icon) = icon {
// If an icon is provided, upload it
@@ -196,7 +203,7 @@ impl ApiProject for ApiV3 {
"/v3/project/{id_or_slug}/icon?ext={ext}",
ext = icon.extension
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_payload(Bytes::from(icon.icon))
.to_request();
@@ -205,16 +212,120 @@ impl ApiProject for ApiV3 {
// If no icon is provided, delete the icon
let req = test::TestRequest::delete()
.uri(&format!("/v3/project/{id_or_slug}/icon"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
}
async fn schedule_project(
&self,
id_or_slug: &str,
requested_status: &str,
date: DateTime<Utc>,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v3/version/{id_or_slug}/schedule"))
.set_json(json!(
{
"requested_status": requested_status,
"time": date,
}
))
.append_pat(pat)
.to_request();
self.call(req).await
}
#[allow(clippy::too_many_arguments)]
async fn add_gallery_item(
&self,
id_or_slug: &str,
image: ImageData,
featured: bool,
title: Option<String>,
description: Option<String>,
ordering: Option<i32>,
pat: Option<&str>,
) -> ServiceResponse {
let mut url = format!(
"/v3/project/{id_or_slug}/gallery?ext={ext}&featured={featured}",
ext = image.extension,
featured = featured
);
if let Some(title) = title {
url.push_str(&format!("&title={}", title));
}
if let Some(description) = description {
url.push_str(&format!("&description={}", description));
}
if let Some(ordering) = ordering {
url.push_str(&format!("&ordering={}", ordering));
}
let req = test::TestRequest::post()
.uri(&url)
.append_pat(pat)
.set_payload(Bytes::from(image.icon))
.to_request();
self.call(req).await
}
async fn edit_gallery_item(
&self,
id_or_slug: &str,
image_url: &str,
patch: HashMap<String, String>,
pat: Option<&str>,
) -> ServiceResponse {
let mut url = format!(
"/v3/project/{id_or_slug}/gallery?url={image_url}",
image_url = urlencoding::encode(image_url)
);
for (key, value) in patch {
url.push_str(&format!(
"&{key}={value}",
key = key,
value = urlencoding::encode(&value)
));
}
let req = test::TestRequest::patch()
.uri(&url)
.append_pat(pat)
.to_request();
let t = self.call(req).await;
println!("Status: {}", t.status());
println!("respone Body: {:?}", t.response().body());
t
}
async fn remove_gallery_item(
&self,
id_or_slug: &str,
url: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!(
"/v3/project/{id_or_slug}/gallery?url={url}",
url = url
))
.append_pat(pat)
.to_request();
self.call(req).await
}
}
impl ApiV3 {
pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: &str) -> Project {
pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: Option<&str>) -> Project {
let resp = self.get_project(id_or_slug, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
@@ -224,7 +335,7 @@ impl ApiV3 {
&self,
query: Option<&str>,
facets: Option<serde_json::Value>,
pat: &str,
pat: Option<&str>,
) -> ReturnSearchResults {
let query_field = if let Some(query) = query {
format!("&query={}", urlencoding::encode(query))
@@ -240,7 +351,7 @@ impl ApiV3 {
let req = test::TestRequest::get()
.uri(&format!("/v3/search?{}{}", query_field, facets_field))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
let resp = self.call(req).await;
let status = resp.status();
@@ -255,7 +366,7 @@ impl ApiV3 {
start_date: Option<DateTime<Utc>>,
end_date: Option<DateTime<Utc>>,
resolution_minutes: Option<u32>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let pv_string = if ids_are_version_ids {
let version_string: String = serde_json::to_string(&id_or_slugs).unwrap();
@@ -286,7 +397,7 @@ impl ApiV3 {
let req = test::TestRequest::get()
.uri(&format!("/v3/analytics/revenue?{pv_string}{extra_args}",))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
@@ -299,7 +410,7 @@ impl ApiV3 {
start_date: Option<DateTime<Utc>>,
end_date: Option<DateTime<Utc>>,
resolution_minutes: Option<u32>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, HashMap<i64, Decimal>> {
let resp = self
.get_analytics_revenue(

View File

@@ -2,8 +2,8 @@
use serde_json::json;
use crate::common::{
api_common::request_data::{ImageData, ProjectCreationRequestData, VersionCreationRequestData},
dummy_data::{DummyImage, TestFile},
api_common::request_data::{ProjectCreationRequestData, VersionCreationRequestData},
dummy_data::TestFile,
};
use labrinth::{
models::projects::ProjectId,
@@ -133,11 +133,3 @@ pub fn get_public_creation_data_multipart(
vec![json_segment]
}
}
pub fn get_icon_data(dummy_icon: DummyImage) -> ImageData {
ImageData {
filename: dummy_icon.filename(),
extension: dummy_icon.extension(),
icon: dummy_icon.bytes(),
}
}

View File

@@ -11,7 +11,7 @@ use labrinth::{
use crate::common::{
api_common::{
models::{CommonCategoryData, CommonLoaderData},
Api, ApiTags,
Api, ApiTags, AppendsOptionalPat,
},
database::ADMIN_USER_PAT,
};
@@ -23,7 +23,7 @@ impl ApiTags for ApiV3 {
async fn get_loaders(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v3/tag/loader")
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}
@@ -41,7 +41,7 @@ impl ApiTags for ApiV3 {
async fn get_categories(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v3/tag/category")
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}
@@ -67,7 +67,7 @@ impl ApiV3 {
pub async fn get_loader_field_variants(&self, loader_field: &str) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/loader_field?loader_field={}", loader_field))
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}
@@ -85,7 +85,7 @@ impl ApiV3 {
async fn get_games(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v3/games")
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
self.call(req).await
}

View File

@@ -10,7 +10,7 @@ use serde_json::json;
use crate::common::{
api_common::{
models::{CommonNotification, CommonTeamMember},
Api, ApiTeams,
Api, ApiTeams, AppendsOptionalPat,
},
asserts::assert_status,
};
@@ -21,14 +21,18 @@ impl ApiV3 {
pub async fn get_organization_members_deserialized(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<TeamMember> {
let resp = self.get_organization_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_team_members_deserialized(&self, team_id: &str, pat: &str) -> Vec<TeamMember> {
pub async fn get_team_members_deserialized(
&self,
team_id: &str,
pat: Option<&str>,
) -> Vec<TeamMember> {
let resp = self.get_team_members(team_id, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
@@ -37,10 +41,10 @@ impl ApiV3 {
#[async_trait(?Send)]
impl ApiTeams for ApiV3 {
async fn get_team_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_team_members(&self, id_or_title: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/team/{id_or_title}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -48,7 +52,7 @@ impl ApiTeams for ApiV3 {
async fn get_team_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember> {
let resp = self.get_team_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -59,10 +63,10 @@ impl ApiTeams for ApiV3 {
serde_json::from_value(value).unwrap()
}
async fn get_project_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_project_members(&self, id_or_title: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{id_or_title}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -70,7 +74,7 @@ impl ApiTeams for ApiV3 {
async fn get_project_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember> {
let resp = self.get_project_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -81,10 +85,14 @@ impl ApiTeams for ApiV3 {
serde_json::from_value(value).unwrap()
}
async fn get_organization_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_organization_members(
&self,
id_or_title: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/organization/{id_or_title}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -92,7 +100,7 @@ impl ApiTeams for ApiV3 {
async fn get_organization_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonTeamMember> {
let resp = self.get_organization_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
@@ -103,18 +111,23 @@ impl ApiTeams for ApiV3 {
serde_json::from_value(value).unwrap()
}
async fn join_team(&self, team_id: &str, pat: &str) -> ServiceResponse {
async fn join_team(&self, team_id: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{team_id}/join"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn remove_from_team(&self, team_id: &str, user_id: &str, pat: &str) -> ServiceResponse {
async fn remove_from_team(
&self,
team_id: &str,
user_id: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v3/team/{team_id}/members/{user_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -124,11 +137,11 @@ impl ApiTeams for ApiV3 {
team_id: &str,
user_id: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v3/team/{team_id}/members/{user_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
self.call(req).await
@@ -138,11 +151,11 @@ impl ApiTeams for ApiV3 {
&self,
team_id: &str,
user_id: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v3/team/{team_id}/owner"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"user_id": user_id,
}))
@@ -150,10 +163,10 @@ impl ApiTeams for ApiV3 {
self.call(req).await
}
async fn get_user_notifications(&self, user_id: &str, pat: &str) -> ServiceResponse {
async fn get_user_notifications(&self, user_id: &str, pat: Option<&str>) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/user/{user_id}/notifications"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -161,7 +174,7 @@ impl ApiTeams for ApiV3 {
async fn get_user_notifications_deserialized_common(
&self,
user_id: &str,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonNotification> {
let resp = self.get_user_notifications(user_id, pat).await;
assert_status(&resp, StatusCode::OK);
@@ -172,10 +185,14 @@ impl ApiTeams for ApiV3 {
serde_json::from_value(value).unwrap()
}
async fn mark_notification_read(&self, notification_id: &str, pat: &str) -> ServiceResponse {
async fn mark_notification_read(
&self,
notification_id: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v3/notification/{notification_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -185,11 +202,11 @@ impl ApiTeams for ApiV3 {
user_id: &str,
project_permissions: Option<ProjectPermissions>,
organization_permissions: Option<OrganizationPermissions>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{team_id}/members"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!( {
"user_id": user_id,
"permissions" : project_permissions.map(|p| p.bits()).unwrap_or_default(),
@@ -199,10 +216,14 @@ impl ApiTeams for ApiV3 {
self.call(req).await
}
async fn delete_notification(&self, notification_id: &str, pat: &str) -> ServiceResponse {
async fn delete_notification(
&self,
notification_id: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v3/notification/{notification_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}

View File

@@ -1,12 +1,15 @@
use std::collections::HashMap;
use super::{request_data::get_public_version_creation_data, ApiV3};
use super::{
request_data::{self, get_public_version_creation_data},
ApiV3,
};
use crate::common::{
api_common::{models::CommonVersion, Api, ApiVersion},
api_common::{models::CommonVersion, Api, ApiVersion, AppendsOptionalPat},
asserts::assert_status,
dummy_data::TestFile,
};
use actix_http::{header::AUTHORIZATION, StatusCode};
use actix_http::StatusCode;
use actix_web::{
dev::ServiceResponse,
test::{self, TestRequest},
@@ -35,7 +38,7 @@ impl ApiV3 {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> Version {
let resp = self
.add_public_version(
@@ -55,7 +58,7 @@ impl ApiV3 {
test::read_body_json(version).await
}
pub async fn get_version_deserialized(&self, id: &str, pat: &str) -> Version {
pub async fn get_version_deserialized(&self, id: &str, pat: Option<&str>) -> Version {
let resp = self.get_version(id, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
@@ -65,11 +68,11 @@ impl ApiV3 {
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v3/version_files/update_individual")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"algorithm": algorithm,
"hashes": hashes
@@ -82,7 +85,7 @@ impl ApiV3 {
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, Version> {
let resp = self.update_individual_files(algorithm, hashes, pat).await;
assert_eq!(resp.status(), 200);
@@ -99,7 +102,7 @@ impl ApiVersion for ApiV3 {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let creation_data = get_public_version_creation_data(
project_id,
@@ -112,7 +115,7 @@ impl ApiVersion for ApiV3 {
// Add a versiom.
let req = TestRequest::post()
.uri("/v3/version")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_multipart(creation_data.segment_data)
.to_request();
self.call(req).await
@@ -125,7 +128,7 @@ impl ApiVersion for ApiV3 {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self
.add_public_version(
@@ -145,15 +148,15 @@ impl ApiVersion for ApiV3 {
serde_json::from_value(value).unwrap()
}
async fn get_version(&self, id: &str, pat: &str) -> ServiceResponse {
async fn get_version(&self, id: &str, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/version/{id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn get_version_deserialized_common(&self, id: &str, pat: &str) -> CommonVersion {
async fn get_version_deserialized_common(&self, id: &str, pat: Option<&str>) -> CommonVersion {
let resp = self.get_version(id, pat).await;
assert_eq!(resp.status(), 200);
// First, deserialize to the non-common format (to test the response is valid for this api version)
@@ -167,11 +170,11 @@ impl ApiVersion for ApiV3 {
&self,
version_id: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v3/version/{version_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -182,11 +185,11 @@ impl ApiVersion for ApiV3 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/version_file/{hash}?algorithm={algorithm}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -195,7 +198,7 @@ impl ApiVersion for ApiV3 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -210,11 +213,11 @@ impl ApiVersion for ApiV3 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = TestRequest::post()
.uri("/v3/version_files")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"hashes": hashes,
"algorithm": algorithm,
@@ -227,7 +230,7 @@ impl ApiVersion for ApiV3 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion> {
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -245,7 +248,7 @@ impl ApiVersion for ApiV3 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let mut json = json!({});
if let Some(loaders) = loaders {
@@ -264,7 +267,7 @@ impl ApiVersion for ApiV3 {
.uri(&format!(
"/v3/version_file/{hash}/update?algorithm={algorithm}"
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json)
.to_request();
self.call(req).await
@@ -277,7 +280,7 @@ impl ApiVersion for ApiV3 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self
.get_update_from_hash(hash, algorithm, loaders, game_versions, version_types, pat)
@@ -297,7 +300,7 @@ impl ApiVersion for ApiV3 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let mut json = json!({
"algorithm": algorithm,
@@ -317,7 +320,7 @@ impl ApiVersion for ApiV3 {
let req = test::TestRequest::post()
.uri("/v3/version_files/update")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json)
.to_request();
self.call(req).await
@@ -330,7 +333,7 @@ impl ApiVersion for ApiV3 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion> {
let resp = self
.update_files(
@@ -361,7 +364,7 @@ impl ApiVersion for ApiV3 {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let mut query_string = String::new();
if let Some(game_versions) = game_versions {
@@ -396,7 +399,7 @@ impl ApiVersion for ApiV3 {
"/v3/project/{project_id_slug}/version?{}",
query_string.trim_start_matches('&')
))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -411,7 +414,7 @@ impl ApiVersion for ApiV3 {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion> {
let resp = self
.get_project_versions(
@@ -438,7 +441,7 @@ impl ApiVersion for ApiV3 {
&self,
version_id: &str,
ordering: Option<i32>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let request = test::TestRequest::patch()
.uri(&format!("/v3/version/{version_id}"))
@@ -447,16 +450,16 @@ impl ApiVersion for ApiV3 {
"ordering": ordering
}
))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(request).await
}
async fn get_versions(&self, version_ids: Vec<String>, pat: &str) -> ServiceResponse {
async fn get_versions(&self, version_ids: Vec<String>, pat: Option<&str>) -> ServiceResponse {
let ids = url_encode_json_serialized_vec(&version_ids);
let request = test::TestRequest::get()
.uri(&format!("/v3/versions?ids={}", ids))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(request).await
}
@@ -464,7 +467,7 @@ impl ApiVersion for ApiV3 {
async fn get_versions_deserialized_common(
&self,
version_ids: Vec<String>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion> {
let resp = self.get_versions(version_ids, pat).await;
assert_status(&resp, StatusCode::OK);
@@ -474,4 +477,46 @@ impl ApiVersion for ApiV3 {
let value = serde_json::to_value(v).unwrap();
serde_json::from_value(value).unwrap()
}
async fn upload_file_to_version(
&self,
version_id: &str,
file: &TestFile,
pat: Option<&str>,
) -> ServiceResponse {
let m = request_data::get_public_creation_data_multipart(
&json!({
"file_parts": [file.filename()]
}),
Some(file),
);
let request = test::TestRequest::post()
.uri(&format!(
"/v3/version/{version_id}/file",
version_id = version_id
))
.append_pat(pat)
.set_multipart(m)
.to_request();
self.call(request).await
}
async fn remove_version(&self, version_id: &str, pat: Option<&str>) -> ServiceResponse {
let request = test::TestRequest::delete()
.uri(&format!(
"/v3/version/{version_id}",
version_id = version_id
))
.append_pat(pat)
.to_request();
self.call(request).await
}
async fn remove_version_file(&self, hash: &str, pat: Option<&str>) -> ServiceResponse {
let request = test::TestRequest::delete()
.uri(&format!("/v3/version_file/{hash}"))
.append_pat(pat)
.to_request();
self.call(request).await
}
}

View File

@@ -27,11 +27,11 @@ pub const FRIEND_USER_ID_PARSED: i64 = 4;
pub const ENEMY_USER_ID_PARSED: i64 = 5;
// These are full-scoped PATs- as if the user was logged in (including illegal scopes).
pub const ADMIN_USER_PAT: &str = "mrp_patadmin";
pub const MOD_USER_PAT: &str = "mrp_patmoderator";
pub const USER_USER_PAT: &str = "mrp_patuser";
pub const FRIEND_USER_PAT: &str = "mrp_patfriend";
pub const ENEMY_USER_PAT: &str = "mrp_patenemy";
pub const ADMIN_USER_PAT: Option<&str> = Some("mrp_patadmin");
pub const MOD_USER_PAT: Option<&str> = Some("mrp_patmoderator");
pub const USER_USER_PAT: Option<&str> = Some("mrp_patuser");
pub const FRIEND_USER_PAT: Option<&str> = Some("mrp_patfriend");
pub const ENEMY_USER_PAT: Option<&str> = Some("mrp_patenemy");
const TEMPLATE_DATABASE_NAME: &str = "labrinth_tests_template";

View File

@@ -16,7 +16,11 @@ use zip::{write::FileOptions, CompressionMethod, ZipWriter};
use crate::common::{api_common::Api, database::USER_USER_PAT};
use labrinth::util::actix::{AppendsMultipart, MultipartSegment, MultipartSegmentData};
use super::{api_common::ApiProject, api_v3::ApiV3, database::TemporaryDatabase};
use super::{
api_common::{request_data::ImageData, ApiProject, AppendsOptionalPat},
api_v3::ApiV3,
database::TemporaryDatabase,
};
use super::{asserts::assert_status, database::USER_USER_ID, get_json_val_str};
@@ -36,9 +40,11 @@ pub const DUMMY_CATEGORIES: &[&str] = &[
pub const DUMMY_OAUTH_CLIENT_ALPHA_SECRET: &str = "abcdefghijklmnopqrstuvwxyz";
#[allow(dead_code)]
#[derive(Clone)]
pub enum TestFile {
DummyProjectAlpha,
DummyProjectBeta,
BasicZip,
BasicMod,
BasicModDifferent,
// Randomly generates a valid .jar with a random hash.
@@ -380,7 +386,7 @@ pub async fn add_project_beta(api: &ApiV3) -> (Project, Version) {
// Add a project.
let req = TestRequest::post()
.uri("/v3/project")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_multipart(vec![json_segment.clone(), file_segment.clone()])
.to_request();
let resp = api.call(req).await;
@@ -393,7 +399,7 @@ pub async fn add_organization_zeta(api: &ApiV3) -> Organization {
// Add an organzation.
let req = TestRequest::post()
.uri("/v3/organization")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"name": "zeta",
"description": "A dummy organization for testing with."
@@ -410,7 +416,7 @@ pub async fn get_project_alpha(api: &ApiV3) -> (Project, Version) {
// Get project
let req = TestRequest::get()
.uri("/v3/project/alpha")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = api.call(req).await;
let project: Project = test::read_body_json(resp).await;
@@ -418,7 +424,7 @@ pub async fn get_project_alpha(api: &ApiV3) -> (Project, Version) {
// Get project's versions
let req = TestRequest::get()
.uri("/v3/project/alpha/version")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = api.call(req).await;
let versions: Vec<Version> = test::read_body_json(resp).await;
@@ -431,7 +437,7 @@ pub async fn get_project_beta(api: &ApiV3) -> (Project, Version) {
// Get project
let req = TestRequest::get()
.uri("/v3/project/beta")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = api.call(req).await;
assert_status(&resp, StatusCode::OK);
@@ -441,7 +447,7 @@ pub async fn get_project_beta(api: &ApiV3) -> (Project, Version) {
// Get project's versions
let req = TestRequest::get()
.uri("/v3/project/beta/version")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = api.call(req).await;
assert_status(&resp, StatusCode::OK);
@@ -455,7 +461,7 @@ pub async fn get_organization_zeta(api: &ApiV3) -> Organization {
// Get organization
let req = TestRequest::get()
.uri("/v3/organization/zeta")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = api.call(req).await;
let organization: Organization = test::read_body_json(resp).await;
@@ -475,6 +481,7 @@ impl TestFile {
match self {
TestFile::DummyProjectAlpha => "dummy-project-alpha.jar",
TestFile::DummyProjectBeta => "dummy-project-beta.jar",
TestFile::BasicZip => "simple-zip.zip",
TestFile::BasicMod => "basic-mod.jar",
TestFile::BasicModDifferent => "basic-mod-different.jar",
TestFile::BasicModRandom { filename, .. } => filename,
@@ -492,6 +499,7 @@ impl TestFile {
include_bytes!("../../tests/files/dummy-project-beta.jar").to_vec()
}
TestFile::BasicMod => include_bytes!("../../tests/files/basic-mod.jar").to_vec(),
TestFile::BasicZip => include_bytes!("../../tests/files/simple-zip.zip").to_vec(),
TestFile::BasicModDifferent => {
include_bytes!("../../tests/files/basic-mod-different.jar").to_vec()
}
@@ -508,10 +516,27 @@ impl TestFile {
TestFile::BasicModDifferent => "mod",
TestFile::BasicModRandom { .. } => "mod",
TestFile::BasicZip => "resourcepack",
TestFile::BasicModpackRandom { .. } => "modpack",
}
.to_string()
}
pub fn content_type(&self) -> Option<String> {
match self {
TestFile::DummyProjectAlpha => Some("application/java-archive"),
TestFile::DummyProjectBeta => Some("application/java-archive"),
TestFile::BasicMod => Some("application/java-archive"),
TestFile::BasicModDifferent => Some("application/java-archive"),
TestFile::BasicModRandom { .. } => Some("application/java-archive"),
TestFile::BasicZip => Some("application/zip"),
TestFile::BasicModpackRandom { .. } => Some("application/x-modrinth-modpack+zip"),
}
.map(|s| s.to_string())
}
}
impl DummyImage {
@@ -534,4 +559,12 @@ impl DummyImage {
DummyImage::SmallIcon => include_bytes!("../../tests/files/200x200.png").to_vec(),
}
}
pub fn get_icon_data(&self) -> ImageData {
ImageData {
filename: self.filename(),
extension: self.extension(),
icon: self.bytes(),
}
}
}

View File

@@ -122,7 +122,7 @@ impl<A: Api> TestEnvironment<A> {
pub async fn assert_read_notifications_status(
&self,
user_id: &str,
pat: &str,
pat: Option<&str>,
status_code: StatusCode,
) {
let resp = self.api.get_user_notifications(user_id, pat).await;
@@ -133,7 +133,7 @@ impl<A: Api> TestEnvironment<A> {
pub async fn assert_read_user_projects_status(
&self,
user_id: &str,
pat: &str,
pat: Option<&str>,
status_code: StatusCode,
) {
let resp = self.api.get_user_projects(user_id, pat).await;

View File

@@ -1,6 +1,7 @@
#![allow(dead_code)]
use actix_http::StatusCode;
use actix_web::test::{self, TestRequest};
use actix_web::{dev::ServiceResponse, test};
use futures::Future;
use itertools::Itertools;
use labrinth::models::teams::{OrganizationPermissions, ProjectPermissions};
use serde_json::json;
@@ -32,7 +33,7 @@ pub struct PermissionsTest<'a, A: Api> {
// User ID to use for the test user, and their PAT
user_id: &'a str,
user_pat: &'a str,
user_pat: Option<&'a str>,
// Whether or not the user ID should be removed from the project/organization team after the test
// (This is mostly reelvant if you are also using an existing project/organization, and want to do
@@ -58,15 +59,14 @@ pub struct PermissionsTest<'a, A: Api> {
failure_json_check: Option<JsonCheck>,
success_json_check: Option<JsonCheck>,
}
pub struct PermissionsTestContext<'a> {
// pub test_env: &'a TestEnvironment<A>,
pub user_id: &'a str,
pub user_pat: &'a str,
pub project_id: Option<&'a str>,
pub team_id: Option<&'a str>,
pub organization_id: Option<&'a str>,
pub organization_team_id: Option<&'a str>,
#[derive(Clone, Debug)]
pub struct PermissionsTestContext {
pub test_pat: Option<String>,
pub user_id: String,
pub project_id: Option<String>,
pub team_id: Option<String>,
pub organization_id: Option<String>,
pub organization_team_id: Option<String>,
}
impl<'a, A: Api> PermissionsTest<'a, A> {
@@ -118,7 +118,12 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
// Set the user ID to use
// (eg: a moderator, or friend)
// remove_user: Whether or not the user ID should be removed from the project/organization team after the test
pub fn with_user(mut self, user_id: &'a str, user_pat: &'a str, remove_user: bool) -> Self {
pub fn with_user(
mut self,
user_id: &'a str,
user_pat: Option<&'a str>,
remove_user: bool,
) -> Self {
self.user_id = user_id;
self.user_pat = user_pat;
self.remove_user = remove_user;
@@ -149,21 +154,22 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
self
}
pub async fn simple_project_permissions_test<T>(
pub async fn simple_project_permissions_test<T, Fut>(
&self,
success_permissions: ProjectPermissions,
req_gen: T,
) -> Result<(), String>
where
T: Fn(&PermissionsTestContext) -> TestRequest,
T: Fn(PermissionsTestContext) -> Fut,
Fut: Future<Output = ServiceResponse>, // Ensure Fut is Send and 'static
{
let test_env = self.test_env;
let failure_project_permissions = self
.failure_project_permissions
.unwrap_or(ProjectPermissions::all() ^ success_permissions);
let test_context = PermissionsTestContext {
user_id: self.user_id,
user_pat: self.user_pat,
test_pat: None,
user_id: self.user_id.to_string(),
project_id: None,
team_id: None,
organization_id: None,
@@ -190,14 +196,12 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
.await;
// Failure test- not logged in
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Failure permissions test failed. Expected failure codes {} got {}",
@@ -215,15 +219,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
}
// Failure test- logged in on a non-team user
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: ENEMY_USER_PAT.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", ENEMY_USER_PAT))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Failure permissions test failed. Expected failure codes {} got {}",
@@ -241,15 +243,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
}
// Failure test- logged in with EVERY non-relevant permission
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp: ServiceResponse = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Failure permissions test failed. Expected failure codes {} got {}",
@@ -277,15 +277,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
.await;
// Successful test
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !resp.status().is_success() {
return Err(format!(
"Success permissions test failed. Expected success, got {}",
@@ -306,21 +304,22 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
Ok(())
}
pub async fn simple_organization_permissions_test<T>(
pub async fn simple_organization_permissions_test<T, Fut>(
&self,
success_permissions: OrganizationPermissions,
req_gen: T,
) -> Result<(), String>
where
T: Fn(&PermissionsTestContext) -> TestRequest,
T: Fn(PermissionsTestContext) -> Fut,
Fut: Future<Output = ServiceResponse>,
{
let test_env = self.test_env;
let failure_organization_permissions = self
.failure_organization_permissions
.unwrap_or(OrganizationPermissions::all() ^ success_permissions);
let test_context = PermissionsTestContext {
user_id: self.user_id,
user_pat: self.user_pat,
test_pat: None,
user_id: self.user_id.to_string(),
project_id: None,
team_id: None,
organization_id: None,
@@ -348,15 +347,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
.await;
// Failure test
let request = req_gen(&PermissionsTestContext {
organization_id: Some(&organization_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
organization_id: Some(organization_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Failure permissions test failed. Expected failure codes {} got {}",
@@ -379,15 +376,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
.await;
// Successful test
let request = req_gen(&PermissionsTestContext {
organization_id: Some(&organization_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
organization_id: Some(organization_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !resp.status().is_success() {
return Err(format!(
"Success permissions test failed. Expected success, got {}",
@@ -403,21 +398,22 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
Ok(())
}
pub async fn full_project_permissions_test<T>(
pub async fn full_project_permissions_test<T, Fut>(
&self,
success_permissions: ProjectPermissions,
req_gen: T,
) -> Result<(), String>
where
T: Fn(&PermissionsTestContext) -> TestRequest,
T: Fn(PermissionsTestContext) -> Fut,
Fut: Future<Output = ServiceResponse>,
{
let test_env = self.test_env;
let failure_project_permissions = self
.failure_project_permissions
.unwrap_or(ProjectPermissions::all() ^ success_permissions);
let test_context = PermissionsTestContext {
user_id: self.user_id,
user_pat: self.user_pat,
test_pat: None,
user_id: self.user_id.to_string(),
project_id: None,
team_id: None,
organization_id: None,
@@ -430,14 +426,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
let test_1 = async {
let (project_id, team_id) = create_dummy_project(&test_env.setup_api).await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: None,
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Test 1 failed. Expected failure codes {} got {}",
@@ -471,14 +466,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
let test_2 = async {
let (project_id, team_id) = create_dummy_project(&test_env.setup_api).await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Test 2 failed. Expected failure codes {} got {}",
@@ -521,15 +515,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Test 3 failed. Expected failure codes {} got {}",
@@ -572,15 +564,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !resp.status().is_success() {
return Err(format!(
"Test 4 failed. Expected success, got {}",
@@ -623,15 +613,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Test 5 failed. Expected failure codes {} got {}",
@@ -678,15 +666,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !resp.status().is_success() {
return Err(format!(
"Test 6 failed. Expected success, got {}",
@@ -739,15 +725,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Test 7 failed. Expected failure codes {} got {}",
@@ -804,15 +788,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
project_id: Some(&project_id),
team_id: Some(&team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
project_id: Some(project_id.clone()),
team_id: Some(team_id.clone()),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !resp.status().is_success() {
return Err(format!(
@@ -844,21 +826,22 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
Ok(())
}
pub async fn full_organization_permissions_tests<T>(
pub async fn full_organization_permissions_tests<T, Fut>(
&self,
success_permissions: OrganizationPermissions,
req_gen: T,
) -> Result<(), String>
where
T: Fn(&PermissionsTestContext) -> TestRequest,
T: Fn(PermissionsTestContext) -> Fut,
Fut: Future<Output = ServiceResponse>,
{
let test_env = self.test_env;
let failure_organization_permissions = self
.failure_organization_permissions
.unwrap_or(OrganizationPermissions::all() ^ success_permissions);
let test_context = PermissionsTestContext {
user_id: self.user_id,
user_pat: self.user_pat,
test_pat: None,
user_id: self.user_id.to_string(),
project_id: None, // Will be overwritten on each test
team_id: None, // Will be overwritten on each test
organization_id: None,
@@ -871,14 +854,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
let (organization_id, organization_team_id) =
create_dummy_org(&test_env.setup_api).await;
let request = req_gen(&PermissionsTestContext {
organization_id: Some(&organization_id),
organization_team_id: Some(&organization_team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
organization_id: Some(organization_id.clone()),
organization_team_id: Some(organization_team_id),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Test 1 failed. Expected failure codes {} got {}",
@@ -921,15 +903,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
organization_id: Some(&organization_id),
organization_team_id: Some(&organization_team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
organization_id: Some(organization_id.clone()),
organization_team_id: Some(organization_team_id),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !self.allowed_failure_codes.contains(&resp.status().as_u16()) {
return Err(format!(
"Test 2 failed. Expected failure codes {} got {}",
@@ -972,15 +952,13 @@ impl<'a, A: Api> PermissionsTest<'a, A> {
)
.await;
let request = req_gen(&PermissionsTestContext {
organization_id: Some(&organization_id),
organization_team_id: Some(&organization_team_id),
..test_context
let resp = req_gen(PermissionsTestContext {
test_pat: self.user_pat.map(|s| s.to_string()),
organization_id: Some(organization_id.clone()),
organization_team_id: Some(organization_team_id),
..test_context.clone()
})
.append_header(("Authorization", self.user_pat))
.to_request();
let resp = test_env.call(request).await;
.await;
if !resp.status().is_success() {
return Err(format!(
"Test 3 failed. Expected success, got {}",
@@ -1054,7 +1032,7 @@ async fn add_project_to_org(setup_api: &ApiV3, project_id: &str, organization_id
async fn add_user_to_team(
user_id: &str,
user_pat: &str,
user_pat: Option<&str>,
team_id: &str,
project_permissions: Option<ProjectPermissions>,
organization_permissions: Option<OrganizationPermissions>,
@@ -1109,7 +1087,7 @@ async fn remove_user_from_team(user_id: &str, team_id: &str, setup_api: &ApiV3)
async fn get_project_permissions(
user_id: &str,
user_pat: &str,
user_pat: Option<&str>,
project_id: &str,
setup_api: &ApiV3,
) -> ProjectPermissions {
@@ -1132,7 +1110,7 @@ async fn get_project_permissions(
async fn get_organization_permissions(
user_id: &str,
user_pat: &str,
user_pat: Option<&str>,
organization_id: &str,
setup_api: &ApiV3,
) -> OrganizationPermissions {

View File

@@ -21,7 +21,10 @@ pub async fn setup_search_projects(test_env: &TestEnvironment<ApiV3>) -> Arc<Has
let mut project_creation_futures = vec![];
let create_async_future =
|id: u64, pat: &'static str, is_modpack: bool, modify_json: Option<json_patch::Patch>| {
|id: u64,
pat: Option<&'static str>,
is_modpack: bool,
modify_json: Option<json_patch::Patch>| {
let slug = format!("{test_name}-searchable-project-{id}");
let jar = if is_modpack {

View File

@@ -71,7 +71,7 @@ async fn oauth_flow_happy_path() {
// Validate the token works
env.assert_read_notifications_status(
FRIEND_USER_ID,
&token_resp.access_token,
Some(&token_resp.access_token),
StatusCode::OK,
)
.await;
@@ -179,17 +179,29 @@ async fn authorize_with_broader_scopes_can_complete_flow() {
env.assert_read_notifications_status(
USER_USER_ID,
&first_access_token,
Some(&first_access_token),
StatusCode::UNAUTHORIZED,
)
.await;
env.assert_read_user_projects_status(USER_USER_ID, &first_access_token, StatusCode::OK)
.await;
env.assert_read_user_projects_status(
USER_USER_ID,
Some(&first_access_token),
StatusCode::OK,
)
.await;
env.assert_read_notifications_status(USER_USER_ID, &second_access_token, StatusCode::OK)
.await;
env.assert_read_user_projects_status(USER_USER_ID, &second_access_token, StatusCode::OK)
.await;
env.assert_read_notifications_status(
USER_USER_ID,
Some(&second_access_token),
StatusCode::OK,
)
.await;
env.assert_read_user_projects_status(
USER_USER_ID,
Some(&second_access_token),
StatusCode::OK,
)
.await;
})
.await;
}
@@ -278,7 +290,7 @@ async fn revoke_authorization_after_issuing_token_revokes_token() {
USER_USER_PAT,
)
.await;
env.assert_read_notifications_status(USER_USER_ID, &access_token, StatusCode::OK)
env.assert_read_notifications_status(USER_USER_ID, Some(&access_token), StatusCode::OK)
.await;
let resp = env
@@ -287,8 +299,12 @@ async fn revoke_authorization_after_issuing_token_revokes_token() {
.await;
assert_status(&resp, StatusCode::OK);
env.assert_read_notifications_status(USER_USER_ID, &access_token, StatusCode::UNAUTHORIZED)
.await;
env.assert_read_notifications_status(
USER_USER_ID,
Some(&access_token),
StatusCode::UNAUTHORIZED,
)
.await;
})
.await;
}

View File

@@ -167,8 +167,12 @@ async fn delete_oauth_client_after_issuing_access_tokens_revokes_tokens() {
env.api.delete_oauth_client(&client_id, USER_USER_PAT).await;
env.assert_read_notifications_status(USER_USER_ID, &access_token, StatusCode::UNAUTHORIZED)
.await;
env.assert_read_notifications_status(
USER_USER_ID,
Some(&access_token),
StatusCode::UNAUTHORIZED,
)
.await;
})
.await;
}

View File

@@ -1,11 +1,8 @@
use crate::common::{
api_common::ApiTeams,
api_v3::request_data::get_icon_data,
database::{generate_random_name, ADMIN_USER_PAT, MOD_USER_ID, MOD_USER_PAT, USER_USER_ID},
dummy_data::DummyImage,
};
use actix_web::test;
use bytes::Bytes;
use common::{
api_v3::ApiV3,
database::{FRIEND_USER_ID, FRIEND_USER_PAT, USER_USER_PAT},
@@ -191,7 +188,7 @@ async fn add_remove_icon() {
let resp = api
.edit_organization_icon(
zeta_organization_id,
Some(get_icon_data(DummyImage::SmallIcon)),
Some(DummyImage::SmallIcon.get_icon_data()),
USER_USER_PAT,
)
.await;
@@ -294,8 +291,9 @@ async fn add_remove_organization_projects() {
#[actix_rt::test]
async fn permissions_patch_organization() {
with_test_environment_all(None, |test_env| async move {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
// For each permission covered by EDIT_DETAILS, ensure the permission is required
let api = &test_env.api;
let edit_details = OrganizationPermissions::EDIT_DETAILS;
let test_pairs = [
("name", json!("")), // generated in the test to not collide slugs
@@ -303,19 +301,22 @@ async fn permissions_patch_organization() {
];
for (key, value) in test_pairs {
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/organization/{}",
ctx.organization_id.unwrap()
))
.set_json(json!({
key: if key == "name" {
json!(generate_random_name("randomslug"))
} else {
value.clone()
},
}))
let req_gen = |ctx: PermissionsTestContext| {
let value = value.clone();
async move {
api.edit_organization(
&ctx.organization_id.unwrap(),
json!({
key: if key == "name" {
json!(generate_random_name("randomslug"))
} else {
value.clone()
},
}),
ctx.test_pat.as_deref(),
)
.await
}
};
PermissionsTest::new(&test_env)
.simple_organization_permissions_test(edit_details, req_gen)
@@ -329,7 +330,7 @@ async fn permissions_patch_organization() {
// Not covered by PATCH /organization
#[actix_rt::test]
async fn permissions_edit_details() {
with_test_environment_all(None, |test_env| async move {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let zeta_organization_id = &test_env
.dummy
.as_ref()
@@ -338,19 +339,18 @@ async fn permissions_edit_details() {
.organization_id;
let zeta_team_id = &test_env.dummy.as_ref().unwrap().organization_zeta.team_id;
let api = &test_env.api;
let edit_details = OrganizationPermissions::EDIT_DETAILS;
// Icon edit
// Uses alpha organization to delete this icon
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/organization/{}/icon?ext=png",
ctx.organization_id.unwrap()
))
.set_payload(Bytes::from(
include_bytes!("../tests/files/200x200.png") as &[u8]
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_organization_icon(
&ctx.organization_id.unwrap(),
Some(DummyImage::SmallIcon.get_icon_data()),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -361,11 +361,9 @@ async fn permissions_edit_details() {
// Icon delete
// Uses alpha project to delete added icon
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/organization/{}/icon?ext=png",
ctx.organization_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_organization_icon(&ctx.organization_id.unwrap(), None, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -394,14 +392,15 @@ async fn permissions_manage_invites() {
let manage_invites = OrganizationPermissions::MANAGE_INVITES;
// Add member
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!("/v3/team/{}/members", ctx.team_id.unwrap()))
.set_json(json!({
"user_id": MOD_USER_ID,
"permissions": 0,
"organization_permissions": 0,
}))
let req_gen = |ctx: PermissionsTestContext| async move {
api.add_user_to_team(
&ctx.team_id.unwrap(),
MOD_USER_ID,
Some(ProjectPermissions::empty()),
Some(OrganizationPermissions::empty()),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -412,15 +411,16 @@ async fn permissions_manage_invites() {
// Edit member
let edit_member = OrganizationPermissions::EDIT_MEMBER;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/team/{}/members/{MOD_USER_ID}",
ctx.team_id.unwrap()
))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_team_member(
&ctx.team_id.unwrap(),
MOD_USER_ID,
json!({
"organization_permissions": 0,
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -431,11 +431,9 @@ async fn permissions_manage_invites() {
// remove member
// requires manage_invites if they have not yet accepted the invite
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/team/{}/members/{MOD_USER_ID}",
ctx.team_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_from_team(&ctx.team_id.unwrap(), MOD_USER_ID, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -454,11 +452,9 @@ async fn permissions_manage_invites() {
// remove existing member (requires remove_member)
let remove_member = OrganizationPermissions::REMOVE_MEMBER;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/team/{}/members/{MOD_USER_ID}",
ctx.team_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_from_team(&ctx.team_id.unwrap(), MOD_USER_ID, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
@@ -473,7 +469,7 @@ async fn permissions_manage_invites() {
#[actix_rt::test]
async fn permissions_add_remove_project() {
with_test_environment_all(None, |test_env| async move {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
let alpha_project_id = &test_env.dummy.as_ref().unwrap().project_alpha.project_id;
@@ -503,15 +499,13 @@ async fn permissions_add_remove_project() {
// Now, FRIEND_USER_ID owns the alpha project
// Add alpha project to zeta organization
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!(
"/v3/organization/{}/projects",
ctx.organization_id.unwrap()
))
.set_json(json!({
"project_id": alpha_project_id,
}))
let req_gen = |ctx: PermissionsTestContext| async move {
api.organization_add_project(
&ctx.organization_id.unwrap(),
alpha_project_id,
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -522,11 +516,13 @@ async fn permissions_add_remove_project() {
// Remove alpha project from zeta organization
let remove_project = OrganizationPermissions::REMOVE_PROJECT;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/organization/{}/projects/{alpha_project_id}",
ctx.organization_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.organization_remove_project(
&ctx.organization_id.unwrap(),
alpha_project_id,
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -540,16 +536,15 @@ async fn permissions_add_remove_project() {
#[actix_rt::test]
async fn permissions_delete_organization() {
with_test_environment_all(None, |test_env| async move {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
let delete_organization = OrganizationPermissions::DELETE_ORGANIZATION;
// Now, FRIEND_USER_ID owns the alpha project
// Add alpha project to zeta organization
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/organization/{}",
ctx.organization_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.delete_organization(&ctx.organization_id.unwrap(), ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.simple_organization_permissions_test(delete_organization, req_gen)
@@ -570,23 +565,27 @@ async fn permissions_add_default_project_permissions() {
.organization_id;
let zeta_team_id = &test_env.dummy.as_ref().unwrap().organization_zeta.team_id;
let api = &test_env.api;
// Add member
let add_member_default_permissions = OrganizationPermissions::MANAGE_INVITES
| OrganizationPermissions::EDIT_MEMBER_DEFAULT_PERMISSIONS;
// Failure test should include MANAGE_INVITES, as it is required to add
// default permissions on an invited user, but should still fail without EDIT_MEMBER_DEFAULT_PERMISSIONS
let failure_with_add_member = (OrganizationPermissions::all() ^ add_member_default_permissions)
let failure_with_add_member = (OrganizationPermissions::all()
^ add_member_default_permissions)
| OrganizationPermissions::MANAGE_INVITES;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!("/v3/team/{}/members", ctx.team_id.unwrap()))
.set_json(json!({
"user_id": MOD_USER_ID,
"permissions": (ProjectPermissions::UPLOAD_VERSION | ProjectPermissions::DELETE_VERSION).bits(),
"organization_permissions": 0,
}))
let req_gen = |ctx: PermissionsTestContext| async move {
api.add_user_to_team(
&ctx.team_id.unwrap(),
MOD_USER_ID,
Some(ProjectPermissions::UPLOAD_VERSION | ProjectPermissions::DELETE_VERSION),
Some(OrganizationPermissions::empty()),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -606,15 +605,16 @@ async fn permissions_add_default_project_permissions() {
^ add_member_default_permissions)
| OrganizationPermissions::EDIT_MEMBER;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/team/{}/members/{MOD_USER_ID}",
ctx.team_id.unwrap()
))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_team_member(
&ctx.team_id.unwrap(),
MOD_USER_ID,
json!({
"permissions": ProjectPermissions::EDIT_DETAILS.bits(),
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_organization(zeta_organization_id, zeta_team_id)
@@ -623,25 +623,26 @@ async fn permissions_add_default_project_permissions() {
.simple_organization_permissions_test(modify_member_default_permission, req_gen)
.await
.unwrap();
}).await;
})
.await;
}
#[actix_rt::test]
async fn permissions_organization_permissions_consistency_test() {
with_test_environment_all(None, |test_env| async move {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
// Ensuring that permission are as we expect them to be
// Full organization permissions test
let success_permissions = OrganizationPermissions::EDIT_DETAILS;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/organization/{}",
ctx.organization_id.unwrap()
))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_organization(
&ctx.organization_id.unwrap(),
json!({
"description": "Example description - changed.",
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.full_organization_permissions_tests(success_permissions, req_gen)

View File

@@ -5,6 +5,8 @@ use common::{database::*, environment::with_test_environment_all};
use labrinth::models::pats::Scopes;
use serde_json::json;
use crate::common::api_common::AppendsOptionalPat;
mod common;
// Full pat test:
@@ -20,7 +22,7 @@ pub async fn pat_full_test() {
// Create a PAT for a full test
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
"name": "test_pat_scopes Test",
@@ -42,7 +44,7 @@ pub async fn pat_full_test() {
// Get PAT again
let req = test::TestRequest::get()
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.uri("/_internal/pat")
.to_request();
let resp = test_env.call(req).await;
@@ -76,7 +78,7 @@ pub async fn pat_full_test() {
// Change scopes and test again
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": 0,
}))
@@ -88,7 +90,7 @@ pub async fn pat_full_test() {
// Change scopes back, and set expiry to the past, and test again
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE,
"expires": Utc::now() + Duration::seconds(1), // expires in 1 second
@@ -104,7 +106,7 @@ pub async fn pat_full_test() {
// Change everything back to normal and test again
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"expires": Utc::now() + Duration::days(1), // no longer expired!
}))
@@ -116,7 +118,7 @@ pub async fn pat_full_test() {
// Patching to a bad expiry should fail
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"expires": Utc::now() - Duration::days(1), // Past
}))
@@ -133,7 +135,7 @@ pub async fn pat_full_test() {
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": scope.bits(),
}))
@@ -147,7 +149,7 @@ pub async fn pat_full_test() {
// Delete PAT
let req = test::TestRequest::delete()
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.uri(&format!("/_internal/pat/{}", id))
.to_request();
let resp = test_env.call(req).await;
@@ -163,7 +165,7 @@ pub async fn bad_pats() {
// Creating a PAT with no name should fail
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
"expires": Utc::now() + Duration::days(1),
@@ -176,7 +178,7 @@ pub async fn bad_pats() {
for name in ["n", "this_name_is_too_long".repeat(16).as_str()] {
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"name": name,
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
@@ -190,7 +192,7 @@ pub async fn bad_pats() {
// Creating a PAT with an expiry in the past should fail
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
"name": "test_pat_scopes Test",
@@ -208,7 +210,7 @@ pub async fn bad_pats() {
}
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": scope.bits(),
"name": format!("test_pat_scopes Name {}", i),
@@ -225,7 +227,7 @@ pub async fn bad_pats() {
// Create a 'good' PAT for patching
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE,
"name": "test_pat_scopes Test",
@@ -241,7 +243,7 @@ pub async fn bad_pats() {
for name in ["n", "this_name_is_too_long".repeat(16).as_str()] {
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"name": name,
}))
@@ -253,7 +255,7 @@ pub async fn bad_pats() {
// Patching to a bad expiry should fail
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"expires": Utc::now() - Duration::days(1), // Past
}))
@@ -270,7 +272,7 @@ pub async fn bad_pats() {
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": scope.bits(),
}))

View File

@@ -2,9 +2,7 @@ use std::collections::HashMap;
use actix_http::StatusCode;
use actix_web::test;
use bytes::Bytes;
use chrono::{Duration, Utc};
use common::api_v3::request_data::get_public_version_creation_data;
use common::api_v3::ApiV3;
use common::database::*;
use common::dummy_data::DUMMY_CATEGORIES;
@@ -17,12 +15,12 @@ use labrinth::database::models::project_item::{PROJECTS_NAMESPACE, PROJECTS_SLUG
use labrinth::models::ids::base62_impl::parse_base62;
use labrinth::models::projects::{Project, ProjectId};
use labrinth::models::teams::ProjectPermissions;
use labrinth::util::actix::{AppendsMultipart, MultipartSegment, MultipartSegmentData};
use labrinth::util::actix::{MultipartSegment, MultipartSegmentData};
use serde_json::json;
use crate::common::api_common::request_data::ProjectCreationRequestData;
use crate::common::api_common::{ApiProject, ApiVersion};
use crate::common::dummy_data::TestFile;
use crate::common::api_common::{ApiProject, ApiTeams, ApiVersion, AppendsOptionalPat};
use crate::common::dummy_data::{DummyImage, TestFile};
mod common;
#[actix_rt::test]
@@ -37,7 +35,7 @@ async fn test_get_project() {
// Perform request on dummy data
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{alpha_project_id}"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let status = resp.status();
@@ -74,7 +72,7 @@ async fn test_get_project() {
// Make the request again, this time it should be cached
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{alpha_project_id}"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let status = resp.status();
@@ -87,7 +85,7 @@ async fn test_get_project() {
// Request should fail on non-existent project
let req = test::TestRequest::get()
.uri("/v3/project/nonexistent")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
@@ -96,7 +94,7 @@ async fn test_get_project() {
// 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_header(("Authorization", ENEMY_USER_PAT))
.append_pat(ENEMY_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
@@ -139,35 +137,35 @@ async fn test_add_remove_project() {
..json_segment.clone()
};
let basic_mod_file = TestFile::BasicMod;
let basic_mod_different_file = TestFile::BasicModDifferent;
// Basic file
let file_segment = MultipartSegment {
name: "basic-mod.jar".to_string(),
filename: Some("basic-mod.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
// TODO: look at these: can be used in the reuse data
data: MultipartSegmentData::Binary(
include_bytes!("../tests/files/basic-mod.jar").to_vec(),
),
// 'Basic'
name: basic_mod_file.filename(),
filename: Some(basic_mod_file.filename()),
content_type: basic_mod_file.content_type(),
data: MultipartSegmentData::Binary(basic_mod_file.bytes()),
};
// Differently named file, with the same content (for hash testing)
// Differently named file, with the SAME content (for hash testing)
let file_diff_name_segment = MultipartSegment {
name: "basic-mod-different.jar".to_string(),
filename: Some("basic-mod-different.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
data: MultipartSegmentData::Binary(
include_bytes!("../tests/files/basic-mod.jar").to_vec(),
),
// 'Different'
name: basic_mod_different_file.filename(),
filename: Some(basic_mod_different_file.filename()),
content_type: basic_mod_different_file.content_type(),
// 'Basic'
data: MultipartSegmentData::Binary(basic_mod_file.bytes()),
};
// Differently named file, with different content
let file_diff_name_content_segment = MultipartSegment {
name: "basic-mod-different.jar".to_string(),
filename: Some("basic-mod-different.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
data: MultipartSegmentData::Binary(
include_bytes!("../tests/files/basic-mod-different.jar").to_vec(),
),
// 'Different'
name: basic_mod_different_file.filename(),
filename: Some(basic_mod_different_file.filename()),
content_type: basic_mod_different_file.content_type(),
data: MultipartSegmentData::Binary(basic_mod_different_file.bytes()),
};
// Add a project- simple, should work.
@@ -193,7 +191,7 @@ async fn test_add_remove_project() {
let uploaded_version_id = project.versions[0];
// Checks files to ensure they were uploaded and correctly identify the file
let hash = sha1::Sha1::from(include_bytes!("../tests/files/basic-mod.jar"))
let hash = sha1::Sha1::from(basic_mod_file.bytes())
.digest()
.to_string();
let version = api
@@ -598,6 +596,7 @@ async fn permissions_patch_project_v3() {
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 api = &test_env.api;
// TODO: This should be a separate test from v3
// - only a couple of these fields are v3-specific
// once we have permissions/scope tests setup to not just take closures, we can split this up
@@ -625,16 +624,22 @@ async fn permissions_patch_project_v3() {
.map(|(key, value)| {
let test_env = test_env.clone();
async move {
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v3/project/{}", ctx.project_id.unwrap()))
.set_json(json!({
key: if key == "slug" {
json!(generate_random_name("randomslug"))
} else {
value.clone()
},
}))
let req_gen = |ctx: PermissionsTestContext| {
let value = value.clone();
async move {
api.edit_project(
&ctx.project_id.unwrap(),
json!({
key: if key == "slug" {
json!(generate_random_name("randomslug"))
} else {
value.clone()
},
}),
ctx.test_pat.as_deref(),
)
.await
}
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(edit_details, req_gen)
@@ -648,13 +653,16 @@ async fn permissions_patch_project_v3() {
// Test with status and requested_status
// This requires a project with a version, so we use alpha_project_id
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v3/project/{}", ctx.project_id.unwrap()))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project(
&ctx.project_id.unwrap(),
json!({
"status": "private",
"requested_status": "private",
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -664,15 +672,15 @@ async fn permissions_patch_project_v3() {
.unwrap();
// Bulk patch projects
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/projects?ids=[{uri}]",
uri = urlencoding::encode(&format!("\"{}\"", ctx.project_id.unwrap()))
))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project_bulk(
&[&ctx.project_id.unwrap()],
json!({
"name": "randomname",
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(edit_details, req_gen)
@@ -682,12 +690,15 @@ async fn permissions_patch_project_v3() {
// Edit body
// Cannot bulk edit body
let edit_body = ProjectPermissions::EDIT_BODY;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v3/project/{}", ctx.project_id.unwrap()))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project(
&ctx.project_id.unwrap(),
json!({
"description": "new description!",
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(edit_body, req_gen)
@@ -706,12 +717,14 @@ async fn permissions_edit_details() {
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 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_header(("Authorization", MOD_USER_PAT))
.append_pat(MOD_USER_PAT)
.set_json(json!({
"status": "unlisted"
}))
@@ -720,15 +733,14 @@ async fn permissions_edit_details() {
assert_eq!(resp.status(), 204);
// Schedule version
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!("/v3/version/{beta_version_id}/schedule")) // beta_version_id is an *approved* version, so we can schedule it
.set_json(json!(
{
"requested_status": "archived",
"time": Utc::now() + Duration::days(1),
}
))
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)
@@ -739,15 +751,13 @@ async fn permissions_edit_details() {
// Icon edit
// Uses alpha project to delete this icon
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/project/{}/icon?ext=png",
ctx.project_id.unwrap()
))
.set_payload(Bytes::from(
include_bytes!("../tests/files/200x200.png") as &[u8]
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project_icon(
&ctx.project_id.unwrap(),
Some(DummyImage::SmallIcon.get_icon_data()),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -758,11 +768,9 @@ async fn permissions_edit_details() {
// Icon delete
// Uses alpha project to delete added icon
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/project/{}/icon?ext=png",
ctx.project_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project_icon(&ctx.project_id.unwrap(), None, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -773,15 +781,17 @@ async fn permissions_edit_details() {
// Add gallery item
// Uses alpha project to add gallery item so we can get its url
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!(
"/v3/project/{}/gallery?ext=png&featured=true",
ctx.project_id.unwrap()
))
.set_payload(Bytes::from(
include_bytes!("../tests/files/200x200.png") as &[u8]
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.add_gallery_item(
&ctx.project_id.unwrap(),
DummyImage::SmallIcon.get_icon_data(),
true,
None,
None,
None,
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -792,7 +802,7 @@ async fn permissions_edit_details() {
// Get project, as we need the gallery image url
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{alpha_project_id}"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
let project: serde_json::Value = test::read_body_json(resp).await;
@@ -800,11 +810,16 @@ async fn permissions_edit_details() {
// Edit gallery item
// Uses alpha project to edit gallery item
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch().uri(&format!(
"/v3/project/{}/gallery?url={gallery_url}",
ctx.project_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_gallery_item(
&ctx.project_id.unwrap(),
gallery_url,
vec![("description".to_string(), "new caption!".to_string())]
.into_iter()
.collect(),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -815,11 +830,13 @@ async fn permissions_edit_details() {
// Remove gallery item
// Uses alpha project to remove gallery item
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/project/{}/gallery?url={gallery_url}",
ctx.project_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_gallery_item(
&ctx.project_id.unwrap(),
gallery_url,
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -839,21 +856,22 @@ async fn permissions_upload_version() {
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 api = &test_env.api;
let upload_version = ProjectPermissions::UPLOAD_VERSION;
// Upload version with basic-mod.jar
let req_gen = |ctx: &PermissionsTestContext| {
let req_gen = |ctx: PermissionsTestContext| async move {
let project_id = ctx.project_id.unwrap();
let project_id = ProjectId(parse_base62(project_id).unwrap());
let multipart = get_public_version_creation_data(
let project_id = ProjectId(parse_base62(&project_id).unwrap());
api.add_public_version(
project_id,
"1.0.0",
TestFile::BasicMod,
None,
None,
);
test::TestRequest::post()
.uri("/v3/version")
.set_multipart(multipart.segment_data)
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(upload_version, req_gen)
@@ -862,30 +880,13 @@ async fn permissions_upload_version() {
// Upload file to existing version
// Uses alpha project, as it has an existing version
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!("/v3/version/{}/file", alpha_version_id))
.set_multipart([
MultipartSegment {
name: "data".to_string(),
filename: None,
content_type: Some("application/json".to_string()),
data: MultipartSegmentData::Text(
serde_json::to_string(&json!({
"file_parts": ["basic-mod-different.jar"],
}))
.unwrap(),
),
},
MultipartSegment {
name: "basic-mod-different.jar".to_string(),
filename: Some("basic-mod-different.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
data: MultipartSegmentData::Binary(
include_bytes!("../tests/files/basic-mod-different.jar").to_vec(),
),
},
])
let req_gen = |ctx: PermissionsTestContext| async move {
api.upload_file_to_version(
alpha_version_id,
&TestFile::BasicModDifferent,
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -896,12 +897,15 @@ async fn permissions_upload_version() {
// Patch version
// Uses alpha project, as it has an existing version
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v3/version/{}", alpha_version_id))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_version(
alpha_version_id,
json!({
"name": "Basic Mod",
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -913,8 +917,9 @@ async fn permissions_upload_version() {
// Delete version file
// Uses alpha project, as it has an existing version
let delete_version = ProjectPermissions::DELETE_VERSION;
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!("/v3/version_file/{}", alpha_file_hash))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_version_file(alpha_file_hash, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
@@ -926,8 +931,9 @@ async fn permissions_upload_version() {
// Delete version
// Uses alpha project, as it has an existing version
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!("/v3/version/{}", alpha_version_id))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_version(alpha_version_id, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -946,16 +952,19 @@ async fn permissions_manage_invites() {
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 api = &test_env.api;
let manage_invites = ProjectPermissions::MANAGE_INVITES;
// Add member
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!("/v3/team/{}/members", ctx.team_id.unwrap()))
.set_json(json!({
"user_id": MOD_USER_ID,
"permissions": 0,
}))
let req_gen = |ctx: PermissionsTestContext| async move {
api.add_user_to_team(
&ctx.team_id.unwrap(),
MOD_USER_ID,
Some(ProjectPermissions::empty()),
None,
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -966,15 +975,16 @@ async fn permissions_manage_invites() {
// Edit member
let edit_member = ProjectPermissions::EDIT_MEMBER;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!(
"/v3/team/{}/members/{MOD_USER_ID}",
ctx.team_id.unwrap()
))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_team_member(
&ctx.team_id.unwrap(),
MOD_USER_ID,
json!({
"permissions": 0,
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -985,11 +995,9 @@ async fn permissions_manage_invites() {
// remove member
// requires manage_invites if they have not yet accepted the invite
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/team/{}/members/{MOD_USER_ID}",
ctx.team_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_from_team(&ctx.team_id.unwrap(), MOD_USER_ID, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -1001,7 +1009,7 @@ async fn permissions_manage_invites() {
// re-add member for testing
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{}/members", alpha_team_id))
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.set_json(json!({
"user_id": MOD_USER_ID,
}))
@@ -1012,18 +1020,16 @@ async fn permissions_manage_invites() {
// Accept invite
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{}/join", alpha_team_id))
.append_header(("Authorization", MOD_USER_PAT))
.append_pat(MOD_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 204);
// remove existing member (requires remove_member)
let remove_member = ProjectPermissions::REMOVE_MEMBER;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!(
"/v3/team/{}/members/{MOD_USER_ID}",
ctx.team_id.unwrap()
))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_from_team(&ctx.team_id.unwrap(), MOD_USER_ID, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
@@ -1041,10 +1047,11 @@ async fn permissions_delete_project() {
// Add member, remove member, edit member
with_test_environment_all(None, |test_env| async move {
let delete_project = ProjectPermissions::DELETE_PROJECT;
let api = &test_env.api;
// Delete project
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!("/v3/project/{}", ctx.project_id.unwrap()))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_project(&ctx.project_id.unwrap(), ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(delete_project, req_gen)
@@ -1061,15 +1068,18 @@ async fn project_permissions_consistency_test() {
with_test_environment_all(Some(10), |test_env| async move {
// Test that the permissions are consistent with each other
// For example, if we get the projectpermissions directly, from an organization's defaults, overriden, etc, they should all be correct & consistent
let api = &test_env.api;
// Full project permissions test with EDIT_DETAILS
let success_permissions = ProjectPermissions::EDIT_DETAILS;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v3/project/{}", ctx.project_id.unwrap()))
.set_json(json!({
"name": "Example title - changed.",
}))
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project(
&ctx.project_id.unwrap(),
json!({
"categories": [],
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.full_project_permissions_test(success_permissions, req_gen)
@@ -1081,12 +1091,15 @@ async fn project_permissions_consistency_test() {
| ProjectPermissions::REMOVE_MEMBER
| ProjectPermissions::DELETE_VERSION
| ProjectPermissions::VIEW_PAYOUTS;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v3/project/{}", ctx.project_id.unwrap()))
.set_json(json!({
"name": "Example title - changed.",
}))
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project(
&ctx.project_id.unwrap(),
json!({
"categories": [],
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.full_project_permissions_test(success_permissions, req_gen)

View File

@@ -14,7 +14,8 @@ use labrinth::models::projects::ProjectId;
use labrinth::util::actix::{AppendsMultipart, MultipartSegment, MultipartSegmentData};
use serde_json::json;
use crate::common::api_common::ApiTeams;
use crate::common::api_common::{ApiTeams, AppendsOptionalPat};
use crate::common::dummy_data::DummyImage;
// For each scope, we (using test_scope):
// - create a PAT with a given set of scopes for a function
@@ -386,7 +387,7 @@ pub async fn project_version_reads_scopes() {
let read_version = Scopes::VERSION_READ;
let req = test::TestRequest::patch()
.uri(&format!("/v3/version/{beta_version_id}"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"status": "draft"
}))
@@ -505,6 +506,8 @@ pub async fn project_write_scopes() {
.team_id
.clone();
let test_icon = DummyImage::SmallIcon;
// Projects writing
let write_project = Scopes::PROJECT_WRITE;
let req_gen = || {
@@ -541,7 +544,7 @@ pub async fn project_write_scopes() {
// Approve beta as private so we can schedule it
let req = test::TestRequest::patch()
.uri(&format!("/v3/project/{beta_project_id}"))
.append_header(("Authorization", MOD_USER_PAT))
.append_pat(MOD_USER_PAT)
.set_json(json!({
"status": "private"
}))
@@ -567,10 +570,11 @@ pub async fn project_write_scopes() {
// Icons and gallery images
let req_gen = || {
test::TestRequest::patch()
.uri(&format!("/v3/project/{beta_project_id}/icon?ext=png"))
.set_payload(Bytes::from(
include_bytes!("../tests/files/200x200.png") as &[u8]
.uri(&format!(
"/v3/project/{beta_project_id}/icon?ext={ext}",
ext = test_icon.extension()
))
.set_payload(test_icon.bytes())
};
ScopeTest::new(&test_env)
.test(req_gen, write_project)
@@ -587,11 +591,10 @@ pub async fn project_write_scopes() {
let req_gen = || {
test::TestRequest::post()
.uri(&format!(
"/v3/project/{beta_project_id}/gallery?ext=png&featured=true"
))
.set_payload(Bytes::from(
include_bytes!("../tests/files/200x200.png") as &[u8]
"/v3/project/{beta_project_id}/gallery?ext={ext}&featured=true",
ext = test_icon.extension()
))
.set_payload(test_icon.bytes())
};
ScopeTest::new(&test_env)
.test(req_gen, write_project)
@@ -601,7 +604,7 @@ pub async fn project_write_scopes() {
// Get project, as we need the gallery image url
let req_gen = test::TestRequest::get()
.uri(&format!("/v3/project/{beta_project_id}"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req_gen).await;
let project: serde_json::Value = test::read_body_json(resp).await;
@@ -728,12 +731,14 @@ pub async fn version_write_scopes() {
.file_hash
.clone();
let basic_zip = TestFile::BasicZip;
let write_version = Scopes::VERSION_WRITE;
// Approve beta version as private so we can schedule it
let req = test::TestRequest::patch()
.uri(&format!("/v3/version/{beta_version_id}"))
.append_header(("Authorization", MOD_USER_PAT))
.append_pat(MOD_USER_PAT)
.set_json(json!({
"status": "unlisted"
}))
@@ -782,7 +787,7 @@ pub async fn version_write_scopes() {
serde_json::to_string(&json!(
{
"file_types": {
"simple-zip.zip": "required-resource-pack"
basic_zip.filename(): "required-resource-pack"
},
}
))
@@ -792,12 +797,10 @@ pub async fn version_write_scopes() {
// Differently named file, with different content
let content_segment = MultipartSegment {
name: "simple-zip.zip".to_string(),
filename: Some("simple-zip.zip".to_string()),
content_type: Some("application/zip".to_string()),
data: MultipartSegmentData::Binary(
include_bytes!("../tests/files/simple-zip.zip").to_vec(),
),
name: basic_zip.filename(),
filename: Some(basic_zip.filename()),
content_type: basic_zip.content_type(),
data: MultipartSegmentData::Binary(basic_zip.bytes()),
};
// Upload version file
@@ -995,7 +998,7 @@ pub async fn thread_scopes() {
// First, get message id
let req_gen = test::TestRequest::get()
.uri(&format!("/v3/thread/{thread_id}"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.to_request();
let resp = test_env.call(req_gen).await;
let success: serde_json::Value = test::read_body_json(resp).await;
@@ -1077,6 +1080,8 @@ pub async fn collections_scopes() {
.project_id
.clone();
let small_icon = DummyImage::SmallIcon;
// Create collection
let collection_create = Scopes::COLLECTION_CREATE;
let req_gen = || {
@@ -1145,10 +1150,11 @@ pub async fn collections_scopes() {
let req_gen = || {
test::TestRequest::patch()
.uri(&format!("/v3/collection/{collection_id}/icon?ext=png"))
.set_payload(Bytes::from(
include_bytes!("../tests/files/200x200.png") as &[u8]
.uri(&format!(
"/v3/collection/{collection_id}/icon?ext={ext}",
ext = small_icon.extension()
))
.set_payload(Bytes::from(small_icon.bytes()))
};
ScopeTest::new(&test_env)
.test(req_gen, collection_write)
@@ -1178,6 +1184,8 @@ pub async fn organization_scopes() {
.project_id
.clone();
let icon = DummyImage::SmallIcon;
// Create organization
let organization_create = Scopes::ORGANIZATION_CREATE;
let req_gen = || {
@@ -1210,10 +1218,11 @@ pub async fn organization_scopes() {
let req_gen = || {
test::TestRequest::patch()
.uri(&format!("/v3/organization/{organization_id}/icon?ext=png"))
.set_payload(Bytes::from(
include_bytes!("../tests/files/200x200.png") as &[u8]
.uri(&format!(
"/v3/organization/{organization_id}/icon?ext={ext}",
ext = icon.extension()
))
.set_payload(Bytes::from(icon.bytes()))
};
ScopeTest::new(&test_env)
.test(req_gen, organization_edit)

View File

@@ -1,4 +1,7 @@
use crate::common::{api_common::ApiTeams, database::*};
use crate::common::{
api_common::{ApiTeams, AppendsOptionalPat},
database::*,
};
use actix_web::test;
use common::{
api_v3::ApiV3,
@@ -36,7 +39,7 @@ async fn test_get_team() {
] {
let req = test::TestRequest::get()
.uri(&uri)
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
@@ -51,7 +54,7 @@ async fn test_get_team() {
// - should not appear in the team members list to enemy users
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{team_id}/members"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(&json!({
"user_id": FRIEND_USER_ID,
}))
@@ -65,7 +68,7 @@ async fn test_get_team() {
] {
let req = test::TestRequest::get()
.uri(&uri)
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 200);
@@ -87,7 +90,7 @@ async fn test_get_team() {
let req = test::TestRequest::get()
.uri(&uri)
.append_header(("Authorization", ENEMY_USER_PAT))
.append_pat(ENEMY_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 200);
@@ -101,7 +104,7 @@ async fn test_get_team() {
// and should be able to see private data about the team
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{team_id}/join"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 204);
@@ -112,7 +115,7 @@ async fn test_get_team() {
] {
let req = test::TestRequest::get()
.uri(&uri)
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 200);
@@ -154,7 +157,7 @@ async fn test_get_team_project_orgs() {
// Attach alpha to zeta
let req = test::TestRequest::post()
.uri(&format!("/v3/organization/{zeta_organization_id}/projects"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"project_id": alpha_project_id,
}))
@@ -165,7 +168,7 @@ async fn test_get_team_project_orgs() {
// Invite and add friend to zeta
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{zeta_team_id}/members"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"user_id": FRIEND_USER_ID,
}))
@@ -175,7 +178,7 @@ async fn test_get_team_project_orgs() {
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{zeta_team_id}/join"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 204);
@@ -185,7 +188,7 @@ async fn test_get_team_project_orgs() {
// - not the ones from the organization
let req = test::TestRequest::get()
.uri(&format!("/v3/team/{alpha_team_id}/members"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 200);
@@ -197,7 +200,7 @@ async fn test_get_team_project_orgs() {
// - the members of the project team including the ones from the organization
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{alpha_project_id}/members"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 200);
@@ -290,7 +293,7 @@ async fn test_patch_organization_team_member() {
let req = test::TestRequest::patch()
.uri(&format!("/v3/team/{zeta_team_id}/members/{USER_USER_ID}"))
.set_json(json!({}))
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 204);
@@ -298,7 +301,7 @@ async fn test_patch_organization_team_member() {
// As a non-owner with full permissions, attempt to edit the owner's permissions
let req = test::TestRequest::patch()
.uri(&format!("/v3/team/{zeta_team_id}/members/{USER_USER_ID}"))
.append_header(("Authorization", ADMIN_USER_PAT))
.append_pat(ADMIN_USER_PAT)
.set_json(json!({
"permissions": 0
}))
@@ -312,7 +315,7 @@ async fn test_patch_organization_team_member() {
// first, invite friend
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{zeta_team_id}/members"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"user_id": FRIEND_USER_ID,
"organization_permissions": (OrganizationPermissions::EDIT_MEMBER | OrganizationPermissions::EDIT_MEMBER_DEFAULT_PERMISSIONS).bits(),
@@ -323,7 +326,7 @@ async fn test_patch_organization_team_member() {
// accept
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{zeta_team_id}/join"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 204);
@@ -331,7 +334,7 @@ async fn test_patch_organization_team_member() {
// try to add permissions- fails, as we do not have EDIT_DETAILS
let req = test::TestRequest::patch()
.uri(&format!("/v3/team/{zeta_team_id}/members/{FRIEND_USER_ID}"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.set_json(json!({
"organization_permissions": (OrganizationPermissions::EDIT_MEMBER | OrganizationPermissions::EDIT_DETAILS).bits()
}))
@@ -344,7 +347,7 @@ async fn test_patch_organization_team_member() {
for payout in [-1, 5001] {
let req = test::TestRequest::patch()
.uri(&format!("/v3/team/{zeta_team_id}/members/{FRIEND_USER_ID}"))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"payouts_split": payout
}))
@@ -356,7 +359,7 @@ async fn test_patch_organization_team_member() {
// Successful patch
let req = test::TestRequest::patch()
.uri(&format!("/v3/team/{zeta_team_id}/members/{FRIEND_USER_ID}"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.set_json(json!({
"payouts_split": 51,
"organization_permissions": (OrganizationPermissions::EDIT_MEMBER).bits(), // reduces permissions
@@ -372,7 +375,7 @@ async fn test_patch_organization_team_member() {
// Check results
let req = test::TestRequest::get()
.uri(&format!("/v3/team/{zeta_team_id}/members"))
.append_header(("Authorization", FRIEND_USER_PAT))
.append_pat(FRIEND_USER_PAT)
.to_request();
let resp = test_env.call(req).await;
assert_eq!(resp.status(), 200);

View File

@@ -1,9 +1,8 @@
use std::sync::Arc;
use crate::common::{
api_common::ApiProject,
api_v2::{
request_data::{get_public_project_creation_data_json, get_public_version_creation_data},
ApiV2,
},
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,
},
@@ -96,41 +95,41 @@ async fn test_add_remove_project() {
..json_segment.clone()
};
let basic_mod_file = TestFile::BasicMod;
let basic_mod_different_file = TestFile::BasicModDifferent;
// Basic file
let file_segment = MultipartSegment {
name: "basic-mod.jar".to_string(),
filename: Some("basic-mod.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
// TODO: look at these: can be simplified with TestFile
data: MultipartSegmentData::Binary(
include_bytes!("../../tests/files/basic-mod.jar").to_vec(),
),
// 'Basic'
name: basic_mod_file.filename(),
filename: Some(basic_mod_file.filename()),
content_type: basic_mod_file.content_type(),
data: MultipartSegmentData::Binary(basic_mod_file.bytes()),
};
// Differently named file, with the same content (for hash testing)
// Differently named file, with the SAME content (for hash testing)
let file_diff_name_segment = MultipartSegment {
name: "basic-mod-different.jar".to_string(),
filename: Some("basic-mod-different.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
data: MultipartSegmentData::Binary(
include_bytes!("../../tests/files/basic-mod.jar").to_vec(),
),
// 'Different'
name: basic_mod_different_file.filename(),
filename: Some(basic_mod_different_file.filename()),
content_type: basic_mod_different_file.content_type(),
// 'Basic'
data: MultipartSegmentData::Binary(basic_mod_file.bytes()),
};
// Differently named file, with different content
let file_diff_name_content_segment = MultipartSegment {
name: "basic-mod-different.jar".to_string(),
filename: Some("basic-mod-different.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
data: MultipartSegmentData::Binary(
include_bytes!("../../tests/files/basic-mod-different.jar").to_vec(),
),
// 'Different'
name: basic_mod_different_file.filename(),
filename: Some(basic_mod_different_file.filename()),
content_type: basic_mod_different_file.content_type(),
data: MultipartSegmentData::Binary(basic_mod_different_file.bytes()),
};
// Add a project- simple, should work.
let req = test::TestRequest::post()
.uri("/v2/project")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_multipart(vec![json_segment.clone(), file_segment.clone()])
.to_request();
let resp = test_env.call(req).await;
@@ -144,7 +143,7 @@ async fn test_add_remove_project() {
let uploaded_version_id = project.versions[0];
// Checks files to ensure they were uploaded and correctly identify the file
let hash = sha1::Sha1::from(include_bytes!("../../tests/files/basic-mod.jar"))
let hash = sha1::Sha1::from(basic_mod_file.bytes())
.digest()
.to_string();
let version = api
@@ -156,7 +155,7 @@ async fn test_add_remove_project() {
// Even if that file is named differently
let req = test::TestRequest::post()
.uri("/v2/project")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_multipart(vec![
json_diff_slug_file_segment.clone(), // Different slug, different file name
file_diff_name_segment.clone(), // Different file name, same content
@@ -169,7 +168,7 @@ async fn test_add_remove_project() {
// Reusing with the same slug and a different file should fail
let req = test::TestRequest::post()
.uri("/v2/project")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_multipart(vec![
json_diff_file_segment.clone(), // Same slug, different file name
file_diff_name_content_segment.clone(), // Different file name, different content
@@ -182,7 +181,7 @@ async fn test_add_remove_project() {
// Different slug, different file should succeed
let req = test::TestRequest::post()
.uri("/v2/project")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_multipart(vec![
json_diff_slug_file_segment.clone(), // Different slug, different file name
file_diff_name_content_segment.clone(), // Different file name, same content
@@ -234,23 +233,24 @@ async fn permissions_upload_version() {
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 api = &test_env.api;
let basic_mod_different_file = TestFile::BasicModDifferent;
let upload_version = ProjectPermissions::UPLOAD_VERSION;
// Upload version with basic-mod.jar
let req_gen = |ctx: &PermissionsTestContext| {
let req_gen = |ctx: PermissionsTestContext| async move {
let project_id = ctx.project_id.unwrap();
let project_id = ProjectId(parse_base62(project_id).unwrap());
let multipart = get_public_version_creation_data(
let project_id = ProjectId(parse_base62(&project_id).unwrap());
api.add_public_version(
project_id,
"1.0.0",
TestFile::BasicMod,
None,
None,
);
test::TestRequest::post()
.uri("/v2/version")
.set_multipart(multipart.segment_data)
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(upload_version, req_gen)
.await
@@ -258,30 +258,13 @@ async fn permissions_upload_version() {
// Upload file to existing version
// Uses alpha project, as it has an existing version
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::post()
.uri(&format!("/v2/version/{}/file", alpha_version_id))
.set_multipart([
MultipartSegment {
name: "data".to_string(),
filename: None,
content_type: Some("application/json".to_string()),
data: MultipartSegmentData::Text(
serde_json::to_string(&json!({
"file_parts": ["basic-mod-different.jar"],
}))
.unwrap(),
),
},
MultipartSegment {
name: "basic-mod-different.jar".to_string(),
filename: Some("basic-mod-different.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
data: MultipartSegmentData::Binary(
include_bytes!("../../tests/files/basic-mod-different.jar").to_vec(),
),
},
])
let file_ref = Arc::new(basic_mod_different_file);
let req_gen = |ctx: PermissionsTestContext| {
let file_ref = file_ref.clone();
async move {
api.upload_file_to_version(alpha_version_id, &file_ref, ctx.test_pat.as_deref())
.await
}
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -292,13 +275,17 @@ async fn permissions_upload_version() {
// Patch version
// Uses alpha project, as it has an existing version
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v2/version/{}", alpha_version_id))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_version(
alpha_version_id,
json!({
"name": "Basic Mod",
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
.with_user(FRIEND_USER_ID, FRIEND_USER_PAT, true)
@@ -309,8 +296,9 @@ async fn permissions_upload_version() {
// Delete version file
// Uses alpha project, as it has an existing version
let delete_version = ProjectPermissions::DELETE_VERSION;
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!("/v2/version_file/{}", alpha_file_hash))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_version_file(alpha_file_hash, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
@@ -322,8 +310,9 @@ async fn permissions_upload_version() {
// Delete version
// Uses alpha project, as it has an existing version
let req_gen = |_: &PermissionsTestContext| {
test::TestRequest::delete().uri(&format!("/v2/version/{}", alpha_version_id))
let req_gen = |ctx: PermissionsTestContext| async move {
api.remove_version(alpha_version_id, ctx.test_pat.as_deref())
.await
};
PermissionsTest::new(&test_env)
.with_existing_project(alpha_project_id, alpha_team_id)
@@ -374,6 +363,8 @@ pub async fn test_patch_v2() {
#[actix_rt::test]
async fn permissions_patch_project_v2() {
with_test_environment(Some(8), |test_env: TestEnvironment<ApiV2>| async move {
let api = &test_env.api;
// TODO: This only includes v2 ones (as it should. See v3)
// For each permission covered by EDIT_DETAILS, ensure the permission is required
let edit_details = ProjectPermissions::EDIT_DETAILS;
@@ -397,16 +388,19 @@ async fn permissions_patch_project_v2() {
.map(|(key, value)| {
let test_env = test_env.clone();
async move {
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v2/project/{}", ctx.project_id.unwrap()))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async {
api.edit_project(
&ctx.project_id.unwrap(),
json!({
key: if key == "slug" {
json!(generate_random_name("randomslug"))
} else {
value.clone()
},
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(edit_details, req_gen)
@@ -421,12 +415,15 @@ async fn permissions_patch_project_v2() {
// Edit body
// Cannot bulk edit body
let edit_body = ProjectPermissions::EDIT_BODY;
let req_gen = |ctx: &PermissionsTestContext| {
test::TestRequest::patch()
.uri(&format!("/v2/project/{}", ctx.project_id.unwrap()))
.set_json(json!({
let req_gen = |ctx: PermissionsTestContext| async move {
api.edit_project(
&ctx.project_id.unwrap(),
json!({
"body": "new body!", // new body
}))
}),
ctx.test_pat.as_deref(),
)
.await
};
PermissionsTest::new(&test_env)
.simple_project_permissions_test(edit_body, req_gen)

View File

@@ -28,7 +28,7 @@ async fn search_projects() {
let create_async_future =
|id: u64,
pat: &'static str,
pat: Option<&'static str>,
is_modpack: bool,
modify_json: Option<json_patch::Patch>| {
let slug = format!("{test_name}-searchable-project-{id}");