feat: add confirmation

This commit is contained in:
Sandro Eiler 2024-03-09 14:06:47 +01:00
parent 1bc7ae4c7a
commit 7eebcb12a2
7 changed files with 65 additions and 6 deletions

View file

@ -1,3 +1,4 @@
mod health_check;
mod helpers;
mod subscriptions;
mod subscriptions_confirm;

View file

@ -97,14 +97,31 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
// Assert
assert_eq!(200, response.status().as_u16());
}
let saved = sqlx::query!("SELECT email, name FROM subscriptions",)
#[tokio::test]
async fn subscribe_persists_the_new_subscriber() {
// Arrange
let app = spawn_app().await;
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
Mock::given(path("/email"))
.and(method("POST"))
.respond_with(ResponseTemplate::new(200))
.mount(&app.email_server)
.await;
// Act
app.post_subscriptions(body.into()).await;
// Assert
let saved = sqlx::query!("SELECT email, name, status FROM subscriptions",)
.fetch_one(&app.db_pool)
.await
.expect("Failed to fetch saved subscription.");
assert_eq!(saved.email, "ursula_le_guin@gmail.com");
assert_eq!(saved.name, "le guin");
assert_eq!(saved.status, "pending_confirmation");
}
#[tokio::test]
@ -125,7 +142,7 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
assert_eq!(
400,
response.status().as_u16(),
"The API did not return a 200 OK when the payload was {}.",
"The API did not fail with 400 when the payload was {}.",
description
);
}

View file

@ -0,0 +1,15 @@
use crate::helpers::spawn_app;
#[tokio::test]
async fn confirmations_without_token_are_rejected_with_a_405() {
// Arrange
let app = spawn_app().await;
// Act
let response = reqwest::get(&format!("{}/subscriptions/confirm", app.address))
.await
.unwrap();
// Assert
assert_eq!(response.status().as_u16(), 405);
}