Test permissions use api (#784)

* initial push

* fmt; clippy

---------

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Wyatt Verchere
2023-12-11 18:24:49 -08:00
committed by GitHub
parent 27ccd3dfa8
commit 6217523cc8
32 changed files with 1558 additions and 1015 deletions

View File

@@ -1,12 +1,15 @@
use std::collections::HashMap;
use super::{request_data::get_public_version_creation_data, ApiV2};
use super::{
request_data::{self, get_public_version_creation_data},
ApiV2,
};
use crate::common::{
api_common::{models::CommonVersion, Api, ApiVersion},
api_common::{models::CommonVersion, Api, ApiVersion, AppendsOptionalPat},
asserts::assert_status,
dummy_data::TestFile,
};
use actix_http::{header::AUTHORIZATION, StatusCode};
use actix_http::StatusCode;
use actix_web::{
dev::ServiceResponse,
test::{self, TestRequest},
@@ -28,7 +31,7 @@ pub fn url_encode_json_serialized_vec(elements: &[String]) -> String {
}
impl ApiV2 {
pub async fn get_version_deserialized(&self, id: &str, pat: &str) -> LegacyVersion {
pub async fn get_version_deserialized(&self, id: &str, pat: Option<&str>) -> LegacyVersion {
let resp = self.get_version(id, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
@@ -38,7 +41,7 @@ impl ApiV2 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> LegacyVersion {
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -49,7 +52,7 @@ impl ApiV2 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, LegacyVersion> {
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -60,11 +63,11 @@ impl ApiV2 {
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v2/version_files/update_individual")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"algorithm": algorithm,
"hashes": hashes
@@ -77,7 +80,7 @@ impl ApiV2 {
&self,
algorithm: &str,
hashes: Vec<FileUpdateData>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, LegacyVersion> {
let resp = self.update_individual_files(algorithm, hashes, pat).await;
assert_eq!(resp.status(), 200);
@@ -94,7 +97,7 @@ impl ApiVersion for ApiV2 {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let creation_data = get_public_version_creation_data(
project_id,
@@ -107,7 +110,7 @@ impl ApiVersion for ApiV2 {
// Add a project.
let req = TestRequest::post()
.uri("/v2/version")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_multipart(creation_data.segment_data)
.to_request();
self.call(req).await
@@ -120,7 +123,7 @@ impl ApiVersion for ApiV2 {
version_jar: TestFile,
ordering: Option<i32>,
modify_json: Option<json_patch::Patch>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self
.add_public_version(
@@ -140,15 +143,15 @@ impl ApiVersion for ApiV2 {
serde_json::from_value(value).unwrap()
}
async fn get_version(&self, id: &str, pat: &str) -> ServiceResponse {
async fn get_version(&self, id: &str, pat: Option<&str>) -> ServiceResponse {
let req = TestRequest::get()
.uri(&format!("/v2/version/{id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
async fn get_version_deserialized_common(&self, id: &str, pat: &str) -> CommonVersion {
async fn get_version_deserialized_common(&self, id: &str, pat: Option<&str>) -> CommonVersion {
let resp = self.get_version(id, pat).await;
assert_eq!(resp.status(), 200);
// First, deserialize to the non-common format (to test the response is valid for this api version)
@@ -162,11 +165,11 @@ impl ApiVersion for ApiV2 {
&self,
version_id: &str,
patch: serde_json::Value,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::patch()
.uri(&format!("/v2/version/{version_id}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(patch)
.to_request();
@@ -177,11 +180,11 @@ impl ApiVersion for ApiV2 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::get()
.uri(&format!("/v2/version_file/{hash}?algorithm={algorithm}"))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -190,7 +193,7 @@ impl ApiVersion for ApiV2 {
&self,
hash: &str,
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self.get_version_from_hash(hash, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -205,11 +208,11 @@ impl ApiVersion for ApiV2 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = TestRequest::post()
.uri("/v2/version_files")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"hashes": hashes,
"algorithm": algorithm,
@@ -222,7 +225,7 @@ impl ApiVersion for ApiV2 {
&self,
hashes: &[&str],
algorithm: &str,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion> {
let resp = self.get_versions_from_hashes(hashes, algorithm, pat).await;
assert_eq!(resp.status(), 200);
@@ -240,13 +243,13 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri(&format!(
"/v2/version_file/{hash}/update?algorithm={algorithm}"
))
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"loaders": loaders,
"game_versions": game_versions,
@@ -263,7 +266,7 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> CommonVersion {
let resp = self
.get_update_from_hash(hash, algorithm, loaders, game_versions, version_types, pat)
@@ -283,11 +286,11 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let req = test::TestRequest::post()
.uri("/v2/version_files/update")
.append_header(("Authorization", pat))
.append_pat(pat)
.set_json(json!({
"algorithm": algorithm,
"hashes": hashes,
@@ -306,7 +309,7 @@ impl ApiVersion for ApiV2 {
loaders: Option<Vec<String>>,
game_versions: Option<Vec<String>>,
version_types: Option<Vec<String>>,
pat: &str,
pat: Option<&str>,
) -> HashMap<String, CommonVersion> {
let resp = self
.update_files(
@@ -337,7 +340,7 @@ impl ApiVersion for ApiV2 {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let mut query_string = String::new();
if let Some(game_versions) = game_versions {
@@ -372,7 +375,7 @@ impl ApiVersion for ApiV2 {
"/v2/project/{project_id_slug}/version?{}",
query_string.trim_start_matches('&')
))
.append_header(("Authorization", pat))
.append_pat(pat)
.to_request();
self.call(req).await
}
@@ -387,7 +390,7 @@ impl ApiVersion for ApiV2 {
version_type: Option<VersionType>,
limit: Option<usize>,
offset: Option<usize>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion> {
let resp = self
.get_project_versions(
@@ -413,7 +416,7 @@ impl ApiVersion for ApiV2 {
&self,
version_id: &str,
ordering: Option<i32>,
pat: &str,
pat: Option<&str>,
) -> ServiceResponse {
let request = test::TestRequest::patch()
.uri(&format!("/v2/version/{version_id}"))
@@ -422,16 +425,16 @@ impl ApiVersion for ApiV2 {
"ordering": ordering
}
))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(request).await
}
async fn get_versions(&self, version_ids: Vec<String>, pat: &str) -> ServiceResponse {
async fn get_versions(&self, version_ids: Vec<String>, pat: Option<&str>) -> ServiceResponse {
let ids = url_encode_json_serialized_vec(&version_ids);
let request = test::TestRequest::get()
.uri(&format!("/v2/versions?ids={}", ids))
.append_header((AUTHORIZATION, pat))
.append_pat(pat)
.to_request();
self.call(request).await
}
@@ -439,7 +442,7 @@ impl ApiVersion for ApiV2 {
async fn get_versions_deserialized_common(
&self,
version_ids: Vec<String>,
pat: &str,
pat: Option<&str>,
) -> Vec<CommonVersion> {
let resp = self.get_versions(version_ids, pat).await;
assert_status(&resp, StatusCode::OK);
@@ -449,4 +452,40 @@ impl ApiVersion for ApiV2 {
let value = serde_json::to_value(v).unwrap();
serde_json::from_value(value).unwrap()
}
async fn upload_file_to_version(
&self,
version_id: &str,
file: &TestFile,
pat: Option<&str>,
) -> ServiceResponse {
let m = request_data::get_public_creation_data_multipart(
&json!({
"file_parts": [file.filename()]
}),
Some(file),
);
let request = test::TestRequest::post()
.uri(&format!("/v2/version/{version_id}/file"))
.append_pat(pat)
.set_multipart(m)
.to_request();
self.call(request).await
}
async fn remove_version(&self, version_id: &str, pat: Option<&str>) -> ServiceResponse {
let request = test::TestRequest::delete()
.uri(&format!("/v2/version/{version_id}"))
.append_pat(pat)
.to_request();
self.call(request).await
}
async fn remove_version_file(&self, hash: &str, pat: Option<&str>) -> ServiceResponse {
let request = test::TestRequest::delete()
.uri(&format!("/v2/version_file/{hash}"))
.append_pat(pat)
.to_request();
self.call(request).await
}
}