Skip to content

Commit

Permalink
feat(backend): Reject a request with to_email field empty or missing (#…
Browse files Browse the repository at this point in the history
…1353)

* reject request if to_mail field is missing

* add BAD_REQUEST on missing email

* tests now pass

* fixed typos

---------

Co-authored-by: Davide-Furlani <davide.furlani0998@gmail.com>
  • Loading branch information
zagoli and Davide-Furlani authored Oct 24, 2023
1 parent a09c853 commit 1d9c29f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
4 changes: 2 additions & 2 deletions backend/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
16 changes: 13 additions & 3 deletions backend/src/routes/check_email/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
32 changes: 32 additions & 0 deletions backend/tests/check_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."}"#);
}

0 comments on commit 1d9c29f

Please sign in to comment.