Skip to content

tests/krate/yanking: Move route tests into routes submodules #5579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 2 additions & 104 deletions src/tests/krate/yanking.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
use crate::builders::{CrateBuilder, PublishBuilder};
use crate::builders::PublishBuilder;
use crate::routes::crates::versions::yank_unyank::YankRequestHelper;
use crate::util::{RequestHelper, TestApp};
use crate::OkBool;
use http::StatusCode;

impl crate::util::MockTokenUser {
/// Yank the specified version of the specified crate and run all pending background jobs
fn yank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
let url = format!("/api/v1/crates/{krate_name}/{version}/yank");
let response = self.delete(&url);
self.app().run_pending_background_jobs();
response
}

/// Unyank the specified version of the specified crate and run all pending background jobs
fn unyank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
let url = format!("/api/v1/crates/{krate_name}/{version}/unyank");
let response = self.put(&url, &[]);
self.app().run_pending_background_jobs();
response
}
}

impl crate::util::MockCookieUser {
/// Yank the specified version of the specified crate and run all pending background jobs
fn yank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
let url = format!("/api/v1/crates/{krate_name}/{version}/yank");
let response = self.delete(&url);
self.app().run_pending_background_jobs();
response
}

/// Unyank the specified version of the specified crate and run all pending background jobs
fn unyank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
let url = format!("/api/v1/crates/{krate_name}/{version}/unyank");
let response = self.put(&url, &[]);
self.app().run_pending_background_jobs();
response
}
}

#[test]
#[allow(unknown_lints, clippy::bool_assert_comparison)] // for claim::assert_some_eq! with bool
Expand Down Expand Up @@ -97,26 +60,6 @@ fn yank_works_as_intended() {
assert!(!json.version.yanked);
}

#[test]
fn yank_by_a_non_owner_fails() {
let (app, _, _, token) = TestApp::full().with_token();

let another_user = app.db_new_user("bar");
let another_user = another_user.as_model();
app.db(|conn| {
CrateBuilder::new("foo_not", another_user.id)
.version("1.0.0")
.expect_build(conn);
});

let response = token.yank("foo_not", "1.0.0");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "must already be an owner to yank or unyank" }] })
);
}

#[test]
fn yank_max_version() {
let (_, anon, _, token) = TestApp::full().with_token();
Expand Down Expand Up @@ -200,48 +143,3 @@ fn publish_after_yank_max_version() {
let json = anon.show_crate("fyk_max");
assert_eq!(json.krate.max_version, "2.0.0");
}

#[test]
fn yank_records_an_audit_action() {
let (_, anon, _, token) = TestApp::full().with_token();

// Upload a new crate, putting it in the git index
let crate_to_publish = PublishBuilder::new("fyk");
token.publish_crate(crate_to_publish).good();

// Yank it
token.yank("fyk", "1.0.0").good();

// Make sure it has one publish and one yank audit action
let json = anon.show_version("fyk", "1.0.0");
let actions = json.version.audit_actions;

assert_eq!(actions.len(), 2);
let action = &actions[1];
assert_eq!(action.action, "yank");
assert_eq!(action.user.id, token.as_model().user_id);
}

#[test]
fn unyank_records_an_audit_action() {
let (_, anon, _, token) = TestApp::full().with_token();

// Upload a new crate
let crate_to_publish = PublishBuilder::new("fyk");
token.publish_crate(crate_to_publish).good();

// Yank version 1.0.0
token.yank("fyk", "1.0.0").good();

// Unyank version 1.0.0
token.unyank("fyk", "1.0.0").good();

// Make sure it has one publish, one yank, and one unyank audit action
let json = anon.show_version("fyk", "1.0.0");
let actions = json.version.audit_actions;

assert_eq!(actions.len(), 3);
let action = &actions[2];
assert_eq!(action.action, "unyank");
assert_eq!(action.user.id, token.as_model().user_id);
}
1 change: 1 addition & 0 deletions src/tests/routes/crates/versions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ mod authors;
pub mod dependencies;
pub mod download;
mod read;
pub mod yank_unyank;
93 changes: 93 additions & 0 deletions src/tests/routes/crates/versions/yank_unyank.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use crate::builders::{CrateBuilder, PublishBuilder};
use crate::util::{RequestHelper, Response, TestApp};
use crate::OkBool;
use http::StatusCode;

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

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

impl<T: RequestHelper> YankRequestHelper for T {
fn yank(&self, krate_name: &str, version: &str) -> Response<OkBool> {
let url = format!("/api/v1/crates/{krate_name}/{version}/yank");
let response = self.delete(&url);
self.app().run_pending_background_jobs();
response
}

fn unyank(&self, krate_name: &str, version: &str) -> Response<OkBool> {
let url = format!("/api/v1/crates/{krate_name}/{version}/unyank");
let response = self.put(&url, &[]);
self.app().run_pending_background_jobs();
response
}
}

#[test]
fn yank_by_a_non_owner_fails() {
let (app, _, _, token) = TestApp::full().with_token();

let another_user = app.db_new_user("bar");
let another_user = another_user.as_model();
app.db(|conn| {
CrateBuilder::new("foo_not", another_user.id)
.version("1.0.0")
.expect_build(conn);
});

let response = token.yank("foo_not", "1.0.0");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "must already be an owner to yank or unyank" }] })
);
}

#[test]
fn yank_records_an_audit_action() {
let (_, anon, _, token) = TestApp::full().with_token();

// Upload a new crate, putting it in the git index
let crate_to_publish = PublishBuilder::new("fyk");
token.publish_crate(crate_to_publish).good();

// Yank it
token.yank("fyk", "1.0.0").good();

// Make sure it has one publish and one yank audit action
let json = anon.show_version("fyk", "1.0.0");
let actions = json.version.audit_actions;

assert_eq!(actions.len(), 2);
let action = &actions[1];
assert_eq!(action.action, "yank");
assert_eq!(action.user.id, token.as_model().user_id);
}

#[test]
fn unyank_records_an_audit_action() {
let (_, anon, _, token) = TestApp::full().with_token();

// Upload a new crate
let crate_to_publish = PublishBuilder::new("fyk");
token.publish_crate(crate_to_publish).good();

// Yank version 1.0.0
token.yank("fyk", "1.0.0").good();

// Unyank version 1.0.0
token.unyank("fyk", "1.0.0").good();

// Make sure it has one publish, one yank, and one unyank audit action
let json = anon.show_version("fyk", "1.0.0");
let actions = json.version.audit_actions;

assert_eq!(actions.len(), 3);
let action = &actions[2];
assert_eq!(action.action, "unyank");
assert_eq!(action.user.id, token.as_model().user_id);
}