Skip to content

Commit 794cc87

Browse files
committed
Use json! macro instead of format! in tests.
1 parent 10851f3 commit 794cc87

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/tests/user.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,19 @@ impl crate::util::MockCookieUser {
4646
// TODO: I don't like the name of this method or the one above; this is starting to look like
4747
// a builder might help? I want to explore alternative abstractions in any case
4848
fn update_email_more_control(&self, user_id: i32, email: Option<&str>) -> Response<OkBool> {
49-
let email_json = match email {
50-
Some(val) => format!("\"{}\"", val),
51-
None => String::from("null"),
52-
};
53-
5449
// When updating your email in crates.io, the request goes to the user route with PUT.
5550
// Ember sends all the user attributes. We check to make sure the ID in the URL matches
5651
// the ID of the currently logged in user, then we ignore everything but the email address.
57-
let body = format!("{{\"user\":{{\"email\":{},\"name\":\"Arbitrary Name\",\"login\":\"arbitrary_login\",\"avatar\":\"https://arbitrary.com/img.jpg\",\"url\":\"https://arbitrary.com\",\"kind\":null}}}}", email_json);
52+
let body = json!({"user": {
53+
"email": email,
54+
"name": "Arbitrary Name",
55+
"login": "arbitrary_login",
56+
"avatar": "https://arbitrary.com/img.jpg",
57+
"url": "https://arbitrary.com",
58+
"kind": null
59+
}});
5860
let url = format!("/api/v1/users/{}", user_id);
59-
60-
self.put(&url, body.as_bytes())
61+
self.put(&url, body.to_string().as_bytes())
6162
}
6263

6364
fn confirm_email(&self, email_token: &str) -> OkBool {
@@ -70,18 +71,19 @@ impl crate::util::MockAnonymousUser {
7071
// TODO: Refactor to get rid of this duplication with the same method implemented on
7172
// MockCookieUser
7273
fn update_email_more_control(&self, user_id: i32, email: Option<&str>) -> Response<OkBool> {
73-
let email_json = match email {
74-
Some(val) => format!("\"{}\"", val),
75-
None => String::from("null"),
76-
};
77-
7874
// When updating your email in crates.io, the request goes to the user route with PUT.
7975
// Ember sends all the user attributes. We check to make sure the ID in the URL matches
8076
// the ID of the currently logged in user, then we ignore everything but the email address.
81-
let body = format!("{{\"user\":{{\"email\":{},\"name\":\"Arbitrary Name\",\"login\":\"arbitrary_login\",\"avatar\":\"https://arbitrary.com/img.jpg\",\"url\":\"https://arbitrary.com\",\"kind\":null}}}}", email_json);
77+
let body = json!({"user": {
78+
"email": email,
79+
"name": "Arbitrary Name",
80+
"login": "arbitrary_login",
81+
"avatar": "https://arbitrary.com/img.jpg",
82+
"url": "https://arbitrary.com",
83+
"kind": null
84+
}});
8285
let url = format!("/api/v1/users/{}", user_id);
83-
84-
self.put(&url, body.as_bytes())
86+
self.put(&url, body.to_string().as_bytes())
8587
}
8688
}
8789

src/tests/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ impl MockTokenUser {
522522
F: Fn(&MockTokenUser, &str, &[u8]) -> Response<OkBool>,
523523
{
524524
let url = format!("/api/v1/crates/{}/owners", krate_name);
525-
let body = format!("{{\"owners\":[\"{}\"]}}", owners.join("\", \""));
526-
method(&self, &url, body.as_bytes())
525+
let body = json!({ "owners": owners }).to_string();
526+
method(&self, &url, body.to_string().as_bytes())
527527
}
528528

529529
/// Add a user as an owner for a crate.

0 commit comments

Comments
 (0)