Skip to content

Commit 1822885

Browse files
committed
Auto merge of #3061 - Turbo87:status, r=pietroalbini
tests/util: Replace `Response::assert_status()` with `Response::status()` `Response::status()` seems easier to use to me and doesn't hide the actual assertion. It also allows us to access things like `StatusCode::is_success()` directly. r? `@jtgeibel`
2 parents 46b765f + 5f8d96f commit 1822885

13 files changed

+83
-85
lines changed

src/tests/account_lock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn account_locked_indefinitely() {
2727
lock_account(&app, user.as_model().id, None);
2828

2929
let response = user.get::<()>(URL);
30-
response.assert_status(StatusCode::FORBIDDEN);
30+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
3131

3232
let error_message = format!(
3333
"This account is indefinitely locked. Reason: {}",
@@ -48,7 +48,7 @@ fn account_locked_with_future_expiry() {
4848

4949
let until = until.format("%Y-%m-%d at %H:%M:%S UTC");
5050
let response = user.get::<()>(URL);
51-
response.assert_status(StatusCode::FORBIDDEN);
51+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
5252

5353
let error_message = format!(
5454
"This account is locked until {}. Reason: {}",

src/tests/krate/dependencies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn dependencies() {
2727
assert_eq!(deps.dependencies[0].crate_id, "bar_deps");
2828

2929
let response = anon.get::<()>("/api/v1/crates/foo_deps/1.0.2/dependencies");
30-
response.assert_status(StatusCode::OK);
30+
assert_eq!(response.status(), StatusCode::OK);
3131
assert_eq!(
3232
response.json(),
3333
json!({ "errors": [{ "detail": "crate `foo_deps` does not have a version `1.0.2`" }] })

src/tests/krate/downloads.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ fn download() {
3737

3838
let download = |name_and_version: &str| {
3939
let url = format!("/api/v1/crates/{}/download", name_and_version);
40-
anon.get::<()>(&url).assert_status(StatusCode::FOUND);
40+
let response = anon.get::<()>(&url);
41+
assert_eq!(response.status(), StatusCode::FOUND);
4142
// TODO: test the with_json code path
4243
};
4344

src/tests/krate/publish.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn new_wrong_token() {
5757
// Try to publish without a token
5858
let crate_to_publish = PublishBuilder::new("foo");
5959
let response = anon.enqueue_publish(crate_to_publish);
60-
response.assert_status(StatusCode::FORBIDDEN);
60+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
6161
assert_eq!(
6262
response.json(),
6363
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -73,7 +73,7 @@ fn new_wrong_token() {
7373

7474
let crate_to_publish = PublishBuilder::new("foo");
7575
let response = token.enqueue_publish(crate_to_publish);
76-
response.assert_status(StatusCode::FORBIDDEN);
76+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
7777
assert_eq!(
7878
response.json(),
7979
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -87,7 +87,7 @@ fn invalid_names() {
8787
let bad_name = |name: &str, error_message: &str| {
8888
let crate_to_publish = PublishBuilder::new(name).version("1.0.0");
8989
let response = token.enqueue_publish(crate_to_publish);
90-
response.assert_status(StatusCode::OK);
90+
assert_eq!(response.status(), StatusCode::OK);
9191

9292
let json = response.json();
9393
let json = json.as_object().unwrap();
@@ -253,7 +253,7 @@ fn reject_new_krate_with_non_exact_dependency() {
253253
.dependency(dependency);
254254

255255
let response = token.enqueue_publish(crate_to_publish);
256-
response.assert_status(StatusCode::OK);
256+
assert_eq!(response.status(), StatusCode::OK);
257257
assert_eq!(
258258
response.json(),
259259
json!({ "errors": [{ "detail": "no known crate named `foo_dep`" }] })
@@ -282,7 +282,7 @@ fn reject_new_crate_with_alternative_registry_dependency() {
282282

283283
let crate_to_publish = PublishBuilder::new("depends-on-alt-registry").dependency(dependency);
284284
let response = token.enqueue_publish(crate_to_publish);
285-
response.assert_status(StatusCode::OK);
285+
assert_eq!(response.status(), StatusCode::OK);
286286
assert_eq!(
287287
response.json(),
288288
json!({ "errors": [{ "detail": "Dependency `dep` is hosted on another registry. Cross-registry dependencies are not permitted on crates.io." }] })
@@ -305,7 +305,7 @@ fn new_krate_with_wildcard_dependency() {
305305
.dependency(dependency);
306306

307307
let response = token.enqueue_publish(crate_to_publish);
308-
response.assert_status(StatusCode::OK);
308+
assert_eq!(response.status(), StatusCode::OK);
309309
assert_eq!(
310310
response.json(),
311311
json!({ "errors": [{ "detail": WILDCARD_ERROR_MESSAGE }] })
@@ -344,7 +344,7 @@ fn new_krate_wrong_user() {
344344
let crate_to_publish = PublishBuilder::new("foo_wrong").version("2.0.0");
345345

346346
let response = another_user.enqueue_publish(crate_to_publish);
347-
response.assert_status(StatusCode::OK);
347+
assert_eq!(response.status(), StatusCode::OK);
348348
assert_eq!(
349349
response.json(),
350350
json!({ "errors": [{ "detail": MISSING_RIGHTS_ERROR_MESSAGE }] })
@@ -359,7 +359,7 @@ fn new_krate_too_big() {
359359
let builder = PublishBuilder::new("foo_big").files(&files);
360360

361361
let response = user.enqueue_publish(builder);
362-
response.assert_status(StatusCode::OK);
362+
assert_eq!(response.status(), StatusCode::OK);
363363
assert_eq!(
364364
response.json(),
365365
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -392,7 +392,7 @@ fn new_krate_wrong_files() {
392392
let builder = PublishBuilder::new("foo").files(&files);
393393

394394
let response = user.enqueue_publish(builder);
395-
response.assert_status(StatusCode::OK);
395+
assert_eq!(response.status(), StatusCode::OK);
396396
assert_eq!(
397397
response.json(),
398398
json!({ "errors": [{ "detail": "invalid tarball uploaded" }] })
@@ -411,7 +411,7 @@ fn new_krate_gzip_bomb() {
411411
.files_with_io(&mut [("foo-1.1.0/a", &mut body, len)]);
412412

413413
let response = token.enqueue_publish(crate_to_publish);
414-
response.assert_status(StatusCode::OK);
414+
assert_eq!(response.status(), StatusCode::OK);
415415
assert_eq!(
416416
response.json(),
417417
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -431,7 +431,7 @@ fn new_krate_duplicate_version() {
431431

432432
let crate_to_publish = PublishBuilder::new("foo_dupe").version("1.0.0");
433433
let response = token.enqueue_publish(crate_to_publish);
434-
response.assert_status(StatusCode::OK);
434+
assert_eq!(response.status(), StatusCode::OK);
435435
assert_eq!(
436436
response.json(),
437437
json!({ "errors": [{ "detail": "crate version `1.0.0` is already uploaded" }] })
@@ -450,7 +450,7 @@ fn new_crate_similar_name() {
450450

451451
let crate_to_publish = PublishBuilder::new("foo_similar").version("1.1.0");
452452
let response = token.enqueue_publish(crate_to_publish);
453-
response.assert_status(StatusCode::OK);
453+
assert_eq!(response.status(), StatusCode::OK);
454454
assert_eq!(
455455
response.json(),
456456
json!({ "errors": [{ "detail": "crate was previously named `Foo_similar`" }] })
@@ -469,7 +469,7 @@ fn new_crate_similar_name_hyphen() {
469469

470470
let crate_to_publish = PublishBuilder::new("foo-bar-hyphen").version("1.1.0");
471471
let response = token.enqueue_publish(crate_to_publish);
472-
response.assert_status(StatusCode::OK);
472+
assert_eq!(response.status(), StatusCode::OK);
473473
assert_eq!(
474474
response.json(),
475475
json!({ "errors": [{ "detail": "crate was previously named `foo_bar_hyphen`" }] })
@@ -488,7 +488,7 @@ fn new_crate_similar_name_underscore() {
488488

489489
let crate_to_publish = PublishBuilder::new("foo_bar_underscore").version("1.1.0");
490490
let response = token.enqueue_publish(crate_to_publish);
491-
response.assert_status(StatusCode::OK);
491+
assert_eq!(response.status(), StatusCode::OK);
492492
assert_eq!(
493493
response.json(),
494494
json!({ "errors": [{ "detail": "crate was previously named `foo-bar-underscore`" }] })
@@ -561,7 +561,7 @@ fn new_krate_dependency_missing() {
561561
let crate_to_publish = PublishBuilder::new("foo_missing").dependency(dependency);
562562

563563
let response = token.enqueue_publish(crate_to_publish);
564-
response.assert_status(StatusCode::OK);
564+
assert_eq!(response.status(), StatusCode::OK);
565565
assert_eq!(
566566
response.json(),
567567
json!({ "errors": [{ "detail": "no known crate named `bar_missing`" }] })
@@ -590,7 +590,7 @@ fn new_krate_without_any_email_fails() {
590590
let crate_to_publish = PublishBuilder::new("foo_no_email");
591591

592592
let response = token.enqueue_publish(crate_to_publish);
593-
response.assert_status(StatusCode::OK);
593+
assert_eq!(response.status(), StatusCode::OK);
594594
assert_eq!(
595595
response.json(),
596596
json!({ "errors": [{ "detail": "A verified email address is required to publish crates to crates.io. Visit https://crates.io/me to set and verify your email address." }] })
@@ -611,7 +611,7 @@ fn new_krate_with_unverified_email_fails() {
611611
let crate_to_publish = PublishBuilder::new("foo_unverified_email");
612612

613613
let response = token.enqueue_publish(crate_to_publish);
614-
response.assert_status(StatusCode::OK);
614+
assert_eq!(response.status(), StatusCode::OK);
615615
assert_eq!(
616616
response.json(),
617617
json!({ "errors": [{ "detail": "A verified email address is required to publish crates to crates.io. Visit https://crates.io/me to set and verify your email address." }] })
@@ -701,23 +701,23 @@ fn bad_keywords() {
701701
let crate_to_publish =
702702
PublishBuilder::new("foo_bad_key").keyword("super-long-keyword-name-oh-no");
703703
let response = token.enqueue_publish(crate_to_publish);
704-
response.assert_status(StatusCode::OK);
704+
assert_eq!(response.status(), StatusCode::OK);
705705
assert_eq!(
706706
response.json(),
707707
json!({ "errors": [{ "detail": "invalid upload request: invalid length 29, expected a keyword with less than 20 characters at line 1 column 221" }] })
708708
);
709709

710710
let crate_to_publish = PublishBuilder::new("foo_bad_key").keyword("?@?%");
711711
let response = token.enqueue_publish(crate_to_publish);
712-
response.assert_status(StatusCode::OK);
712+
assert_eq!(response.status(), StatusCode::OK);
713713
assert_eq!(
714714
response.json(),
715715
json!({ "errors": [{ "detail": "invalid upload request: invalid value: string \"?@?%\", expected a valid keyword specifier at line 1 column 196" }] })
716716
);
717717

718718
let crate_to_publish = PublishBuilder::new("foo_bad_key").keyword("áccênts");
719719
let response = token.enqueue_publish(crate_to_publish);
720-
response.assert_status(StatusCode::OK);
720+
assert_eq!(response.status(), StatusCode::OK);
721721
assert_eq!(
722722
response.json(),
723723
json!({ "errors": [{ "detail": "invalid upload request: invalid value: string \"áccênts\", expected a valid keyword specifier at line 1 column 201" }] })
@@ -829,7 +829,7 @@ fn author_license_and_description_required() {
829829
.unset_authors();
830830

831831
let response = token.enqueue_publish(crate_to_publish);
832-
response.assert_status(StatusCode::OK);
832+
assert_eq!(response.status(), StatusCode::OK);
833833
assert_eq!(
834834
response.json(),
835835
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description", "license", "authors"]) }] })
@@ -842,7 +842,7 @@ fn author_license_and_description_required() {
842842
.author("");
843843

844844
let response = token.enqueue_publish(crate_to_publish);
845-
response.assert_status(StatusCode::OK);
845+
assert_eq!(response.status(), StatusCode::OK);
846846
assert_eq!(
847847
response.json(),
848848
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description", "authors"]) }] })
@@ -855,7 +855,7 @@ fn author_license_and_description_required() {
855855
.unset_description();
856856

857857
let response = token.enqueue_publish(crate_to_publish);
858-
response.assert_status(StatusCode::OK);
858+
assert_eq!(response.status(), StatusCode::OK);
859859
assert_eq!(
860860
response.json(),
861861
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description"]) }] })
@@ -882,7 +882,7 @@ fn new_krate_tarball_with_hard_links() {
882882
let crate_to_publish = PublishBuilder::new("foo").version("1.1.0").tarball(tarball);
883883

884884
let response = token.enqueue_publish(crate_to_publish);
885-
response.assert_status(StatusCode::OK);
885+
assert_eq!(response.status(), StatusCode::OK);
886886
assert_eq!(
887887
response.json(),
888888
json!({ "errors": [{ "detail": "invalid tarball uploaded" }] })
@@ -901,13 +901,12 @@ fn publish_new_crate_rate_limited() {
901901

902902
// Uploading a second crate is limited
903903
let crate_to_publish = PublishBuilder::new("rate_limited2");
904-
token
905-
.enqueue_publish(crate_to_publish)
906-
.assert_status(StatusCode::TOO_MANY_REQUESTS);
904+
let response = token.enqueue_publish(crate_to_publish);
905+
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
907906
app.run_pending_background_jobs();
908907

909-
anon.get::<()>("/api/v1/crates/rate_limited2")
910-
.assert_status(StatusCode::NOT_FOUND);
908+
let response = anon.get::<()>("/api/v1/crates/rate_limited2");
909+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
911910

912911
// Wait for the limit to be up
913912
thread::sleep(Duration::from_millis(500));

src/tests/krate/search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,15 @@ fn pagination_parameters_only_accept_integers() {
699699

700700
let response =
701701
anon.get_with_query::<()>("/api/v1/crates", "page=1&per_page=100%22%EF%BC%8Cexception");
702-
response.assert_status(StatusCode::BAD_REQUEST);
702+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
703703
assert_eq!(
704704
response.json(),
705705
json!({ "errors": [{ "detail": "invalid digit found in string" }] })
706706
);
707707

708708
let response =
709709
anon.get_with_query::<()>("/api/v1/crates", "page=100%22%EF%BC%8Cexception&per_page=1");
710-
response.assert_status(StatusCode::BAD_REQUEST);
710+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
711711
assert_eq!(
712712
response.json(),
713713
json!({ "errors": [{ "detail": "invalid digit found in string" }] })

src/tests/krate/yanking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn yank_by_a_non_owner_fails() {
110110
});
111111

112112
let response = token.yank("foo_not", "1.0.0");
113-
response.assert_status(StatusCode::OK);
113+
assert_eq!(response.status(), StatusCode::OK);
114114
assert_eq!(
115115
response.json(),
116116
json!({ "errors": [{ "detail": "must already be an owner to yank or unyank" }] })

src/tests/owners.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn owners_can_remove_self() {
136136

137137
// Deleting yourself when you're the only owner isn't allowed.
138138
let response = token.remove_named_owner("owners_selfremove", username);
139-
response.assert_status(StatusCode::OK);
139+
assert_eq!(response.status(), StatusCode::OK);
140140
assert_eq!(
141141
response.json(),
142142
json!({ "errors": [{ "detail": "cannot remove all individual owners of a crate. Team member don't have permission to modify owners, so at least one individual owner is required." }] })
@@ -146,15 +146,15 @@ fn owners_can_remove_self() {
146146

147147
// Deleting yourself when there are other owners is allowed.
148148
let response = token.remove_named_owner("owners_selfremove", username);
149-
response.assert_status(StatusCode::OK);
149+
assert_eq!(response.status(), StatusCode::OK);
150150
assert_eq!(
151151
response.json(),
152152
json!({ "msg": "owners successfully removed", "ok": true })
153153
);
154154

155155
// After you delete yourself, you no longer have permisions to manage the crate.
156156
let response = token.remove_named_owner("owners_selfremove", username);
157-
response.assert_status(StatusCode::OK);
157+
assert_eq!(response.status(), StatusCode::OK);
158158
assert_eq!(
159159
response.json(),
160160
json!({ "errors": [{ "detail": "only owners have permission to modify owners" }] })
@@ -175,7 +175,7 @@ fn modify_multiple_owners() {
175175

176176
// Deleting all owners is not allowed.
177177
let response = token.remove_named_owners("owners_multiple", &[username, "user2", "user3"]);
178-
response.assert_status(StatusCode::OK);
178+
assert_eq!(response.status(), StatusCode::OK);
179179
assert_eq!(
180180
response.json(),
181181
json!({ "errors": [{ "detail": "cannot remove all individual owners of a crate. Team member don't have permission to modify owners, so at least one individual owner is required." }] })
@@ -184,7 +184,7 @@ fn modify_multiple_owners() {
184184

185185
// Deleting two owners at once is allowed.
186186
let response = token.remove_named_owners("owners_multiple", &["user2", "user3"]);
187-
response.assert_status(StatusCode::OK);
187+
assert_eq!(response.status(), StatusCode::OK);
188188
assert_eq!(
189189
response.json(),
190190
json!({ "msg": "owners successfully removed", "ok": true })
@@ -193,7 +193,7 @@ fn modify_multiple_owners() {
193193

194194
// Adding multiple users fails if one of them already is an owner.
195195
let response = token.add_named_owners("owners_multiple", &["user2", username]);
196-
response.assert_status(StatusCode::OK);
196+
assert_eq!(response.status(), StatusCode::OK);
197197
assert_eq!(
198198
response.json(),
199199
json!({ "errors": [{ "detail": "`foo` is already an owner" }] })
@@ -202,7 +202,7 @@ fn modify_multiple_owners() {
202202

203203
// Adding multiple users at once succeeds.
204204
let response = token.add_named_owners("owners_multiple", &["user2", "user3"]);
205-
response.assert_status(StatusCode::OK);
205+
assert_eq!(response.status(), StatusCode::OK);
206206
assert_eq!(
207207
response.json(),
208208
json!({

src/tests/read_only_mode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use diesel::prelude::*;
88
fn can_hit_read_only_endpoints_in_read_only_mode() {
99
let (app, anon) = TestApp::init().empty();
1010
app.db(set_read_only).unwrap();
11-
anon.get::<()>("/api/v1/crates")
12-
.assert_status(StatusCode::OK);
11+
let response = anon.get::<()>("/api/v1/crates");
12+
assert_eq!(response.status(), StatusCode::OK);
1313
}
1414

1515
#[test]
@@ -21,9 +21,9 @@ fn cannot_hit_endpoint_which_writes_db_in_read_only_mode() {
2121
.expect_build(conn);
2222
set_read_only(conn).unwrap();
2323
});
24-
token
25-
.delete::<()>("/api/v1/crates/foo_yank_read_only/1.0.0/yank")
26-
.assert_status(StatusCode::SERVICE_UNAVAILABLE);
24+
25+
let response = token.delete::<()>("/api/v1/crates/foo_yank_read_only/1.0.0/yank");
26+
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
2727

2828
// Restore the transaction so `TestApp::drop` can still access the transaction
2929
app.db(|conn| {
@@ -44,8 +44,8 @@ fn can_download_crate_in_read_only_mode() {
4444
set_read_only(conn).unwrap();
4545
});
4646

47-
anon.get::<()>("/api/v1/crates/foo_download_read_only/1.0.0/download")
48-
.assert_status(StatusCode::FOUND);
47+
let response = anon.get::<()>("/api/v1/crates/foo_download_read_only/1.0.0/download");
48+
assert_eq!(response.status(), StatusCode::FOUND);
4949

5050
// We're in read only mode so the download should not have been counted
5151
app.db(|conn| {

0 commit comments

Comments
 (0)