Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
finished re-adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Nov 17, 2023
1 parent 35ba6b7 commit 71220f9
Show file tree
Hide file tree
Showing 27 changed files with 627 additions and 564 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ derive-new = "0.5.9"

[dev-dependencies]
actix-http = "3.4.0"
json-patch = "*"
3 changes: 2 additions & 1 deletion src/models/v2/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ pub struct LegacyVersion {
/// A list of loaders this project supports (has a newtype struct)
pub loaders: Vec<Loader>,

// TODO: remove this once we have v3 testing, as this is a v3 field and tests for it should be isolated to v3
// TODO: should we remove this? as this is a v3 field and tests for it should be isolated to v3
// it allows us to keep tests that use this struct in common
pub ordering: Option<i32>,

pub id: VersionId,
Expand Down
1 change: 0 additions & 1 deletion src/routes/v3/project_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,6 @@ async fn create_initial_version(
&mut loader_field_enum_values,
)?;

println!("Made it past here");
let dependencies = version_data
.dependencies
.iter()
Expand Down
9 changes: 7 additions & 2 deletions tests/common/api_common/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::collections::HashMap;

use actix_web::dev::ServiceResponse;
use async_trait::async_trait;
use labrinth::{search::SearchResults, models::{teams::{ProjectPermissions, OrganizationPermissions}, projects::VersionType}};
use labrinth::{search::SearchResults, models::{teams::{ProjectPermissions, OrganizationPermissions}, projects::{VersionType, ProjectId}}};

use crate::common::{api_v2::ApiV2, api_v3::ApiV3};
use crate::common::{api_v2::ApiV2, api_v3::ApiV3, dummy_data::TestFile};

use super::{Api, ApiProject, ApiTags, ApiTeams, ApiVersion, models::{CommonProject, CommonImageData, CommonVersion}};

Expand Down Expand Up @@ -59,6 +59,7 @@ impl Api for GenericApi {
delegate_api_variant!(
#[async_trait(?Send)]
impl ApiProject for GenericApi {
[add_public_project, (CommonProject, Vec<CommonVersion>), slug: &str, version_jar: Option<TestFile>, modify_json: Option<json_patch::Patch>, pat: &str],
[remove_project, ServiceResponse, project_slug_or_id: &str, pat: &str],
[get_project, ServiceResponse, id_or_slug: &str, pat: &str],
[get_project_deserialized_common, CommonProject, id_or_slug: &str, pat: &str],
Expand Down Expand Up @@ -107,8 +108,12 @@ delegate_api_variant!(
delegate_api_variant!(
#[async_trait(?Send)]
impl ApiVersion for GenericApi {
[add_public_version, ServiceResponse, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: &str],
[add_public_version_deserialized_common, CommonVersion, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: &str],
[get_version, ServiceResponse, id_or_slug: &str, pat: &str],
[get_version_deserialized_common, CommonVersion, id_or_slug: &str, pat: &str],
[get_versions, ServiceResponse, ids_or_slugs: Vec<String>, pat: &str],
[get_versions_deserialized_common, Vec<CommonVersion>, ids_or_slugs: Vec<String>, pat: &str],
[edit_version, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: &str],
[get_version_from_hash, ServiceResponse, id_or_slug: &str, hash: &str, pat: &str],
[get_version_from_hash_deserialized_common, CommonVersion, id_or_slug: &str, hash: &str, pat: &str],
Expand Down
32 changes: 30 additions & 2 deletions tests/common/api_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use std::collections::HashMap;
use actix_web::dev::ServiceResponse;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use labrinth::{search::SearchResults, models::{teams::{ProjectPermissions, OrganizationPermissions}, projects::VersionType}, LabrinthConfig};
use labrinth::{search::SearchResults, models::{teams::{ProjectPermissions, OrganizationPermissions}, projects::{VersionType, ProjectId}}, LabrinthConfig};
use rust_decimal::Decimal;

use self::models::{CommonProject, CommonImageData, CommonLoaderData, CommonCategoryData, CommonTeamMember, CommonNotification, CommonVersion};

use super::dummy_data::TestFile;

pub mod generic;
pub mod models;

#[async_trait(?Send)]
pub trait ApiBuildable : Api {
async fn build(labrinth_config: LabrinthConfig) -> Self;
Expand All @@ -24,6 +25,13 @@ pub trait Api: ApiProject + ApiTags + ApiTeams + ApiVersion {

#[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: &str,
) -> (CommonProject, Vec<CommonVersion>);
async fn remove_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_project_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonProject;
Expand Down Expand Up @@ -66,8 +74,28 @@ pub trait ApiTeams {

#[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: &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: &str,
) -> CommonVersion;
async fn get_version(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_version_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonVersion;
async fn get_versions(&self, ids_or_slugs: Vec<String>, pat: &str) -> ServiceResponse;
async fn get_versions_deserialized_common(&self, ids_or_slugs: Vec<String>, pat: &str) -> Vec<CommonVersion>;
async fn edit_version(&self, id_or_slug: &str, patch: serde_json::Value, pat: &str) -> ServiceResponse;
async fn get_version_from_hash(&self, id_or_slug: &str, hash: &str, pat: &str) -> ServiceResponse;
async fn get_version_from_hash_deserialized_common(&self, id_or_slug: &str, hash: &str, pat: &str) -> CommonVersion;
Expand Down
3 changes: 3 additions & 0 deletions tests/common/api_common/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub struct CommonVersion {
pub requested_status: Option<VersionStatus>,
pub files: Vec<VersionFile>,
pub dependencies: Vec<Dependency>,

// TODO: should ordering be in v2?
pub ordering: Option<i32>,
}

#[derive(Deserialize)]
Expand Down
57 changes: 32 additions & 25 deletions tests/common/api_v2/project.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::{api_v2::request_data::ProjectCreationRequestData, api_common::{ApiProject, models::{CommonImageData, CommonProject, CommonVersion}, Api}};
use crate::common::{api_common::{ApiProject, models::{CommonImageData, CommonProject, CommonVersion}, Api}, dummy_data::TestFile};
use actix_http::StatusCode;
use actix_web::{
dev::ServiceResponse,
Expand All @@ -17,14 +17,42 @@ use std::collections::HashMap;

use crate::common::{asserts::assert_status, database::MOD_USER_PAT};

use super::ApiV2;
use super::{ApiV2, request_data::get_public_project_creation_data};

impl ApiV2 {
pub async fn add_public_project(
pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: &str) -> LegacyProject {
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_deserialized(
&self,
creation_data: ProjectCreationRequestData,
user_id_or_username: &str,
pat: &str,
) -> Vec<LegacyProject> {
let resp = self.get_user_projects(user_id_or_username, pat).await;
assert_eq!(resp.status(), 200);
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: &str,
) -> (CommonProject, Vec<CommonVersion>) {

let creation_data = get_public_project_creation_data(
slug,
version_jar,
modify_json,
);

// Add a project.
let req = TestRequest::post()
.uri("/v2/project")
Expand Down Expand Up @@ -62,27 +90,6 @@ impl ApiV2 {
(project, versions)
}

pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: &str) -> LegacyProject {
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_deserialized(
&self,
user_id_or_username: &str,
pat: &str,
) -> Vec<LegacyProject> {
let resp = self.get_user_projects(user_id_or_username, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}


}

#[async_trait(?Send)]
impl ApiProject for ApiV2 {
async fn remove_project(&self, project_slug_or_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v2/project/{project_slug_or_id}"))
Expand Down
24 changes: 19 additions & 5 deletions tests/common/api_v2/request_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -42,9 +46,14 @@ 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, &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 {
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(),
Expand All @@ -55,9 +64,10 @@ 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 {
json!({
let mut j = json!({
"file_parts": [version_jar.filename()],
"version_number": version_number,
"version_title": "start",
Expand All @@ -66,15 +76,19 @@ pub fn get_public_version_creation_data_json(
"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", jar)])
json!([get_public_version_creation_data_json("1.2.3", None, jar)])
} else {
json!([])
};
Expand Down
Loading

0 comments on commit 71220f9

Please sign in to comment.