Register form and confirmation messages
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
use crate::helpers::TestApp;
|
||||
use crate::helpers::{TestApp, assert_is_redirect_to};
|
||||
use wiremock::{
|
||||
Mock, ResponseTemplate,
|
||||
matchers::{method, path},
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
async fn subscribe_displays_a_confirmation_message_for_valid_form_data() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
Mock::given(path("/v1/email"))
|
||||
@@ -14,10 +14,13 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
|
||||
let response = app.post_subscriptions(body.into()).await;
|
||||
let email = "alphonse.paix@outlook.com";
|
||||
let body = format!("name=Alphonse&email={0}&email_check={0}", email);
|
||||
let response = app.post_subscriptions(body).await;
|
||||
|
||||
assert_eq!(200, response.status().as_u16());
|
||||
assert_is_redirect_to(&response, "/register");
|
||||
let page_html = app.get_register_html().await;
|
||||
assert!(page_html.contains("A confirmation email has been sent"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -30,10 +33,13 @@ async fn subscribe_persists_the_new_subscriber() {
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
|
||||
let response = app.post_subscriptions(body.into()).await;
|
||||
let email = "alphonse.paix@outlook.com";
|
||||
let body = format!("name=Alphonse&email={0}&email_check={0}", email);
|
||||
let response = app.post_subscriptions(body).await;
|
||||
|
||||
assert_eq!(200, response.status().as_u16());
|
||||
assert_is_redirect_to(&response, "/register");
|
||||
let page_html = app.get_register_html().await;
|
||||
assert!(page_html.contains("A confirmation email has been sent"));
|
||||
|
||||
let saved = sqlx::query!("SELECT email, name, status FROM subscriptions")
|
||||
.fetch_one(&app.connection_pool)
|
||||
@@ -67,21 +73,32 @@ async fn subscribe_returns_a_422_when_data_is_missing() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
||||
async fn subscribe_shows_an_error_message_when_fields_are_present_but_invalid() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let test_cases = [
|
||||
("name=&email=alphonse.paix%40outlook.com", "empty name"),
|
||||
("name=Alphonse&email=", "empty email"),
|
||||
("name=Alphonse&email=not-an-email", "invalid email"),
|
||||
("name=&email=alphonse.paix%40outlook.com", "an empty name"),
|
||||
("name=Alphonse&email=&email_check=", "an empty email"),
|
||||
(
|
||||
"name=Alphonse&email=not-an-email&email_check=not-an_email",
|
||||
"an invalid email",
|
||||
),
|
||||
(
|
||||
"name=Alphonse&email=alphonse.paix@outlook.com&email_check=alphonse.paix@outlook.fr",
|
||||
"two different email addresses",
|
||||
),
|
||||
];
|
||||
for (body, description) in test_cases {
|
||||
let response = app.post_subscriptions(body.into()).await;
|
||||
let response_text = app
|
||||
.post_subscriptions(body.into())
|
||||
.await
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
400,
|
||||
response.status().as_u16(),
|
||||
"the API did not fail with 400 Bad Request when the payload had an {}.",
|
||||
assert!(
|
||||
!response_text.contains("Your account has been confirmed"),
|
||||
"the API did not displayed an error message when the payload had an {}.",
|
||||
description
|
||||
);
|
||||
}
|
||||
@@ -91,7 +108,8 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
||||
async fn subscribe_sends_a_confirmation_email_for_valid_data() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
|
||||
let email = "alphonse.paix@outlook.com";
|
||||
let body = format!("name=Alphonse&email={0}&email_check={0}", email);
|
||||
|
||||
Mock::given(path("v1/email"))
|
||||
.and(method("POST"))
|
||||
@@ -100,14 +118,15 @@ async fn subscribe_sends_a_confirmation_email_for_valid_data() {
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
app.post_subscriptions(body.into()).await;
|
||||
app.post_subscriptions(body).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_sends_a_confirmation_email_with_a_link() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
|
||||
let email = "alphonse.paix@outlook.com";
|
||||
let body = format!("name=Alphonse&email={0}&email_check={0}", email);
|
||||
|
||||
Mock::given(path("v1/email"))
|
||||
.and(method("POST"))
|
||||
@@ -116,7 +135,7 @@ async fn subscribe_sends_a_confirmation_email_with_a_link() {
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
app.post_subscriptions(body.into()).await;
|
||||
app.post_subscriptions(body).await;
|
||||
|
||||
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
||||
let confirmation_links = app.get_confirmation_links(email_request);
|
||||
@@ -127,14 +146,15 @@ async fn subscribe_sends_a_confirmation_email_with_a_link() {
|
||||
async fn subscribe_fails_if_there_is_a_fatal_database_error() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
|
||||
let email = "alphonse.paix@outlook.com";
|
||||
let body = format!("name=Alphonse&email={0}&email_check={0}", email);
|
||||
|
||||
sqlx::query!("ALTER TABLE subscriptions DROP COLUMN email")
|
||||
.execute(&app.connection_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let response = app.post_subscriptions(body.into()).await;
|
||||
let response = app.post_subscriptions(body).await;
|
||||
|
||||
assert_eq!(response.status().as_u16(), 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user