Skip to content

Commit 6ce80a7

Browse files
committed
tests: define assert_forbidden and assert_not_found for any response
The existing uses of these functions meant that uses of `get` and `post` didn't have to explicitly turbofish into `()`, but defining them this way means they can't be used on other responses (for example, the ones returned from `yank` and `unyank`). Moving the definitions into `Response<T>` means we can now use these assertion helpers on any response type, at the cost of having some more turbofish.
1 parent f86ffa1 commit 6ce80a7

File tree

11 files changed

+26
-27
lines changed

11 files changed

+26
-27
lines changed

src/tests/owners.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ fn api_token_cannot_list_invitations_v1() {
589589
let (_, _, _, token) = TestApp::init().with_token();
590590

591591
token
592-
.get("/api/v1/me/crate_owner_invitations")
592+
.get::<()>("/api/v1/me/crate_owner_invitations")
593593
.assert_forbidden();
594594
}
595595

src/tests/routes/categories/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn show() {
1111
let url = "/api/v1/categories/foo-bar";
1212

1313
// Return not found if a category doesn't exist
14-
anon.get(url).assert_not_found();
14+
anon.get::<()>(url).assert_not_found();
1515

1616
// Create a category and a subcategory
1717
app.db(|conn| {

src/tests/routes/crates/following.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::util::{RequestHelper, TestApp};
55
fn diesel_not_found_results_in_404() {
66
let (_, _, user) = TestApp::init().with_user();
77

8-
user.get("/api/v1/crates/foo_following/following")
8+
user.get::<()>("/api/v1/crates/foo_following/following")
99
.assert_not_found();
1010
}
1111

@@ -22,6 +22,6 @@ fn disallow_api_token_auth_for_get_crate_following_status() {
2222

2323
// Token auth on GET for get following status is disallowed
2424
token
25-
.get(&format!("/api/v1/crates/{a_crate}/following"))
25+
.get::<()>(&format!("/api/v1/crates/{a_crate}/following"))
2626
.assert_forbidden();
2727
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn download_nonexistent_version_of_existing_crate_404s() {
1010
CrateBuilder::new("foo_bad", user.id).expect_build(conn);
1111
});
1212

13-
anon.get("/api/v1/crates/foo_bad/0.1.0/download")
13+
anon.get::<()>("/api/v1/crates/foo_bad/0.1.0/download")
1414
.assert_not_found();
1515
}
1616

src/tests/routes/keywords/read.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct GoodKeyword {
1212
fn show() {
1313
let url = "/api/v1/keywords/foo";
1414
let (app, anon) = TestApp::init().empty();
15-
anon.get(url).assert_not_found();
15+
anon.get::<()>(url).assert_not_found();
1616

1717
app.db(|conn| {
1818
Keyword::find_or_create_all(conn, &["foo"]).unwrap();
@@ -25,7 +25,7 @@ fn show() {
2525
fn uppercase() {
2626
let url = "/api/v1/keywords/UPPER";
2727
let (app, anon) = TestApp::init().empty();
28-
anon.get(url).assert_not_found();
28+
anon.get::<()>(url).assert_not_found();
2929

3030
app.db(|conn| {
3131
Keyword::find_or_create_all(conn, &["UPPER"]).unwrap();

src/tests/routes/me/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct UserShowPrivateResponse {
1919
fn me() {
2020
let url = "/api/v1/me";
2121
let (app, anon) = TestApp::init().empty();
22-
anon.get(url).assert_forbidden();
22+
anon.get::<()>(url).assert_forbidden();
2323

2424
let user = app.db_new_user("foo");
2525
let json = user.show_me();

src/tests/routes/me/tokens/create.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ static NEW_BAR: &[u8] = br#"{ "api_token": { "name": "bar" } }"#;
1111
#[test]
1212
fn create_token_logged_out() {
1313
let (_, anon) = TestApp::init().empty();
14-
anon.put("/api/v1/me/tokens", NEW_BAR).assert_forbidden();
14+
anon.put::<()>("/api/v1/me/tokens", NEW_BAR)
15+
.assert_forbidden();
1516
}
1617

1718
#[test]

src/tests/routes/me/tokens/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use http::StatusCode;
88
#[test]
99
fn list_logged_out() {
1010
let (_, anon) = TestApp::init().empty();
11-
anon.get("/api/v1/me/tokens").assert_forbidden();
11+
anon.get::<()>("/api/v1/me/tokens").assert_forbidden();
1212
}
1313

1414
#[test]
1515
fn list_with_api_token_is_forbidden() {
1616
let (_, _, _, token) = TestApp::init().with_token();
17-
token.get("/api/v1/me/tokens").assert_forbidden();
17+
token.get::<()>("/api/v1/me/tokens").assert_forbidden();
1818
}
1919

2020
#[test]

src/tests/routes/me/updates.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use http::StatusCode;
1010
#[test]
1111
fn api_token_cannot_get_user_updates() {
1212
let (_, _, _, token) = TestApp::init().with_token();
13-
token.get("/api/v1/me/updates").assert_forbidden();
13+
token.get::<()>("/api/v1/me/updates").assert_forbidden();
1414
}
1515

1616
#[test]

src/tests/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn using_token_updates_last_used_at() {
99
let url = "/api/v1/me";
1010
let (app, anon, user, token) = TestApp::init().with_token();
1111

12-
anon.get(url).assert_forbidden();
12+
anon.get::<()>(url).assert_forbidden();
1313
user.get::<EncodableMe>(url).good();
1414
assert_none!(token.as_model().last_used_at);
1515

src/tests/util/response.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ where
2323
}
2424
json(self.response)
2525
}
26+
27+
/// Assert that the status code is 404
28+
#[track_caller]
29+
pub fn assert_not_found(&self) {
30+
assert_eq!(StatusCode::NOT_FOUND, self.status());
31+
}
32+
33+
/// Assert that the status code is 403
34+
#[track_caller]
35+
pub fn assert_forbidden(&self) {
36+
assert_eq!(StatusCode::FORBIDDEN, self.status());
37+
}
2638
}
2739

2840
impl<T> Response<T> {
@@ -59,20 +71,6 @@ impl<T> Response<T> {
5971
}
6072
}
6173

62-
impl Response<()> {
63-
/// Assert that the status code is 404
64-
#[track_caller]
65-
pub fn assert_not_found(&self) {
66-
assert_eq!(StatusCode::NOT_FOUND, self.status());
67-
}
68-
69-
/// Assert that the status code is 403
70-
#[track_caller]
71-
pub fn assert_forbidden(&self) {
72-
assert_eq!(StatusCode::FORBIDDEN, self.status());
73-
}
74-
}
75-
7674
impl<T> Deref for Response<T> {
7775
type Target = reqwest::blocking::Response;
7876

0 commit comments

Comments
 (0)