zero-to-production icon indicating copy to clipboard operation
zero-to-production copied to clipboard

Chapter 11.8: `newsletter_creation_is_idempotent` test does not pass

Open devmatteini opened this issue 3 years ago • 0 comments

Hi!

After completing chapter 11.8 the new test that we added newsletter_creation_is_idempotent in tests/api/newsletter.rs does not pass as expected.

cargo t newsletter_creation_is_idempotent output:

running 1 test
test newsletters::newsletter_creation_is_idempotent ... FAILED

failures:

---- newsletters::newsletter_creation_is_idempotent stdout ----
thread 'newsletters::newsletter_creation_is_idempotent' panicked at 'assertion failed: html_page.contains(\"<p><i>The newsletter issue has been published!</i></p>\")', tests/api/newsletters.rs:131:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The assertions that fails is:

//! tests/api/newsletter.rs

#[tokio::test]
async fn newsletter_creation_is_idempotent() {
    // [...]

    // Act - Part 3 - Submit newsletter form **again**
    let response = app.post_publish_newsletter(&newsletter_request_body).await;
    assert_is_redirect_to(&response, "/admin/newsletters");

    // Act - Part 4 - Follow the redirect
    let html_page = app.get_publish_newsletter_html().await;
    // Fails here 👇
    assert!(html_page.contains("<p><i>The newsletter issue has been published!</i></p>"));
    // Mock verifies on Drop that we have sent the newsletter email **once**
}

To make the test pass, I added this line when we find a saved response in publish_newsletter:

//! src/routes/admin/newsletter/post.rs
pub async fn publish_newsletter(/**/) -> Result<HttpResponse, actix_web::Error> {
    // [...]
    if let Some(saved_response) = get_saved_response(&pool, &idempotency_key, *user_id)
        .await
        .map_err(e500)?
    {
        // I added this line here 👇
        FlashMessage::info("The newsletter issue has been published!").send();
        return Ok(saved_response);
    }
    // [...]
}

I guess this the right way to do it because looking at commit from root-chapter-11 it was done the same thing in the chapters after 11.8: https://github.com/LukeMathWalker/zero-to-production/blob/f4fc0138219a1115a9c22161866f804475fe84fd/src/routes/admin/newsletter/post.rs#L49-L51

devmatteini avatar Aug 22 '22 17:08 devmatteini