Check if an email address exists without sending any email, written in Rust. Comes with a ⚙️ HTTP backend.
Rust emailemail-validationemail-validation-apiemail-verificationemail-verification-apiemail-verifierhacktoberfest
feat(backend): Reject a request with to_email field empty or missing (#1353)
Jacopo1d9c29f5a486
diff --git a/backend/src/errors.rs b/backend/src/errors.rsindex a69a50b7..b4eafa84 100644--- a/backend/src/errors.rs+++ b/backend/src/errors.rs@@ -24,8 +24,8 @@ use warp::{http, reject};#[derive(Serialize, Debug)]pub struct ReacherResponseError {#[serde(skip)]- code: http::StatusCode,- message: String,+ pub code: http::StatusCode,+ pub message: String,}impl reject::Reject for ReacherResponseError {}diff --git a/backend/src/routes/check_email/post.rs b/backend/src/routes/check_email/post.rsindex 65fc67b4..a36cbacb 100644--- a/backend/src/routes/check_email/post.rs+++ b/backend/src/routes/check_email/post.rs@@ -18,14 +18,23 @@use check_if_email_exists::CheckEmailInput;use check_if_email_exists::LOG_TARGET;-use warp::Filter;+use warp::{http, Filter};use crate::check::{check_email, check_header};+use crate::errors;/// The main endpoint handler that implements the logic of this route.async fn handler(body: CheckEmailInput) -> Result<impl warp::Reply, warp::Rejection> {- // Run the future to check an email.- Ok(warp::reply::json(&check_email(body).await))+ // The to_email field must be present+ if body.to_email.is_empty() {+ Err(warp::reject::custom(errors::ReacherResponseError {+ code: http::StatusCode::BAD_REQUEST,+ message: "to_email field is required.".to_string(),+ }))+ } else {+ // Run the future to check an email.+ Ok(warp::reply::json(&check_email(body).await))+ }}/// Create the `POST /check_email` endpoint.@@ -41,4 +50,5 @@ pub fn post_check_email(.and_then(handler)// View access logs by setting `RUST_LOG=reacher`..with(warp::log(LOG_TARGET))+ .recover(errors::handle_rejection)}diff --git a/backend/tests/check_email.rs b/backend/tests/check_email.rsindex ca857d5a..ee48781c 100644--- a/backend/tests/check_email.rs+++ b/backend/tests/check_email.rs@@ -104,3 +104,35 @@ async fn test_reacher_secret_correct_secret() {assert_eq!(resp.status(), StatusCode::OK, "{:?}", resp.body());assert_eq!(resp.body(), FOO_BAR_RESPONSE);}++#[tokio::test]+async fn test_reacher_to_mail_empty() {+ env::set_var("RCH_HEADER_SECRET", "foobar");++ let resp = request()+ .path("/v0/check_email")+ .method("POST")+ .header(REACHER_SECRET_HEADER, "foobar")+ .json(&serde_json::from_str::<CheckEmailInput>(r#"{"to_email": ""}"#).unwrap())+ .reply(&create_routes(None))+ .await;++ assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "{:?}", resp.body());+ assert_eq!(resp.body(), r#"{"message":"to_email field is required."}"#);+}++#[tokio::test]+async fn test_reacher_to_mail_missing() {+ env::set_var("RCH_HEADER_SECRET", "foobar");++ let resp = request()+ .path("/v0/check_email")+ .method("POST")+ .header(REACHER_SECRET_HEADER, "foobar")+ .json(&serde_json::from_str::<CheckEmailInput>(r#"{}"#).unwrap())+ .reply(&create_routes(None))+ .await;++ assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "{:?}", resp.body());+ assert_eq!(resp.body(), r#"{"message":"to_email field is required."}"#);+}