Skip to content

tests/util: Replace Response::assert_status() with Response::status() #3061

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

Merged
merged 1 commit into from
Jan 15, 2021
Merged
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
4 changes: 2 additions & 2 deletions src/tests/account_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn account_locked_indefinitely() {
lock_account(&app, user.as_model().id, None);

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

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

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

let error_message = format!(
"This account is locked until {}. Reason: {}",
Expand Down
2 changes: 1 addition & 1 deletion src/tests/krate/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn dependencies() {
assert_eq!(deps.dependencies[0].crate_id, "bar_deps");

let response = anon.get::<()>("/api/v1/crates/foo_deps/1.0.2/dependencies");
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": "crate `foo_deps` does not have a version `1.0.2`" }] })
Expand Down
3 changes: 2 additions & 1 deletion src/tests/krate/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn download() {

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

Expand Down
57 changes: 28 additions & 29 deletions src/tests/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn new_wrong_token() {
// Try to publish without a token
let crate_to_publish = PublishBuilder::new("foo");
let response = anon.enqueue_publish(crate_to_publish);
response.assert_status(StatusCode::FORBIDDEN);
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
Expand All @@ -73,7 +73,7 @@ fn new_wrong_token() {

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

let json = response.json();
let json = json.as_object().unwrap();
Expand Down Expand Up @@ -253,7 +253,7 @@ fn reject_new_krate_with_non_exact_dependency() {
.dependency(dependency);

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

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

let response = token.enqueue_publish(crate_to_publish);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": WILDCARD_ERROR_MESSAGE }] })
Expand Down Expand Up @@ -344,7 +344,7 @@ fn new_krate_wrong_user() {
let crate_to_publish = PublishBuilder::new("foo_wrong").version("2.0.0");

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

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

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

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

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

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

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

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

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

let response = token.enqueue_publish(crate_to_publish);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
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." }] })
Expand All @@ -611,7 +611,7 @@ fn new_krate_with_unverified_email_fails() {
let crate_to_publish = PublishBuilder::new("foo_unverified_email");

let response = token.enqueue_publish(crate_to_publish);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
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." }] })
Expand Down Expand Up @@ -701,23 +701,23 @@ fn bad_keywords() {
let crate_to_publish =
PublishBuilder::new("foo_bad_key").keyword("super-long-keyword-name-oh-no");
let response = token.enqueue_publish(crate_to_publish);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": "invalid upload request: invalid length 29, expected a keyword with less than 20 characters at line 1 column 221" }] })
);

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

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

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

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

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

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

// Uploading a second crate is limited
let crate_to_publish = PublishBuilder::new("rate_limited2");
token
.enqueue_publish(crate_to_publish)
.assert_status(StatusCode::TOO_MANY_REQUESTS);
let response = token.enqueue_publish(crate_to_publish);
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
app.run_pending_background_jobs();

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

// Wait for the limit to be up
thread::sleep(Duration::from_millis(500));
Expand Down
4 changes: 2 additions & 2 deletions src/tests/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,15 +699,15 @@ fn pagination_parameters_only_accept_integers() {

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

let response =
anon.get_with_query::<()>("/api/v1/crates", "page=100%22%EF%BC%8Cexception&per_page=1");
response.assert_status(StatusCode::BAD_REQUEST);
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": "invalid digit found in string" }] })
Expand Down
2 changes: 1 addition & 1 deletion src/tests/krate/yanking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn yank_by_a_non_owner_fails() {
});

let response = token.yank("foo_not", "1.0.0");
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": "must already be an owner to yank or unyank" }] })
Expand Down
14 changes: 7 additions & 7 deletions src/tests/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn owners_can_remove_self() {

// Deleting yourself when you're the only owner isn't allowed.
let response = token.remove_named_owner("owners_selfremove", username);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
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." }] })
Expand All @@ -146,15 +146,15 @@ fn owners_can_remove_self() {

// Deleting yourself when there are other owners is allowed.
let response = token.remove_named_owner("owners_selfremove", username);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
json!({ "msg": "owners successfully removed", "ok": true })
);

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

// Deleting all owners is not allowed.
let response = token.remove_named_owners("owners_multiple", &[username, "user2", "user3"]);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
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." }] })
Expand All @@ -184,7 +184,7 @@ fn modify_multiple_owners() {

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

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

// Adding multiple users at once succeeds.
let response = token.add_named_owners("owners_multiple", &["user2", "user3"]);
response.assert_status(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.json(),
json!({
Expand Down
14 changes: 7 additions & 7 deletions src/tests/read_only_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use diesel::prelude::*;
fn can_hit_read_only_endpoints_in_read_only_mode() {
let (app, anon) = TestApp::init().empty();
app.db(set_read_only).unwrap();
anon.get::<()>("/api/v1/crates")
.assert_status(StatusCode::OK);
let response = anon.get::<()>("/api/v1/crates");
assert_eq!(response.status(), StatusCode::OK);
}

#[test]
Expand All @@ -21,9 +21,9 @@ fn cannot_hit_endpoint_which_writes_db_in_read_only_mode() {
.expect_build(conn);
set_read_only(conn).unwrap();
});
token
.delete::<()>("/api/v1/crates/foo_yank_read_only/1.0.0/yank")
.assert_status(StatusCode::SERVICE_UNAVAILABLE);

let response = token.delete::<()>("/api/v1/crates/foo_yank_read_only/1.0.0/yank");
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);

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

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

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