Skip to content

Commit 6fa419c

Browse files
committed
tests/krate/yanking: Move route tests into routes submodules
1 parent a729c0e commit 6fa419c

File tree

5 files changed

+96
-92
lines changed

5 files changed

+96
-92
lines changed

src/tests/krate/yanking.rs

Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +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-
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) -> crate::util::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) -> crate::util::Response<OkBool>;
12-
}
13-
14-
impl<T: RequestHelper> YankRequestHelper for T {
15-
fn yank(&self, krate_name: &str, version: &str) -> crate::util::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) -> crate::util::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-
}
294

305
#[test]
316
#[allow(unknown_lints, clippy::bool_assert_comparison)] // for claim::assert_some_eq! with bool
@@ -85,26 +60,6 @@ fn yank_works_as_intended() {
8560
assert!(!json.version.yanked);
8661
}
8762

88-
#[test]
89-
fn yank_by_a_non_owner_fails() {
90-
let (app, _, _, token) = TestApp::full().with_token();
91-
92-
let another_user = app.db_new_user("bar");
93-
let another_user = another_user.as_model();
94-
app.db(|conn| {
95-
CrateBuilder::new("foo_not", another_user.id)
96-
.version("1.0.0")
97-
.expect_build(conn);
98-
});
99-
100-
let response = token.yank("foo_not", "1.0.0");
101-
assert_eq!(response.status(), StatusCode::OK);
102-
assert_eq!(
103-
response.into_json(),
104-
json!({ "errors": [{ "detail": "must already be an owner to yank or unyank" }] })
105-
);
106-
}
107-
10863
#[test]
10964
fn yank_max_version() {
11065
let (_, anon, _, token) = TestApp::full().with_token();
@@ -188,48 +143,3 @@ fn publish_after_yank_max_version() {
188143
let json = anon.show_crate("fyk_max");
189144
assert_eq!(json.krate.max_version, "2.0.0");
190145
}
191-
192-
#[test]
193-
fn yank_records_an_audit_action() {
194-
let (_, anon, _, token) = TestApp::full().with_token();
195-
196-
// Upload a new crate, putting it in the git index
197-
let crate_to_publish = PublishBuilder::new("fyk");
198-
token.publish_crate(crate_to_publish).good();
199-
200-
// Yank it
201-
token.yank("fyk", "1.0.0").good();
202-
203-
// Make sure it has one publish and one yank audit action
204-
let json = anon.show_version("fyk", "1.0.0");
205-
let actions = json.version.audit_actions;
206-
207-
assert_eq!(actions.len(), 2);
208-
let action = &actions[1];
209-
assert_eq!(action.action, "yank");
210-
assert_eq!(action.user.id, token.as_model().user_id);
211-
}
212-
213-
#[test]
214-
fn unyank_records_an_audit_action() {
215-
let (_, anon, _, token) = TestApp::full().with_token();
216-
217-
// Upload a new crate
218-
let crate_to_publish = PublishBuilder::new("fyk");
219-
token.publish_crate(crate_to_publish).good();
220-
221-
// Yank version 1.0.0
222-
token.yank("fyk", "1.0.0").good();
223-
224-
// Unyank version 1.0.0
225-
token.unyank("fyk", "1.0.0").good();
226-
227-
// Make sure it has one publish, one yank, and one unyank audit action
228-
let json = anon.show_version("fyk", "1.0.0");
229-
let actions = json.version.audit_actions;
230-
231-
assert_eq!(actions.len(), 3);
232-
let action = &actions[2];
233-
assert_eq!(action.action, "unyank");
234-
assert_eq!(action.user.id, token.as_model().user_id);
235-
}

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)