Misc v3 linear tasks (#767)

* v3_reroute 404 error

* hash change

* fixed issue with error conversion

* added new model confirmation tests
+ title name change

* renaming, fields

* owner; test changes

* clippy prepare

* fmt

* merge fixes

* clippy

* working merge

* revs

* merge fixes
This commit is contained in:
Wyatt Verchere
2023-12-01 19:15:00 -08:00
committed by GitHub
parent 2d92b08404
commit a70df067bc
119 changed files with 2897 additions and 1334 deletions

View File

@@ -1,6 +1,7 @@
use crate::common::{
api_common::{
models::{CommonImageData, CommonProject, CommonVersion},
request_data::ProjectCreationRequestData,
Api, ApiProject,
},
dummy_data::TestFile,
@@ -20,7 +21,10 @@ use serde_json::json;
use crate::common::{asserts::assert_status, database::MOD_USER_PAT};
use super::{request_data::get_public_project_creation_data, ApiV2};
use super::{
request_data::{self, get_public_project_creation_data},
ApiV2,
};
impl ApiV2 {
pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: &str) -> LegacyProject {
@@ -80,17 +84,13 @@ impl ApiProject for ApiV2 {
let creation_data = get_public_project_creation_data(slug, version_jar, modify_json);
// Add a project.
let req = TestRequest::post()
.uri("/v2/project")
.append_header(("Authorization", pat))
.set_multipart(creation_data.segment_data)
.to_request();
let resp = self.call(req).await;
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/{}", creation_data.slug))
.uri(&format!("/v2/project/{}", slug))
.append_header(("Authorization", MOD_USER_PAT))
.set_json(json!(
{
@@ -101,13 +101,11 @@ impl ApiProject for ApiV2 {
let resp = self.call(req).await;
assert_status(&resp, StatusCode::NO_CONTENT);
let project = self
.get_project_deserialized_common(&creation_data.slug, pat)
.await;
let project = self.get_project_deserialized_common(&slug, pat).await;
// Get project's versions
let req = TestRequest::get()
.uri(&format!("/v2/project/{}/version", creation_data.slug))
.uri(&format!("/v2/project/{}/version", slug))
.append_header(("Authorization", pat))
.to_request();
let resp = self.call(req).await;
@@ -116,6 +114,27 @@ impl ApiProject for ApiV2 {
(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: &str,
) -> ServiceResponse {
let req = TestRequest::post()
.uri("/v2/project")
.append_header(("Authorization", pat))
.set_multipart(creation_data.segment_data)
.to_request();
self.call(req).await
}
async fn remove_project(&self, project_slug_or_id: &str, pat: &str) -> ServiceResponse {
let req = test::TestRequest::delete()
.uri(&format!("/v2/project/{project_slug_or_id}"))
@@ -137,7 +156,11 @@ impl ApiProject for ApiV2 {
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
// 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_user_projects(&self, user_id_or_username: &str, pat: &str) -> ServiceResponse {
@@ -155,7 +178,11 @@ impl ApiProject for ApiV2 {
) -> 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
// 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(

View File

@@ -1,30 +1,15 @@
#![allow(dead_code)]
use serde_json::json;
use crate::common::dummy_data::{DummyImage, TestFile};
use crate::common::{
api_common::request_data::{ImageData, ProjectCreationRequestData, VersionCreationRequestData},
dummy_data::{DummyImage, TestFile},
};
use labrinth::{
models::projects::ProjectId,
util::actix::{MultipartSegment, MultipartSegmentData},
};
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>,
}
pub fn get_public_project_creation_data(
slug: &str,
version_jar: Option<TestFile>,

View File

@@ -72,7 +72,11 @@ impl ApiTags for ApiV2 {
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
// 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 {
@@ -86,6 +90,10 @@ impl ApiTags for ApiV2 {
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
// 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()
}
}

View File

@@ -1,7 +1,10 @@
use actix_http::StatusCode;
use actix_web::{dev::ServiceResponse, test};
use async_trait::async_trait;
use labrinth::models::teams::{OrganizationPermissions, ProjectPermissions};
use labrinth::models::{
teams::{OrganizationPermissions, ProjectPermissions},
v2::{notifications::LegacyNotification, teams::LegacyTeamMember},
};
use serde_json::json;
use crate::common::{
@@ -14,6 +17,38 @@ use crate::common::{
use super::ApiV2;
impl ApiV2 {
pub async fn get_organization_members_deserialized(
&self,
id_or_title: &str,
pat: &str,
) -> Vec<LegacyTeamMember> {
let resp = self.get_organization_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_team_members_deserialized(
&self,
team_id: &str,
pat: &str,
) -> Vec<LegacyTeamMember> {
let resp = self.get_team_members(team_id, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn get_user_notifications_deserialized(
&self,
user_id: &str,
pat: &str,
) -> Vec<LegacyNotification> {
let resp = self.get_user_notifications(user_id, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
}
#[async_trait(?Send)]
impl ApiTeams for ApiV2 {
async fn get_team_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse {
@@ -31,6 +66,9 @@ impl ApiTeams for ApiV2 {
) -> Vec<CommonTeamMember> {
let resp = self.get_team_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
// TODO: Note, this does NOT deserialize to any other struct first, as currently TeamMember is the same in v2 and v3.
// CommonTeamMember = TeamMember (v3)
// This may yet change, so we should keep common struct.
test::read_body_json(resp).await
}
@@ -49,6 +87,9 @@ impl ApiTeams for ApiV2 {
) -> Vec<CommonTeamMember> {
let resp = self.get_project_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
// TODO: Note, this does NOT deserialize to any other struct first, as currently TeamMember is the same in v2 and v3.
// CommonTeamMember = TeamMember (v3)
// This may yet change, so we should keep common struct.
test::read_body_json(resp).await
}
@@ -67,6 +108,9 @@ impl ApiTeams for ApiV2 {
) -> Vec<CommonTeamMember> {
let resp = self.get_organization_members(id_or_title, pat).await;
assert_eq!(resp.status(), 200);
// TODO: Note, this does NOT deserialize to any other struct first, as currently TeamMember is the same in v2 and v3.
// CommonTeamMember = TeamMember (v3)
// This may yet change, so we should keep common struct.
test::read_body_json(resp).await
}
@@ -132,7 +176,11 @@ impl ApiTeams for ApiV2 {
) -> Vec<CommonNotification> {
let resp = self.get_user_notifications(user_id, pat).await;
assert_status(&resp, StatusCode::OK);
test::read_body_json(resp).await
// 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 mark_notification_read(&self, notification_id: &str, pat: &str) -> ServiceResponse {

View File

@@ -133,7 +133,11 @@ impl ApiVersion for ApiV2 {
)
.await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
// 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: &str) -> ServiceResponse {
@@ -147,7 +151,11 @@ impl ApiVersion for ApiV2 {
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
// 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 edit_version(
@@ -186,7 +194,11 @@ impl ApiVersion for ApiV2 {
) -> CommonVersion {
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
// 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(
@@ -214,7 +226,11 @@ impl ApiVersion for ApiV2 {
) -> 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
// 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(
@@ -253,7 +269,11 @@ impl ApiVersion for ApiV2 {
.get_update_from_hash(hash, algorithm, loaders, game_versions, version_types, pat)
.await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
// 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(
@@ -299,7 +319,11 @@ impl ApiVersion for ApiV2 {
)
.await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
// 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
@@ -378,7 +402,11 @@ impl ApiVersion for ApiV2 {
)
.await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
// 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(
@@ -415,6 +443,10 @@ impl ApiVersion for ApiV2 {
) -> Vec<CommonVersion> {
let resp = self.get_versions(version_ids, pat).await;
assert_status(&resp, StatusCode::OK);
test::read_body_json(resp).await
// 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()
}
}