Register form and confirmation messages
This commit is contained in:
@@ -177,6 +177,17 @@ impl TestApp {
|
||||
self.get_admin_dashboard().await.text().await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_register_html(&self) -> String {
|
||||
self.api_client
|
||||
.get(format!("{}/register", &self.address))
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request")
|
||||
.text()
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_change_password(&self) -> reqwest::Response {
|
||||
self.api_client
|
||||
.get(format!("{}/admin/password", &self.address))
|
||||
|
||||
@@ -199,7 +199,8 @@ async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
|
||||
let email: String = SafeEmail().fake();
|
||||
let body = serde_urlencoded::to_string(serde_json::json!({
|
||||
"name": name,
|
||||
"email": email
|
||||
"email": email,
|
||||
"email_check": email
|
||||
}))
|
||||
.unwrap();
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -15,10 +15,11 @@ async fn confirmation_links_without_token_are_rejected_with_a_400() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
||||
async fn clicking_on_the_link_shows_a_confiramtion_message() {
|
||||
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={email}&email_check={email}");
|
||||
|
||||
Mock::given(path("v1/email"))
|
||||
.and(method("POST"))
|
||||
@@ -27,19 +28,27 @@ async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
||||
.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);
|
||||
let response = reqwest::get(confirmation_links.html).await.unwrap();
|
||||
assert_eq!(response.status().as_u16(), 200);
|
||||
assert!(
|
||||
response
|
||||
.text()
|
||||
.await
|
||||
.unwrap()
|
||||
.contains("Your account has been confirmed")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn clicking_on_the_confirmation_link_confirms_a_subscriber() {
|
||||
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={email}&email_check={email}");
|
||||
|
||||
Mock::given(path("v1/email"))
|
||||
.and(method("POST"))
|
||||
@@ -48,7 +57,7 @@ async fn clicking_on_the_confirmation_link_confirms_a_subscriber() {
|
||||
.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);
|
||||
|
||||
Reference in New Issue
Block a user