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

Commit

Permalink
initial push
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Dec 4, 2023
1 parent b2be4a7 commit 40ef0f5
Show file tree
Hide file tree
Showing 32 changed files with 1,234 additions and 1,043 deletions.
36 changes: 35 additions & 1 deletion src/routes/v2/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::models::projects::{Dependency, FileType, Version, VersionStatus, Vers
use crate::models::v2::projects::LegacyVersion;
use crate::queue::session::AuthQueue;
use crate::routes::{v2_reroute, v3};
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse, post};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use validator::Validate;
Expand All @@ -22,6 +23,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.service(version_get)
.service(version_delete)
.service(version_edit)
.service(version_schedule)
.service(super::version_creation::upload_file_to_version),
);
}
Expand Down Expand Up @@ -270,3 +272,35 @@ pub async fn version_delete(
.await
.or_else(v2_reroute::flatten_404_error)
}

#[derive(Deserialize)]
pub struct SchedulingData {
pub time : DateTime<Utc>,
pub requested_status : VersionStatus,
}

#[post("{id}/schedule")]
pub async fn version_schedule(
req: HttpRequest,
info: web::Path<(VersionId,)>,
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
scheduling_data: web::Json<SchedulingData>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let scheduling_data = scheduling_data.into_inner();
let scheduling_data = v3::versions::SchedulingData {
time: scheduling_data.time,
requested_status: scheduling_data.requested_status,
};
v3::versions::version_schedule(
req,
info,
pool,
redis,
web::Json(scheduling_data),
session_queue,
)
.await
.or_else(v2_reroute::flatten_404_error)
}
26 changes: 12 additions & 14 deletions tests/analytics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use actix_web::test;
use chrono::{DateTime, Duration, Utc};
use common::permissions::PermissionsTest;
use common::permissions::PermissionsTestContext;
Expand Down Expand Up @@ -166,15 +165,15 @@ pub async fn permissions_analytics_revenue() {
.team_id
.clone();

let api = &test_env.api;

let view_analytics = ProjectPermissions::VIEW_ANALYTICS;

// first, do check with a project
let req_gen = |ctx: &PermissionsTestContext| {
let projects_string = serde_json::to_string(&vec![ctx.project_id]).unwrap();
let projects_string = urlencoding::encode(&projects_string);
test::TestRequest::get().uri(&format!(
"/v3/analytics/revenue?project_ids={projects_string}&resolution_minutes=5",
))
let req_gen = |ctx: PermissionsTestContext| async move {
let project_id = ctx.project_id.unwrap();
let ids_or_slugs = vec![project_id.as_str()];
api.get_analytics_revenue(ids_or_slugs, false, None, None, Some(5), ctx.test_pat.as_deref()).await
};

PermissionsTest::new(&test_env)
Expand All @@ -197,13 +196,12 @@ pub async fn permissions_analytics_revenue() {

// Now with a version
// Need to use alpha
let req_gen = |_: &PermissionsTestContext| {
let versions_string = serde_json::to_string(&vec![alpha_version_id.clone()]).unwrap();
let versions_string = urlencoding::encode(&versions_string);
test::TestRequest::get().uri(&format!(
"/v3/analytics/revenue?version_ids={versions_string}&resolution_minutes=5",
))
};
let req_gen = |ctx: PermissionsTestContext| {
let alpha_version_id = alpha_version_id.clone();
async move {
let ids_or_slugs = vec![alpha_version_id.as_str()];
api.get_analytics_revenue(ids_or_slugs, true, None, None, Some(5), ctx.test_pat.as_deref()).await
}};

PermissionsTest::new(&test_env)
.with_failure_codes(vec![200, 401])
Expand Down
97 changes: 52 additions & 45 deletions tests/common/api_common/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use labrinth::models::{
use crate::common::{api_v2::ApiV2, api_v3::ApiV3, dummy_data::TestFile};

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

Expand Down Expand Up @@ -65,17 +65,21 @@ impl Api for GenericApi {
delegate_api_variant!(
#[async_trait(?Send)]
impl ApiProject for GenericApi {
[add_public_project, (CommonProject, Vec<CommonVersion>), slug: &str, version_jar: Option<TestFile>, modify_json: Option<json_patch::Patch>, pat: &str],
[add_public_project, (CommonProject, Vec<CommonVersion>), slug: &str, version_jar: Option<TestFile>, modify_json: Option<json_patch::Patch>, pat: Option<&str>],
[get_public_project_creation_data_json, serde_json::Value, slug: &str, version_jar: Option<&TestFile>],
[create_project, ServiceResponse, creation_data: ProjectCreationRequestData, pat: &str],
[remove_project, ServiceResponse, project_slug_or_id: &str, pat: &str],
[get_project, ServiceResponse, id_or_slug: &str, pat: &str],
[get_project_deserialized_common, CommonProject, id_or_slug: &str, pat: &str],
[get_user_projects, ServiceResponse, user_id_or_username: &str, pat: &str],
[get_user_projects_deserialized_common, Vec<CommonProject>, user_id_or_username: &str, pat: &str],
[edit_project, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: &str],
[edit_project_bulk, ServiceResponse, ids_or_slugs: &[&str], patch: serde_json::Value, pat: &str],
[edit_project_icon, ServiceResponse, id_or_slug: &str, icon: Option<CommonImageData>, pat: &str],
[create_project, ServiceResponse, creation_data: ProjectCreationRequestData, pat: Option<&str>],
[remove_project, ServiceResponse, project_slug_or_id: &str, pat: Option<&str>],
[get_project, ServiceResponse, id_or_slug: &str, pat: Option<&str>],
[get_project_deserialized_common, CommonProject, id_or_slug: &str, pat: Option<&str>],
[get_user_projects, ServiceResponse, user_id_or_username: &str, pat: Option<&str>],
[get_user_projects_deserialized_common, Vec<CommonProject>, user_id_or_username: &str, pat: Option<&str>],
[edit_project, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: Option<&str>],
[edit_project_bulk, ServiceResponse, ids_or_slugs: &[&str], patch: serde_json::Value, pat: Option<&str>],
[edit_project_icon, ServiceResponse, id_or_slug: &str, icon: Option<ImageData>, pat: Option<&str>],
[schedule_project, ServiceResponse, id_or_slug: &str, requested_status: &str, date : chrono::DateTime<chrono::Utc>, pat: Option<&str>],
[add_gallery_item, ServiceResponse, id_or_slug: &str, image: ImageData, featured: bool, title: Option<String>, description: Option<String>, ordering: Option<i32>, pat: Option<&str>],
[remove_gallery_item, ServiceResponse, id_or_slug: &str, image_url: &str, pat: Option<&str>],
[edit_gallery_item, ServiceResponse, id_or_slug: &str, image_url: &str, patch: HashMap<String, String>, pat: Option<&str>],
}
);

Expand All @@ -92,44 +96,47 @@ delegate_api_variant!(
delegate_api_variant!(
#[async_trait(?Send)]
impl ApiTeams for GenericApi {
[get_team_members, ServiceResponse, team_id: &str, pat: &str],
[get_team_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, team_id: &str, pat: &str],
[get_project_members, ServiceResponse, id_or_slug: &str, pat: &str],
[get_project_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_slug: &str, pat: &str],
[get_organization_members, ServiceResponse, id_or_title: &str, pat: &str],
[get_organization_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_title: &str, pat: &str],
[join_team, ServiceResponse, team_id: &str, pat: &str],
[remove_from_team, ServiceResponse, team_id: &str, user_id: &str, pat: &str],
[edit_team_member, ServiceResponse, team_id: &str, user_id: &str, patch: serde_json::Value, pat: &str],
[transfer_team_ownership, ServiceResponse, team_id: &str, user_id: &str, pat: &str],
[get_user_notifications, ServiceResponse, user_id: &str, pat: &str],
[get_user_notifications_deserialized_common, Vec<crate::common::api_common::models::CommonNotification>, user_id: &str, pat: &str],
[mark_notification_read, ServiceResponse, notification_id: &str, pat: &str],
[add_user_to_team, ServiceResponse, team_id: &str, user_id: &str, project_permissions: Option<ProjectPermissions>, organization_permissions: Option<OrganizationPermissions>, pat: &str],
[delete_notification, ServiceResponse, notification_id: &str, pat: &str],
[get_team_members, ServiceResponse, team_id: &str, pat: Option<&str>],
[get_team_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, team_id: &str, pat: Option<&str>],
[get_project_members, ServiceResponse, id_or_slug: &str, pat: Option<&str>],
[get_project_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_slug: &str, pat: Option<&str>],
[get_organization_members, ServiceResponse, id_or_title: &str, pat: Option<&str>],
[get_organization_members_deserialized_common, Vec<crate::common::api_common::models::CommonTeamMember>, id_or_title: &str, pat: Option<&str>],
[join_team, ServiceResponse, team_id: &str, pat: Option<&str>],
[remove_from_team, ServiceResponse, team_id: &str, user_id: &str, pat: Option<&str>],
[edit_team_member, ServiceResponse, team_id: &str, user_id: &str, patch: serde_json::Value, pat: Option<&str>],
[transfer_team_ownership, ServiceResponse, team_id: &str, user_id: &str, pat: Option<&str>],
[get_user_notifications, ServiceResponse, user_id: &str, pat: Option<&str>],
[get_user_notifications_deserialized_common, Vec<crate::common::api_common::models::CommonNotification>, user_id: &str, pat: Option<&str>],
[mark_notification_read, ServiceResponse, notification_id: &str, pat: Option<&str>],
[add_user_to_team, ServiceResponse, team_id: &str, user_id: &str, project_permissions: Option<ProjectPermissions>, organization_permissions: Option<OrganizationPermissions>, pat: Option<&str>],
[delete_notification, ServiceResponse, notification_id: &str, pat: Option<&str>],
}
);

delegate_api_variant!(
#[async_trait(?Send)]
impl ApiVersion for GenericApi {
[add_public_version, ServiceResponse, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: &str],
[add_public_version_deserialized_common, CommonVersion, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: &str],
[get_version, ServiceResponse, id_or_slug: &str, pat: &str],
[get_version_deserialized_common, CommonVersion, id_or_slug: &str, pat: &str],
[get_versions, ServiceResponse, ids_or_slugs: Vec<String>, pat: &str],
[get_versions_deserialized_common, Vec<CommonVersion>, ids_or_slugs: Vec<String>, pat: &str],
[edit_version, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: &str],
[get_version_from_hash, ServiceResponse, id_or_slug: &str, hash: &str, pat: &str],
[get_version_from_hash_deserialized_common, CommonVersion, id_or_slug: &str, hash: &str, pat: &str],
[get_versions_from_hashes, ServiceResponse, hashes: &[&str], algorithm: &str, pat: &str],
[get_versions_from_hashes_deserialized_common, HashMap<String, CommonVersion>, hashes: &[&str], algorithm: &str, pat: &str],
[get_update_from_hash, ServiceResponse, hash: &str, algorithm: &str, loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: &str],
[get_update_from_hash_deserialized_common, CommonVersion, hash: &str, algorithm: &str,loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>,version_types: Option<Vec<String>>, pat: &str],
[update_files, ServiceResponse, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: &str],
[update_files_deserialized_common, HashMap<String, CommonVersion>, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: &str],
[get_project_versions, ServiceResponse, project_id_slug: &str, game_versions: Option<Vec<String>>,loaders: Option<Vec<String>>,featured: Option<bool>, version_type: Option<VersionType>, limit: Option<usize>, offset: Option<usize>,pat: &str],
[get_project_versions_deserialized_common, Vec<CommonVersion>, project_id_slug: &str, game_versions: Option<Vec<String>>, loaders: Option<Vec<String>>,featured: Option<bool>,version_type: Option<VersionType>,limit: Option<usize>,offset: Option<usize>,pat: &str],
[edit_version_ordering, ServiceResponse, version_id: &str,ordering: Option<i32>,pat: &str],
[add_public_version, ServiceResponse, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: Option<&str>],
[add_public_version_deserialized_common, CommonVersion, project_id: ProjectId, version_number: &str, version_jar: TestFile, ordering: Option<i32>, modify_json: Option<json_patch::Patch>, pat: Option<&str>],
[get_version, ServiceResponse, id_or_slug: &str, pat: Option<&str>],
[get_version_deserialized_common, CommonVersion, id_or_slug: &str, pat: Option<&str>],
[get_versions, ServiceResponse, ids_or_slugs: Vec<String>, pat: Option<&str>],
[get_versions_deserialized_common, Vec<CommonVersion>, ids_or_slugs: Vec<String>, pat: Option<&str>],
[edit_version, ServiceResponse, id_or_slug: &str, patch: serde_json::Value, pat: Option<&str>],
[get_version_from_hash, ServiceResponse, id_or_slug: &str, hash: &str, pat: Option<&str>],
[get_version_from_hash_deserialized_common, CommonVersion, id_or_slug: &str, hash: &str, pat: Option<&str>],
[get_versions_from_hashes, ServiceResponse, hashes: &[&str], algorithm: &str, pat: Option<&str>],
[get_versions_from_hashes_deserialized_common, HashMap<String, CommonVersion>, hashes: &[&str], algorithm: &str, pat: Option<&str>],
[get_update_from_hash, ServiceResponse, hash: &str, algorithm: &str, loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: Option<&str>],
[get_update_from_hash_deserialized_common, CommonVersion, hash: &str, algorithm: &str,loaders: Option<Vec<String>>,game_versions: Option<Vec<String>>,version_types: Option<Vec<String>>, pat: Option<&str>],
[update_files, ServiceResponse, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: Option<&str>],
[update_files_deserialized_common, HashMap<String, CommonVersion>, algorithm: &str, hashes: Vec<String>, loaders: Option<Vec<String>>, game_versions: Option<Vec<String>>, version_types: Option<Vec<String>>, pat: Option<&str>],
[get_project_versions, ServiceResponse, project_id_slug: &str, game_versions: Option<Vec<String>>,loaders: Option<Vec<String>>,featured: Option<bool>, version_type: Option<VersionType>, limit: Option<usize>, offset: Option<usize>,pat: Option<&str>],
[get_project_versions_deserialized_common, Vec<CommonVersion>, project_id_slug: &str, game_versions: Option<Vec<String>>, loaders: Option<Vec<String>>,featured: Option<bool>,version_type: Option<VersionType>,limit: Option<usize>,offset: Option<usize>,pat: Option<&str>],
[edit_version_ordering, ServiceResponse, version_id: &str,ordering: Option<i32>,pat: Option<&str>],
[upload_file_to_version, ServiceResponse, version_id: &str, file: &TestFile, pat: Option<&str>],
[remove_version, ServiceResponse, version_id: &str, pat: Option<&str>],
[remove_version_file, ServiceResponse, hash: &str, pat: Option<&str>],
}
);
Loading

0 comments on commit 40ef0f5

Please sign in to comment.