You've already forked AstralRinth
forked from didirus/AstralRinth
Expose test utils to Labrinth dependents (#4703)
* Expose test utils to Labrinth dependents * Feature gate `labrinth::test` * Unify db migrators * Expose `NotificationBuilder::insert_many_deliveries` * Add logging utils to common crate * Remove unused console-subscriber layer * fix CI
This commit is contained in:
@@ -160,7 +160,7 @@ impl NotificationBuilder {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert_many_deliveries(
|
||||
pub async fn insert_many_deliveries(
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
redis: &RedisPool,
|
||||
notification_ids: &[i64],
|
||||
|
||||
@@ -37,10 +37,12 @@ pub mod routes;
|
||||
pub mod scheduler;
|
||||
pub mod search;
|
||||
pub mod sync;
|
||||
pub mod test;
|
||||
pub mod util;
|
||||
pub mod validate;
|
||||
|
||||
#[cfg(feature = "test")]
|
||||
pub mod test;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Pepper {
|
||||
pub pepper: String,
|
||||
|
||||
@@ -17,15 +17,9 @@ use labrinth::util::ratelimit::rate_limit_middleware;
|
||||
use labrinth::utoipa_app_config;
|
||||
use labrinth::{check_env_vars, clickhouse, database, file_hosting};
|
||||
use std::ffi::CStr;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use tracing::level_filters::LevelFilter;
|
||||
use tracing::{Instrument, error, info, info_span};
|
||||
use tracing_actix_web::TracingLogger;
|
||||
use tracing_ecs::ECSLayerBuilder;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
use utoipa::OpenApi;
|
||||
use utoipa_actix_web::AppExt;
|
||||
use utoipa_swagger_ui::SwaggerUi;
|
||||
@@ -59,59 +53,13 @@ struct Args {
|
||||
run_background_task: Option<BackgroundTask>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
enum OutputFormat {
|
||||
#[default]
|
||||
Human,
|
||||
Json,
|
||||
}
|
||||
|
||||
impl FromStr for OutputFormat {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"human" => Ok(Self::Human),
|
||||
"json" => Ok(Self::Json),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
color_eyre::install().expect("failed to install `color-eyre`");
|
||||
dotenvy::dotenv().ok();
|
||||
let console_layer = console_subscriber::spawn();
|
||||
let env_filter = EnvFilter::builder()
|
||||
.with_default_directive(LevelFilter::INFO.into())
|
||||
.from_env_lossy();
|
||||
|
||||
let output_format =
|
||||
dotenvy::var("LABRINTH_FORMAT").map_or(OutputFormat::Human, |format| {
|
||||
format
|
||||
.parse::<OutputFormat>()
|
||||
.unwrap_or_else(|_| panic!("invalid output format '{format}'"))
|
||||
});
|
||||
|
||||
match output_format {
|
||||
OutputFormat::Human => {
|
||||
tracing_subscriber::registry()
|
||||
.with(console_layer)
|
||||
.with(env_filter)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
}
|
||||
OutputFormat::Json => {
|
||||
tracing_subscriber::registry()
|
||||
.with(console_layer)
|
||||
.with(env_filter)
|
||||
.with(ECSLayerBuilder::default().stdout())
|
||||
.init();
|
||||
}
|
||||
}
|
||||
modrinth_util::log::init().expect("failed to initialize logging");
|
||||
|
||||
if check_env_vars() {
|
||||
error!("Some environment variables are missing!");
|
||||
|
||||
171
apps/labrinth/src/test/api_common/generic.rs
Normal file
171
apps/labrinth/src/test/api_common/generic.rs
Normal file
@@ -0,0 +1,171 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::models::ids::ProjectId;
|
||||
use crate::models::{
|
||||
projects::VersionType,
|
||||
teams::{OrganizationPermissions, ProjectPermissions},
|
||||
};
|
||||
use crate::test::{api_v2::ApiV2, api_v3::ApiV3, dummy_data::TestFile};
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use async_trait::async_trait;
|
||||
|
||||
use super::{
|
||||
Api, ApiProject, ApiTags, ApiTeams, ApiUser, ApiVersion,
|
||||
models::{CommonProject, CommonVersion},
|
||||
request_data::{ImageData, ProjectCreationRequestData},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum GenericApi {
|
||||
V2(ApiV2),
|
||||
V3(ApiV3),
|
||||
}
|
||||
|
||||
macro_rules! delegate_api_variant {
|
||||
(
|
||||
$(#[$meta:meta])*
|
||||
impl $impl_name:ident for $struct_name:ident {
|
||||
$(
|
||||
[$method_name:ident, $ret:ty, $($param_name:ident: $param_type:ty),*]
|
||||
),* $(,)?
|
||||
}
|
||||
|
||||
) => {
|
||||
$(#[$meta])*
|
||||
impl $impl_name for $struct_name {
|
||||
$(
|
||||
async fn $method_name(&self, $($param_name: $param_type),*) -> $ret {
|
||||
match self {
|
||||
$struct_name::V2(api) => api.$method_name($($param_name),*).await,
|
||||
$struct_name::V3(api) => api.$method_name($($param_name),*).await,
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Api for GenericApi {
|
||||
async fn call(&self, req: actix_http::Request) -> ServiceResponse {
|
||||
match self {
|
||||
Self::V2(api) => api.call(req).await,
|
||||
Self::V3(api) => api.call(req).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn reset_search_index(&self) -> ServiceResponse {
|
||||
match self {
|
||||
Self::V2(api) => api.reset_search_index().await,
|
||||
Self::V3(api) => api.reset_search_index().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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: Option<&str>],
|
||||
[get_public_project_creation_data_json, serde_json::Value, slug: &str, version_jar: Option<&TestFile>],
|
||||
[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_projects, ServiceResponse, ids_or_slugs: &[&str], pat: Option<&str>],
|
||||
[get_project_dependencies, ServiceResponse, 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>],
|
||||
[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>],
|
||||
[create_report, ServiceResponse, report_type: &str, id: &str, item_type: crate::test::api_common::models::CommonItemType, body: &str, pat: Option<&str>],
|
||||
[get_report, ServiceResponse, id: &str, pat: Option<&str>],
|
||||
[get_reports, ServiceResponse, ids: &[&str], pat: Option<&str>],
|
||||
[get_user_reports, ServiceResponse, pat: Option<&str>],
|
||||
[edit_report, ServiceResponse, id: &str, patch: serde_json::Value, pat: Option<&str>],
|
||||
[delete_report, ServiceResponse, id: &str, pat: Option<&str>],
|
||||
[get_thread, ServiceResponse, id: &str, pat: Option<&str>],
|
||||
[get_threads, ServiceResponse, ids: &[&str], pat: Option<&str>],
|
||||
[write_to_thread, ServiceResponse, id: &str, r#type : &str, message: &str, pat: Option<&str>],
|
||||
[get_moderation_inbox, ServiceResponse, pat: Option<&str>],
|
||||
[read_thread, ServiceResponse, id: &str, pat: Option<&str>],
|
||||
[delete_thread_message, ServiceResponse, id: &str, pat: Option<&str>],
|
||||
}
|
||||
);
|
||||
|
||||
delegate_api_variant!(
|
||||
#[async_trait(?Send)]
|
||||
impl ApiTags for GenericApi {
|
||||
[get_loaders, ServiceResponse,],
|
||||
[get_loaders_deserialized_common, Vec<crate::test::api_common::models::CommonLoaderData>,],
|
||||
[get_categories, ServiceResponse,],
|
||||
[get_categories_deserialized_common, Vec<crate::test::api_common::models::CommonCategoryData>,],
|
||||
}
|
||||
);
|
||||
|
||||
delegate_api_variant!(
|
||||
#[async_trait(?Send)]
|
||||
impl ApiTeams for GenericApi {
|
||||
[get_team_members, ServiceResponse, team_id: &str, pat: Option<&str>],
|
||||
[get_team_members_deserialized_common, Vec<crate::test::api_common::models::CommonTeamMember>, team_id: &str, pat: Option<&str>],
|
||||
[get_teams_members, ServiceResponse, ids: &[&str], pat: Option<&str>],
|
||||
[get_project_members, ServiceResponse, id_or_slug: &str, pat: Option<&str>],
|
||||
[get_project_members_deserialized_common, Vec<crate::test::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::test::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::test::api_common::models::CommonNotification>, user_id: &str, pat: Option<&str>],
|
||||
[get_notification, ServiceResponse, notification_id: &str, pat: Option<&str>],
|
||||
[get_notifications, ServiceResponse, ids: &[&str], pat: Option<&str>],
|
||||
[mark_notification_read, ServiceResponse, notification_id: &str, pat: Option<&str>],
|
||||
[mark_notifications_read, ServiceResponse, ids: &[&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>],
|
||||
[delete_notifications, ServiceResponse, ids: &[&str], pat: Option<&str>],
|
||||
}
|
||||
);
|
||||
|
||||
delegate_api_variant!(
|
||||
#[async_trait(?Send)]
|
||||
impl ApiUser for GenericApi {
|
||||
[get_user, ServiceResponse, id_or_username: &str, pat: Option<&str>],
|
||||
[get_current_user, ServiceResponse, pat: Option<&str>],
|
||||
[edit_user, ServiceResponse, id_or_username: &str, patch: serde_json::Value, pat: Option<&str>],
|
||||
[delete_user, ServiceResponse, id_or_username: &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: 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>],
|
||||
[download_version_redirect, ServiceResponse, hash: &str, algorithm: &str, 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>],
|
||||
}
|
||||
);
|
||||
491
apps/labrinth/src/test/api_common/mod.rs
Normal file
491
apps/labrinth/src/test/api_common/mod.rs
Normal file
@@ -0,0 +1,491 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use self::models::{
|
||||
CommonCategoryData, CommonItemType, CommonLoaderData, CommonNotification,
|
||||
CommonProject, CommonTeamMember, CommonVersion,
|
||||
};
|
||||
use self::request_data::{ImageData, ProjectCreationRequestData};
|
||||
use super::dummy_data::TestFile;
|
||||
use crate::models::ids::ProjectId;
|
||||
use crate::{
|
||||
LabrinthConfig,
|
||||
models::{
|
||||
projects::VersionType,
|
||||
teams::{OrganizationPermissions, ProjectPermissions},
|
||||
},
|
||||
};
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use async_trait::async_trait;
|
||||
|
||||
pub mod generic;
|
||||
pub mod models;
|
||||
pub mod request_data;
|
||||
#[async_trait(?Send)]
|
||||
pub trait ApiBuildable: Api {
|
||||
async fn build(labrinth_config: LabrinthConfig) -> Self;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait Api: ApiProject + ApiTags + ApiTeams + ApiUser + ApiVersion {
|
||||
async fn call(&self, req: actix_http::Request) -> ServiceResponse;
|
||||
async fn reset_search_index(&self) -> ServiceResponse;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait ApiProject {
|
||||
async fn add_public_project(
|
||||
&self,
|
||||
slug: &str,
|
||||
version_jar: Option<TestFile>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> (CommonProject, Vec<CommonVersion>);
|
||||
async fn create_project(
|
||||
&self,
|
||||
creation_data: ProjectCreationRequestData,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_public_project_creation_data_json(
|
||||
&self,
|
||||
slug: &str,
|
||||
version_jar: Option<&TestFile>,
|
||||
) -> serde_json::Value;
|
||||
|
||||
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_projects(
|
||||
&self,
|
||||
ids_or_slugs: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_project_dependencies(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
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: Option<&str>,
|
||||
) -> Vec<CommonProject>;
|
||||
async fn edit_project(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn edit_project_bulk(
|
||||
&self,
|
||||
ids_or_slugs: &[&str],
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn edit_project_icon(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
icon: Option<ImageData>,
|
||||
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;
|
||||
async fn create_report(
|
||||
&self,
|
||||
report_type: &str,
|
||||
id: &str,
|
||||
item_type: CommonItemType,
|
||||
body: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_report(&self, id: &str, pat: Option<&str>) -> ServiceResponse;
|
||||
async fn get_reports(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_user_reports(&self, pat: Option<&str>) -> ServiceResponse;
|
||||
async fn edit_report(
|
||||
&self,
|
||||
id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn delete_report(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_thread(&self, id: &str, pat: Option<&str>) -> ServiceResponse;
|
||||
async fn get_threads(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn write_to_thread(
|
||||
&self,
|
||||
id: &str,
|
||||
r#type: &str,
|
||||
message: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_moderation_inbox(&self, pat: Option<&str>) -> ServiceResponse;
|
||||
async fn read_thread(&self, id: &str, pat: Option<&str>)
|
||||
-> ServiceResponse;
|
||||
async fn delete_thread_message(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait ApiTags {
|
||||
async fn get_loaders(&self) -> ServiceResponse;
|
||||
async fn get_loaders_deserialized_common(&self) -> Vec<CommonLoaderData>;
|
||||
async fn get_categories(&self) -> ServiceResponse;
|
||||
async fn get_categories_deserialized_common(
|
||||
&self,
|
||||
) -> Vec<CommonCategoryData>;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait ApiTeams {
|
||||
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: Option<&str>,
|
||||
) -> Vec<CommonTeamMember>;
|
||||
async fn get_teams_members(
|
||||
&self,
|
||||
team_ids: &[&str],
|
||||
pat: Option<&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: Option<&str>,
|
||||
) -> Vec<CommonTeamMember>;
|
||||
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: Option<&str>,
|
||||
) -> Vec<CommonTeamMember>;
|
||||
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: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn transfer_team_ownership(
|
||||
&self,
|
||||
team_id: &str,
|
||||
user_id: &str,
|
||||
pat: Option<&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: Option<&str>,
|
||||
) -> Vec<CommonNotification>;
|
||||
async fn get_notification(
|
||||
&self,
|
||||
notification_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_notifications(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn mark_notification_read(
|
||||
&self,
|
||||
notification_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn mark_notifications_read(
|
||||
&self,
|
||||
ids: &[&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: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn delete_notification(
|
||||
&self,
|
||||
notification_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn delete_notifications(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait ApiUser {
|
||||
async fn get_user(
|
||||
&self,
|
||||
id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_current_user(&self, pat: Option<&str>) -> ServiceResponse;
|
||||
async fn edit_user(
|
||||
&self,
|
||||
id_or_username: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn delete_user(
|
||||
&self,
|
||||
id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait ApiVersion {
|
||||
async fn add_public_version(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn add_public_version_deserialized_common(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion;
|
||||
async fn get_version(&self, id: &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: Vec<String>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_versions_deserialized_common(
|
||||
&self,
|
||||
ids: Vec<String>,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonVersion>;
|
||||
async fn download_version_redirect(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn edit_version(
|
||||
&self,
|
||||
id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_version_from_hash(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_version_from_hash_deserialized_common(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion;
|
||||
async fn get_versions_from_hashes(
|
||||
&self,
|
||||
hashes: &[&str],
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_versions_from_hashes_deserialized_common(
|
||||
&self,
|
||||
hashes: &[&str],
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, CommonVersion>;
|
||||
async fn get_update_from_hash(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn get_update_from_hash_deserialized_common(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion;
|
||||
async fn update_files(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<String>,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse;
|
||||
async fn update_files_deserialized_common(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<String>,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, CommonVersion>;
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn get_project_versions(
|
||||
&self,
|
||||
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>,
|
||||
) -> ServiceResponse;
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn get_project_versions_deserialized_common(
|
||||
&self,
|
||||
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>,
|
||||
) -> Vec<CommonVersion>;
|
||||
async fn edit_version_ordering(
|
||||
&self,
|
||||
version_id: &str,
|
||||
ordering: Option<i32>,
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
231
apps/labrinth/src/test/api_common/models.rs
Normal file
231
apps/labrinth/src/test/api_common/models.rs
Normal file
@@ -0,0 +1,231 @@
|
||||
use crate::models::ids::{
|
||||
ImageId, NotificationId, OrganizationId, ProjectId, ReportId, TeamId,
|
||||
ThreadId, ThreadMessageId, VersionId,
|
||||
};
|
||||
use crate::{
|
||||
auth::AuthProvider,
|
||||
models::{
|
||||
projects::{
|
||||
Dependency, GalleryItem, License, ModeratorMessage,
|
||||
MonetizationStatus, ProjectStatus, VersionFile, VersionStatus,
|
||||
VersionType,
|
||||
},
|
||||
teams::ProjectPermissions,
|
||||
users::{Badges, Role, User, UserPayoutData},
|
||||
},
|
||||
};
|
||||
use ariadne::ids::UserId;
|
||||
use chrono::{DateTime, Utc};
|
||||
use rust_decimal::Decimal;
|
||||
use serde::Deserialize;
|
||||
// Fields shared by every version of the API.
|
||||
// No struct in here should have ANY field that
|
||||
// is not present in *every* version of the API.
|
||||
|
||||
// Exceptions are fields that *should* be changing across the API, and older versions
|
||||
// should be unsupported on API version increase- for example, payouts related financial fields.
|
||||
|
||||
// These are used for common tests- tests that can be used on both V2 AND v3 of the API and have the same results.
|
||||
|
||||
// Any test that requires version-specific fields should have its own test that is not done for each version,
|
||||
// as the environment generator for both uses common fields.
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonProject {
|
||||
// For example, for CommonProject, we do not include:
|
||||
// - game_versions (v2 only)
|
||||
// - loader_fields (v3 only)
|
||||
// - etc.
|
||||
// For any tests that require those fields, we make a separate test with separate API functions that do not use Common models.
|
||||
pub id: ProjectId,
|
||||
pub slug: Option<String>,
|
||||
pub organization: Option<OrganizationId>,
|
||||
pub published: DateTime<Utc>,
|
||||
pub updated: DateTime<Utc>,
|
||||
pub approved: Option<DateTime<Utc>>,
|
||||
pub queued: Option<DateTime<Utc>>,
|
||||
pub status: ProjectStatus,
|
||||
pub requested_status: Option<ProjectStatus>,
|
||||
pub moderator_message: Option<ModeratorMessage>,
|
||||
pub license: License,
|
||||
pub downloads: u32,
|
||||
pub followers: u32,
|
||||
pub categories: Vec<String>,
|
||||
pub additional_categories: Vec<String>,
|
||||
pub loaders: Vec<String>,
|
||||
pub versions: Vec<VersionId>,
|
||||
pub icon_url: Option<String>,
|
||||
pub gallery: Vec<GalleryItem>,
|
||||
pub color: Option<u32>,
|
||||
pub thread_id: ThreadId,
|
||||
pub monetization_status: MonetizationStatus,
|
||||
}
|
||||
#[derive(Deserialize, Clone)]
|
||||
pub struct CommonVersion {
|
||||
pub id: VersionId,
|
||||
pub loaders: Vec<String>,
|
||||
pub project_id: ProjectId,
|
||||
pub author_id: UserId,
|
||||
pub featured: bool,
|
||||
pub name: String,
|
||||
pub version_number: String,
|
||||
pub changelog: String,
|
||||
pub date_published: DateTime<Utc>,
|
||||
pub downloads: u32,
|
||||
pub version_type: VersionType,
|
||||
pub status: VersionStatus,
|
||||
pub requested_status: Option<VersionStatus>,
|
||||
pub files: Vec<VersionFile>,
|
||||
pub dependencies: Vec<Dependency>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonLoaderData {
|
||||
pub icon: String,
|
||||
pub name: String,
|
||||
pub supported_project_types: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonCategoryData {
|
||||
pub icon: String,
|
||||
pub name: String,
|
||||
pub project_type: String,
|
||||
pub header: String,
|
||||
}
|
||||
|
||||
/// A member of a team
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonTeamMember {
|
||||
pub team_id: TeamId,
|
||||
pub user: User,
|
||||
pub role: String,
|
||||
|
||||
pub permissions: Option<ProjectPermissions>,
|
||||
|
||||
pub accepted: bool,
|
||||
pub payouts_split: Option<Decimal>,
|
||||
pub ordering: i64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonNotification {
|
||||
pub id: NotificationId,
|
||||
pub user_id: UserId,
|
||||
pub read: bool,
|
||||
pub created: DateTime<Utc>,
|
||||
// Body is absent as one of the variants differs
|
||||
pub text: String,
|
||||
pub link: String,
|
||||
pub actions: Vec<CommonNotificationAction>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonNotificationAction {
|
||||
pub action_route: (String, String),
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum CommonItemType {
|
||||
Project,
|
||||
Version,
|
||||
User,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl CommonItemType {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
CommonItemType::Project => "project",
|
||||
CommonItemType::Version => "version",
|
||||
CommonItemType::User => "user",
|
||||
CommonItemType::Unknown => "unknown",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonReport {
|
||||
pub id: ReportId,
|
||||
pub report_type: String,
|
||||
pub item_id: String,
|
||||
pub item_type: CommonItemType,
|
||||
pub reporter: UserId,
|
||||
pub body: String,
|
||||
pub created: DateTime<Utc>,
|
||||
pub closed: bool,
|
||||
pub thread_id: ThreadId,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub enum LegacyItemType {
|
||||
Project,
|
||||
Version,
|
||||
User,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonThread {
|
||||
pub id: ThreadId,
|
||||
#[serde(rename = "type")]
|
||||
pub type_: CommonThreadType,
|
||||
pub project_id: Option<ProjectId>,
|
||||
pub report_id: Option<ReportId>,
|
||||
pub messages: Vec<CommonThreadMessage>,
|
||||
pub members: Vec<User>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonThreadMessage {
|
||||
pub id: ThreadMessageId,
|
||||
pub author_id: Option<UserId>,
|
||||
pub body: CommonMessageBody,
|
||||
pub created: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub enum CommonMessageBody {
|
||||
Text {
|
||||
body: String,
|
||||
#[serde(default)]
|
||||
private: bool,
|
||||
replying_to: Option<ThreadMessageId>,
|
||||
#[serde(default)]
|
||||
associated_images: Vec<ImageId>,
|
||||
},
|
||||
StatusChange {
|
||||
new_status: ProjectStatus,
|
||||
old_status: ProjectStatus,
|
||||
},
|
||||
ThreadClosure,
|
||||
ThreadReopen,
|
||||
Deleted,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub enum CommonThreadType {
|
||||
Report,
|
||||
Project,
|
||||
DirectMessage,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CommonUser {
|
||||
pub id: UserId,
|
||||
pub username: String,
|
||||
pub name: Option<String>,
|
||||
pub avatar_url: Option<String>,
|
||||
pub bio: Option<String>,
|
||||
pub created: DateTime<Utc>,
|
||||
pub role: Role,
|
||||
pub badges: Badges,
|
||||
pub auth_providers: Option<Vec<AuthProvider>>,
|
||||
pub email: Option<String>,
|
||||
pub email_verified: Option<bool>,
|
||||
pub has_password: Option<bool>,
|
||||
pub has_totp: Option<bool>,
|
||||
pub payout_data: Option<UserPayoutData>,
|
||||
pub github_id: Option<u64>,
|
||||
}
|
||||
24
apps/labrinth/src/test/api_common/request_data.rs
Normal file
24
apps/labrinth/src/test/api_common/request_data.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
// The structures for project/version creation.
|
||||
// These are created differently, but are essentially the same between versions.
|
||||
|
||||
use crate::util::actix::MultipartSegment;
|
||||
|
||||
use crate::test::dummy_data::TestFile;
|
||||
|
||||
pub struct ProjectCreationRequestData {
|
||||
pub slug: String,
|
||||
pub jar: Option<TestFile>,
|
||||
pub segment_data: Vec<MultipartSegment>,
|
||||
}
|
||||
|
||||
pub struct VersionCreationRequestData {
|
||||
pub version: String,
|
||||
pub jar: Option<TestFile>,
|
||||
pub segment_data: Vec<MultipartSegment>,
|
||||
}
|
||||
|
||||
pub struct ImageData {
|
||||
pub filename: String,
|
||||
pub extension: String,
|
||||
pub icon: Vec<u8>,
|
||||
}
|
||||
56
apps/labrinth/src/test/api_v2/mod.rs
Normal file
56
apps/labrinth/src/test/api_v2/mod.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use super::{
|
||||
api_common::{Api, ApiBuildable},
|
||||
environment::LocalService,
|
||||
};
|
||||
use crate::LabrinthConfig;
|
||||
use actix_web::{App, dev::ServiceResponse, test};
|
||||
use async_trait::async_trait;
|
||||
use std::rc::Rc;
|
||||
use utoipa_actix_web::AppExt;
|
||||
|
||||
pub mod project;
|
||||
pub mod request_data;
|
||||
pub mod tags;
|
||||
pub mod team;
|
||||
pub mod user;
|
||||
pub mod version;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ApiV2 {
|
||||
pub test_app: Rc<dyn LocalService>,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiBuildable for ApiV2 {
|
||||
async fn build(labrinth_config: LabrinthConfig) -> Self {
|
||||
let app = App::new()
|
||||
.into_utoipa_app()
|
||||
.configure(|cfg| {
|
||||
crate::utoipa_app_config(cfg, labrinth_config.clone())
|
||||
})
|
||||
.into_app()
|
||||
.configure(|cfg| crate::app_config(cfg, labrinth_config.clone()));
|
||||
let test_app: Rc<dyn LocalService> =
|
||||
Rc::new(test::init_service(app).await);
|
||||
|
||||
Self { test_app }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Api for ApiV2 {
|
||||
async fn call(&self, req: actix_http::Request) -> ServiceResponse {
|
||||
self.test_app.call(req).await.unwrap()
|
||||
}
|
||||
|
||||
async fn reset_search_index(&self) -> ServiceResponse {
|
||||
let req = actix_web::test::TestRequest::post()
|
||||
.uri("/v2/admin/_force_reindex")
|
||||
.append_header((
|
||||
"Modrinth-Admin",
|
||||
dotenvy::var("LABRINTH_ADMIN_KEY").unwrap(),
|
||||
))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
550
apps/labrinth/src/test/api_v2/project.rs
Normal file
550
apps/labrinth/src/test/api_v2/project.rs
Normal file
@@ -0,0 +1,550 @@
|
||||
use std::{collections::HashMap, fmt::Write};
|
||||
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{
|
||||
Api, ApiProject, AppendsOptionalPat,
|
||||
models::{CommonItemType, CommonProject, CommonVersion},
|
||||
request_data::{ImageData, ProjectCreationRequestData},
|
||||
},
|
||||
dummy_data::TestFile,
|
||||
};
|
||||
use crate::{
|
||||
models::v2::{projects::LegacyProject, search::LegacySearchResults},
|
||||
util::actix::AppendsMultipart,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::test::database::MOD_USER_PAT;
|
||||
|
||||
use super::{
|
||||
ApiV2,
|
||||
request_data::{self, get_public_project_creation_data},
|
||||
};
|
||||
|
||||
impl ApiV2 {
|
||||
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_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_user_projects_deserialized(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<LegacyProject> {
|
||||
let resp = self.get_user_projects(user_id_or_username, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn search_deserialized(
|
||||
&self,
|
||||
query: Option<&str>,
|
||||
facets: Option<serde_json::Value>,
|
||||
pat: Option<&str>,
|
||||
) -> LegacySearchResults {
|
||||
let query_field = if let Some(query) = query {
|
||||
format!("&query={}", urlencoding::encode(query))
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
let facets_field = if let Some(facets) = facets {
|
||||
format!("&facets={}", urlencoding::encode(&facets.to_string()))
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v2/search?{query_field}{facets_field}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiProject for ApiV2 {
|
||||
async fn add_public_project(
|
||||
&self,
|
||||
slug: &str,
|
||||
version_jar: Option<TestFile>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> (CommonProject, Vec<CommonVersion>) {
|
||||
let creation_data =
|
||||
get_public_project_creation_data(slug, version_jar, modify_json);
|
||||
|
||||
// Add a project.
|
||||
let slug = creation_data.slug.clone();
|
||||
let resp = self.create_project(creation_data, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
|
||||
// Approve as a moderator.
|
||||
let req = TestRequest::patch()
|
||||
.uri(&format!("/v2/project/{slug}"))
|
||||
.append_pat(MOD_USER_PAT)
|
||||
.set_json(json!(
|
||||
{
|
||||
"status": "approved"
|
||||
}
|
||||
))
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::NO_CONTENT);
|
||||
|
||||
let project = self.get_project_deserialized_common(&slug, pat).await;
|
||||
|
||||
// Get project's versions
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v2/project/{slug}/version"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
let versions: Vec<CommonVersion> = test::read_body_json(resp).await;
|
||||
|
||||
(project, versions)
|
||||
}
|
||||
|
||||
async fn get_public_project_creation_data_json(
|
||||
&self,
|
||||
slug: &str,
|
||||
version_jar: Option<&TestFile>,
|
||||
) -> serde_json::Value {
|
||||
request_data::get_public_project_creation_data_json(slug, version_jar)
|
||||
}
|
||||
|
||||
async fn create_project(
|
||||
&self,
|
||||
creation_data: ProjectCreationRequestData,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::post()
|
||||
.uri("/v2/project")
|
||||
.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: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v2/project/{project_slug_or_id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let project: LegacyProject = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(project).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_projects(
|
||||
&self,
|
||||
ids_or_slugs: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_or_slugs = serde_json::to_string(ids_or_slugs).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v2/projects?ids={encoded}",
|
||||
encoded = urlencoding::encode(&ids_or_slugs)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_project_dependencies(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v2/project/{id_or_slug}/dependencies"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_projects(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v2/user/{user_id_or_username}/projects"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_projects_deserialized_common(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonProject> {
|
||||
let resp = self.get_user_projects(user_id_or_username, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let projects: Vec<LegacyProject> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(projects).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn edit_project(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v2/project/{id_or_slug}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_project_bulk(
|
||||
&self,
|
||||
ids_or_slugs: &[&str],
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let projects_str = ids_or_slugs
|
||||
.iter()
|
||||
.map(|s| format!("\"{s}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v2/projects?ids={encoded}",
|
||||
encoded = urlencoding::encode(&format!("[{projects_str}]"))
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_project_icon(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
icon: Option<ImageData>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
if let Some(icon) = icon {
|
||||
// If an icon is provided, upload it
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v2/project/{id_or_slug}/icon?ext={ext}",
|
||||
ext = icon.extension
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_payload(Bytes::from(icon.icon))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
} else {
|
||||
// If no icon is provided, delete the icon
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v2/project/{id_or_slug}/icon"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_report(
|
||||
&self,
|
||||
report_type: &str,
|
||||
id: &str,
|
||||
item_type: CommonItemType,
|
||||
body: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v2/report")
|
||||
.append_pat(pat)
|
||||
.set_json(json!(
|
||||
{
|
||||
"report_type": report_type,
|
||||
"item_id": id,
|
||||
"item_type": item_type.as_str(),
|
||||
"body": body,
|
||||
}
|
||||
))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_report(&self, id: &str, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v2/report/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_reports(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_str = serde_json::to_string(ids).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v2/reports?ids={encoded}",
|
||||
encoded = urlencoding::encode(&ids_str)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_reports(&self, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v2/report")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_report(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v2/report/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_report(
|
||||
&self,
|
||||
id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v2/report/{id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_thread(&self, id: &str, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/thread/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_threads(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_str = serde_json::to_string(ids).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/threads?ids={encoded}",
|
||||
encoded = urlencoding::encode(&ids_str)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn write_to_thread(
|
||||
&self,
|
||||
id: &str,
|
||||
r#type: &str,
|
||||
content: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!("/v2/thread/{id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"body" : {
|
||||
"type": r#type,
|
||||
"body": content,
|
||||
}
|
||||
}))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_moderation_inbox(&self, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v2/thread/inbox")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn read_thread(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!("/v2/thread/{id}/read"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_thread_message(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v2/message/{id}"))
|
||||
.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 {
|
||||
write!(&mut url, "&title={title}").unwrap();
|
||||
}
|
||||
if let Some(description) = description {
|
||||
write!(&mut url, "&description={description}").unwrap();
|
||||
}
|
||||
if let Some(ordering) = ordering {
|
||||
write!(&mut url, "&ordering={ordering}").unwrap();
|
||||
}
|
||||
|
||||
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 {
|
||||
write!(
|
||||
&mut url,
|
||||
"&{key}={value}",
|
||||
value = urlencoding::encode(&value)
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
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}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
132
apps/labrinth/src/test/api_v2/request_data.rs
Normal file
132
apps/labrinth/src/test/api_v2/request_data.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
use serde_json::json;
|
||||
|
||||
use crate::models::ids::ProjectId;
|
||||
use crate::test::{
|
||||
api_common::request_data::{
|
||||
ProjectCreationRequestData, VersionCreationRequestData,
|
||||
},
|
||||
dummy_data::TestFile,
|
||||
};
|
||||
use crate::util::actix::{MultipartSegment, MultipartSegmentData};
|
||||
|
||||
pub fn get_public_project_creation_data(
|
||||
slug: &str,
|
||||
version_jar: Option<TestFile>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
) -> ProjectCreationRequestData {
|
||||
let mut json_data =
|
||||
get_public_project_creation_data_json(slug, version_jar.as_ref());
|
||||
if let Some(modify_json) = modify_json {
|
||||
json_patch::patch(&mut json_data, &modify_json).unwrap();
|
||||
}
|
||||
let multipart_data =
|
||||
get_public_creation_data_multipart(&json_data, version_jar.as_ref());
|
||||
ProjectCreationRequestData {
|
||||
slug: slug.to_string(),
|
||||
jar: version_jar,
|
||||
segment_data: multipart_data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_public_version_creation_data(
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
) -> VersionCreationRequestData {
|
||||
let mut json_data = get_public_version_creation_data_json(
|
||||
version_number,
|
||||
ordering,
|
||||
&version_jar,
|
||||
);
|
||||
json_data["project_id"] = json!(project_id);
|
||||
if let Some(modify_json) = modify_json {
|
||||
json_patch::patch(&mut json_data, &modify_json).unwrap();
|
||||
}
|
||||
let multipart_data =
|
||||
get_public_creation_data_multipart(&json_data, Some(&version_jar));
|
||||
VersionCreationRequestData {
|
||||
version: version_number.to_string(),
|
||||
jar: Some(version_jar),
|
||||
segment_data: multipart_data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_public_version_creation_data_json(
|
||||
version_number: &str,
|
||||
ordering: Option<i32>,
|
||||
version_jar: &TestFile,
|
||||
) -> serde_json::Value {
|
||||
let mut j = json!({
|
||||
"file_parts": [version_jar.filename()],
|
||||
"version_number": version_number,
|
||||
"version_title": "start",
|
||||
"dependencies": [],
|
||||
"game_versions": ["1.20.1"] ,
|
||||
"release_channel": "release",
|
||||
"loaders": ["fabric"],
|
||||
"featured": true
|
||||
});
|
||||
if let Some(ordering) = ordering {
|
||||
j["ordering"] = json!(ordering);
|
||||
}
|
||||
j
|
||||
}
|
||||
|
||||
pub fn get_public_project_creation_data_json(
|
||||
slug: &str,
|
||||
version_jar: Option<&TestFile>,
|
||||
) -> serde_json::Value {
|
||||
let initial_versions = if let Some(jar) = version_jar {
|
||||
json!([get_public_version_creation_data_json("1.2.3", None, jar)])
|
||||
} else {
|
||||
json!([])
|
||||
};
|
||||
|
||||
let is_draft = version_jar.is_none();
|
||||
json!(
|
||||
{
|
||||
"title": format!("Test Project {slug}"),
|
||||
"slug": slug,
|
||||
"project_type": version_jar.as_ref().map_or("mod".to_string(), |f| f.project_type()),
|
||||
"description": "A dummy project for testing with.",
|
||||
"body": "This project is approved, and versions are listed.",
|
||||
"client_side": "required",
|
||||
"server_side": "optional",
|
||||
"initial_versions": initial_versions,
|
||||
"is_draft": is_draft,
|
||||
"categories": [],
|
||||
"license_id": "MIT",
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_public_creation_data_multipart(
|
||||
json_data: &serde_json::Value,
|
||||
version_jar: Option<&TestFile>,
|
||||
) -> Vec<MultipartSegment> {
|
||||
// Basic json
|
||||
let json_segment = MultipartSegment {
|
||||
name: "data".to_string(),
|
||||
filename: None,
|
||||
content_type: Some("application/json".to_string()),
|
||||
data: MultipartSegmentData::Text(
|
||||
serde_json::to_string(json_data).unwrap(),
|
||||
),
|
||||
};
|
||||
|
||||
if let Some(jar) = version_jar {
|
||||
// Basic file
|
||||
let file_segment = MultipartSegment {
|
||||
name: jar.filename(),
|
||||
filename: Some(jar.filename()),
|
||||
content_type: Some("application/java-archive".to_string()),
|
||||
data: MultipartSegmentData::Binary(jar.bytes()),
|
||||
};
|
||||
|
||||
vec![json_segment, file_segment]
|
||||
} else {
|
||||
vec![json_segment]
|
||||
}
|
||||
}
|
||||
121
apps/labrinth/src/test/api_v2/tags.rs
Normal file
121
apps/labrinth/src/test/api_v2/tags.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use crate::routes::v2::tags::{
|
||||
CategoryData, DonationPlatformQueryData, GameVersionQueryData, LoaderData,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{
|
||||
Api, ApiTags, AppendsOptionalPat,
|
||||
models::{CommonCategoryData, CommonLoaderData},
|
||||
},
|
||||
database::ADMIN_USER_PAT,
|
||||
};
|
||||
|
||||
use super::ApiV2;
|
||||
|
||||
impl ApiV2 {
|
||||
async fn get_side_types(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v2/tag/side_type")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_side_types_deserialized(&self) -> Vec<String> {
|
||||
let resp = self.get_side_types().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_game_versions(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v2/tag/game_version")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_game_versions_deserialized(
|
||||
&self,
|
||||
) -> Vec<GameVersionQueryData> {
|
||||
let resp = self.get_game_versions().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_loaders_deserialized(&self) -> Vec<LoaderData> {
|
||||
let resp = self.get_loaders().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_categories_deserialized(&self) -> Vec<CategoryData> {
|
||||
let resp = self.get_categories().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_donation_platforms(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v2/tag/donation_platform")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_donation_platforms_deserialized(
|
||||
&self,
|
||||
) -> Vec<DonationPlatformQueryData> {
|
||||
let resp = self.get_donation_platforms().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiTags for ApiV2 {
|
||||
async fn get_loaders(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v2/tag/loader")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_loaders_deserialized_common(&self) -> Vec<CommonLoaderData> {
|
||||
let resp = self.get_loaders().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LoaderData> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_categories(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v2/tag/category")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_categories_deserialized_common(
|
||||
&self,
|
||||
) -> Vec<CommonCategoryData> {
|
||||
let resp = self.get_categories().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<CategoryData> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
}
|
||||
331
apps/labrinth/src/test/api_v2/team.rs
Normal file
331
apps/labrinth/src/test/api_v2/team.rs
Normal file
@@ -0,0 +1,331 @@
|
||||
use crate::models::{
|
||||
teams::{OrganizationPermissions, ProjectPermissions},
|
||||
v2::{notifications::LegacyNotification, teams::LegacyTeamMember},
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{dev::ServiceResponse, test};
|
||||
use async_trait::async_trait;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::test::api_common::{
|
||||
Api, ApiTeams, AppendsOptionalPat,
|
||||
models::{CommonNotification, CommonTeamMember},
|
||||
};
|
||||
use crate::test::asserts::assert_status;
|
||||
|
||||
use super::ApiV2;
|
||||
|
||||
impl ApiV2 {
|
||||
pub async fn get_organization_members_deserialized(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<LegacyTeamMember> {
|
||||
let resp = self.get_organization_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_team_members_deserialized(
|
||||
&self,
|
||||
team_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<LegacyTeamMember> {
|
||||
let resp = self.get_team_members(team_id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_user_notifications_deserialized(
|
||||
&self,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<LegacyNotification> {
|
||||
let resp = self.get_user_notifications(user_id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiTeams for ApiV2 {
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_team_members_deserialized_common(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonTeamMember> {
|
||||
let resp = self.get_team_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LegacyTeamMember> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_teams_members(
|
||||
&self,
|
||||
ids_or_titles: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_or_titles = serde_json::to_string(ids_or_titles).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v2/teams?ids={}",
|
||||
urlencoding::encode(&ids_or_titles)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_project_members_deserialized_common(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonTeamMember> {
|
||||
let resp = self.get_project_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LegacyTeamMember> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_organization_members_deserialized_common(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonTeamMember> {
|
||||
let resp = self.get_organization_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LegacyTeamMember> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_team_member(
|
||||
&self,
|
||||
team_id: &str,
|
||||
user_id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v2/team/{team_id}/members/{user_id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn transfer_team_ownership(
|
||||
&self,
|
||||
team_id: &str,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v2/team/{team_id}/owner"))
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"user_id": user_id,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_notifications_deserialized_common(
|
||||
&self,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonNotification> {
|
||||
let resp = self.get_user_notifications(user_id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LegacyNotification> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_notification(
|
||||
&self,
|
||||
notification_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v2/notification/{notification_id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_notifications(
|
||||
&self,
|
||||
notification_ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let notification_ids = serde_json::to_string(notification_ids).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v2/notifications?ids={}",
|
||||
urlencoding::encode(¬ification_ids)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn mark_notifications_read(
|
||||
&self,
|
||||
notification_ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let notification_ids = serde_json::to_string(notification_ids).unwrap();
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v2/notifications?ids={}",
|
||||
urlencoding::encode(¬ification_ids)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn add_user_to_team(
|
||||
&self,
|
||||
team_id: &str,
|
||||
user_id: &str,
|
||||
project_permissions: Option<ProjectPermissions>,
|
||||
organization_permissions: Option<OrganizationPermissions>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!("/v2/team/{team_id}/members"))
|
||||
.append_pat(pat)
|
||||
.set_json(json!( {
|
||||
"user_id": user_id,
|
||||
"permissions" : project_permissions.map(|p| p.bits()).unwrap_or_default(),
|
||||
"organization_permissions" : organization_permissions.map(|p| p.bits()),
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_notification(
|
||||
&self,
|
||||
notification_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v2/notification/{notification_id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_notifications(
|
||||
&self,
|
||||
notification_ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let notification_ids = serde_json::to_string(notification_ids).unwrap();
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!(
|
||||
"/v2/notifications?ids={}",
|
||||
urlencoding::encode(¬ification_ids)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
55
apps/labrinth/src/test/api_v2/user.rs
Normal file
55
apps/labrinth/src/test/api_v2/user.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use super::ApiV2;
|
||||
use crate::test::api_common::{Api, ApiUser, AppendsOptionalPat};
|
||||
use actix_web::{dev::ServiceResponse, test};
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiUser for ApiV2 {
|
||||
async fn get_user(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v2/user/{user_id_or_username}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_current_user(&self, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v2/user")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_user(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v2/user/{user_id_or_username}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_user(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v2/user/{user_id_or_username}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
545
apps/labrinth/src/test/api_v2/version.rs
Normal file
545
apps/labrinth/src/test/api_v2/version.rs
Normal file
@@ -0,0 +1,545 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Write;
|
||||
|
||||
use super::{
|
||||
ApiV2,
|
||||
request_data::{self, get_public_version_creation_data},
|
||||
};
|
||||
use crate::models::ids::ProjectId;
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{Api, ApiVersion, AppendsOptionalPat, models::CommonVersion},
|
||||
dummy_data::TestFile,
|
||||
};
|
||||
use crate::{
|
||||
models::{projects::VersionType, v2::projects::LegacyVersion},
|
||||
routes::v2::version_file::FileUpdateData,
|
||||
util::actix::AppendsMultipart,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use serde_json::json;
|
||||
|
||||
pub fn url_encode_json_serialized_vec(elements: &[String]) -> String {
|
||||
let serialized = serde_json::to_string(&elements).unwrap();
|
||||
urlencoding::encode(&serialized).to_string()
|
||||
}
|
||||
|
||||
impl ApiV2 {
|
||||
pub async fn get_version_deserialized(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> LegacyVersion {
|
||||
let resp = self.get_version(id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_version_from_hash_deserialized(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> LegacyVersion {
|
||||
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_versions_from_hashes_deserialized(
|
||||
&self,
|
||||
hashes: &[&str],
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, LegacyVersion> {
|
||||
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn update_individual_files(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<FileUpdateData>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v2/version_files/update_individual")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"algorithm": algorithm,
|
||||
"hashes": hashes
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn update_individual_files_deserialized(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<FileUpdateData>,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, LegacyVersion> {
|
||||
let resp = self.update_individual_files(algorithm, hashes, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiVersion for ApiV2 {
|
||||
async fn add_public_version(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let creation_data = get_public_version_creation_data(
|
||||
project_id,
|
||||
version_number,
|
||||
version_jar,
|
||||
ordering,
|
||||
modify_json,
|
||||
);
|
||||
|
||||
// Add a project.
|
||||
let req = TestRequest::post()
|
||||
.uri("/v2/version")
|
||||
.append_pat(pat)
|
||||
.set_multipart(creation_data.segment_data)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn add_public_version_deserialized_common(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self
|
||||
.add_public_version(
|
||||
project_id,
|
||||
version_number,
|
||||
version_jar,
|
||||
ordering,
|
||||
modify_json,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: LegacyVersion = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_version(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v2/version/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_version_deserialized_common(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self.get_version(id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: LegacyVersion = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn download_version_redirect(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v2/version_file/{hash}/download",))
|
||||
.set_json(json!({
|
||||
"algorithm": algorithm,
|
||||
}))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_version(
|
||||
&self,
|
||||
version_id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v2/version/{version_id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_version_from_hash(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v2/version_file/{hash}?algorithm={algorithm}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_version_from_hash_deserialized_common(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: LegacyVersion = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_versions_from_hashes(
|
||||
&self,
|
||||
hashes: &[&str],
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::post()
|
||||
.uri("/v2/version_files")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"hashes": hashes,
|
||||
"algorithm": algorithm,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_versions_from_hashes_deserialized_common(
|
||||
&self,
|
||||
hashes: &[&str],
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, CommonVersion> {
|
||||
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: HashMap<String, LegacyVersion> =
|
||||
test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_update_from_hash(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!(
|
||||
"/v2/version_file/{hash}/update?algorithm={algorithm}"
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"loaders": loaders,
|
||||
"game_versions": game_versions,
|
||||
"version_types": version_types,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_update_from_hash_deserialized_common(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self
|
||||
.get_update_from_hash(
|
||||
hash,
|
||||
algorithm,
|
||||
loaders,
|
||||
game_versions,
|
||||
version_types,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: LegacyVersion = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn update_files(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<String>,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v2/version_files/update")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"algorithm": algorithm,
|
||||
"hashes": hashes,
|
||||
"loaders": loaders,
|
||||
"game_versions": game_versions,
|
||||
"version_types": version_types,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn update_files_deserialized_common(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<String>,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, CommonVersion> {
|
||||
let resp = self
|
||||
.update_files(
|
||||
algorithm,
|
||||
hashes,
|
||||
loaders,
|
||||
game_versions,
|
||||
version_types,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: HashMap<String, LegacyVersion> =
|
||||
test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
// TODO: Not all fields are tested currently in the V2 tests, only the v2-v3 relevant ones are
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn get_project_versions(
|
||||
&self,
|
||||
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>,
|
||||
) -> ServiceResponse {
|
||||
let mut query_string = String::new();
|
||||
if let Some(game_versions) = game_versions {
|
||||
write!(
|
||||
&mut query_string,
|
||||
"&game_versions={}",
|
||||
urlencoding::encode(
|
||||
&serde_json::to_string(&game_versions).unwrap()
|
||||
)
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(loaders) = loaders {
|
||||
write!(
|
||||
&mut query_string,
|
||||
"&loaders={}",
|
||||
urlencoding::encode(&serde_json::to_string(&loaders).unwrap())
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(featured) = featured {
|
||||
write!(&mut query_string, "&featured={featured}").unwrap();
|
||||
}
|
||||
if let Some(version_type) = version_type {
|
||||
write!(&mut query_string, "&version_type={version_type}").unwrap();
|
||||
}
|
||||
if let Some(limit) = limit {
|
||||
let limit = limit.to_string();
|
||||
write!(&mut query_string, "&limit={limit}").unwrap();
|
||||
}
|
||||
if let Some(offset) = offset {
|
||||
let offset = offset.to_string();
|
||||
write!(&mut query_string, "&offset={offset}").unwrap();
|
||||
}
|
||||
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v2/project/{project_id_slug}/version?{}",
|
||||
query_string.trim_start_matches('&')
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn get_project_versions_deserialized_common(
|
||||
&self,
|
||||
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>,
|
||||
) -> Vec<CommonVersion> {
|
||||
let resp = self
|
||||
.get_project_versions(
|
||||
slug,
|
||||
game_versions,
|
||||
loaders,
|
||||
featured,
|
||||
version_type,
|
||||
limit,
|
||||
offset,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LegacyVersion> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn edit_version_ordering(
|
||||
&self,
|
||||
version_id: &str,
|
||||
ordering: Option<i32>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let request = test::TestRequest::patch()
|
||||
.uri(&format!("/v2/version/{version_id}"))
|
||||
.set_json(json!(
|
||||
{
|
||||
"ordering": ordering
|
||||
}
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(request).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(request).await
|
||||
}
|
||||
|
||||
async fn get_versions_deserialized_common(
|
||||
&self,
|
||||
version_ids: Vec<String>,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonVersion> {
|
||||
let resp = self.get_versions(version_ids, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LegacyVersion> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
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
|
||||
}
|
||||
}
|
||||
179
apps/labrinth/src/test/api_v3/collections.rs
Normal file
179
apps/labrinth/src/test/api_v3/collections.rs
Normal file
@@ -0,0 +1,179 @@
|
||||
use crate::models::{collections::Collection, v3::projects::Project};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::test::api_common::{
|
||||
Api, AppendsOptionalPat, request_data::ImageData,
|
||||
};
|
||||
use crate::test::asserts::assert_status;
|
||||
|
||||
use super::ApiV3;
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn create_collection(
|
||||
&self,
|
||||
collection_title: &str,
|
||||
description: &str,
|
||||
projects: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v3/collection")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"name": collection_title,
|
||||
"description": description,
|
||||
"projects": projects,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_collection(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v3/collection/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_collection_deserialized(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Collection {
|
||||
let resp = self.get_collection(id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_collections(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids = serde_json::to_string(ids).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/collections?ids={}",
|
||||
urlencoding::encode(&ids)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_collection_projects(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/collection/{id}/projects"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_collection_projects_deserialized(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<Project> {
|
||||
let resp = self.get_collection_projects(id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn edit_collection(
|
||||
&self,
|
||||
id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/collection/{id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn edit_collection_icon(
|
||||
&self,
|
||||
id: &str,
|
||||
icon: Option<ImageData>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
if let Some(icon) = icon {
|
||||
// If an icon is provided, upload it
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v3/collection/{id}/icon?ext={ext}",
|
||||
ext = icon.extension
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_payload(Bytes::from(icon.icon))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
} else {
|
||||
// If no icon is provided, delete the icon
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/collection/{id}/icon"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_collection(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/collection/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_user_collections(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/user/{user_id_or_username}/collections"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_user_collections_deserialized_common(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<Collection> {
|
||||
let resp = self.get_user_collections(user_id_or_username, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let projects: Vec<Project> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(projects).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
}
|
||||
44
apps/labrinth/src/test/api_v3/limits.rs
Normal file
44
apps/labrinth/src/test/api_v3/limits.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use crate::models::v3::user_limits::UserLimits;
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::test;
|
||||
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{Api, AppendsOptionalPat},
|
||||
api_v3::ApiV3,
|
||||
};
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn get_project_limits(&self, pat: Option<&str>) -> UserLimits {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v3/limits/projects")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_organization_limits(
|
||||
&self,
|
||||
pat: Option<&str>,
|
||||
) -> UserLimits {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v3/limits/organizations")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_collection_limits(&self, pat: Option<&str>) -> UserLimits {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v3/limits/collections")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
61
apps/labrinth/src/test/api_v3/mod.rs
Normal file
61
apps/labrinth/src/test/api_v3/mod.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use super::{
|
||||
api_common::{Api, ApiBuildable},
|
||||
environment::LocalService,
|
||||
};
|
||||
use crate::LabrinthConfig;
|
||||
use actix_web::{App, dev::ServiceResponse, test};
|
||||
use async_trait::async_trait;
|
||||
use std::rc::Rc;
|
||||
use utoipa_actix_web::AppExt;
|
||||
|
||||
pub mod collections;
|
||||
pub mod limits;
|
||||
pub mod oauth;
|
||||
pub mod oauth_clients;
|
||||
pub mod organization;
|
||||
pub mod project;
|
||||
pub mod request_data;
|
||||
pub mod tags;
|
||||
pub mod team;
|
||||
pub mod user;
|
||||
pub mod version;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ApiV3 {
|
||||
pub test_app: Rc<dyn LocalService>,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiBuildable for ApiV3 {
|
||||
async fn build(labrinth_config: LabrinthConfig) -> Self {
|
||||
let app = App::new()
|
||||
.into_utoipa_app()
|
||||
.configure(|cfg| {
|
||||
crate::utoipa_app_config(cfg, labrinth_config.clone())
|
||||
})
|
||||
.into_app()
|
||||
.configure(|cfg| crate::app_config(cfg, labrinth_config.clone()));
|
||||
let test_app: Rc<dyn LocalService> =
|
||||
Rc::new(test::init_service(app).await);
|
||||
|
||||
Self { test_app }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Api for ApiV3 {
|
||||
async fn call(&self, req: actix_http::Request) -> ServiceResponse {
|
||||
self.test_app.call(req).await.unwrap()
|
||||
}
|
||||
|
||||
async fn reset_search_index(&self) -> ServiceResponse {
|
||||
let req = actix_web::test::TestRequest::post()
|
||||
.uri("/_internal/admin/_force_reindex")
|
||||
.append_header((
|
||||
"Modrinth-Admin",
|
||||
dotenvy::var("LABRINTH_ADMIN_KEY").unwrap(),
|
||||
))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
174
apps/labrinth/src/test/api_v3/oauth.rs
Normal file
174
apps/labrinth/src/test/api_v3/oauth.rs
Normal file
@@ -0,0 +1,174 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::auth::oauth::{
|
||||
OAuthClientAccessRequest, RespondToOAuthClientScopes, TokenRequest,
|
||||
TokenResponse,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::http::header::{AUTHORIZATION, LOCATION};
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
|
||||
use crate::test::api_common::{Api, AppendsOptionalPat};
|
||||
use crate::test::asserts::assert_status;
|
||||
|
||||
use super::ApiV3;
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn complete_full_authorize_flow(
|
||||
&self,
|
||||
client_id: &str,
|
||||
client_secret: &str,
|
||||
scope: Option<&str>,
|
||||
redirect_uri: Option<&str>,
|
||||
state: Option<&str>,
|
||||
user_pat: Option<&str>,
|
||||
) -> String {
|
||||
let auth_resp = self
|
||||
.oauth_authorize(client_id, scope, redirect_uri, state, user_pat)
|
||||
.await;
|
||||
let flow_id = get_authorize_accept_flow_id(auth_resp).await;
|
||||
let redirect_resp = self.oauth_accept(&flow_id, user_pat).await;
|
||||
let auth_code =
|
||||
get_auth_code_from_redirect_params(&redirect_resp).await;
|
||||
let token_resp = self
|
||||
.oauth_token(auth_code, None, client_id.to_string(), client_secret)
|
||||
.await;
|
||||
get_access_token(token_resp).await
|
||||
}
|
||||
|
||||
pub async fn oauth_authorize(
|
||||
&self,
|
||||
client_id: &str,
|
||||
scope: Option<&str>,
|
||||
redirect_uri: Option<&str>,
|
||||
state: Option<&str>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let uri = generate_authorize_uri(client_id, scope, redirect_uri, state);
|
||||
let req = TestRequest::get().uri(&uri).append_pat(pat).to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn oauth_accept(
|
||||
&self,
|
||||
flow: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
self.call(
|
||||
TestRequest::post()
|
||||
.uri("/_internal/oauth/accept")
|
||||
.append_pat(pat)
|
||||
.set_json(RespondToOAuthClientScopes {
|
||||
flow: flow.to_string(),
|
||||
})
|
||||
.to_request(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn oauth_reject(
|
||||
&self,
|
||||
flow: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
self.call(
|
||||
TestRequest::post()
|
||||
.uri("/_internal/oauth/reject")
|
||||
.append_pat(pat)
|
||||
.set_json(RespondToOAuthClientScopes {
|
||||
flow: flow.to_string(),
|
||||
})
|
||||
.to_request(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn oauth_token(
|
||||
&self,
|
||||
auth_code: String,
|
||||
original_redirect_uri: Option<String>,
|
||||
client_id: String,
|
||||
client_secret: &str,
|
||||
) -> ServiceResponse {
|
||||
self.call(
|
||||
TestRequest::post()
|
||||
.uri("/_internal/oauth/token")
|
||||
.append_header((AUTHORIZATION, client_secret))
|
||||
.set_form(TokenRequest {
|
||||
grant_type: "authorization_code".to_string(),
|
||||
code: auth_code,
|
||||
redirect_uri: original_redirect_uri,
|
||||
client_id: serde_json::from_str(&format!(
|
||||
"\"{client_id}\""
|
||||
))
|
||||
.unwrap(),
|
||||
})
|
||||
.to_request(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_authorize_uri(
|
||||
client_id: &str,
|
||||
scope: Option<&str>,
|
||||
redirect_uri: Option<&str>,
|
||||
state: Option<&str>,
|
||||
) -> String {
|
||||
format!(
|
||||
"/_internal/oauth/authorize?client_id={}{}{}{}",
|
||||
urlencoding::encode(client_id),
|
||||
optional_query_param("redirect_uri", redirect_uri),
|
||||
optional_query_param("scope", scope),
|
||||
optional_query_param("state", state),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn get_authorize_accept_flow_id(response: ServiceResponse) -> String {
|
||||
assert_status!(&response, StatusCode::OK);
|
||||
test::read_body_json::<OAuthClientAccessRequest, _>(response)
|
||||
.await
|
||||
.flow_id
|
||||
}
|
||||
|
||||
pub async fn get_auth_code_from_redirect_params(
|
||||
response: &ServiceResponse,
|
||||
) -> String {
|
||||
assert_status!(response, StatusCode::OK);
|
||||
let query_params = get_redirect_location_query_params(response);
|
||||
query_params.get("code").unwrap().to_string()
|
||||
}
|
||||
|
||||
pub async fn get_access_token(response: ServiceResponse) -> String {
|
||||
assert_status!(&response, StatusCode::OK);
|
||||
test::read_body_json::<TokenResponse, _>(response)
|
||||
.await
|
||||
.access_token
|
||||
}
|
||||
|
||||
pub fn get_redirect_location_query_params(
|
||||
response: &ServiceResponse,
|
||||
) -> actix_web::web::Query<HashMap<String, String>> {
|
||||
let redirect_location = response
|
||||
.headers()
|
||||
.get(LOCATION)
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
actix_web::web::Query::<HashMap<String, String>>::from_query(
|
||||
redirect_location.split_once('?').unwrap().1,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn optional_query_param(key: &str, value: Option<&str>) -> String {
|
||||
if let Some(val) = value {
|
||||
format!("&{key}={}", urlencoding::encode(val))
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
129
apps/labrinth/src/test/api_v3/oauth_clients.rs
Normal file
129
apps/labrinth/src/test/api_v3/oauth_clients.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
use crate::{
|
||||
models::{
|
||||
oauth_clients::{OAuthClient, OAuthClientAuthorization},
|
||||
pats::Scopes,
|
||||
},
|
||||
routes::v3::oauth_clients::OAuthClientEdit,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::test::api_common::{Api, AppendsOptionalPat};
|
||||
use crate::test::asserts::assert_status;
|
||||
|
||||
use super::ApiV3;
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn add_oauth_client(
|
||||
&self,
|
||||
name: String,
|
||||
max_scopes: Scopes,
|
||||
redirect_uris: Vec<String>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let max_scopes = max_scopes.bits();
|
||||
let req = TestRequest::post()
|
||||
.uri("/_internal/oauth/app")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"name": name,
|
||||
"max_scopes": max_scopes,
|
||||
"redirect_uris": redirect_uris
|
||||
}))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_user_oauth_clients(
|
||||
&self,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<OAuthClient> {
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v3/user/{user_id}/oauth_apps"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn edit_oauth_client(
|
||||
&self,
|
||||
client_id: &str,
|
||||
edit: OAuthClientEdit,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/_internal/oauth/app/{}",
|
||||
urlencoding::encode(client_id)
|
||||
))
|
||||
.set_json(edit)
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_user_oauth_authorizations(
|
||||
&self,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<OAuthClientAuthorization> {
|
||||
let req = TestRequest::get()
|
||||
.uri("/_internal/oauth/authorizations")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
190
apps/labrinth/src/test/api_v3/organization.rs
Normal file
190
apps/labrinth/src/test/api_v3/organization.rs
Normal file
@@ -0,0 +1,190 @@
|
||||
use crate::models::{organizations::Organization, v3::projects::Project};
|
||||
use crate::test::api_common::{
|
||||
Api, AppendsOptionalPat, request_data::ImageData,
|
||||
};
|
||||
use crate::test::asserts::assert_status;
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use ariadne::ids::UserId;
|
||||
use bytes::Bytes;
|
||||
use serde_json::json;
|
||||
|
||||
use super::ApiV3;
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn create_organization(
|
||||
&self,
|
||||
organization_title: &str,
|
||||
organization_slug: &str,
|
||||
description: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v3/organization")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"name": organization_title,
|
||||
"slug": organization_slug,
|
||||
"description": description,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_organization_deserialized(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Organization {
|
||||
let resp = self.get_organization(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_organizations(
|
||||
&self,
|
||||
ids_or_titles: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_or_titles = serde_json::to_string(ids_or_titles).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/organizations?ids={}",
|
||||
urlencoding::encode(&ids_or_titles)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_organization_projects_deserialized(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<Project> {
|
||||
let resp = self.get_organization_projects(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn edit_organization(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/organization/{id_or_title}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn edit_organization_icon(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
icon: Option<ImageData>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
if let Some(icon) = icon {
|
||||
// If an icon is provided, upload it
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v3/organization/{id_or_title}/icon?ext={ext}",
|
||||
ext = icon.extension
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_payload(Bytes::from(icon.icon))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
} else {
|
||||
// If no icon is provided, delete the icon
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/organization/{id_or_title}/icon"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn organization_add_project(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
project_id_or_slug: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!("/v3/organization/{id_or_title}/projects"))
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"project_id": project_id_or_slug,
|
||||
}))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn organization_remove_project(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
project_id_or_slug: &str,
|
||||
new_owner_user_id: UserId,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!(
|
||||
"/v3/organization/{id_or_title}/projects/{project_id_or_slug}"
|
||||
))
|
||||
.set_json(json!({
|
||||
"new_owner": new_owner_user_id,
|
||||
}))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
638
apps/labrinth/src/test/api_v3/project.rs
Normal file
638
apps/labrinth/src/test/api_v3/project.rs
Normal file
@@ -0,0 +1,638 @@
|
||||
use std::{collections::HashMap, fmt::Write};
|
||||
|
||||
use crate::{
|
||||
models::{organizations::Organization, projects::Project},
|
||||
search::SearchResults,
|
||||
util::actix::AppendsMultipart,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use chrono::{DateTime, Utc};
|
||||
use rust_decimal::Decimal;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{
|
||||
Api, ApiProject, AppendsOptionalPat,
|
||||
models::{CommonItemType, CommonProject, CommonVersion},
|
||||
request_data::{ImageData, ProjectCreationRequestData},
|
||||
},
|
||||
database::MOD_USER_PAT,
|
||||
dummy_data::TestFile,
|
||||
};
|
||||
|
||||
use super::{
|
||||
ApiV3,
|
||||
request_data::{self, get_public_project_creation_data},
|
||||
};
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiProject for ApiV3 {
|
||||
async fn add_public_project(
|
||||
&self,
|
||||
slug: &str,
|
||||
version_jar: Option<TestFile>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> (CommonProject, Vec<CommonVersion>) {
|
||||
let creation_data =
|
||||
get_public_project_creation_data(slug, version_jar, modify_json);
|
||||
|
||||
// Add a project.
|
||||
let slug = creation_data.slug.clone();
|
||||
let resp = self.create_project(creation_data, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
|
||||
// Approve as a moderator.
|
||||
let req = TestRequest::patch()
|
||||
.uri(&format!("/v3/project/{slug}"))
|
||||
.append_pat(MOD_USER_PAT)
|
||||
.set_json(json!(
|
||||
{
|
||||
"status": "approved"
|
||||
}
|
||||
))
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::NO_CONTENT);
|
||||
|
||||
let project = self.get_project(&slug, pat).await;
|
||||
let project = test::read_body_json(project).await;
|
||||
|
||||
// Get project's versions
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v3/project/{slug}/version"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
let versions: Vec<CommonVersion> = test::read_body_json(resp).await;
|
||||
|
||||
(project, versions)
|
||||
}
|
||||
|
||||
async fn get_public_project_creation_data_json(
|
||||
&self,
|
||||
slug: &str,
|
||||
version_jar: Option<&TestFile>,
|
||||
) -> serde_json::Value {
|
||||
request_data::get_public_project_creation_data_json(slug, version_jar)
|
||||
}
|
||||
|
||||
async fn create_project(
|
||||
&self,
|
||||
creation_data: ProjectCreationRequestData,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::post()
|
||||
.uri("/v3/project")
|
||||
.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: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/project/{project_slug_or_id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let project: Project = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(project).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_projects(
|
||||
&self,
|
||||
ids_or_slugs: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_or_slugs = serde_json::to_string(ids_or_slugs).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/projects?ids={encoded}",
|
||||
encoded = urlencoding::encode(&ids_or_slugs)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_project_dependencies(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v3/project/{id_or_slug}/dependencies"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_projects(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/user/{user_id_or_username}/projects"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_projects_deserialized_common(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonProject> {
|
||||
let resp = self.get_user_projects(user_id_or_username, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let projects: Vec<Project> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(projects).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn edit_project(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/project/{id_or_slug}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_project_bulk(
|
||||
&self,
|
||||
ids_or_slugs: &[&str],
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let projects_str = ids_or_slugs
|
||||
.iter()
|
||||
.map(|s| format!("\"{s}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v3/projects?ids={encoded}",
|
||||
encoded = urlencoding::encode(&format!("[{projects_str}]"))
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_project_icon(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
icon: Option<ImageData>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
if let Some(icon) = icon {
|
||||
// If an icon is provided, upload it
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v3/project/{id_or_slug}/icon?ext={ext}",
|
||||
ext = icon.extension
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_payload(Bytes::from(icon.icon))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
} else {
|
||||
// If no icon is provided, delete the icon
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/project/{id_or_slug}/icon"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_report(
|
||||
&self,
|
||||
report_type: &str,
|
||||
id: &str,
|
||||
item_type: CommonItemType,
|
||||
body: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v3/report")
|
||||
.append_pat(pat)
|
||||
.set_json(json!(
|
||||
{
|
||||
"report_type": report_type,
|
||||
"item_id": id,
|
||||
"item_type": item_type.as_str(),
|
||||
"body": body,
|
||||
}
|
||||
))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_report(&self, id: &str, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/report/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_reports(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_str = serde_json::to_string(ids).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/reports?ids={encoded}",
|
||||
encoded = urlencoding::encode(&ids_str)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_reports(&self, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v3/report")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_report(
|
||||
&self,
|
||||
id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/report/{id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_report(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/report/{id}"))
|
||||
.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 {
|
||||
write!(&mut url, "&title={title}").unwrap();
|
||||
}
|
||||
if let Some(description) = description {
|
||||
write!(&mut url, "&description={description}").unwrap();
|
||||
}
|
||||
if let Some(ordering) = ordering {
|
||||
write!(&mut url, "&ordering={ordering}").unwrap();
|
||||
}
|
||||
|
||||
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 {
|
||||
write!(
|
||||
&mut url,
|
||||
"&{key}={value}",
|
||||
value = urlencoding::encode(&value)
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
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!("/v3/project/{id_or_slug}/gallery?url={url}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_thread(&self, id: &str, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/thread/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_threads(
|
||||
&self,
|
||||
ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_str = serde_json::to_string(ids).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/threads?ids={encoded}",
|
||||
encoded = urlencoding::encode(&ids_str)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn write_to_thread(
|
||||
&self,
|
||||
id: &str,
|
||||
r#type: &str,
|
||||
content: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!("/v3/thread/{id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"body": {
|
||||
"type": r#type,
|
||||
"body": content
|
||||
}
|
||||
}))
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_moderation_inbox(&self, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v3/thread/inbox")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn read_thread(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!("/v3/thread/{id}/read"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_thread_message(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/message/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
|
||||
impl ApiV3 {
|
||||
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_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_project_organization(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/project/{id_or_slug}/organization"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_project_organization_deserialized(
|
||||
&self,
|
||||
id_or_slug: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Organization {
|
||||
let resp = self.get_project_organization(id_or_slug, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn search_deserialized(
|
||||
&self,
|
||||
query: Option<&str>,
|
||||
facets: Option<serde_json::Value>,
|
||||
pat: Option<&str>,
|
||||
) -> SearchResults {
|
||||
let query_field = if let Some(query) = query {
|
||||
format!("&query={}", urlencoding::encode(query))
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
let facets_field = if let Some(facets) = facets {
|
||||
format!("&facets={}", urlencoding::encode(&facets.to_string()))
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/search?{query_field}{facets_field}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
let resp = self.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_analytics_revenue(
|
||||
&self,
|
||||
id_or_slugs: Vec<&str>,
|
||||
ids_are_version_ids: bool,
|
||||
start_date: Option<DateTime<Utc>>,
|
||||
end_date: Option<DateTime<Utc>>,
|
||||
resolution_minutes: Option<u32>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let pv_string = if ids_are_version_ids {
|
||||
let version_string: String =
|
||||
serde_json::to_string(&id_or_slugs).unwrap();
|
||||
let version_string = urlencoding::encode(&version_string);
|
||||
format!("version_ids={version_string}")
|
||||
} else {
|
||||
let projects_string: String =
|
||||
serde_json::to_string(&id_or_slugs).unwrap();
|
||||
let projects_string = urlencoding::encode(&projects_string);
|
||||
format!("project_ids={projects_string}")
|
||||
};
|
||||
|
||||
let mut extra_args = String::new();
|
||||
if let Some(start_date) = start_date {
|
||||
let start_date = start_date.to_rfc3339();
|
||||
// let start_date = serde_json::to_string(&start_date).unwrap();
|
||||
let start_date = urlencoding::encode(&start_date);
|
||||
write!(&mut extra_args, "&start_date={start_date}").unwrap();
|
||||
}
|
||||
if let Some(end_date) = end_date {
|
||||
let end_date = end_date.to_rfc3339();
|
||||
// let end_date = serde_json::to_string(&end_date).unwrap();
|
||||
let end_date = urlencoding::encode(&end_date);
|
||||
write!(&mut extra_args, "&end_date={end_date}").unwrap();
|
||||
}
|
||||
if let Some(resolution_minutes) = resolution_minutes {
|
||||
write!(&mut extra_args, "&resolution_minutes={resolution_minutes}")
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/analytics/revenue?{pv_string}{extra_args}",))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_analytics_revenue_deserialized(
|
||||
&self,
|
||||
id_or_slugs: Vec<&str>,
|
||||
ids_are_version_ids: bool,
|
||||
start_date: Option<DateTime<Utc>>,
|
||||
end_date: Option<DateTime<Utc>>,
|
||||
resolution_minutes: Option<u32>,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, HashMap<i64, Decimal>> {
|
||||
let resp = self
|
||||
.get_analytics_revenue(
|
||||
id_or_slugs,
|
||||
ids_are_version_ids,
|
||||
start_date,
|
||||
end_date,
|
||||
resolution_minutes,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
139
apps/labrinth/src/test/api_v3/request_data.rs
Normal file
139
apps/labrinth/src/test/api_v3/request_data.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
use serde_json::json;
|
||||
|
||||
use crate::models::ids::ProjectId;
|
||||
use crate::test::{
|
||||
api_common::request_data::{
|
||||
ProjectCreationRequestData, VersionCreationRequestData,
|
||||
},
|
||||
dummy_data::TestFile,
|
||||
};
|
||||
use crate::util::actix::{MultipartSegment, MultipartSegmentData};
|
||||
|
||||
pub fn get_public_project_creation_data(
|
||||
slug: &str,
|
||||
version_jar: Option<TestFile>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
) -> ProjectCreationRequestData {
|
||||
let mut json_data =
|
||||
get_public_project_creation_data_json(slug, version_jar.as_ref());
|
||||
if let Some(modify_json) = modify_json {
|
||||
json_patch::patch(&mut json_data, &modify_json).unwrap();
|
||||
}
|
||||
let multipart_data =
|
||||
get_public_creation_data_multipart(&json_data, version_jar.as_ref());
|
||||
ProjectCreationRequestData {
|
||||
slug: slug.to_string(),
|
||||
jar: version_jar,
|
||||
segment_data: multipart_data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_public_version_creation_data(
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
// closure that takes in a &mut serde_json::Value
|
||||
// and modifies it before it is serialized and sent
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
) -> VersionCreationRequestData {
|
||||
let mut json_data = get_public_version_creation_data_json(
|
||||
version_number,
|
||||
ordering,
|
||||
&version_jar,
|
||||
);
|
||||
json_data["project_id"] = json!(project_id);
|
||||
if let Some(modify_json) = modify_json {
|
||||
json_patch::patch(&mut json_data, &modify_json).unwrap();
|
||||
}
|
||||
|
||||
let multipart_data =
|
||||
get_public_creation_data_multipart(&json_data, Some(&version_jar));
|
||||
VersionCreationRequestData {
|
||||
version: version_number.to_string(),
|
||||
jar: Some(version_jar),
|
||||
segment_data: multipart_data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_public_version_creation_data_json(
|
||||
version_number: &str,
|
||||
ordering: Option<i32>,
|
||||
version_jar: &TestFile,
|
||||
) -> serde_json::Value {
|
||||
let is_modpack = version_jar.project_type() == "modpack";
|
||||
let mut j = json!({
|
||||
"file_parts": [version_jar.filename()],
|
||||
"version_number": version_number,
|
||||
"version_title": "start",
|
||||
"dependencies": [],
|
||||
"release_channel": "release",
|
||||
"loaders": [if is_modpack { "mrpack" } else { "fabric" }],
|
||||
"featured": true,
|
||||
|
||||
// Loader fields
|
||||
"game_versions": ["1.20.1"],
|
||||
"environment": "client_only_server_optional",
|
||||
});
|
||||
if is_modpack {
|
||||
j["mrpack_loaders"] = json!(["fabric"]);
|
||||
}
|
||||
if let Some(ordering) = ordering {
|
||||
j["ordering"] = json!(ordering);
|
||||
}
|
||||
j
|
||||
}
|
||||
|
||||
pub fn get_public_project_creation_data_json(
|
||||
slug: &str,
|
||||
version_jar: Option<&TestFile>,
|
||||
) -> serde_json::Value {
|
||||
let initial_versions = if let Some(jar) = version_jar {
|
||||
json!([get_public_version_creation_data_json("1.2.3", None, jar)])
|
||||
} else {
|
||||
json!([])
|
||||
};
|
||||
|
||||
let is_draft = version_jar.is_none();
|
||||
json!(
|
||||
{
|
||||
"name": format!("Test Project {slug}"),
|
||||
"slug": slug,
|
||||
"summary": "A dummy project for testing with.",
|
||||
"description": "This project is approved, and versions are listed.",
|
||||
"initial_versions": initial_versions,
|
||||
"is_draft": is_draft,
|
||||
"categories": [],
|
||||
"license_id": "MIT",
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_public_creation_data_multipart(
|
||||
json_data: &serde_json::Value,
|
||||
version_jar: Option<&TestFile>,
|
||||
) -> Vec<MultipartSegment> {
|
||||
// Basic json
|
||||
let json_segment = MultipartSegment {
|
||||
name: "data".to_string(),
|
||||
filename: None,
|
||||
content_type: Some("application/json".to_string()),
|
||||
data: MultipartSegmentData::Text(
|
||||
serde_json::to_string(json_data).unwrap(),
|
||||
),
|
||||
};
|
||||
|
||||
if let Some(jar) = version_jar {
|
||||
// Basic file
|
||||
let file_segment = MultipartSegment {
|
||||
name: jar.filename(),
|
||||
filename: Some(jar.filename()),
|
||||
content_type: Some("application/java-archive".to_string()),
|
||||
data: MultipartSegmentData::Binary(jar.bytes()),
|
||||
};
|
||||
|
||||
vec![json_segment, file_segment]
|
||||
} else {
|
||||
vec![json_segment]
|
||||
}
|
||||
}
|
||||
105
apps/labrinth/src/test/api_v3/tags.rs
Normal file
105
apps/labrinth/src/test/api_v3/tags.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use crate::routes::v3::tags::{GameData, LoaderData};
|
||||
use crate::{
|
||||
database::models::loader_fields::LoaderFieldEnumValue,
|
||||
routes::v3::tags::CategoryData,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{
|
||||
Api, ApiTags, AppendsOptionalPat,
|
||||
models::{CommonCategoryData, CommonLoaderData},
|
||||
},
|
||||
database::ADMIN_USER_PAT,
|
||||
};
|
||||
|
||||
use super::ApiV3;
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiTags for ApiV3 {
|
||||
async fn get_loaders(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/tag/loader")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_loaders_deserialized_common(&self) -> Vec<CommonLoaderData> {
|
||||
let resp = self.get_loaders().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<LoaderData> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_categories(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/tag/category")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_categories_deserialized_common(
|
||||
&self,
|
||||
) -> Vec<CommonCategoryData> {
|
||||
let resp = self.get_categories().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<CategoryData> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn get_loaders_deserialized(&self) -> Vec<LoaderData> {
|
||||
let resp = self.get_loaders().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
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_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_loader_field_variants_deserialized(
|
||||
&self,
|
||||
loader_field: &str,
|
||||
) -> Vec<LoaderFieldEnumValue> {
|
||||
let resp = self.get_loader_field_variants(loader_field).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
async fn get_games(&self) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/games")
|
||||
.append_pat(ADMIN_USER_PAT)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn get_games_deserialized(&self) -> Vec<GameData> {
|
||||
let resp = self.get_games().await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
331
apps/labrinth/src/test/api_v3/team.rs
Normal file
331
apps/labrinth/src/test/api_v3/team.rs
Normal file
@@ -0,0 +1,331 @@
|
||||
use crate::models::{
|
||||
notifications::Notification,
|
||||
teams::{OrganizationPermissions, ProjectPermissions, TeamMember},
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{dev::ServiceResponse, test};
|
||||
use async_trait::async_trait;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::test::api_common::{
|
||||
Api, ApiTeams, AppendsOptionalPat,
|
||||
models::{CommonNotification, CommonTeamMember},
|
||||
};
|
||||
use crate::test::asserts::assert_status;
|
||||
|
||||
use super::ApiV3;
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn get_organization_members_deserialized(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<TeamMember> {
|
||||
let resp = self.get_organization_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
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_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_project_members_deserialized(
|
||||
&self,
|
||||
project_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<TeamMember> {
|
||||
let resp = self.get_project_members(project_id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiTeams for ApiV3 {
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_team_members_deserialized_common(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonTeamMember> {
|
||||
let resp = self.get_team_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<TeamMember> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_teams_members(
|
||||
&self,
|
||||
ids_or_titles: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let ids_or_titles = serde_json::to_string(ids_or_titles).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/teams?ids={}",
|
||||
urlencoding::encode(&ids_or_titles)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_project_members_deserialized_common(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonTeamMember> {
|
||||
let resp = self.get_project_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<TeamMember> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_organization_members_deserialized_common(
|
||||
&self,
|
||||
id_or_title: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonTeamMember> {
|
||||
let resp = self.get_organization_members(id_or_title, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<TeamMember> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_team_member(
|
||||
&self,
|
||||
team_id: &str,
|
||||
user_id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/team/{team_id}/members/{user_id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn transfer_team_ownership(
|
||||
&self,
|
||||
team_id: &str,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/team/{team_id}/owner"))
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"user_id": user_id,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_user_notifications_deserialized_common(
|
||||
&self,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonNotification> {
|
||||
let resp = self.get_user_notifications(user_id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<Notification> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_notification(
|
||||
&self,
|
||||
notification_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/notification/{notification_id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_notifications(
|
||||
&self,
|
||||
notification_ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let notification_ids = serde_json::to_string(notification_ids).unwrap();
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/notifications?ids={}",
|
||||
urlencoding::encode(¬ification_ids)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn mark_notifications_read(
|
||||
&self,
|
||||
notification_ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let notification_ids = serde_json::to_string(notification_ids).unwrap();
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!(
|
||||
"/v3/notifications?ids={}",
|
||||
urlencoding::encode(¬ification_ids)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn add_user_to_team(
|
||||
&self,
|
||||
team_id: &str,
|
||||
user_id: &str,
|
||||
project_permissions: Option<ProjectPermissions>,
|
||||
organization_permissions: Option<OrganizationPermissions>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!("/v3/team/{team_id}/members"))
|
||||
.append_pat(pat)
|
||||
.set_json(json!( {
|
||||
"user_id": user_id,
|
||||
"permissions" : project_permissions.map(|p| p.bits()).unwrap_or_default(),
|
||||
"organization_permissions" : organization_permissions.map(|p| p.bits()),
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_notification(
|
||||
&self,
|
||||
notification_id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/notification/{notification_id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_notifications(
|
||||
&self,
|
||||
notification_ids: &[&str],
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let notification_ids = serde_json::to_string(notification_ids).unwrap();
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!(
|
||||
"/v3/notifications?ids={}",
|
||||
urlencoding::encode(¬ification_ids)
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
56
apps/labrinth/src/test/api_v3/user.rs
Normal file
56
apps/labrinth/src/test/api_v3/user.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use actix_web::{dev::ServiceResponse, test};
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::test::api_common::{Api, ApiUser, AppendsOptionalPat};
|
||||
|
||||
use super::ApiV3;
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiUser for ApiV3 {
|
||||
async fn get_user(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/user/{user_id_or_username}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_current_user(&self, pat: Option<&str>) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/v3/user")
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn edit_user(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/user/{user_id_or_username}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn delete_user(
|
||||
&self,
|
||||
user_id_or_username: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::delete()
|
||||
.uri(&format!("/v3/user/{user_id_or_username}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
}
|
||||
578
apps/labrinth/src/test/api_v3/version.rs
Normal file
578
apps/labrinth/src/test/api_v3/version.rs
Normal file
@@ -0,0 +1,578 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Write;
|
||||
|
||||
use super::{
|
||||
ApiV3,
|
||||
request_data::{self, get_public_version_creation_data},
|
||||
};
|
||||
use crate::models::ids::ProjectId;
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{Api, ApiVersion, AppendsOptionalPat, models::CommonVersion},
|
||||
dummy_data::TestFile,
|
||||
};
|
||||
use crate::{
|
||||
models::{projects::VersionType, v3::projects::Version},
|
||||
routes::v3::version_file::FileUpdateData,
|
||||
util::actix::AppendsMultipart,
|
||||
};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
test::{self, TestRequest},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use serde_json::json;
|
||||
|
||||
pub fn url_encode_json_serialized_vec(elements: &[String]) -> String {
|
||||
let serialized = serde_json::to_string(&elements).unwrap();
|
||||
urlencoding::encode(&serialized).to_string()
|
||||
}
|
||||
|
||||
impl ApiV3 {
|
||||
pub async fn add_public_version_deserialized(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> Version {
|
||||
let resp = self
|
||||
.add_public_version(
|
||||
project_id,
|
||||
version_number,
|
||||
version_jar,
|
||||
ordering,
|
||||
modify_json,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
let value: serde_json::Value = test::read_body_json(resp).await;
|
||||
let version_id = value["id"].as_str().unwrap();
|
||||
let version = self.get_version(version_id, pat).await;
|
||||
assert_status!(&version, StatusCode::OK);
|
||||
test::read_body_json(version).await
|
||||
}
|
||||
|
||||
pub async fn get_version_deserialized(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> Version {
|
||||
let resp = self.get_version(id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn get_versions_deserialized(
|
||||
&self,
|
||||
version_ids: Vec<String>,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<Version> {
|
||||
let resp = self.get_versions(version_ids, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
|
||||
pub async fn update_individual_files(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<FileUpdateData>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v3/version_files/update_individual")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"algorithm": algorithm,
|
||||
"hashes": hashes
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
pub async fn update_individual_files_deserialized(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<FileUpdateData>,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, Version> {
|
||||
let resp = self.update_individual_files(algorithm, hashes, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
test::read_body_json(resp).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ApiVersion for ApiV3 {
|
||||
async fn add_public_version(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let creation_data = get_public_version_creation_data(
|
||||
project_id,
|
||||
version_number,
|
||||
version_jar,
|
||||
ordering,
|
||||
modify_json,
|
||||
);
|
||||
|
||||
// Add a version.
|
||||
let req = TestRequest::post()
|
||||
.uri("/v3/version")
|
||||
.append_pat(pat)
|
||||
.set_multipart(creation_data.segment_data)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn add_public_version_deserialized_common(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
version_number: &str,
|
||||
version_jar: TestFile,
|
||||
ordering: Option<i32>,
|
||||
modify_json: Option<json_patch::Patch>,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self
|
||||
.add_public_version(
|
||||
project_id,
|
||||
version_number,
|
||||
version_jar,
|
||||
ordering,
|
||||
modify_json,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Version = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_version(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/v3/version/{id}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_version_deserialized_common(
|
||||
&self,
|
||||
id: &str,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self.get_version(id, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Version = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn edit_version(
|
||||
&self,
|
||||
version_id: &str,
|
||||
patch: serde_json::Value,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/version/{version_id}"))
|
||||
.append_pat(pat)
|
||||
.set_json(patch)
|
||||
.to_request();
|
||||
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn download_version_redirect(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/version_file/{hash}/download",))
|
||||
.set_json(json!({
|
||||
"algorithm": algorithm,
|
||||
}))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_version_from_hash(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!("/v3/version_file/{hash}?algorithm={algorithm}"))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_version_from_hash_deserialized_common(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Version = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_versions_from_hashes(
|
||||
&self,
|
||||
hashes: &[&str],
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let req = TestRequest::post()
|
||||
.uri("/v3/version_files")
|
||||
.append_pat(pat)
|
||||
.set_json(json!({
|
||||
"hashes": hashes,
|
||||
"algorithm": algorithm,
|
||||
}))
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_versions_from_hashes_deserialized_common(
|
||||
&self,
|
||||
hashes: &[&str],
|
||||
algorithm: &str,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, CommonVersion> {
|
||||
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: HashMap<String, Version> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn get_update_from_hash(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let mut json = json!({});
|
||||
if let Some(loaders) = loaders {
|
||||
json["loaders"] = serde_json::to_value(loaders).unwrap();
|
||||
}
|
||||
if let Some(game_versions) = game_versions {
|
||||
json["loader_fields"] = json!({
|
||||
"game_versions": game_versions,
|
||||
});
|
||||
}
|
||||
if let Some(version_types) = version_types {
|
||||
json["version_types"] =
|
||||
serde_json::to_value(version_types).unwrap();
|
||||
}
|
||||
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&format!(
|
||||
"/v3/version_file/{hash}/update?algorithm={algorithm}"
|
||||
))
|
||||
.append_pat(pat)
|
||||
.set_json(json)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn get_update_from_hash_deserialized_common(
|
||||
&self,
|
||||
hash: &str,
|
||||
algorithm: &str,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> CommonVersion {
|
||||
let resp = self
|
||||
.get_update_from_hash(
|
||||
hash,
|
||||
algorithm,
|
||||
loaders,
|
||||
game_versions,
|
||||
version_types,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Version = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn update_files(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<String>,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let mut json = json!({
|
||||
"algorithm": algorithm,
|
||||
"hashes": hashes,
|
||||
});
|
||||
if let Some(loaders) = loaders {
|
||||
json["loaders"] = serde_json::to_value(loaders).unwrap();
|
||||
}
|
||||
if let Some(game_versions) = game_versions {
|
||||
json["game_versions"] =
|
||||
serde_json::to_value(game_versions).unwrap();
|
||||
}
|
||||
if let Some(version_types) = version_types {
|
||||
json["version_types"] =
|
||||
serde_json::to_value(version_types).unwrap();
|
||||
}
|
||||
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/v3/version_files/update")
|
||||
.append_pat(pat)
|
||||
.set_json(json)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
async fn update_files_deserialized_common(
|
||||
&self,
|
||||
algorithm: &str,
|
||||
hashes: Vec<String>,
|
||||
loaders: Option<Vec<String>>,
|
||||
game_versions: Option<Vec<String>>,
|
||||
version_types: Option<Vec<String>>,
|
||||
pat: Option<&str>,
|
||||
) -> HashMap<String, CommonVersion> {
|
||||
let resp = self
|
||||
.update_files(
|
||||
algorithm,
|
||||
hashes,
|
||||
loaders,
|
||||
game_versions,
|
||||
version_types,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: HashMap<String, Version> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
// TODO: Not all fields are tested currently in the v3 tests, only the v2-v3 relevant ones are
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn get_project_versions(
|
||||
&self,
|
||||
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>,
|
||||
) -> ServiceResponse {
|
||||
let mut query_string = String::new();
|
||||
if let Some(game_versions) = game_versions {
|
||||
write!(
|
||||
&mut query_string,
|
||||
"&game_versions={}",
|
||||
urlencoding::encode(
|
||||
&serde_json::to_string(&game_versions).unwrap()
|
||||
)
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(loaders) = loaders {
|
||||
write!(
|
||||
&mut query_string,
|
||||
"&loaders={}",
|
||||
urlencoding::encode(&serde_json::to_string(&loaders).unwrap())
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(featured) = featured {
|
||||
write!(&mut query_string, "&featured={featured}").unwrap();
|
||||
}
|
||||
if let Some(version_type) = version_type {
|
||||
write!(&mut query_string, "&version_type={version_type}").unwrap();
|
||||
}
|
||||
if let Some(limit) = limit {
|
||||
let limit = limit.to_string();
|
||||
write!(&mut query_string, "&limit={limit}").unwrap();
|
||||
}
|
||||
if let Some(offset) = offset {
|
||||
let offset = offset.to_string();
|
||||
write!(&mut query_string, "&offset={offset}").unwrap();
|
||||
}
|
||||
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&format!(
|
||||
"/v3/project/{project_id_slug}/version?{}",
|
||||
query_string.trim_start_matches('&')
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(req).await
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn get_project_versions_deserialized_common(
|
||||
&self,
|
||||
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>,
|
||||
) -> Vec<CommonVersion> {
|
||||
let resp = self
|
||||
.get_project_versions(
|
||||
slug,
|
||||
game_versions,
|
||||
loaders,
|
||||
featured,
|
||||
version_type,
|
||||
limit,
|
||||
offset,
|
||||
pat,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<Version> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
let value = serde_json::to_value(v).unwrap();
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
||||
async fn edit_version_ordering(
|
||||
&self,
|
||||
version_id: &str,
|
||||
ordering: Option<i32>,
|
||||
pat: Option<&str>,
|
||||
) -> ServiceResponse {
|
||||
let request = test::TestRequest::patch()
|
||||
.uri(&format!("/v3/version/{version_id}"))
|
||||
.set_json(json!(
|
||||
{
|
||||
"ordering": ordering
|
||||
}
|
||||
))
|
||||
.append_pat(pat)
|
||||
.to_request();
|
||||
self.call(request).await
|
||||
}
|
||||
|
||||
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_pat(pat)
|
||||
.to_request();
|
||||
self.call(request).await
|
||||
}
|
||||
|
||||
async fn get_versions_deserialized_common(
|
||||
&self,
|
||||
version_ids: Vec<String>,
|
||||
pat: Option<&str>,
|
||||
) -> Vec<CommonVersion> {
|
||||
let resp = self.get_versions(version_ids, pat).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
// First, deserialize to the non-common format (to test the response is valid for this api version)
|
||||
let v: Vec<Version> = test::read_body_json(resp).await;
|
||||
// Then, deserialize to the common format
|
||||
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"))
|
||||
.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}"))
|
||||
.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
|
||||
}
|
||||
}
|
||||
37
apps/labrinth/src/test/asserts.rs
Normal file
37
apps/labrinth/src/test/asserts.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use crate::models::v3::projects::Version;
|
||||
use crate::test::get_json_val_str;
|
||||
use itertools::Itertools;
|
||||
|
||||
use super::api_common::models::CommonVersion;
|
||||
|
||||
macro_rules! assert_status {
|
||||
($response:expr, $status:expr) => {
|
||||
assert_eq!(
|
||||
$response.status(),
|
||||
$status,
|
||||
"{:#?}",
|
||||
$response.response().body()
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use assert_status;
|
||||
|
||||
pub fn assert_version_ids(versions: &[Version], expected_ids: Vec<String>) {
|
||||
let version_ids = versions
|
||||
.iter()
|
||||
.map(|v| get_json_val_str(v.id))
|
||||
.collect_vec();
|
||||
assert_eq!(version_ids, expected_ids);
|
||||
}
|
||||
|
||||
pub fn assert_common_version_ids(
|
||||
versions: &[CommonVersion],
|
||||
expected_ids: Vec<String>,
|
||||
) {
|
||||
let version_ids = versions
|
||||
.iter()
|
||||
.map(|v| get_json_val_str(v.id))
|
||||
.collect_vec();
|
||||
assert_eq!(version_ids, expected_ids);
|
||||
}
|
||||
275
apps/labrinth/src/test/database.rs
Normal file
275
apps/labrinth/src/test/database.rs
Normal file
@@ -0,0 +1,275 @@
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::database::{MIGRATOR, ReadOnlyPgPool};
|
||||
use crate::search;
|
||||
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
|
||||
use crate::test::{dummy_data, environment::TestEnvironment};
|
||||
|
||||
use super::{api_v3::ApiV3, dummy_data::DUMMY_DATA_UPDATE};
|
||||
|
||||
// The dummy test database adds a fair bit of 'dummy' data to test with.
|
||||
// Some constants are used to refer to that data, and are described here.
|
||||
// The rest can be accessed in the TestEnvironment 'dummy' field.
|
||||
|
||||
// The user IDs are as follows:
|
||||
pub const ADMIN_USER_ID: &str = "1";
|
||||
pub const MOD_USER_ID: &str = "2";
|
||||
pub const USER_USER_ID: &str = "3"; // This is the 'main' user ID, and is used for most tests.
|
||||
pub const FRIEND_USER_ID: &str = "4"; // This is exactly the same as USER_USER_ID, but could be used for testing friend-only endpoints (ie: teams, etc)
|
||||
pub const ENEMY_USER_ID: &str = "5"; // This is exactly the same as USER_USER_ID, but could be used for testing friend-only endpoints (ie: teams, etc)
|
||||
|
||||
pub const ADMIN_USER_ID_PARSED: i64 = 1;
|
||||
pub const MOD_USER_ID_PARSED: i64 = 2;
|
||||
pub const USER_USER_ID_PARSED: i64 = 3;
|
||||
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: 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";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TemporaryDatabase {
|
||||
pub pool: PgPool,
|
||||
pub ro_pool: ReadOnlyPgPool,
|
||||
pub redis_pool: RedisPool,
|
||||
pub search_config: crate::search::SearchConfig,
|
||||
pub database_name: String,
|
||||
}
|
||||
|
||||
impl TemporaryDatabase {
|
||||
// Creates a temporary database like sqlx::test does (panics)
|
||||
// 1. Logs into the main database
|
||||
// 2. Creates a new randomly generated database
|
||||
// 3. Runs migrations on the new database
|
||||
// 4. (Optionally, by using create_with_dummy) adds dummy data to the database
|
||||
// If a db is created with create_with_dummy, it must be cleaned up with cleanup.
|
||||
// This means that dbs will only 'remain' if a test fails (for examination of the db), and will be cleaned up otherwise.
|
||||
pub async fn create(max_connections: Option<u32>) -> Self {
|
||||
let temp_database_name = generate_random_name("labrinth_tests_db_");
|
||||
println!("Creating temporary database: {}", &temp_database_name);
|
||||
|
||||
let database_url =
|
||||
dotenvy::var("DATABASE_URL").expect("No database URL");
|
||||
|
||||
// Create the temporary (and template database, if needed)
|
||||
Self::create_temporary(&database_url, &temp_database_name).await;
|
||||
|
||||
// Pool to the temporary database
|
||||
let mut temporary_url =
|
||||
Url::parse(&database_url).expect("Invalid database URL");
|
||||
|
||||
temporary_url.set_path(&format!("/{}", &temp_database_name));
|
||||
let temp_db_url = temporary_url.to_string();
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.min_connections(0)
|
||||
.max_connections(max_connections.unwrap_or(4))
|
||||
.max_lifetime(Some(Duration::from_secs(60)))
|
||||
.connect(&temp_db_url)
|
||||
.await
|
||||
.expect("Connection to temporary database failed");
|
||||
|
||||
let ro_pool = ReadOnlyPgPool::from(pool.clone());
|
||||
|
||||
println!("Running migrations on temporary database");
|
||||
|
||||
// Performs migrations
|
||||
MIGRATOR.run(&pool).await.expect("Migrations failed");
|
||||
|
||||
println!("Migrations complete");
|
||||
|
||||
// Gets new Redis pool
|
||||
let redis_pool = RedisPool::new(Some(temp_database_name.clone()));
|
||||
|
||||
// Create new meilisearch config
|
||||
let search_config =
|
||||
search::SearchConfig::new(Some(temp_database_name.clone()));
|
||||
Self {
|
||||
pool,
|
||||
ro_pool,
|
||||
database_name: temp_database_name,
|
||||
redis_pool,
|
||||
search_config,
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a template and temporary database (panics)
|
||||
// 1. Waits to obtain a pg lock on the main database
|
||||
// 2. Creates a new template database called 'TEMPLATE_DATABASE_NAME', if needed
|
||||
// 3. Switches to the template database
|
||||
// 4. Runs migrations on the new database (for most tests, this should not take time)
|
||||
// 5. Creates dummy data on the new db
|
||||
// 6. Creates a temporary database at 'temp_database_name' from the template
|
||||
// 7. Drops lock and all created connections in the function
|
||||
async fn create_temporary(database_url: &str, temp_database_name: &str) {
|
||||
let main_pool = PgPool::connect(database_url)
|
||||
.await
|
||||
.expect("Connection to database failed");
|
||||
|
||||
loop {
|
||||
// Try to acquire an advisory lock
|
||||
let lock_acquired: bool =
|
||||
sqlx::query_scalar("SELECT pg_try_advisory_lock(1)")
|
||||
.fetch_one(&main_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if lock_acquired {
|
||||
// Create the db template if it doesn't exist
|
||||
// Check if template_db already exists
|
||||
let db_exists: Option<i32> = sqlx::query_scalar(&format!(
|
||||
"SELECT 1 FROM pg_database WHERE datname = '{TEMPLATE_DATABASE_NAME}'"
|
||||
))
|
||||
.fetch_optional(&main_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
if db_exists.is_none() {
|
||||
create_template_database(&main_pool).await;
|
||||
}
|
||||
|
||||
// Switch to template
|
||||
let url =
|
||||
dotenvy::var("DATABASE_URL").expect("No database URL");
|
||||
let mut template_url =
|
||||
Url::parse(&url).expect("Invalid database URL");
|
||||
template_url.set_path(&format!("/{TEMPLATE_DATABASE_NAME}"));
|
||||
|
||||
let pool = PgPool::connect(template_url.as_str())
|
||||
.await
|
||||
.expect("Connection to database failed");
|
||||
|
||||
// Check if dummy data exists- a fake 'dummy_data' table is created if it does
|
||||
let mut dummy_data_exists: bool = sqlx::query_scalar(
|
||||
"SELECT to_regclass('dummy_data') IS NOT NULL",
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
if dummy_data_exists {
|
||||
// Check if the dummy data needs to be updated
|
||||
let dummy_data_update = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT update_id FROM dummy_data",
|
||||
)
|
||||
.fetch_optional(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let needs_update = dummy_data_update
|
||||
.is_none_or(|d| d != DUMMY_DATA_UPDATE);
|
||||
if needs_update {
|
||||
println!(
|
||||
"Dummy data updated, so template DB tables will be dropped and re-created"
|
||||
);
|
||||
// Drop all tables in the database so they can be re-created and later filled with updated dummy data
|
||||
sqlx::query("DROP SCHEMA public CASCADE;")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("CREATE SCHEMA public;")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
dummy_data_exists = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Run migrations on the template
|
||||
MIGRATOR.run(&pool).await.expect("Migrations failed");
|
||||
|
||||
if !dummy_data_exists {
|
||||
// Add dummy data
|
||||
let name = generate_random_name("test_template_");
|
||||
let db = TemporaryDatabase {
|
||||
pool: pool.clone(),
|
||||
ro_pool: ReadOnlyPgPool::from(pool.clone()),
|
||||
database_name: TEMPLATE_DATABASE_NAME.to_string(),
|
||||
redis_pool: RedisPool::new(Some(name.clone())),
|
||||
search_config: search::SearchConfig::new(Some(name)),
|
||||
};
|
||||
let setup_api =
|
||||
TestEnvironment::<ApiV3>::build_setup_api(&db).await;
|
||||
dummy_data::add_dummy_data(&setup_api, db.clone()).await;
|
||||
db.pool.close().await;
|
||||
}
|
||||
pool.close().await;
|
||||
drop(pool);
|
||||
|
||||
// Create the temporary database from the template
|
||||
let create_db_query = format!(
|
||||
"CREATE DATABASE {} TEMPLATE {}",
|
||||
&temp_database_name, TEMPLATE_DATABASE_NAME
|
||||
);
|
||||
|
||||
sqlx::query(&create_db_query)
|
||||
.execute(&main_pool)
|
||||
.await
|
||||
.expect("Database creation failed");
|
||||
|
||||
// Release the advisory lock
|
||||
sqlx::query("SELECT pg_advisory_unlock(1)")
|
||||
.execute(&main_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
main_pool.close().await;
|
||||
break;
|
||||
}
|
||||
// Wait for the lock to be released
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
|
||||
}
|
||||
}
|
||||
|
||||
// Deletes the temporary database (panics)
|
||||
// If a temporary db is created, it must be cleaned up with cleanup.
|
||||
// This means that dbs will only 'remain' if a test fails (for examination of the db), and will be cleaned up otherwise.
|
||||
pub async fn cleanup(mut self) {
|
||||
let database_url =
|
||||
dotenvy::var("DATABASE_URL").expect("No database URL");
|
||||
self.pool.close().await;
|
||||
|
||||
self.pool = PgPool::connect(&database_url)
|
||||
.await
|
||||
.expect("Connection to main database failed");
|
||||
|
||||
// Forcibly terminate all existing connections to this version of the temporary database
|
||||
// We are done and deleting it, so we don't need them anymore
|
||||
let terminate_query = format!(
|
||||
"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname = '{}' AND pid <> pg_backend_pid()",
|
||||
&self.database_name
|
||||
);
|
||||
sqlx::query(&terminate_query)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Execute the deletion query asynchronously
|
||||
let drop_db_query =
|
||||
format!("DROP DATABASE IF EXISTS {}", &self.database_name);
|
||||
sqlx::query(&drop_db_query)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.expect("Database deletion failed");
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_template_database(pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||
let create_db_query = format!("CREATE DATABASE {TEMPLATE_DATABASE_NAME}");
|
||||
sqlx::query(&create_db_query)
|
||||
.execute(pool)
|
||||
.await
|
||||
.expect("Database creation failed");
|
||||
}
|
||||
|
||||
// Appends a random 8-digit number to the end of the str
|
||||
pub fn generate_random_name(str: &str) -> String {
|
||||
let mut str = String::from(str);
|
||||
str.push_str(&rand::random::<u64>().to_string()[..8]);
|
||||
str
|
||||
}
|
||||
566
apps/labrinth/src/test/dummy_data.rs
Normal file
566
apps/labrinth/src/test/dummy_data.rs
Normal file
@@ -0,0 +1,566 @@
|
||||
use std::io::{Cursor, Write};
|
||||
|
||||
use crate::models::ids::ProjectId;
|
||||
use crate::models::{
|
||||
oauth_clients::OAuthClient,
|
||||
organizations::Organization,
|
||||
projects::{Project, Version},
|
||||
};
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{api_common::Api, api_v3, database::USER_USER_PAT};
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::test::{self, TestRequest};
|
||||
use serde_json::json;
|
||||
use zip::{CompressionMethod, ZipWriter, write::FileOptions};
|
||||
|
||||
use super::{
|
||||
api_common::{ApiProject, AppendsOptionalPat, request_data::ImageData},
|
||||
api_v3::ApiV3,
|
||||
database::TemporaryDatabase,
|
||||
};
|
||||
|
||||
use super::{database::USER_USER_ID, get_json_val_str};
|
||||
|
||||
pub const DUMMY_DATA_UPDATE: i64 = 7;
|
||||
|
||||
pub const DUMMY_CATEGORIES: &[&str] = &[
|
||||
"combat",
|
||||
"decoration",
|
||||
"economy",
|
||||
"food",
|
||||
"magic",
|
||||
"mobs",
|
||||
"optimization",
|
||||
];
|
||||
|
||||
pub const DUMMY_OAUTH_CLIENT_ALPHA_SECRET: &str = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum TestFile {
|
||||
DummyProjectAlpha,
|
||||
DummyProjectBeta,
|
||||
BasicZip,
|
||||
BasicMod,
|
||||
BasicModDifferent,
|
||||
// Randomly generates a valid .jar with a random hash.
|
||||
// Unlike the other dummy jar files, this one is not a static file.
|
||||
// and BasicModRandom.bytes() will return a different file each time.
|
||||
BasicModRandom { filename: String, bytes: Vec<u8> },
|
||||
BasicModpackRandom { filename: String, bytes: Vec<u8> },
|
||||
}
|
||||
|
||||
impl TestFile {
|
||||
pub fn build_random_jar() -> Self {
|
||||
let filename = format!("random-mod-{}.jar", rand::random::<u64>());
|
||||
|
||||
let fabric_mod_json = serde_json::json!({
|
||||
"schemaVersion": 1,
|
||||
"id": filename,
|
||||
"version": "1.0.1",
|
||||
|
||||
"name": filename,
|
||||
"description": "Does nothing",
|
||||
"authors": [
|
||||
"user"
|
||||
],
|
||||
"contact": {
|
||||
"homepage": "https://www.modrinth.com",
|
||||
"sources": "https://www.modrinth.com",
|
||||
"issues": "https://www.modrinth.com"
|
||||
},
|
||||
|
||||
"license": "MIT",
|
||||
"icon": "none.png",
|
||||
|
||||
"environment": "client",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"io.github.modrinth.Modrinth"
|
||||
]
|
||||
},
|
||||
"depends": {
|
||||
"minecraft": ">=1.20-"
|
||||
}
|
||||
}
|
||||
)
|
||||
.to_string();
|
||||
|
||||
// Create a simulated zip file
|
||||
let mut cursor = Cursor::new(Vec::new());
|
||||
{
|
||||
let mut zip = ZipWriter::new(&mut cursor);
|
||||
zip.start_file(
|
||||
"fabric.mod.json",
|
||||
FileOptions::<()>::default()
|
||||
.compression_method(CompressionMethod::Stored),
|
||||
)
|
||||
.unwrap();
|
||||
zip.write_all(fabric_mod_json.as_bytes()).unwrap();
|
||||
|
||||
zip.start_file(
|
||||
"META-INF/mods.toml",
|
||||
FileOptions::<()>::default()
|
||||
.compression_method(CompressionMethod::Stored),
|
||||
)
|
||||
.unwrap();
|
||||
zip.write_all(fabric_mod_json.as_bytes()).unwrap();
|
||||
|
||||
zip.finish().unwrap();
|
||||
}
|
||||
let bytes = cursor.into_inner();
|
||||
|
||||
TestFile::BasicModRandom { filename, bytes }
|
||||
}
|
||||
|
||||
pub fn build_random_mrpack() -> Self {
|
||||
let filename =
|
||||
format!("random-modpack-{}.mrpack", rand::random::<u64>());
|
||||
|
||||
let modrinth_index_json = serde_json::json!({
|
||||
"formatVersion": 1,
|
||||
"game": "minecraft",
|
||||
"versionId": "1.20.1-9.6",
|
||||
"name": filename,
|
||||
"files": [
|
||||
{
|
||||
"path": "mods/animatica-0.6+1.20.jar",
|
||||
"hashes": {
|
||||
"sha1": "3bcb19c759f313e69d3f7848b03c48f15167b88d",
|
||||
"sha512": "7d50f3f34479f8b052bfb9e2482603b4906b8984039777dc2513ecf18e9af2b599c9d094e88cec774f8525345859e721a394c8cd7c14a789c9538d2533c71d65"
|
||||
},
|
||||
"env": {
|
||||
"client": "required",
|
||||
"server": "required"
|
||||
},
|
||||
"downloads": [
|
||||
"https://cdn.modrinth.com/data/PRN43VSY/versions/uNgEPb10/animatica-0.6%2B1.20.jar"
|
||||
],
|
||||
"fileSize": 69810
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"fabric-loader": "0.14.22",
|
||||
"minecraft": "1.20.1"
|
||||
}
|
||||
}
|
||||
)
|
||||
.to_string();
|
||||
|
||||
// Create a simulated zip file
|
||||
let mut cursor = Cursor::new(Vec::new());
|
||||
{
|
||||
let mut zip = ZipWriter::new(&mut cursor);
|
||||
zip.start_file(
|
||||
"modrinth.index.json",
|
||||
FileOptions::<()>::default()
|
||||
.compression_method(CompressionMethod::Stored),
|
||||
)
|
||||
.unwrap();
|
||||
zip.write_all(modrinth_index_json.as_bytes()).unwrap();
|
||||
zip.finish().unwrap();
|
||||
}
|
||||
let bytes = cursor.into_inner();
|
||||
|
||||
TestFile::BasicModpackRandom { filename, bytes }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum DummyImage {
|
||||
SmallIcon, // 200x200
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DummyData {
|
||||
/// Alpha project:
|
||||
/// This is a dummy project created by USER user.
|
||||
/// It's approved, listed, and visible to the public.
|
||||
pub project_alpha: DummyProjectAlpha,
|
||||
|
||||
/// Beta project:
|
||||
/// This is a dummy project created by USER user.
|
||||
/// It's not approved, unlisted, and not visible to the public.
|
||||
pub project_beta: DummyProjectBeta,
|
||||
|
||||
/// Zeta organization:
|
||||
/// This is a dummy organization created by USER user.
|
||||
/// There are no projects in it.
|
||||
pub organization_zeta: DummyOrganizationZeta,
|
||||
|
||||
/// Alpha OAuth Client:
|
||||
/// This is a dummy OAuth client created by USER user.
|
||||
///
|
||||
/// All scopes are included in its max scopes
|
||||
///
|
||||
/// It has one valid redirect URI
|
||||
pub oauth_client_alpha: DummyOAuthClientAlpha,
|
||||
}
|
||||
|
||||
impl DummyData {
|
||||
pub fn new(
|
||||
project_alpha: Project,
|
||||
project_alpha_version: Version,
|
||||
project_beta: Project,
|
||||
project_beta_version: Version,
|
||||
organization_zeta: Organization,
|
||||
oauth_client_alpha: OAuthClient,
|
||||
) -> Self {
|
||||
DummyData {
|
||||
project_alpha: DummyProjectAlpha {
|
||||
team_id: project_alpha.team_id.to_string(),
|
||||
project_id: project_alpha.id.to_string(),
|
||||
project_slug: project_alpha.slug.unwrap(),
|
||||
project_id_parsed: project_alpha.id,
|
||||
version_id: project_alpha_version.id.to_string(),
|
||||
thread_id: project_alpha.thread_id.to_string(),
|
||||
file_hash: project_alpha_version.files[0].hashes["sha1"]
|
||||
.clone(),
|
||||
},
|
||||
|
||||
project_beta: DummyProjectBeta {
|
||||
team_id: project_beta.team_id.to_string(),
|
||||
project_id: project_beta.id.to_string(),
|
||||
project_slug: project_beta.slug.unwrap(),
|
||||
project_id_parsed: project_beta.id,
|
||||
version_id: project_beta_version.id.to_string(),
|
||||
thread_id: project_beta.thread_id.to_string(),
|
||||
file_hash: project_beta_version.files[0].hashes["sha1"].clone(),
|
||||
},
|
||||
|
||||
organization_zeta: DummyOrganizationZeta {
|
||||
organization_id: organization_zeta.id.to_string(),
|
||||
team_id: organization_zeta.team_id.to_string(),
|
||||
organization_slug: organization_zeta.slug,
|
||||
},
|
||||
|
||||
oauth_client_alpha: DummyOAuthClientAlpha {
|
||||
client_id: get_json_val_str(oauth_client_alpha.id),
|
||||
client_secret: DUMMY_OAUTH_CLIENT_ALPHA_SECRET.to_string(),
|
||||
valid_redirect_uri: oauth_client_alpha
|
||||
.redirect_uris
|
||||
.first()
|
||||
.unwrap()
|
||||
.uri
|
||||
.clone(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DummyProjectAlpha {
|
||||
pub project_id: String,
|
||||
pub project_slug: String,
|
||||
pub project_id_parsed: ProjectId,
|
||||
pub version_id: String,
|
||||
pub thread_id: String,
|
||||
pub file_hash: String,
|
||||
pub team_id: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DummyProjectBeta {
|
||||
pub project_id: String,
|
||||
pub project_slug: String,
|
||||
pub project_id_parsed: ProjectId,
|
||||
pub version_id: String,
|
||||
pub thread_id: String,
|
||||
pub file_hash: String,
|
||||
pub team_id: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DummyOrganizationZeta {
|
||||
pub organization_id: String,
|
||||
pub organization_slug: String,
|
||||
pub team_id: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DummyOAuthClientAlpha {
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
pub valid_redirect_uri: String,
|
||||
}
|
||||
|
||||
pub async fn add_dummy_data(api: &ApiV3, db: TemporaryDatabase) -> DummyData {
|
||||
// Adds basic dummy data to the database directly with sql (user, pats)
|
||||
let pool = &db.pool.clone();
|
||||
|
||||
crate::test::db::add_dummy_data(pool).await.unwrap();
|
||||
|
||||
let (alpha_project, alpha_version) = add_project_alpha(api).await;
|
||||
let (beta_project, beta_version) = add_project_beta(api).await;
|
||||
|
||||
let zeta_organization = add_organization_zeta(api).await;
|
||||
|
||||
let oauth_client_alpha = get_oauth_client_alpha(api).await;
|
||||
|
||||
sqlx::query("INSERT INTO dummy_data (update_id) VALUES ($1)")
|
||||
.bind(DUMMY_DATA_UPDATE)
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
DummyData::new(
|
||||
alpha_project,
|
||||
alpha_version,
|
||||
beta_project,
|
||||
beta_version,
|
||||
zeta_organization,
|
||||
oauth_client_alpha,
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn get_dummy_data(api: &ApiV3) -> DummyData {
|
||||
let (alpha_project, alpha_version) = get_project_alpha(api).await;
|
||||
let (beta_project, beta_version) = get_project_beta(api).await;
|
||||
|
||||
let zeta_organization = get_organization_zeta(api).await;
|
||||
|
||||
let oauth_client_alpha = get_oauth_client_alpha(api).await;
|
||||
|
||||
DummyData::new(
|
||||
alpha_project,
|
||||
alpha_version,
|
||||
beta_project,
|
||||
beta_version,
|
||||
zeta_organization,
|
||||
oauth_client_alpha,
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn add_project_alpha(api: &ApiV3) -> (Project, Version) {
|
||||
let (project, versions) = api
|
||||
.add_public_project(
|
||||
"alpha",
|
||||
Some(TestFile::DummyProjectAlpha),
|
||||
None,
|
||||
USER_USER_PAT,
|
||||
)
|
||||
.await;
|
||||
let alpha_project = api
|
||||
.get_project_deserialized(
|
||||
project.id.to_string().as_str(),
|
||||
USER_USER_PAT,
|
||||
)
|
||||
.await;
|
||||
let alpha_version = api
|
||||
.get_version_deserialized(
|
||||
&versions.into_iter().next().unwrap().id.to_string(),
|
||||
USER_USER_PAT,
|
||||
)
|
||||
.await;
|
||||
(alpha_project, alpha_version)
|
||||
}
|
||||
|
||||
pub async fn add_project_beta(api: &ApiV3) -> (Project, Version) {
|
||||
// Adds dummy data to the database with sqlx (projects, versions, threads)
|
||||
// Generate test project data.
|
||||
let jar = TestFile::DummyProjectBeta;
|
||||
// TODO: this shouldnt be hardcoded (nor should other similar ones be)
|
||||
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/summary", "value": "A dummy project for testing with." },
|
||||
{ "op": "add", "path": "/description", "value": "This project is not-yet-approved, and versions are draft." },
|
||||
{ "op": "add", "path": "/initial_versions/0/status", "value": "unlisted" },
|
||||
{ "op": "add", "path": "/status", "value": "private" },
|
||||
{ "op": "add", "path": "/requested_status", "value": "private" },
|
||||
]))
|
||||
.unwrap();
|
||||
|
||||
let creation_data = api_v3::request_data::get_public_project_creation_data(
|
||||
"beta",
|
||||
Some(jar),
|
||||
Some(modify_json),
|
||||
);
|
||||
api.create_project(creation_data, USER_USER_PAT).await;
|
||||
|
||||
get_project_beta(api).await
|
||||
}
|
||||
|
||||
pub async fn add_organization_zeta(api: &ApiV3) -> Organization {
|
||||
// Add an organization.
|
||||
let req = TestRequest::post()
|
||||
.uri("/v3/organization")
|
||||
.append_pat(USER_USER_PAT)
|
||||
.set_json(json!({
|
||||
"name": "Zeta",
|
||||
"slug": "zeta",
|
||||
"description": "A dummy organization for testing with."
|
||||
}))
|
||||
.to_request();
|
||||
let resp = api.call(req).await;
|
||||
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
|
||||
get_organization_zeta(api).await
|
||||
}
|
||||
|
||||
pub async fn get_project_alpha(api: &ApiV3) -> (Project, Version) {
|
||||
// Get project
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/project/alpha")
|
||||
.append_pat(USER_USER_PAT)
|
||||
.to_request();
|
||||
let resp = api.call(req).await;
|
||||
let project: Project = test::read_body_json(resp).await;
|
||||
|
||||
// Get project's versions
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/project/alpha/version")
|
||||
.append_pat(USER_USER_PAT)
|
||||
.to_request();
|
||||
let resp = api.call(req).await;
|
||||
let versions: Vec<Version> = test::read_body_json(resp).await;
|
||||
let version = versions.into_iter().next().unwrap();
|
||||
|
||||
(project, version)
|
||||
}
|
||||
|
||||
pub async fn get_project_beta(api: &ApiV3) -> (Project, Version) {
|
||||
// Get project
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/project/beta")
|
||||
.append_pat(USER_USER_PAT)
|
||||
.to_request();
|
||||
let resp = api.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
let project: serde_json::Value = test::read_body_json(resp).await;
|
||||
let project: Project = serde_json::from_value(project).unwrap();
|
||||
|
||||
// Get project's versions
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/project/beta/version")
|
||||
.append_pat(USER_USER_PAT)
|
||||
.to_request();
|
||||
let resp = api.call(req).await;
|
||||
assert_status!(&resp, StatusCode::OK);
|
||||
let versions: Vec<Version> = test::read_body_json(resp).await;
|
||||
let version = versions.into_iter().next().unwrap();
|
||||
|
||||
(project, version)
|
||||
}
|
||||
|
||||
pub async fn get_organization_zeta(api: &ApiV3) -> Organization {
|
||||
// Get organization
|
||||
let req = TestRequest::get()
|
||||
.uri("/v3/organization/zeta")
|
||||
.append_pat(USER_USER_PAT)
|
||||
.to_request();
|
||||
let resp = api.call(req).await;
|
||||
let organization: Organization = test::read_body_json(resp).await;
|
||||
|
||||
organization
|
||||
}
|
||||
|
||||
pub async fn get_oauth_client_alpha(api: &ApiV3) -> OAuthClient {
|
||||
let oauth_clients = api
|
||||
.get_user_oauth_clients(USER_USER_ID, USER_USER_PAT)
|
||||
.await;
|
||||
oauth_clients.into_iter().next().unwrap()
|
||||
}
|
||||
|
||||
impl TestFile {
|
||||
pub fn filename(&self) -> String {
|
||||
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,
|
||||
TestFile::BasicModpackRandom { filename, .. } => filename,
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn bytes(&self) -> Vec<u8> {
|
||||
match self {
|
||||
TestFile::DummyProjectAlpha => {
|
||||
include_bytes!("../../tests/files/dummy-project-alpha.jar")
|
||||
.to_vec()
|
||||
}
|
||||
TestFile::DummyProjectBeta => {
|
||||
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()
|
||||
}
|
||||
TestFile::BasicModRandom { bytes, .. } => bytes.clone(),
|
||||
TestFile::BasicModpackRandom { bytes, .. } => bytes.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn project_type(&self) -> String {
|
||||
match self {
|
||||
TestFile::DummyProjectAlpha => "mod",
|
||||
TestFile::DummyProjectBeta => "mod",
|
||||
TestFile::BasicMod => "mod",
|
||||
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 {
|
||||
pub fn filename(&self) -> String {
|
||||
match self {
|
||||
DummyImage::SmallIcon => "200x200.png",
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn extension(&self) -> String {
|
||||
match self {
|
||||
DummyImage::SmallIcon => "png",
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn bytes(&self) -> Vec<u8> {
|
||||
match self {
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
174
apps/labrinth/src/test/environment.rs
Normal file
174
apps/labrinth/src/test/environment.rs
Normal file
@@ -0,0 +1,174 @@
|
||||
use super::{
|
||||
api_common::{Api, ApiBuildable, generic::GenericApi},
|
||||
api_v2::ApiV2,
|
||||
api_v3::ApiV3,
|
||||
database::{FRIEND_USER_ID, TemporaryDatabase, USER_USER_PAT},
|
||||
dummy_data,
|
||||
};
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::setup;
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use futures::Future;
|
||||
|
||||
pub async fn with_test_environment<Fut, A>(
|
||||
max_connections: Option<u32>,
|
||||
f: impl FnOnce(TestEnvironment<A>) -> Fut,
|
||||
) where
|
||||
Fut: Future<Output = ()>,
|
||||
A: ApiBuildable + 'static,
|
||||
{
|
||||
let test_env: TestEnvironment<A> =
|
||||
TestEnvironment::build(max_connections).await;
|
||||
let db = test_env.db.clone();
|
||||
f(test_env).await;
|
||||
db.cleanup().await;
|
||||
}
|
||||
|
||||
pub async fn with_test_environment_all<Fut, F>(
|
||||
max_connections: Option<u32>,
|
||||
f: F,
|
||||
) where
|
||||
Fut: Future<Output = ()>,
|
||||
F: Fn(TestEnvironment<GenericApi>) -> Fut,
|
||||
{
|
||||
println!("Test environment: API v3");
|
||||
let test_env_api_v3 =
|
||||
TestEnvironment::<ApiV3>::build(max_connections).await;
|
||||
let test_env_api_v3 = TestEnvironment {
|
||||
db: test_env_api_v3.db.clone(),
|
||||
api: GenericApi::V3(test_env_api_v3.api),
|
||||
setup_api: test_env_api_v3.setup_api,
|
||||
dummy: test_env_api_v3.dummy,
|
||||
};
|
||||
let db = test_env_api_v3.db.clone();
|
||||
f(test_env_api_v3).await;
|
||||
db.cleanup().await;
|
||||
|
||||
println!("Test environment: API v2");
|
||||
let test_env_api_v2 =
|
||||
TestEnvironment::<ApiV2>::build(max_connections).await;
|
||||
let test_env_api_v2 = TestEnvironment {
|
||||
db: test_env_api_v2.db.clone(),
|
||||
api: GenericApi::V2(test_env_api_v2.api),
|
||||
setup_api: test_env_api_v2.setup_api,
|
||||
dummy: test_env_api_v2.dummy,
|
||||
};
|
||||
let db = test_env_api_v2.db.clone();
|
||||
f(test_env_api_v2).await;
|
||||
db.cleanup().await;
|
||||
}
|
||||
|
||||
// A complete test environment, with a test actix app and a database.
|
||||
// Must be called in an #[actix_rt::test] context. It also simulates a
|
||||
// temporary sqlx db like #[sqlx::test] would.
|
||||
// Use .call(req) on it directly to make a test call as if test::call_service(req) were being used.
|
||||
#[derive(Clone)]
|
||||
pub struct TestEnvironment<A> {
|
||||
pub db: TemporaryDatabase,
|
||||
pub api: A,
|
||||
pub setup_api: ApiV3, // Used for setting up tests only (ie: in ScopesTest)
|
||||
pub dummy: dummy_data::DummyData,
|
||||
}
|
||||
|
||||
impl<A: ApiBuildable> TestEnvironment<A> {
|
||||
async fn build(max_connections: Option<u32>) -> Self {
|
||||
let db = TemporaryDatabase::create(max_connections).await;
|
||||
let labrinth_config = setup(&db).await;
|
||||
let api = A::build(labrinth_config.clone()).await;
|
||||
let setup_api = ApiV3::build(labrinth_config).await;
|
||||
let dummy = dummy_data::get_dummy_data(&setup_api).await;
|
||||
Self {
|
||||
db,
|
||||
api,
|
||||
setup_api,
|
||||
dummy,
|
||||
}
|
||||
}
|
||||
pub async fn build_setup_api(db: &TemporaryDatabase) -> ApiV3 {
|
||||
let labrinth_config = setup(db).await;
|
||||
ApiV3::build(labrinth_config).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Api> TestEnvironment<A> {
|
||||
pub async fn cleanup(self) {
|
||||
self.db.cleanup().await;
|
||||
}
|
||||
|
||||
pub async fn call(&self, req: actix_http::Request) -> ServiceResponse {
|
||||
self.api.call(req).await
|
||||
}
|
||||
|
||||
// Setup data, create a friend user notification
|
||||
pub async fn generate_friend_user_notification(&self) {
|
||||
let resp = self
|
||||
.api
|
||||
.add_user_to_team(
|
||||
&self.dummy.project_alpha.team_id,
|
||||
FRIEND_USER_ID,
|
||||
None,
|
||||
None,
|
||||
USER_USER_PAT,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
// Setup data, assert that a user can read notifications
|
||||
pub async fn assert_read_notifications_status(
|
||||
&self,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
status_code: StatusCode,
|
||||
) {
|
||||
let resp = self.api.get_user_notifications(user_id, pat).await;
|
||||
assert_status!(&resp, status_code);
|
||||
}
|
||||
|
||||
// Setup data, assert that a user can read projects notifications
|
||||
pub async fn assert_read_user_projects_status(
|
||||
&self,
|
||||
user_id: &str,
|
||||
pat: Option<&str>,
|
||||
status_code: StatusCode,
|
||||
) {
|
||||
let resp = self.api.get_user_projects(user_id, pat).await;
|
||||
assert_status!(&resp, status_code);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait LocalService {
|
||||
fn call(
|
||||
&self,
|
||||
req: actix_http::Request,
|
||||
) -> std::pin::Pin<
|
||||
Box<
|
||||
dyn std::future::Future<
|
||||
Output = Result<ServiceResponse, actix_web::Error>,
|
||||
>,
|
||||
>,
|
||||
>;
|
||||
}
|
||||
impl<S> LocalService for S
|
||||
where
|
||||
S: actix_web::dev::Service<
|
||||
actix_http::Request,
|
||||
Response = ServiceResponse,
|
||||
Error = actix_web::Error,
|
||||
>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
fn call(
|
||||
&self,
|
||||
req: actix_http::Request,
|
||||
) -> std::pin::Pin<
|
||||
Box<
|
||||
dyn std::future::Future<
|
||||
Output = Result<ServiceResponse, actix_web::Error>,
|
||||
>,
|
||||
>,
|
||||
> {
|
||||
Box::pin(self.call(req))
|
||||
}
|
||||
}
|
||||
@@ -1 +1,76 @@
|
||||
use crate::queue::email::EmailQueue;
|
||||
use crate::util::anrok;
|
||||
use crate::util::gotenberg::GotenbergClient;
|
||||
use crate::{LabrinthConfig, file_hosting};
|
||||
use crate::{check_env_vars, clickhouse};
|
||||
use modrinth_maxmind::MaxMind;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub mod api_common;
|
||||
pub mod api_v2;
|
||||
pub mod api_v3;
|
||||
pub mod asserts;
|
||||
pub mod database;
|
||||
pub mod db;
|
||||
pub mod dummy_data;
|
||||
pub mod environment;
|
||||
pub mod pats;
|
||||
pub mod permissions;
|
||||
pub mod scopes;
|
||||
pub mod search;
|
||||
|
||||
// Testing equivalent to 'setup' function, producing a LabrinthConfig
|
||||
// If making a test, you should probably use environment::TestEnvironment::build() (which calls this)
|
||||
pub async fn setup(db: &database::TemporaryDatabase) -> LabrinthConfig {
|
||||
println!("Setting up labrinth config");
|
||||
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
if check_env_vars() {
|
||||
println!("Some environment variables are missing!");
|
||||
}
|
||||
|
||||
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||
|
||||
let pool = db.pool.clone();
|
||||
let ro_pool = db.ro_pool.clone();
|
||||
let redis_pool = db.redis_pool.clone();
|
||||
let search_config = db.search_config.clone();
|
||||
let file_host: Arc<dyn file_hosting::FileHost + Send + Sync> =
|
||||
Arc::new(file_hosting::MockHost::new());
|
||||
let mut clickhouse = clickhouse::init_client().await.unwrap();
|
||||
|
||||
let maxmind_reader = MaxMind::new().await;
|
||||
|
||||
let stripe_client =
|
||||
stripe::Client::new(dotenvy::var("STRIPE_API_KEY").unwrap());
|
||||
|
||||
let anrok_client = anrok::Client::from_env().unwrap();
|
||||
let email_queue =
|
||||
EmailQueue::init(pool.clone(), redis_pool.clone()).unwrap();
|
||||
let gotenberg_client =
|
||||
GotenbergClient::from_env().expect("Failed to create Gotenberg client");
|
||||
|
||||
crate::app_setup(
|
||||
pool.clone(),
|
||||
ro_pool.clone(),
|
||||
redis_pool.clone(),
|
||||
search_config,
|
||||
&mut clickhouse,
|
||||
file_host.clone(),
|
||||
maxmind_reader,
|
||||
stripe_client,
|
||||
anrok_client,
|
||||
email_queue,
|
||||
gotenberg_client,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_json_val_str(val: impl serde::Serialize) -> String {
|
||||
serde_json::to_value(val)
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
32
apps/labrinth/src/test/pats.rs
Normal file
32
apps/labrinth/src/test/pats.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use crate::{
|
||||
database::{self, models::generate_pat_id},
|
||||
models::pats::Scopes,
|
||||
};
|
||||
use chrono::Utc;
|
||||
|
||||
use super::database::TemporaryDatabase;
|
||||
|
||||
// Creates a PAT with the given scopes, and returns the access token
|
||||
// Interfacing with the db directly, rather than using a ourte,
|
||||
// allows us to test with scopes that are not allowed to be created by PATs
|
||||
pub async fn create_test_pat(
|
||||
scopes: Scopes,
|
||||
user_id: i64,
|
||||
db: &TemporaryDatabase,
|
||||
) -> String {
|
||||
let mut transaction = db.pool.begin().await.unwrap();
|
||||
let id = generate_pat_id(&mut transaction).await.unwrap();
|
||||
let pat = database::models::pat_item::DBPersonalAccessToken {
|
||||
id,
|
||||
name: format!("test_pat_{}", scopes.bits()),
|
||||
access_token: format!("mrp_{}", id.0),
|
||||
scopes,
|
||||
user_id: database::models::ids::DBUserId(user_id),
|
||||
created: Utc::now(),
|
||||
expires: Utc::now() + chrono::Duration::days(1),
|
||||
last_used: None,
|
||||
};
|
||||
pat.insert(&mut transaction).await.unwrap();
|
||||
transaction.commit().await.unwrap();
|
||||
pat.access_token
|
||||
}
|
||||
1211
apps/labrinth/src/test/permissions.rs
Normal file
1211
apps/labrinth/src/test/permissions.rs
Normal file
File diff suppressed because it is too large
Load Diff
125
apps/labrinth/src/test/scopes.rs
Normal file
125
apps/labrinth/src/test/scopes.rs
Normal file
@@ -0,0 +1,125 @@
|
||||
use crate::models::pats::Scopes;
|
||||
use actix_web::{dev::ServiceResponse, test};
|
||||
use futures::Future;
|
||||
|
||||
use super::{
|
||||
api_common::Api, database::USER_USER_ID_PARSED,
|
||||
environment::TestEnvironment, pats::create_test_pat,
|
||||
};
|
||||
|
||||
// A reusable test type that works for any scope test testing an endpoint that:
|
||||
// - returns a known 'expected_failure_code' if the scope is not present (defaults to 401)
|
||||
// - returns a 200-299 if the scope is present
|
||||
// - returns failure and success JSON bodies for requests that are 200 (for performing non-simple follow-up tests on)
|
||||
// This uses a builder format, so you can chain methods to set the parameters to non-defaults (most will probably be not need to be set).
|
||||
pub struct ScopeTest<'a, A> {
|
||||
test_env: &'a TestEnvironment<A>,
|
||||
// Scopes expected to fail on this test. By default, this is all scopes except the success scopes.
|
||||
// (To ensure we have isolated the scope we are testing)
|
||||
failure_scopes: Option<Scopes>,
|
||||
// User ID to use for the PATs. By default, this is the USER_USER_ID_PARSED constant.
|
||||
user_id: i64,
|
||||
// The code that is expected to be returned if the scope is not present. By default, this is 401 (Unauthorized)
|
||||
expected_failure_code: u16,
|
||||
}
|
||||
|
||||
impl<'a, A: Api> ScopeTest<'a, A> {
|
||||
pub fn new(test_env: &'a TestEnvironment<A>) -> Self {
|
||||
Self {
|
||||
test_env,
|
||||
failure_scopes: None,
|
||||
user_id: USER_USER_ID_PARSED,
|
||||
expected_failure_code: 401,
|
||||
}
|
||||
}
|
||||
|
||||
// Set non-standard failure scopes
|
||||
// If not set, it will be set to all scopes except the success scopes
|
||||
// (eg: if a combination of scopes is needed, but you want to make sure that the endpoint does not work with all-but-one of them)
|
||||
pub fn with_failure_scopes(mut self, scopes: Scopes) -> Self {
|
||||
self.failure_scopes = Some(scopes);
|
||||
self
|
||||
}
|
||||
|
||||
// Set the user ID to use
|
||||
// (eg: a moderator, or friend)
|
||||
pub fn with_user_id(mut self, user_id: i64) -> Self {
|
||||
self.user_id = user_id;
|
||||
self
|
||||
}
|
||||
|
||||
// If a non-401 code is expected.
|
||||
// (eg: a 404 for a hidden resource, or 200 for a resource with hidden values deeper in)
|
||||
pub fn with_failure_code(mut self, code: u16) -> Self {
|
||||
self.expected_failure_code = code;
|
||||
self
|
||||
}
|
||||
|
||||
// Call the endpoint generated by req_gen twice, once with a PAT with the failure scopes, and once with the success scopes.
|
||||
// success_scopes : the scopes that we are testing that should succeed
|
||||
// returns a tuple of (failure_body, success_body)
|
||||
// Should return a String error if on unexpected status code, allowing unwrapping in tests.
|
||||
pub async fn test<T, Fut>(
|
||||
&self,
|
||||
req_gen: T,
|
||||
success_scopes: Scopes,
|
||||
) -> Result<(serde_json::Value, serde_json::Value), String>
|
||||
where
|
||||
T: Fn(Option<String>) -> Fut,
|
||||
Fut: Future<Output = ServiceResponse>, // Ensure Fut is Send and 'static
|
||||
{
|
||||
// First, create a PAT with failure scopes
|
||||
let failure_scopes = self
|
||||
.failure_scopes
|
||||
.unwrap_or(Scopes::all() ^ success_scopes);
|
||||
let access_token_all_others =
|
||||
create_test_pat(failure_scopes, self.user_id, &self.test_env.db)
|
||||
.await;
|
||||
|
||||
// Create a PAT with the success scopes
|
||||
let access_token =
|
||||
create_test_pat(success_scopes, self.user_id, &self.test_env.db)
|
||||
.await;
|
||||
|
||||
// Perform test twice, once with each PAT
|
||||
// the first time, we expect a 401 (or known failure code)
|
||||
let resp = req_gen(Some(access_token_all_others.clone())).await;
|
||||
if resp.status().as_u16() != self.expected_failure_code {
|
||||
return Err(format!(
|
||||
"Expected failure code {}, got {} ({:#?})",
|
||||
self.expected_failure_code,
|
||||
resp.status().as_u16(),
|
||||
resp.response()
|
||||
));
|
||||
}
|
||||
|
||||
let failure_body = if resp.status() == 200
|
||||
&& resp.headers().contains_key("Content-Type")
|
||||
&& resp.headers().get("Content-Type").unwrap() == "application/json"
|
||||
{
|
||||
test::read_body_json(resp).await
|
||||
} else {
|
||||
serde_json::Value::Null
|
||||
};
|
||||
|
||||
// The second time, we expect a success code
|
||||
let resp = req_gen(Some(access_token.clone())).await;
|
||||
if !(resp.status().is_success() || resp.status().is_redirection()) {
|
||||
return Err(format!(
|
||||
"Expected success code, got {} ({:#?})",
|
||||
resp.status().as_u16(),
|
||||
resp.response()
|
||||
));
|
||||
}
|
||||
|
||||
let success_body = if resp.status() == 200
|
||||
&& resp.headers().contains_key("Content-Type")
|
||||
&& resp.headers().get("Content-Type").unwrap() == "application/json"
|
||||
{
|
||||
test::read_body_json(resp).await
|
||||
} else {
|
||||
serde_json::Value::Null
|
||||
};
|
||||
Ok((failure_body, success_body))
|
||||
}
|
||||
}
|
||||
232
apps/labrinth/src/test/search.rs
Normal file
232
apps/labrinth/src/test/search.rs
Normal file
@@ -0,0 +1,232 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use actix_http::StatusCode;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::test::asserts::assert_status;
|
||||
use crate::test::{
|
||||
api_common::{Api, ApiProject, ApiVersion},
|
||||
database::{FRIEND_USER_PAT, MOD_USER_PAT, USER_USER_PAT},
|
||||
dummy_data::{DUMMY_CATEGORIES, TestFile},
|
||||
};
|
||||
|
||||
use super::{api_v3::ApiV3, environment::TestEnvironment};
|
||||
|
||||
pub async fn setup_search_projects(
|
||||
test_env: &TestEnvironment<ApiV3>,
|
||||
) -> Arc<HashMap<u64, u64>> {
|
||||
// Test setup and dummy data
|
||||
let api = &test_env.api;
|
||||
let test_name = test_env.db.database_name.clone();
|
||||
let zeta_organization_id =
|
||||
&test_env.dummy.organization_zeta.organization_id;
|
||||
|
||||
// Add dummy projects of various categories for searchability
|
||||
let mut project_creation_futures = vec![];
|
||||
|
||||
let create_async_future =
|
||||
|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 {
|
||||
TestFile::build_random_mrpack()
|
||||
} else {
|
||||
TestFile::build_random_jar()
|
||||
};
|
||||
async move {
|
||||
// Add a project- simple, should work.
|
||||
let req =
|
||||
api.add_public_project(&slug, Some(jar), modify_json, pat);
|
||||
let (project, _) = req.await;
|
||||
|
||||
// Approve, so that the project is searchable
|
||||
let resp = api
|
||||
.edit_project(
|
||||
&project.id.to_string(),
|
||||
json!({
|
||||
"status": "approved"
|
||||
}),
|
||||
MOD_USER_PAT,
|
||||
)
|
||||
.await;
|
||||
assert_status!(&resp, StatusCode::NO_CONTENT);
|
||||
(project.id.0, id)
|
||||
}
|
||||
};
|
||||
|
||||
// Test project 0
|
||||
let id = 0;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[4..6] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "server_only" },
|
||||
{ "op": "add", "path": "/license_id", "value": "LGPL-3.0-or-later" },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
USER_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 1
|
||||
let id = 1;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[0..2] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "client_or_server" },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
USER_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 2
|
||||
let id = 2;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[0..2] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "server_only" },
|
||||
{ "op": "add", "path": "/name", "value": "Mysterious Project" },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
USER_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 3
|
||||
let id = 3;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[0..3] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "server_only" },
|
||||
{ "op": "add", "path": "/initial_versions/0/game_versions", "value": ["1.20.4"] },
|
||||
{ "op": "add", "path": "/name", "value": "Mysterious Project" },
|
||||
{ "op": "add", "path": "/license_id", "value": "LicenseRef-All-Rights-Reserved" },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
FRIEND_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 4
|
||||
let id = 4;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[0..3] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "client_or_server" },
|
||||
{ "op": "add", "path": "/initial_versions/0/game_versions", "value": ["1.20.5"] },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
USER_USER_PAT,
|
||||
true,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 5
|
||||
let id = 5;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[5..6] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "client_or_server" },
|
||||
{ "op": "add", "path": "/initial_versions/0/game_versions", "value": ["1.20.5"] },
|
||||
{ "op": "add", "path": "/license_id", "value": "LGPL-3.0-or-later" },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
USER_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 6
|
||||
let id = 6;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[5..6] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "client_or_server_prefers_both" },
|
||||
{ "op": "add", "path": "/license_id", "value": "LGPL-3.0-or-later" },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
FRIEND_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 7 (testing the search bug)
|
||||
// This project has an initial private forge version that is 1.20.3, and a fabric 1.20.5 version.
|
||||
// This means that a search for fabric + 1.20.3 or forge + 1.20.5 should not return this project.
|
||||
let id = 7;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/categories", "value": DUMMY_CATEGORIES[5..6] },
|
||||
{ "op": "add", "path": "/initial_versions/0/environment", "value": "client_or_server_prefers_both" },
|
||||
{ "op": "add", "path": "/license_id", "value": "LGPL-3.0-or-later" },
|
||||
{ "op": "add", "path": "/initial_versions/0/loaders", "value": ["forge"] },
|
||||
{ "op": "add", "path": "/initial_versions/0/game_versions", "value": ["1.20.2"] },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
USER_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Test project 9 (organization)
|
||||
// This project gets added to the Zeta organization automatically
|
||||
let id = 9;
|
||||
let modify_json = serde_json::from_value(json!([
|
||||
{ "op": "add", "path": "/organization_id", "value": zeta_organization_id },
|
||||
]))
|
||||
.unwrap();
|
||||
project_creation_futures.push(create_async_future(
|
||||
id,
|
||||
USER_USER_PAT,
|
||||
false,
|
||||
Some(modify_json),
|
||||
));
|
||||
|
||||
// Await all project creation
|
||||
// Returns a mapping of:
|
||||
// project id -> test id
|
||||
let id_conversion: Arc<HashMap<u64, u64>> = Arc::new(
|
||||
futures::future::join_all(project_creation_futures)
|
||||
.await
|
||||
.into_iter()
|
||||
.collect(),
|
||||
);
|
||||
|
||||
// Create a second version for project 7
|
||||
let project_7 = api
|
||||
.get_project_deserialized_common(
|
||||
&format!("{test_name}-searchable-project-7"),
|
||||
USER_USER_PAT,
|
||||
)
|
||||
.await;
|
||||
api.add_public_version(
|
||||
project_7.id,
|
||||
"1.0.0",
|
||||
TestFile::build_random_jar(),
|
||||
None,
|
||||
None,
|
||||
USER_USER_PAT,
|
||||
)
|
||||
.await;
|
||||
|
||||
// Forcibly reset the search index
|
||||
let resp = api.reset_search_index().await;
|
||||
assert_status!(&resp, StatusCode::NO_CONTENT);
|
||||
|
||||
id_conversion
|
||||
}
|
||||
Reference in New Issue
Block a user