Skip to content

Reduce OkBool usage in tests #7493

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 3 commits into from
Nov 9, 2023
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
10 changes: 6 additions & 4 deletions src/tests/routes/me/email_notifications.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::builders::CrateBuilder;
use crate::new_user;
use crate::util::{RequestHelper, TestApp};
use crate::{new_user, OkBool};
use crates_io::schema::crate_owners;
use diesel::prelude::*;
use http::StatusCode;

#[derive(Serialize)]
struct EmailNotificationsUpdate {
Expand All @@ -11,9 +12,10 @@ struct EmailNotificationsUpdate {
}

impl crate::util::MockCookieUser {
fn update_email_notifications(&self, updates: Vec<EmailNotificationsUpdate>) -> OkBool {
self.put("/api/v1/me/email_notifications", json!(updates).to_string())
.good()
fn update_email_notifications(&self, updates: Vec<EmailNotificationsUpdate>) {
let response = self.put::<()>("/api/v1/me/email_notifications", json!(updates).to_string());
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.into_json(), json!({ "ok": true }));
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/tests/routes/users/update.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::util::{RequestHelper, Response, TestApp};
use crate::OkBool;
use http::StatusCode;

pub trait MockEmailHelper: RequestHelper {
// TODO: I don't like the name of this method or `update_email` on the `MockCookieUser` impl;
// this is starting to look like a builder might help?
// I want to explore alternative abstractions in any case.
fn update_email_more_control(&self, user_id: i32, email: Option<&str>) -> Response<OkBool> {
fn update_email_more_control(&self, user_id: i32, email: Option<&str>) -> Response<()> {
// When updating your email in crates.io, the request goes to the user route with PUT.
// Ember sends all the user attributes. We check to make sure the ID in the URL matches
// the ID of the currently logged in user, then we ignore everything but the email address.
Expand All @@ -27,9 +26,11 @@ impl MockEmailHelper for crate::util::MockCookieUser {}
impl MockEmailHelper for crate::util::MockAnonymousUser {}

impl crate::util::MockCookieUser {
pub fn update_email(&self, email: &str) -> OkBool {
pub fn update_email(&self, email: &str) {
let model = self.as_model();
self.update_email_more_control(model.id, Some(email)).good()
let response = self.update_email_more_control(model.id, Some(email));
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.into_json(), json!({ "ok": true }));
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/tests/user.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::{
new_user,
util::{MockCookieUser, RequestHelper},
OkBool, TestApp,
TestApp,
};
use crates_io::models::{Email, NewUser, User};
use diesel::prelude::*;
use http::StatusCode;
use secrecy::ExposeSecret;

impl crate::util::MockCookieUser {
fn confirm_email(&self, email_token: &str) -> OkBool {
fn confirm_email(&self, email_token: &str) {
let url = format!("/api/v1/confirm/{email_token}");
self.put(&url, &[] as &[u8]).good()
let response = self.put::<()>(&url, &[] as &[u8]);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.into_json(), json!({ "ok": true }));
}
}

Expand Down