Tests v2 recreate (#760)

* added common project information; setup for v2 test change

* all tests now use with_test_environment

* progress, failing

* finished re-adding tests

* prepare

* cargo sqlx prepare -- --tests

* fmt; clippy; prepare

* sqlx prepare

* adds version_create fix and corresponding test

* merge fixes; rev

* fmt, clippy, prepare

* test cargo sqlx prepare
This commit is contained in:
Wyatt Verchere
2023-11-25 13:42:39 -08:00
committed by GitHub
parent ade8c162cd
commit 172b93d07f
51 changed files with 7573 additions and 6631 deletions

View File

@@ -1,7 +1,12 @@
#![allow(dead_code)]
use super::environment::LocalService;
use actix_web::dev::ServiceResponse;
use super::{
api_common::{Api, ApiBuildable},
environment::LocalService,
};
use actix_web::{dev::ServiceResponse, test, App};
use async_trait::async_trait;
use labrinth::LabrinthConfig;
use std::rc::Rc;
pub mod oauth;
@@ -18,12 +23,23 @@ pub struct ApiV3 {
pub test_app: Rc<dyn LocalService>,
}
impl ApiV3 {
pub async fn call(&self, req: actix_http::Request) -> ServiceResponse {
#[async_trait(?Send)]
impl ApiBuildable for ApiV3 {
async fn build(labrinth_config: LabrinthConfig) -> Self {
let app = App::new().configure(|cfg| labrinth::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()
}
pub async fn reset_search_index(&self) -> ServiceResponse {
async fn reset_search_index(&self) -> ServiceResponse {
let req = actix_web::test::TestRequest::post()
.uri("/v3/admin/_force_reindex")
.append_header((

View File

@@ -10,7 +10,7 @@ use labrinth::auth::oauth::{
};
use reqwest::header::{AUTHORIZATION, LOCATION};
use crate::common::asserts::assert_status;
use crate::common::{api_common::Api, asserts::assert_status};
use super::ApiV3;

View File

@@ -13,7 +13,7 @@ use labrinth::{
use reqwest::header::AUTHORIZATION;
use serde_json::json;
use crate::common::asserts::assert_status;
use crate::common::{api_common::Api, asserts::assert_status};
use super::ApiV3;

View File

@@ -6,6 +6,8 @@ use bytes::Bytes;
use labrinth::models::{organizations::Organization, v3::projects::Project};
use serde_json::json;
use crate::common::api_common::Api;
use super::{request_data::ImageData, ApiV3};
impl ApiV3 {

View File

@@ -5,29 +5,36 @@ use actix_web::{
dev::ServiceResponse,
test::{self, TestRequest},
};
use async_trait::async_trait;
use bytes::Bytes;
use chrono::{DateTime, Utc};
use labrinth::{
models::v3::projects::{Project, Version},
search::SearchResults,
util::actix::AppendsMultipart,
};
use labrinth::{search::SearchResults, util::actix::AppendsMultipart};
use rust_decimal::Decimal;
use serde_json::json;
use crate::common::{asserts::assert_status, database::MOD_USER_PAT};
use super::{
request_data::{ImageData, ProjectCreationRequestData},
ApiV3,
use crate::common::{
api_common::{
models::{CommonImageData, CommonProject, CommonVersion},
Api, ApiProject,
},
asserts::assert_status,
database::MOD_USER_PAT,
dummy_data::TestFile,
};
impl ApiV3 {
pub async fn add_public_project(
use super::{request_data::get_public_project_creation_data, ApiV3};
#[async_trait(?Send)]
impl ApiProject for ApiV3 {
async fn add_public_project(
&self,
creation_data: ProjectCreationRequestData,
slug: &str,
version_jar: Option<TestFile>,
modify_json: Option<json_patch::Patch>,
pat: &str,
) -> (Project, Vec<Version>) {
) -> (CommonProject, Vec<CommonVersion>) {
let creation_data = get_public_project_creation_data(slug, version_jar, modify_json);
// Add a project.
let req = TestRequest::post()
.uri("/v3/project")
@@ -50,9 +57,8 @@ impl ApiV3 {
let resp = self.call(req).await;
assert_status(&resp, StatusCode::NO_CONTENT);
let project = self
.get_project_deserialized(&creation_data.slug, pat)
.await;
let project = self.get_project(&creation_data.slug, pat).await;
let project = test::read_body_json(project).await;
// Get project's versions
let req = TestRequest::get()
@@ -60,12 +66,12 @@ impl ApiV3 {
.append_header(("Authorization", pat))
.to_request();
let resp = self.call(req).await;
let versions: Vec<Version> = test::read_body_json(resp).await;
let versions: Vec<CommonVersion> = test::read_body_json(resp).await;
(project, versions)
}
pub async fn remove_project(&self, project_slug_or_id: &str, pat: &str) -> ServiceResponse {
async fn remove_project(&self, project_slug_or_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v3/project/{project_slug_or_id}"))
.append_header(("Authorization", pat))
@@ -75,20 +81,21 @@ impl ApiV3 {
resp
}
pub async fn get_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse {
async fn get_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/project/{id_or_slug}"))
.append_header(("Authorization", pat))
.to_request();
self.call(req).await
}
pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: &str) -> Project {
async fn get_project_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonProject {
let resp = self.get_project(id_or_slug, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_user_projects(&self, user_id_or_username: &str, pat: &str) -> ServiceResponse {
async fn get_user_projects(&self, user_id_or_username: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/user/{}/projects", user_id_or_username))
.append_header(("Authorization", pat))
@@ -96,17 +103,17 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_user_projects_deserialized(
async fn get_user_projects_deserialized_common(
&self,
user_id_or_username: &str,
pat: &str,
) -> Vec<Project> {
) -> Vec<CommonProject> {
let resp = self.get_user_projects(user_id_or_username, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn edit_project(
async fn edit_project(
&self,
id_or_slug: &str,
patch: serde_json::Value,
@@ -121,14 +128,14 @@ impl ApiV3 {
self.call(req).await
}
pub async fn edit_project_bulk(
async fn edit_project_bulk(
&self,
ids_or_slugs: impl IntoIterator<Item = &str>,
ids_or_slugs: &[&str],
patch: serde_json::Value,
pat: &str,
) -> ServiceResponse {
let projects_str = ids_or_slugs
.into_iter()
.iter()
.map(|s| format!("\"{}\"", s))
.collect::<Vec<_>>()
.join(",");
@@ -144,10 +151,10 @@ impl ApiV3 {
self.call(req).await
}
pub async fn edit_project_icon(
async fn edit_project_icon(
&self,
id_or_slug: &str,
icon: Option<ImageData>,
icon: Option<CommonImageData>,
pat: &str,
) -> ServiceResponse {
if let Some(icon) = icon {
@@ -173,7 +180,7 @@ impl ApiV3 {
}
}
pub async fn search_deserialized(
async fn search_deserialized_common(
&self,
query: Option<&str>,
facets: Option<serde_json::Value>,
@@ -200,7 +207,9 @@ impl ApiV3 {
assert_eq!(status, 200);
test::read_body_json(resp).await
}
}
impl ApiV3 {
pub async fn get_analytics_revenue(
&self,
id_or_slugs: Vec<&str>,

View File

@@ -28,8 +28,12 @@ pub struct ImageData {
pub fn get_public_project_creation_data(
slug: &str,
version_jar: Option<TestFile>,
modify_json: Option<json_patch::Patch>,
) -> ProjectCreationRequestData {
let json_data = get_public_project_creation_data_json(slug, version_jar.as_ref());
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(),
@@ -42,14 +46,16 @@ 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<impl FnOnce(&mut serde_json::Value)>,
modify_json: Option<json_patch::Patch>,
) -> VersionCreationRequestData {
let mut json_data = get_public_version_creation_data_json(version_number, &version_jar);
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 {
modify_json(&mut json_data);
json_patch::patch(&mut json_data, &modify_json).unwrap();
}
let multipart_data = get_public_creation_data_multipart(&json_data, Some(&version_jar));
@@ -62,6 +68,7 @@ pub fn get_public_version_creation_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";
@@ -82,6 +89,9 @@ pub fn get_public_version_creation_data_json(
if is_modpack {
j["mrpack_loaders"] = json!(["fabric"]);
}
if let Some(ordering) = ordering {
j["ordering"] = json!(ordering);
}
j
}
@@ -90,7 +100,7 @@ pub fn get_public_project_creation_data_json(
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", jar)])
json!([get_public_version_creation_data_json("1.2.3", None, jar)])
} else {
json!([])
};

View File

@@ -2,18 +2,23 @@ use actix_web::{
dev::ServiceResponse,
test::{self, TestRequest},
};
use async_trait::async_trait;
use labrinth::database::models::loader_fields::LoaderFieldEnumValue;
use labrinth::routes::v3::tags::GameData;
use labrinth::{
database::models::loader_fields::LoaderFieldEnumValue,
routes::v3::tags::{CategoryData, LoaderData},
};
use crate::common::database::ADMIN_USER_PAT;
use crate::common::{
api_common::{
models::{CommonCategoryData, CommonLoaderData},
Api, ApiTags,
},
database::ADMIN_USER_PAT,
};
use super::ApiV3;
impl ApiV3 {
pub async fn get_loaders(&self) -> ServiceResponse {
#[async_trait(?Send)]
impl ApiTags for ApiV3 {
async fn get_loaders(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v3/tag/loader")
.append_header(("Authorization", ADMIN_USER_PAT))
@@ -21,13 +26,13 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_loaders_deserialized(&self) -> Vec<LoaderData> {
async fn get_loaders_deserialized_common(&self) -> Vec<CommonLoaderData> {
let resp = self.get_loaders().await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_categories(&self) -> ServiceResponse {
async fn get_categories(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v3/tag/category")
.append_header(("Authorization", ADMIN_USER_PAT))
@@ -35,12 +40,14 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_categories_deserialized(&self) -> Vec<CategoryData> {
async fn get_categories_deserialized_common(&self) -> Vec<CommonCategoryData> {
let resp = self.get_categories().await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
}
impl ApiV3 {
pub async fn get_loader_field_variants(&self, loader_field: &str) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/loader_field?loader_field={}", loader_field))
@@ -59,7 +66,7 @@ impl ApiV3 {
}
// TODO: fold this into v3 API of other v3 testing PR
pub async fn get_games(&self) -> ServiceResponse {
async fn get_games(&self) -> ServiceResponse {
let req = TestRequest::get()
.uri("/v3/games")
.append_header(("Authorization", ADMIN_USER_PAT))

View File

@@ -1,17 +1,22 @@
use actix_http::StatusCode;
use actix_web::{dev::ServiceResponse, test};
use labrinth::models::{
notifications::Notification,
teams::{OrganizationPermissions, ProjectPermissions, TeamMember},
};
use async_trait::async_trait;
use labrinth::models::teams::{OrganizationPermissions, ProjectPermissions};
use serde_json::json;
use crate::common::asserts::assert_status;
use crate::common::{
api_common::{
models::{CommonNotification, CommonTeamMember},
Api, ApiTeams,
},
asserts::assert_status,
};
use super::ApiV3;
impl ApiV3 {
pub async fn get_team_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
#[async_trait(?Send)]
impl ApiTeams for ApiV3 {
async fn get_team_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/team/{id_or_title}/members"))
.append_header(("Authorization", pat))
@@ -19,17 +24,17 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_team_members_deserialized(
async fn get_team_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
) -> Vec<TeamMember> {
) -> Vec<CommonTeamMember> {
let resp = self.get_team_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_project_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_project_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/project/{id_or_title}/members"))
.append_header(("Authorization", pat))
@@ -37,17 +42,17 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_project_members_deserialized(
async fn get_project_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
) -> Vec<TeamMember> {
) -> Vec<CommonTeamMember> {
let resp = self.get_project_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_organization_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
async fn get_organization_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/organization/{id_or_title}/members"))
.append_header(("Authorization", pat))
@@ -55,17 +60,17 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_organization_members_deserialized(
async fn get_organization_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
) -> Vec<TeamMember> {
) -> Vec<CommonTeamMember> {
let resp = self.get_organization_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn join_team(&self, team_id: &str, pat: &str) -> ServiceResponse {
async fn join_team(&self, team_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!("/v3/team/{team_id}/join"))
.append_header(("Authorization", pat))
@@ -73,12 +78,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn remove_from_team(
&self,
team_id: &str,
user_id: &str,
pat: &str,
) -> ServiceResponse {
async fn remove_from_team(&self, team_id: &str, user_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v3/team/{team_id}/members/{user_id}"))
.append_header(("Authorization", pat))
@@ -86,7 +86,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn edit_team_member(
async fn edit_team_member(
&self,
team_id: &str,
user_id: &str,
@@ -101,7 +101,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn transfer_team_ownership(
async fn transfer_team_ownership(
&self,
team_id: &str,
user_id: &str,
@@ -117,7 +117,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_user_notifications(&self, user_id: &str, pat: &str) -> ServiceResponse {
async fn get_user_notifications(&self, user_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v3/user/{user_id}/notifications"))
.append_header(("Authorization", pat))
@@ -125,28 +125,24 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_user_notifications_deserialized(
async fn get_user_notifications_deserialized_common(
&self,
user_id: &str,
pat: &str,
) -> Vec<Notification> {
) -> Vec<CommonNotification> {
let resp = self.get_user_notifications(user_id, pat).await;
assert_status(&resp, StatusCode::OK);
test::read_body_json(resp).await
}
pub async fn mark_notification_read(
&self,
notification_id: &str,
pat: &str,
) -> ServiceResponse {
async fn mark_notification_read(&self, notification_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v3/notification/{notification_id}"))
.append_header(("Authorization", pat))
.to_request();
self.call(req).await
}
pub async fn add_user_to_team(
async fn add_user_to_team(
&self,
team_id: &str,
user_id: &str,
@@ -166,7 +162,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn delete_notification(&self, notification_id: &str, pat: &str) -> ServiceResponse {
async fn delete_notification(&self, notification_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v3/notification/{notification_id}"))
.append_header(("Authorization", pat))

View File

@@ -1,59 +1,58 @@
use std::collections::HashMap;
use super::{request_data::get_public_version_creation_data, ApiV3};
use crate::common::{
api_common::{models::CommonVersion, Api, ApiVersion},
asserts::assert_status,
dummy_data::TestFile,
};
use actix_http::{header::AUTHORIZATION, StatusCode};
use actix_web::{
dev::ServiceResponse,
test::{self, TestRequest},
};
use async_trait::async_trait;
use labrinth::{
models::{projects::VersionType, v3::projects::Version},
models::{
projects::{ProjectId, VersionType},
v3::projects::Version,
},
routes::v3::version_file::FileUpdateData,
util::actix::AppendsMultipart,
};
use serde_json::json;
use crate::common::asserts::assert_status;
use super::{request_data::VersionCreationRequestData, ApiV3};
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(
&self,
creation_data: VersionCreationRequestData,
pat: &str,
) -> ServiceResponse {
// Add a project.
let req = TestRequest::post()
.uri("/v3/version")
.append_header(("Authorization", pat))
.set_multipart(creation_data.segment_data)
.to_request();
self.call(req).await
}
pub async fn add_public_version_deserialized(
&self,
creation_data: VersionCreationRequestData,
project_id: ProjectId,
version_number: &str,
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
) -> Version {
let resp = self.add_public_version(creation_data, pat).await;
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();
self.get_version_deserialized(version_id, pat).await
}
pub async fn get_version(&self, id: &str, pat: &str) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/version/{id}"))
.append_header(("Authorization", pat))
.to_request();
self.call(req).await
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: &str) -> Version {
@@ -62,7 +61,101 @@ impl ApiV3 {
test::read_body_json(resp).await
}
pub async fn edit_version(
pub async fn update_individual_files(
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v3/version_files/update_individual")
.append_header(("Authorization", 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: &str,
) -> HashMap<String, Version> {
let resp = self.update_individual_files(algorithm, hashes, pat).await;
assert_eq!(resp.status(), 200);
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: &str,
) -> ServiceResponse {
let creation_data = get_public_version_creation_data(
project_id,
version_number,
version_jar,
ordering,
modify_json,
);
// Add a versiom.
let req = TestRequest::post()
.uri("/v3/version")
.append_header(("Authorization", 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: &str,
) -> CommonVersion {
let resp = self
.add_public_version(
project_id,
version_number,
version_jar,
ordering,
modify_json,
pat,
)
.await;
assert_status(&resp, StatusCode::OK);
test::read_body_json(resp).await
}
async fn get_version(&self, id: &str, pat: &str) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v3/version/{id}"))
.append_header(("Authorization", pat))
.to_request();
self.call(req).await
}
async fn get_version_deserialized_common(&self, id: &str, pat: &str) -> CommonVersion {
let resp = self.get_version(id, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
async fn edit_version(
&self,
version_id: &str,
patch: serde_json::Value,
@@ -77,7 +170,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_version_from_hash(
async fn get_version_from_hash(
&self,
hash: &str,
algorithm: &str,
@@ -90,18 +183,18 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_version_from_hash_deserialized(
async fn get_version_from_hash_deserialized_common(
&self,
hash: &str,
algorithm: &str,
pat: &str,
) -> Version {
) -> CommonVersion {
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_versions_from_hashes(
async fn get_versions_from_hashes(
&self,
hashes: &[&str],
algorithm: &str,
@@ -118,18 +211,18 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_versions_from_hashes_deserialized(
async fn get_versions_from_hashes_deserialized_common(
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
) -> HashMap<String, Version> {
) -> HashMap<String, CommonVersion> {
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_update_from_hash(
async fn get_update_from_hash(
&self,
hash: &str,
algorithm: &str,
@@ -161,7 +254,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn get_update_from_hash_deserialized(
async fn get_update_from_hash_deserialized_common(
&self,
hash: &str,
algorithm: &str,
@@ -169,7 +262,7 @@ impl ApiV3 {
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
) -> Version {
) -> CommonVersion {
let resp = self
.get_update_from_hash(hash, algorithm, loaders, game_versions, version_types, pat)
.await;
@@ -177,7 +270,7 @@ impl ApiV3 {
test::read_body_json(resp).await
}
pub async fn update_files(
async fn update_files(
&self,
algorithm: &str,
hashes: Vec<String>,
@@ -210,7 +303,7 @@ impl ApiV3 {
self.call(req).await
}
pub async fn update_files_deserialized(
async fn update_files_deserialized_common(
&self,
algorithm: &str,
hashes: Vec<String>,
@@ -218,7 +311,7 @@ impl ApiV3 {
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
) -> HashMap<String, Version> {
) -> HashMap<String, CommonVersion> {
let resp = self
.update_files(
algorithm,
@@ -233,37 +326,9 @@ impl ApiV3 {
test::read_body_json(resp).await
}
pub async fn update_individual_files(
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v3/version_files/update_individual")
.append_header(("Authorization", 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: &str,
) -> HashMap<String, Version> {
let resp = self.update_individual_files(algorithm, hashes, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
// TODO: Not all fields are tested currently in the v3 tests, only the v2-v3 relevant ones are
#[allow(clippy::too_many_arguments)]
pub async fn get_project_versions(
async fn get_project_versions(
&self,
project_id_slug: &str,
game_versions: Option<Vec<String>>,
@@ -313,7 +378,7 @@ impl ApiV3 {
}
#[allow(clippy::too_many_arguments)]
pub async fn get_project_versions_deserialized(
async fn get_project_versions_deserialized_common(
&self,
slug: &str,
game_versions: Option<Vec<String>>,
@@ -323,7 +388,7 @@ impl ApiV3 {
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
) -> Vec<Version> {
) -> Vec<CommonVersion> {
let resp = self
.get_project_versions(
slug,
@@ -341,68 +406,7 @@ impl ApiV3 {
}
// TODO: remove redundancy in these functions
pub async fn create_default_version(
&self,
project_id: &str,
ordering: Option<i32>,
pat: &str,
) -> Version {
let json_data = json!(
{
"project_id": project_id,
"file_parts": ["basic-mod-different.jar"],
"version_number": "1.2.3.4",
"version_title": "start",
"dependencies": [],
"game_versions": ["1.20.1"] ,
"client_side": "required",
"server_side": "optional",
"release_channel": "release",
"loaders": ["fabric"],
"featured": true,
"ordering": ordering,
}
);
let json_segment = labrinth::util::actix::MultipartSegment {
name: "data".to_string(),
filename: None,
content_type: Some("application/json".to_string()),
data: labrinth::util::actix::MultipartSegmentData::Text(
serde_json::to_string(&json_data).unwrap(),
),
};
let file_segment = labrinth::util::actix::MultipartSegment {
name: "basic-mod-different.jar".to_string(),
filename: Some("basic-mod.jar".to_string()),
content_type: Some("application/java-archive".to_string()),
data: labrinth::util::actix::MultipartSegmentData::Binary(
include_bytes!("../../../tests/files/basic-mod-different.jar").to_vec(),
),
};
let request = test::TestRequest::post()
.uri("/v3/version")
.set_multipart(vec![json_segment.clone(), file_segment.clone()])
.append_header((AUTHORIZATION, pat))
.to_request();
let resp = self.call(request).await;
assert_status(&resp, StatusCode::OK);
test::read_body_json(resp).await
}
pub async fn get_versions(&self, version_ids: Vec<String>, pat: &str) -> Vec<Version> {
let ids = url_encode_json_serialized_vec(&version_ids);
let request = test::TestRequest::get()
.uri(&format!("/v3/versions?ids={}", ids))
.append_header((AUTHORIZATION, pat))
.to_request();
let resp = self.call(request).await;
assert_status(&resp, StatusCode::OK);
test::read_body_json(resp).await
}
pub async fn edit_version_ordering(
async fn edit_version_ordering(
&self,
version_id: &str,
ordering: Option<i32>,
@@ -419,4 +423,23 @@ impl ApiV3 {
.to_request();
self.call(request).await
}
async fn get_versions(&self, version_ids: Vec<String>, pat: &str) -> ServiceResponse {
let ids = url_encode_json_serialized_vec(&version_ids);
let request = test::TestRequest::get()
.uri(&format!("/v3/versions?ids={}", ids))
.append_header((AUTHORIZATION, pat))
.to_request();
self.call(request).await
}
async fn get_versions_deserialized_common(
&self,
version_ids: Vec<String>,
pat: &str,
) -> Vec<CommonVersion> {
let resp = self.get_versions(version_ids, pat).await;
assert_status(&resp, StatusCode::OK);
test::read_body_json(resp).await
}
}