Skip to content

Commit 39d007c

Browse files
authored
Merge pull request #5579 from Turbo87/yank-tests
tests/krate/yanking: Move route tests into routes submodules
2 parents d87579a + 6fa419c commit 39d007c

File tree

5 files changed

+96
-104
lines changed

5 files changed

+96
-104
lines changed

src/tests/krate/yanking.rs

Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,6 @@
1-
use crate::builders::{CrateBuilder, PublishBuilder};
1+
use crate::builders::PublishBuilder;
2+
use crate::routes::crates::versions::yank_unyank::YankRequestHelper;
23
use crate::util::{RequestHelper, TestApp};
3-
use crate::OkBool;
4-
use http::StatusCode;
5-
6-
impl crate::util::MockTokenUser {
7-
/// Yank the specified version of the specified crate and run all pending background jobs
8-
fn yank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
9-
let url = format!("/api/v1/crates/{krate_name}/{version}/yank");
10-
let response = self.delete(&url);
11-
self.app().run_pending_background_jobs();
12-
response
13-
}
14-
15-
/// Unyank the specified version of the specified crate and run all pending background jobs
16-
fn unyank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
17-
let url = format!("/api/v1/crates/{krate_name}/{version}/unyank");
18-
let response = self.put(&url, &[]);
19-
self.app().run_pending_background_jobs();
20-
response
21-
}
22-
}
23-
24-
impl crate::util::MockCookieUser {
25-
/// Yank the specified version of the specified crate and run all pending background jobs
26-
fn yank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
27-
let url = format!("/api/v1/crates/{krate_name}/{version}/yank");
28-
let response = self.delete(&url);
29-
self.app().run_pending_background_jobs();
30-
response
31-
}
32-
33-
/// Unyank the specified version of the specified crate and run all pending background jobs
34-
fn unyank(&self, krate_name: &str, version: &str) -> crate::util::Response<OkBool> {
35-
let url = format!("/api/v1/crates/{krate_name}/{version}/unyank");
36-
let response = self.put(&url, &[]);
37-
self.app().run_pending_background_jobs();
38-
response
39-
}
40-
}
414

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

100-
#[test]
101-
fn yank_by_a_non_owner_fails() {
102-
let (app, _, _, token) = TestApp::full().with_token();
103-
104-
let another_user = app.db_new_user("bar");
105-
let another_user = another_user.as_model();
106-
app.db(|conn| {
107-
CrateBuilder::new("foo_not", another_user.id)
108-
.version("1.0.0")
109-
.expect_build(conn);
110-
});
111-
112-
let response = token.yank("foo_not", "1.0.0");
113-
assert_eq!(response.status(), StatusCode::OK);
114-
assert_eq!(
115-
response.into_json(),
116-
json!({ "errors": [{ "detail": "must already be an owner to yank or unyank" }] })
117-
);
118-
}
119-
12063
#[test]
12164
fn yank_max_version() {
12265
let (_, anon, _, token) = TestApp::full().with_token();
@@ -200,48 +143,3 @@ fn publish_after_yank_max_version() {
200143
let json = anon.show_crate("fyk_max");
201144
assert_eq!(json.krate.max_version, "2.0.0");
202145
}
203-
204-
#[test]
205-
fn yank_records_an_audit_action() {
206-
let (_, anon, _, token) = TestApp::full().with_token();
207-
208-
// Upload a new crate, putting it in the git index
209-
let crate_to_publish = PublishBuilder::new("fyk");
210-
token.publish_crate(crate_to_publish).good();
211-
212-
// Yank it
213-
token.yank("fyk", "1.0.0").good();
214-
215-
// Make sure it has one publish and one yank audit action
216-
let json = anon.show_version("fyk", "1.0.0");
217-
let actions = json.version.audit_actions;
218-
219-
assert_eq!(actions.len(), 2);
220-
let action = &actions[1];
221-
assert_eq!(action.action, "yank");
222-
assert_eq!(action.user.id, token.as_model().user_id);
223-
}
224-
225-
#[test]
226-
fn unyank_records_an_audit_action() {
227-
let (_, anon, _, token) = TestApp::full().with_token();
228-
229-
// Upload a new crate
230-
let crate_to_publish = PublishBuilder::new("fyk");
231-
token.publish_crate(crate_to_publish).good();
232-
233-
// Yank version 1.0.0
234-
token.yank("fyk", "1.0.0").good();
235-
236-
// Unyank version 1.0.0
237-
token.unyank("fyk", "1.0.0").good();
238-
239-
// Make sure it has one publish, one yank, and one unyank audit action
240-
let json = anon.show_version("fyk", "1.0.0");
241-
let actions = json.version.audit_actions;
242-
243-
assert_eq!(actions.len(), 3);
244-
let action = &actions[2];
245-
assert_eq!(action.action, "unyank");
246-
assert_eq!(action.user.id, token.as_model().user_id);
247-
}

src/tests/routes/crates/versions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ mod authors;
22
pub mod dependencies;
33
pub mod download;
44
mod read;
5+
pub mod yank_unyank;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use crate::builders::{CrateBuilder, PublishBuilder};
2+
use crate::util::{RequestHelper, Response, TestApp};
3+
use crate::OkBool;
4+
use http::StatusCode;
5+
6+
pub trait YankRequestHelper {
7+
/// Yank the specified version of the specified crate and run all pending background jobs
8+
fn yank(&self, krate_name: &str, version: &str) -> Response<OkBool>;
9+
10+
/// Unyank the specified version of the specified crate and run all pending background jobs
11+
fn unyank(&self, krate_name: &str, version: &str) -> Response<OkBool>;
12+
}
13+
14+
impl<T: RequestHelper> YankRequestHelper for T {
15+
fn yank(&self, krate_name: &str, version: &str) -> Response<OkBool> {
16+
let url = format!("/api/v1/crates/{krate_name}/{version}/yank");
17+
let response = self.delete(&url);
18+
self.app().run_pending_background_jobs();
19+
response
20+
}
21+
22+
fn unyank(&self, krate_name: &str, version: &str) -> Response<OkBool> {
23+
let url = format!("/api/v1/crates/{krate_name}/{version}/unyank");
24+
let response = self.put(&url, &[]);
25+
self.app().run_pending_background_jobs();
26+
response
27+
}
28+
}
29+
30+
#[test]
31+
fn yank_by_a_non_owner_fails() {
32+
let (app, _, _, token) = TestApp::full().with_token();
33+
34+
let another_user = app.db_new_user("bar");
35+
let another_user = another_user.as_model();
36+
app.db(|conn| {
37+
CrateBuilder::new("foo_not", another_user.id)
38+
.version("1.0.0")
39+
.expect_build(conn);
40+
});
41+
42+
let response = token.yank("foo_not", "1.0.0");
43+
assert_eq!(response.status(), StatusCode::OK);
44+
assert_eq!(
45+
response.into_json(),
46+
json!({ "errors": [{ "detail": "must already be an owner to yank or unyank" }] })
47+
);
48+
}
49+
50+
#[test]
51+
fn yank_records_an_audit_action() {
52+
let (_, anon, _, token) = TestApp::full().with_token();
53+
54+
// Upload a new crate, putting it in the git index
55+
let crate_to_publish = PublishBuilder::new("fyk");
56+
token.publish_crate(crate_to_publish).good();
57+
58+
// Yank it
59+
token.yank("fyk", "1.0.0").good();
60+
61+
// Make sure it has one publish and one yank audit action
62+
let json = anon.show_version("fyk", "1.0.0");
63+
let actions = json.version.audit_actions;
64+
65+
assert_eq!(actions.len(), 2);
66+
let action = &actions[1];
67+
assert_eq!(action.action, "yank");
68+
assert_eq!(action.user.id, token.as_model().user_id);
69+
}
70+
71+
#[test]
72+
fn unyank_records_an_audit_action() {
73+
let (_, anon, _, token) = TestApp::full().with_token();
74+
75+
// Upload a new crate
76+
let crate_to_publish = PublishBuilder::new("fyk");
77+
token.publish_crate(crate_to_publish).good();
78+
79+
// Yank version 1.0.0
80+
token.yank("fyk", "1.0.0").good();
81+
82+
// Unyank version 1.0.0
83+
token.unyank("fyk", "1.0.0").good();
84+
85+
// Make sure it has one publish, one yank, and one unyank audit action
86+
let json = anon.show_version("fyk", "1.0.0");
87+
let actions = json.version.audit_actions;
88+
89+
assert_eq!(actions.len(), 3);
90+
let action = &actions[2];
91+
assert_eq!(action.action, "unyank");
92+
assert_eq!(action.user.id, token.as_model().user_id);
93+
}

0 commit comments

Comments
 (0)