Skip to content

Commit 012b72c

Browse files
committed
tests: Use json! macro to assert on error responses instead of bad_with_status()
1 parent 8b2edc0 commit 012b72c

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

src/tests/krate/dependencies.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ fn dependencies() {
2626
.good();
2727
assert_eq!(deps.dependencies[0].crate_id, "bar_deps");
2828

29-
anon.get::<()>("/api/v1/crates/foo_deps/1.0.2/dependencies")
30-
.bad_with_status(StatusCode::OK);
29+
let response = anon.get::<()>("/api/v1/crates/foo_deps/1.0.2/dependencies");
30+
response.assert_status(StatusCode::OK);
31+
assert_eq!(
32+
response.json(),
33+
json!({ "errors": [{ "detail": "crate `foo_deps` does not have a version `1.0.2`" }] })
34+
);
3135
}

src/tests/krate/publish.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ fn invalid_names() {
8686

8787
let bad_name = |name: &str, error_message: &str| {
8888
let crate_to_publish = PublishBuilder::new(name).version("1.0.0");
89-
let json = token
90-
.enqueue_publish(crate_to_publish)
91-
.bad_with_status(StatusCode::OK);
92-
93-
assert!(
94-
json.errors[0].detail.contains(error_message,),
95-
"{:?}",
96-
json.errors
97-
);
89+
let response = token.enqueue_publish(crate_to_publish);
90+
response.assert_status(StatusCode::OK);
91+
92+
let json = response.json();
93+
let json = json.as_object().unwrap();
94+
let errors = json.get("errors").unwrap().as_array().unwrap();
95+
let first_error = errors.first().unwrap().as_object().unwrap();
96+
let detail = first_error.get("detail").unwrap().as_str().unwrap();
97+
assert!(detail.contains(error_message), "{:?}", detail);
9898
};
9999

100100
let error_message = "expected a valid crate name";
@@ -251,9 +251,13 @@ fn reject_new_krate_with_non_exact_dependency() {
251251
let crate_to_publish = PublishBuilder::new("new_dep")
252252
.version("1.0.0")
253253
.dependency(dependency);
254-
token
255-
.enqueue_publish(crate_to_publish)
256-
.bad_with_status(StatusCode::OK);
254+
255+
let response = token.enqueue_publish(crate_to_publish);
256+
response.assert_status(StatusCode::OK);
257+
assert_eq!(
258+
response.json(),
259+
json!({ "errors": [{ "detail": "no known crate named `foo_dep`" }] })
260+
);
257261
}
258262

259263
#[test]
@@ -696,19 +700,28 @@ fn bad_keywords() {
696700
let (_, _, _, token) = TestApp::init().with_token();
697701
let crate_to_publish =
698702
PublishBuilder::new("foo_bad_key").keyword("super-long-keyword-name-oh-no");
699-
token
700-
.enqueue_publish(crate_to_publish)
701-
.bad_with_status(StatusCode::OK);
703+
let response = token.enqueue_publish(crate_to_publish);
704+
response.assert_status(StatusCode::OK);
705+
assert_eq!(
706+
response.json(),
707+
json!({ "errors": [{ "detail": "invalid upload request: invalid length 29, expected a keyword with less than 20 characters at line 1 column 221" }] })
708+
);
702709

703710
let crate_to_publish = PublishBuilder::new("foo_bad_key").keyword("?@?%");
704-
token
705-
.enqueue_publish(crate_to_publish)
706-
.bad_with_status(StatusCode::OK);
711+
let response = token.enqueue_publish(crate_to_publish);
712+
response.assert_status(StatusCode::OK);
713+
assert_eq!(
714+
response.json(),
715+
json!({ "errors": [{ "detail": "invalid upload request: invalid value: string \"?@?%\", expected a valid keyword specifier at line 1 column 196" }] })
716+
);
707717

708718
let crate_to_publish = PublishBuilder::new("foo_bad_key").keyword("áccênts");
709-
token
710-
.enqueue_publish(crate_to_publish)
711-
.bad_with_status(StatusCode::OK);
719+
let response = token.enqueue_publish(crate_to_publish);
720+
response.assert_status(StatusCode::OK);
721+
assert_eq!(
722+
response.json(),
723+
json!({ "errors": [{ "detail": "invalid upload request: invalid value: string \"áccênts\", expected a valid keyword specifier at line 1 column 201" }] })
724+
);
712725
}
713726

714727
#[test]

src/tests/user.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,12 @@ fn following() {
297297
assert_eq!(r.versions.len(), 0);
298298
assert_eq!(r.meta.more, false);
299299

300-
user.get_with_query::<()>("/api/v1/me/updates", "page=0")
301-
.bad_with_status(StatusCode::BAD_REQUEST);
300+
let response = user.get_with_query::<()>("/api/v1/me/updates", "page=0");
301+
response.assert_status(StatusCode::BAD_REQUEST);
302+
assert_eq!(
303+
response.json(),
304+
json!({ "errors": [{ "detail": "page indexing starts from 1, page 0 is invalid" }] })
305+
);
302306
}
303307

304308
#[test]
@@ -526,11 +530,15 @@ fn test_other_users_cannot_change_my_email() {
526530
json!({ "errors": [{ "detail": "current user does not match requested user" }] })
527531
);
528532

529-
anon.update_email_more_control(
533+
let response = anon.update_email_more_control(
530534
another_user_model.id,
531535
532-
)
533-
.bad_with_status(StatusCode::FORBIDDEN);
536+
);
537+
response.assert_status(StatusCode::FORBIDDEN);
538+
assert_eq!(
539+
response.json(),
540+
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
541+
);
534542
}
535543

536544
/* Given a new user, test that their email can be added

0 commit comments

Comments
 (0)