Skip to content

Commit

Permalink
Add unit tests for the patch API
Browse files Browse the repository at this point in the history
Signed-off-by: Rustin170506 <[email protected]>
  • Loading branch information
Rustin170506 committed Sep 11, 2024
1 parent 23bbe2a commit 58b48b0
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo/1.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo/1.0.0/readme",
"rust_version": "1.69",
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo/1.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
}
63 changes: 63 additions & 0 deletions src/tests/krate/yanking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,66 @@ async fn publish_after_yank_max_version() {
let json = anon.show_crate("fyk_max").await;
assert_eq!(json.krate.max_version, "2.0.0");
}

#[tokio::test(flavor = "multi_thread")]
async fn patch_version_yank_unyank() {
let (_, anon, _, token) = TestApp::full().with_token();

// Upload a new crate
let crate_to_publish = PublishBuilder::new("patchable", "1.0.0");
token.publish_crate(crate_to_publish).await.good();

// Check initial state
let json = anon.show_version("patchable", "1.0.0").await;
assert!(!json.version.yanked);
assert_eq!(json.version.yank_message, None);

// Yank with message
token
.update_yank_status("patchable", "1.0.0", Some(true), Some("Yanking reason"))
.await
.good();

let json = anon.show_version("patchable", "1.0.0").await;
assert!(json.version.yanked);
assert_eq!(
json.version.yank_message,
Some("Yanking reason".to_string())
);

// Update yank message
token
.update_yank_status("patchable", "1.0.0", None, Some("Updated reason"))
.await
.good();

let json = anon.show_version("patchable", "1.0.0").await;
assert!(json.version.yanked);
assert_eq!(
json.version.yank_message,
Some("Updated reason".to_string())
);

// Unyank
token
.update_yank_status("patchable", "1.0.0", Some(false), None)
.await
.good();

let json = anon.show_version("patchable", "1.0.0").await;
assert!(!json.version.yanked);
assert_eq!(json.version.yank_message, None);

// Attempt to set yank message on unyanked version (should fail)
token
.update_yank_status("patchable", "1.0.0", None, Some("Invalid message"))
.await
.status()
.is_client_error();
// Attempt to unyank with message (should fail)
token
.update_yank_status("patchable", "1.0.0", Some(false), Some("Invalid message"))
.await
.status()
.is_client_error();
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo_show/1.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
},
{
Expand Down Expand Up @@ -103,6 +104,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo_show/0.5.1/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
},
{
Expand Down Expand Up @@ -135,6 +137,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo_show/0.5.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/c3/1.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/c2/1.1.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/c3/3.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
},
{
Expand All @@ -89,6 +90,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/c2/2.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/c2/1.0.18446744073709551615/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/c2/2.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/c2/2.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo_versions/1.0.0/readme",
"rust_version": "1.64",
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
},
{
Expand Down Expand Up @@ -60,6 +61,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo_versions/0.5.1/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
},
{
Expand Down Expand Up @@ -92,6 +94,7 @@ expression: response.json()
"readme_path": "/api/v1/crates/foo_versions/0.5.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ expression: json
"readme_path": "/api/v1/crates/foo_vers_show_no_pb/1.0.0/readme",
"rust_version": null,
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ expression: json
"readme_path": "/api/v1/crates/foo_vers_show/2.0.0/readme",
"rust_version": "1.64",
"updated_at": "[datetime]",
"yank_message": null,
"yanked": false
}
}
32 changes: 31 additions & 1 deletion src/tests/routes/crates/versions/yank_unyank.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
use crate::builders::{CrateBuilder, PublishBuilder};
use crate::util::{RequestHelper, Response, TestApp};
use crate::OkBool;
use crate::{OkBool, VersionResponse};
use http::StatusCode;
use serde_json::json;

pub trait YankRequestHelper {
/// Yank the specified version of the specified crate and run all pending background jobs
async fn yank(&self, krate_name: &str, version: &str) -> Response<OkBool>;

/// Unyank the specified version of the specified crate and run all pending background jobs
async fn unyank(&self, krate_name: &str, version: &str) -> Response<OkBool>;

/// Update the yank status of the specified version of the specified crate with a patch request and run all pending background jobs
async fn update_yank_status(
&self,
krate_name: &str,
version: &str,
yanked: Option<bool>,
yank_message: Option<&str>,
) -> Response<VersionResponse>;
}

impl<T: RequestHelper> YankRequestHelper for T {
Expand All @@ -25,6 +35,26 @@ impl<T: RequestHelper> YankRequestHelper for T {
self.app().run_pending_background_jobs().await;
response
}

async fn update_yank_status(
&self,
krate_name: &str,
version: &str,
yanked: Option<bool>,
yank_message: Option<&str>,
) -> Response<VersionResponse> {
let url = format!("/api/v1/crates/{krate_name}/{version}");

let json_body = json!({
"yanked": yanked,
"yank_message": yank_message
});
let body = serde_json::to_string(&json_body).expect("Failed to serialize JSON body");

let response = self.patch(&url, body).await;
self.app().run_pending_background_jobs().await;
response
}
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
14 changes: 14 additions & 0 deletions src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ pub trait RequestHelper {
self.run(request).await
}

/// Issue a PATCH request
async fn patch<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
let body = body.into();
let is_json = body.starts_with(b"{") && body.ends_with(b"}");

let mut request = self.request_builder(Method::PATCH, path);
*request.body_mut() = body;
if is_json {
request.header(header::CONTENT_TYPE, "application/json");
}

self.run(request).await
}

/// Issue a DELETE request
async fn delete<T>(&self, path: &str) -> Response<T> {
let request = self.request_builder(Method::DELETE, path);
Expand Down
4 changes: 4 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ pub struct EncodableVersion {
pub downloads: i32,
pub features: serde_json::Value,
pub yanked: bool,
pub yank_message: Option<String>,
pub lib_links: Option<String>,
// NOTE: Used by shields.io, altering `license` requires a PR with shields.io
pub license: Option<String>,
Expand Down Expand Up @@ -586,6 +587,7 @@ impl EncodableVersion {
downloads,
features,
yanked,
yank_message,
links: lib_links,
license,
crate_size,
Expand Down Expand Up @@ -613,6 +615,7 @@ impl EncodableVersion {
downloads,
features,
yanked,
yank_message,
lib_links,
license,
links,
Expand Down Expand Up @@ -736,6 +739,7 @@ mod tests {
downloads: 0,
features: serde_json::from_str("{}").unwrap(),
yanked: false,
yank_message: None,
license: None,
lib_links: None,
links: EncodableVersionLinks {
Expand Down

0 comments on commit 58b48b0

Please sign in to comment.