Skip to content

Commit 2ffe240

Browse files
authored
tests: Inline assert_not_found() and assert_forbidden() fns (#11344)
These functions are somewhat inflexible, and while they use a more fluent chaining API, the rest of our assertions don't, which makes these a bit harder to use in some cases where other assertions are running on the same response instances.
1 parent afe701a commit 2ffe240

File tree

10 files changed

+31
-37
lines changed

10 files changed

+31
-37
lines changed

src/tests/owners.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,8 @@ async fn invitations_are_empty_by_default_v1() {
421421
async fn api_token_cannot_list_invitations_v1() {
422422
let (_, _, _, token) = TestApp::init().with_token().await;
423423

424-
token
425-
.get("/api/v1/me/crate_owner_invitations")
426-
.await
427-
.assert_forbidden();
424+
let response = token.get::<()>("/api/v1/me/crate_owner_invitations").await;
425+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
428426
}
429427

430428
#[tokio::test(flavor = "multi_thread")]

src/tests/routes/categories/get.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::tests::util::{MockAnonymousUser, RequestHelper, TestApp};
55
use crates_io_database::schema::categories;
66
use diesel::insert_into;
77
use diesel_async::RunQueryDsl;
8+
use http::StatusCode;
89
use insta::assert_json_snapshot;
910
use serde_json::Value;
1011

@@ -16,7 +17,8 @@ async fn show() -> anyhow::Result<()> {
1617
let url = "/api/v1/categories/foo-bar";
1718

1819
// Return not found if a category doesn't exist
19-
anon.get(url).await.assert_not_found();
20+
let response = anon.get::<()>(url).await;
21+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
2022

2123
// Create a category and a subcategory
2224
let cats = vec![

src/tests/routes/crates/following.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::tests::builders::CrateBuilder;
22
use crate::tests::util::{RequestHelper, TestApp};
3+
use http::StatusCode;
34

45
#[tokio::test(flavor = "multi_thread")]
56
async fn diesel_not_found_results_in_404() {
67
let (_, _, user) = TestApp::init().with_user().await;
78

8-
user.get("/api/v1/crates/foo_following/following")
9-
.await
10-
.assert_not_found();
9+
let url = "/api/v1/crates/foo_following/following";
10+
let response = user.get::<()>(url).await;
11+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
1112
}
1213

1314
#[tokio::test(flavor = "multi_thread")]
@@ -23,8 +24,7 @@ async fn disallow_api_token_auth_for_get_crate_following_status() {
2324
.await;
2425

2526
// Token auth on GET for get following status is disallowed
26-
token
27-
.get(&format!("/api/v1/crates/{a_crate}/following"))
28-
.await
29-
.assert_forbidden();
27+
let url = format!("/api/v1/crates/{a_crate}/following");
28+
let response = token.get::<()>(&url).await;
29+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
3030
}

src/tests/routes/keywords/read.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::models::Keyword;
22
use crate::tests::builders::CrateBuilder;
33
use crate::tests::util::{RequestHelper, TestApp};
44
use crate::views::EncodableKeyword;
5+
use http::StatusCode;
56

67
#[derive(Deserialize)]
78
struct GoodKeyword {
@@ -14,7 +15,8 @@ async fn show() -> anyhow::Result<()> {
1415
let (app, anon) = TestApp::init().empty().await;
1516
let mut conn = app.db_conn().await;
1617

17-
anon.get(url).await.assert_not_found();
18+
let response = anon.get::<()>(url).await;
19+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
1820

1921
Keyword::find_or_create_all(&mut conn, &["foo"]).await?;
2022

@@ -30,7 +32,8 @@ async fn uppercase() -> anyhow::Result<()> {
3032
let (app, anon) = TestApp::init().empty().await;
3133
let mut conn = app.db_conn().await;
3234

33-
anon.get(url).await.assert_not_found();
35+
let response = anon.get::<()>(url).await;
36+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
3437

3538
Keyword::find_or_create_all(&mut conn, &["UPPER"]).await?;
3639

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ static NEW_BAR: &[u8] = br#"{ "api_token": { "name": "bar" } }"#;
1414
#[tokio::test(flavor = "multi_thread")]
1515
async fn create_token_logged_out() {
1616
let (_, anon) = TestApp::init().empty().await;
17-
anon.put("/api/v1/me/tokens", NEW_BAR)
18-
.await
19-
.assert_forbidden();
17+
let response = anon.put::<()>("/api/v1/me/tokens", NEW_BAR).await;
18+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
2019
}
2120

2221
#[tokio::test(flavor = "multi_thread")]

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use insta::assert_json_snapshot;
88
async fn show_token_non_existing() {
99
let url = "/api/v1/me/tokens/10086";
1010
let (_, _, user, _) = TestApp::init().with_token().await;
11-
user.get(url).await.assert_not_found();
11+
let response = user.get::<()>(url).await;
12+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
1213
}
1314

1415
#[tokio::test(flavor = "multi_thread")]
@@ -58,7 +59,8 @@ async fn show_token_with_scopes() {
5859
async fn show_with_anonymous_user() {
5960
let url = "/api/v1/me/tokens/1";
6061
let (_, anon) = TestApp::init().empty().await;
61-
anon.get(url).await.assert_forbidden();
62+
let response = anon.get::<()>(url).await;
63+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
6264
}
6365

6466
#[tokio::test(flavor = "multi_thread")]

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ use serde_json::json;
99
#[tokio::test(flavor = "multi_thread")]
1010
async fn list_logged_out() {
1111
let (_, anon) = TestApp::init().empty().await;
12-
anon.get("/api/v1/me/tokens").await.assert_forbidden();
12+
let response = anon.get::<()>("/api/v1/me/tokens").await;
13+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
1314
}
1415

1516
#[tokio::test(flavor = "multi_thread")]
1617
async fn list_with_api_token_is_forbidden() {
1718
let (_, _, _, token) = TestApp::init().with_token().await;
18-
token.get("/api/v1/me/tokens").await.assert_forbidden();
19+
let response = token.get::<()>("/api/v1/me/tokens").await;
20+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
1921
}
2022

2123
#[tokio::test(flavor = "multi_thread")]

src/tests/routes/me/updates.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use insta::assert_snapshot;
1313
#[tokio::test(flavor = "multi_thread")]
1414
async fn api_token_cannot_get_user_updates() {
1515
let (_, _, _, token) = TestApp::init().with_token().await;
16-
token.get("/api/v1/me/updates").await.assert_forbidden();
16+
let response = token.get::<()>("/api/v1/me/updates").await;
17+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
1718
}
1819

1920
#[tokio::test(flavor = "multi_thread")]

src/tests/token.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ async fn using_token_updates_last_used_at() {
1212
let (app, anon, user, token) = TestApp::init().with_token().await;
1313
let mut conn = app.db_conn().await;
1414

15-
anon.get(url).await.assert_forbidden();
15+
let response = anon.get::<()>(url).await;
16+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
1617
user.get::<EncodableMe>(url).await.good();
1718
assert_none!(token.as_model().last_used_at);
1819

src/tests/util/response.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ impl<T> Response<T> {
8383
}
8484
}
8585

86-
impl Response<()> {
87-
/// Assert that the status code is 404
88-
#[track_caller]
89-
pub fn assert_not_found(&self) {
90-
assert_eq!(self.status(), StatusCode::NOT_FOUND);
91-
}
92-
93-
/// Assert that the status code is 403
94-
#[track_caller]
95-
pub fn assert_forbidden(&self) {
96-
assert_eq!(self.status(), StatusCode::FORBIDDEN);
97-
}
98-
}
99-
10086
fn json<T>(r: &hyper::Response<Bytes>) -> T
10187
where
10288
for<'de> T: serde::Deserialize<'de>,

0 commit comments

Comments
 (0)