Skip to content

tests: define assert_forbidden and assert_not_found for any response #6585

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/tests/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ fn api_token_cannot_list_invitations_v1() {
let (_, _, _, token) = TestApp::init().with_token();

token
.get("/api/v1/me/crate_owner_invitations")
.get::<()>("/api/v1/me/crate_owner_invitations")
.assert_forbidden();
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/routes/categories/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn show() {
let url = "/api/v1/categories/foo-bar";

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

// Create a category and a subcategory
app.db(|conn| {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/routes/crates/following.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::util::{RequestHelper, TestApp};
fn diesel_not_found_results_in_404() {
let (_, _, user) = TestApp::init().with_user();

user.get("/api/v1/crates/foo_following/following")
user.get::<()>("/api/v1/crates/foo_following/following")
.assert_not_found();
}

Expand All @@ -22,6 +22,6 @@ fn disallow_api_token_auth_for_get_crate_following_status() {

// Token auth on GET for get following status is disallowed
token
.get(&format!("/api/v1/crates/{a_crate}/following"))
.get::<()>(&format!("/api/v1/crates/{a_crate}/following"))
.assert_forbidden();
}
2 changes: 1 addition & 1 deletion src/tests/routes/crates/versions/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn download_nonexistent_version_of_existing_crate_404s() {
CrateBuilder::new("foo_bad", user.id).expect_build(conn);
});

anon.get("/api/v1/crates/foo_bad/0.1.0/download")
anon.get::<()>("/api/v1/crates/foo_bad/0.1.0/download")
.assert_not_found();
}

Expand Down
4 changes: 2 additions & 2 deletions src/tests/routes/keywords/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct GoodKeyword {
fn show() {
let url = "/api/v1/keywords/foo";
let (app, anon) = TestApp::init().empty();
anon.get(url).assert_not_found();
anon.get::<()>(url).assert_not_found();

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

app.db(|conn| {
Keyword::find_or_create_all(conn, &["UPPER"]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/routes/me/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct UserShowPrivateResponse {
fn me() {
let url = "/api/v1/me";
let (app, anon) = TestApp::init().empty();
anon.get(url).assert_forbidden();
anon.get::<()>(url).assert_forbidden();

let user = app.db_new_user("foo");
let json = user.show_me();
Expand Down
3 changes: 2 additions & 1 deletion src/tests/routes/me/tokens/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ static NEW_BAR: &[u8] = br#"{ "api_token": { "name": "bar" } }"#;
#[test]
fn create_token_logged_out() {
let (_, anon) = TestApp::init().empty();
anon.put("/api/v1/me/tokens", NEW_BAR).assert_forbidden();
anon.put::<()>("/api/v1/me/tokens", NEW_BAR)
.assert_forbidden();
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/tests/routes/me/tokens/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use http::StatusCode;
#[test]
fn list_logged_out() {
let (_, anon) = TestApp::init().empty();
anon.get("/api/v1/me/tokens").assert_forbidden();
anon.get::<()>("/api/v1/me/tokens").assert_forbidden();
}

#[test]
fn list_with_api_token_is_forbidden() {
let (_, _, _, token) = TestApp::init().with_token();
token.get("/api/v1/me/tokens").assert_forbidden();
token.get::<()>("/api/v1/me/tokens").assert_forbidden();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/tests/routes/me/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use http::StatusCode;
#[test]
fn api_token_cannot_get_user_updates() {
let (_, _, _, token) = TestApp::init().with_token();
token.get("/api/v1/me/updates").assert_forbidden();
token.get::<()>("/api/v1/me/updates").assert_forbidden();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/tests/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn using_token_updates_last_used_at() {
let url = "/api/v1/me";
let (app, anon, user, token) = TestApp::init().with_token();

anon.get(url).assert_forbidden();
anon.get::<()>(url).assert_forbidden();
user.get::<EncodableMe>(url).good();
assert_none!(token.as_model().last_used_at);

Expand Down
26 changes: 12 additions & 14 deletions src/tests/util/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ where
}
json(self.response)
}

/// Assert that the status code is 404
#[track_caller]
pub fn assert_not_found(&self) {
assert_eq!(StatusCode::NOT_FOUND, self.status());
}

/// Assert that the status code is 403
#[track_caller]
pub fn assert_forbidden(&self) {
assert_eq!(StatusCode::FORBIDDEN, self.status());
}
}

impl<T> Response<T> {
Expand Down Expand Up @@ -59,20 +71,6 @@ impl<T> Response<T> {
}
}

impl Response<()> {
/// Assert that the status code is 404
#[track_caller]
pub fn assert_not_found(&self) {
assert_eq!(StatusCode::NOT_FOUND, self.status());
}

/// Assert that the status code is 403
#[track_caller]
pub fn assert_forbidden(&self) {
assert_eq!(StatusCode::FORBIDDEN, self.status());
}
}

impl<T> Deref for Response<T> {
type Target = reqwest::blocking::Response;

Expand Down