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

Commit

Permalink
progress, failing
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Nov 17, 2023
1 parent 17b8e04 commit 74712b7
Show file tree
Hide file tree
Showing 42 changed files with 551 additions and 435 deletions.
6 changes: 3 additions & 3 deletions tests/analytics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{DateTime, Duration, Utc};
use common::{database::*, environment::with_test_environment};
use common::{database::*, environment::{with_test_environment, TestEnvironment}, api_v3::ApiV3};
use itertools::Itertools;
use labrinth::models::ids::base62_impl::parse_base62;
use rust_decimal::{prelude::ToPrimitive, Decimal};
Expand All @@ -10,8 +10,8 @@ mod common;

#[actix_rt::test]
pub async fn analytics_revenue() {
with_test_environment(None, |test_env| async move {
let api = &test_env.v3;
with_test_environment(None, |test_env : TestEnvironment<ApiV3>| async move {
let api = &test_env.api;

let alpha_project_id = test_env
.dummy
Expand Down
27 changes: 0 additions & 27 deletions tests/common/api.rs

This file was deleted.

125 changes: 125 additions & 0 deletions tests/common/api_common/generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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 crate::common::{api_v2::ApiV2, api_v3::ApiV3};

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

#[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 {
[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],
[search_deserialized_common, SearchResults, query: Option<&str>, facets: Option<serde_json::Value>, pat: &str],
[get_analytics_revenue, ServiceResponse, id_or_slugs: Vec<&str>, start_date: Option<chrono::DateTime<chrono::Utc>>, end_date: Option<chrono::DateTime<chrono::Utc>>, resolution_minutes: Option<u32>, pat: &str],
[get_analytics_revenue_deserialized, std::collections::HashMap<String, std::collections::HashMap<i64, rust_decimal::Decimal>>, id_or_slugs: Vec<&str>, start_date: Option<chrono::DateTime<chrono::Utc>>, end_date: Option<chrono::DateTime<chrono::Utc>>, resolution_minutes: Option<u32>, pat: &str],
}
);

delegate_api_variant!(
#[async_trait(?Send)]
impl ApiTags for GenericApi {
[get_loaders, ServiceResponse,],
[get_loaders_deserialized_common, Vec<crate::common::api_common::models::CommonLoaderData>,],
[get_categories, ServiceResponse,],
[get_categories_deserialized_common, Vec<crate::common::api_common::models::CommonCategoryData>,],
}
);

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],
}
);

delegate_api_variant!(
#[async_trait(?Send)]
impl ApiVersion for GenericApi {
[get_version, ServiceResponse, id_or_slug: &str, pat: &str],
[get_version_deserialized_common, CommonVersion, id_or_slug: &str, 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],
}
);
11 changes: 9 additions & 2 deletions tests/common/api_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ 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}};
use labrinth::{search::SearchResults, models::{teams::{ProjectPermissions, OrganizationPermissions}, projects::VersionType}, LabrinthConfig};
use rust_decimal::Decimal;

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

pub mod generic;
pub mod models;

#[async_trait(?Send)]
pub trait ApiBuildable : Api {
async fn build(labrinth_config: LabrinthConfig) -> Self;
}

#[async_trait(?Send)]
pub trait Api: ApiProject + ApiTags + ApiTeams + ApiVersion {
async fn reset_search_index(&self) -> ServiceResponse;
async fn call(&self, req: actix_http::Request) -> ServiceResponse;
async fn reset_search_index(&self) -> ServiceResponse;
}

#[async_trait(?Send)]
Expand Down
19 changes: 14 additions & 5 deletions tests/common/api_v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(dead_code)]

use super::{environment::LocalService, api_common::Api};
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 project;
Expand All @@ -16,14 +17,22 @@ pub struct ApiV2 {
pub test_app: Rc<dyn LocalService>,
}

impl ApiV2 {
pub async fn call(&self, req: actix_http::Request) -> ServiceResponse {
self.test_app.call(req).await.unwrap()
#[async_trait(?Send)]
impl ApiBuildable for ApiV2 {
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 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")
Expand Down
2 changes: 1 addition & 1 deletion 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}}};
use crate::common::{api_v2::request_data::ProjectCreationRequestData, api_common::{ApiProject, models::{CommonImageData, CommonProject, CommonVersion}, Api}};
use actix_http::StatusCode;
use actix_web::{
dev::ServiceResponse,
Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v2/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use actix_web::{
use async_trait::async_trait;
use labrinth::routes::v2::tags::{CategoryData, GameVersionQueryData, LoaderData};

use crate::common::{database::ADMIN_USER_PAT, api_common::{ApiTags, models::{CommonLoaderData, CommonCategoryData}}};
use crate::common::{database::ADMIN_USER_PAT, api_common::{ApiTags, models::{CommonLoaderData, CommonCategoryData}, Api}};

use super::ApiV2;

Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v2/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use labrinth::models::teams::{OrganizationPermissions, ProjectPermissions};
use serde_json::json;

use crate::common::{asserts::assert_status, api_common::{ApiTeams, models::{CommonTeamMember, CommonNotification}}};
use crate::common::{asserts::assert_status, api_common::{ApiTeams, models::{CommonTeamMember, CommonNotification}, Api}};

use super::ApiV2;

Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v2/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use labrinth::{
};
use serde_json::json;

use crate::common::{asserts::assert_status, api_common::{ApiVersion, models::CommonVersion}};
use crate::common::{asserts::assert_status, api_common::{ApiVersion, models::CommonVersion, Api}};

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

Expand Down
20 changes: 15 additions & 5 deletions tests/common/api_v3/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(dead_code)]

use super::{environment::LocalService, api_common::Api};
use actix_web::dev::ServiceResponse;
use super::{api_common::{Api, ApiBuildable}, environment::LocalService};
use actix_web::{dev::ServiceResponse, App, test};
use async_trait::async_trait;
use labrinth::LabrinthConfig;
use std::rc::Rc;

pub mod oauth;
Expand All @@ -19,14 +20,23 @@ pub struct ApiV3 {
pub test_app: Rc<dyn LocalService>,
}

impl ApiV3 {
pub async fn call(&self, req: actix_http::Request) -> ServiceResponse {
self.test_app.call(req).await.unwrap()
#[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()
}

async fn reset_search_index(&self) -> ServiceResponse {
let req = actix_web::test::TestRequest::post()
.uri("/v3/admin/_force_reindex")
Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v3/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use labrinth::auth::oauth::{
};
use reqwest::header::{AUTHORIZATION, LOCATION};

use crate::common::asserts::assert_status;
use crate::common::{asserts::assert_status, api_common::Api};

use super::ApiV3;

Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v3/oauth_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use labrinth::{
use reqwest::header::AUTHORIZATION;
use serde_json::json;

use crate::common::asserts::assert_status;
use crate::common::{asserts::assert_status, api_common::Api};

use super::ApiV3;

Expand Down
2 changes: 2 additions & 0 deletions tests/common/api_v3/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v3/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use labrinth::{
use rust_decimal::Decimal;
use serde_json::json;

use crate::common::{asserts::assert_status, database::MOD_USER_PAT, api_common::{ApiProject, models::{CommonProject, CommonImageData}}};
use crate::common::{asserts::assert_status, database::MOD_USER_PAT, api_common::{ApiProject, models::{CommonProject, CommonImageData}, Api}};

use super::{
request_data::ProjectCreationRequestData,
Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v3/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use async_trait::async_trait;
use labrinth::routes::v3::tags::GameData;
use labrinth::database::models::loader_fields::LoaderFieldEnumValue;

use crate::common::{database::ADMIN_USER_PAT, api_common::{ApiTags, models::{CommonLoaderData, CommonCategoryData}}};
use crate::common::{database::ADMIN_USER_PAT, api_common::{ApiTags, models::{CommonLoaderData, CommonCategoryData}, Api}};

use super::ApiV3;

Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v3/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use labrinth::models::teams::{OrganizationPermissions, ProjectPermissions};
use serde_json::json;

use crate::common::{asserts::assert_status, api_common::{ApiTeams, models::{CommonTeamMember, CommonNotification}}};
use crate::common::{asserts::assert_status, api_common::{ApiTeams, models::{CommonTeamMember, CommonNotification}, Api}};

use super::ApiV3;

Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v3/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use labrinth::{
};
use serde_json::json;

use crate::common::{asserts::assert_status, api_common::{ApiVersion, models::CommonVersion}};
use crate::common::{asserts::assert_status, api_common::{ApiVersion, models::CommonVersion, Api}};

use super::{request_data::VersionCreationRequestData, ApiV3};

Expand Down
6 changes: 3 additions & 3 deletions tests/common/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use url::Url;

use crate::common::{dummy_data, environment::TestEnvironment};

use super::dummy_data::DUMMY_DATA_UPDATE;
use super::{dummy_data::DUMMY_DATA_UPDATE, api_v3::ApiV3};

// 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.
Expand Down Expand Up @@ -168,13 +168,13 @@ impl TemporaryDatabase {

if !dummy_data_exists {
// Add dummy data
let temporary_test_env = TestEnvironment::build_with_db(TemporaryDatabase {
let temporary_test_env = TestEnvironment::<ApiV3>::build_with_db(TemporaryDatabase {
pool: pool.clone(),
database_name: TEMPLATE_DATABASE_NAME.to_string(),
redis_pool: RedisPool::new(Some(generate_random_name("test_template_"))),
})
.await;
dummy_data::add_dummy_data(&temporary_test_env).await;
dummy_data::add_dummy_data(&temporary_test_env.setup_api, temporary_test_env.db.clone()).await;
temporary_test_env.db.pool.close().await;
}
pool.close().await;
Expand Down
Loading

0 comments on commit 74712b7

Please sign in to comment.