Tests update
This commit is contained in:
@@ -58,38 +58,6 @@ async fn subscribe_returns_a_422_when_data_is_missing() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
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", "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_text = app
|
||||
.post_subscriptions(body.into())
|
||||
.await
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
!response_text.contains("Your account has been confirmed"),
|
||||
"the API did not displayed an error message when the payload had an {}.",
|
||||
description
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_sends_a_confirmation_email_for_valid_data() {
|
||||
let app = TestApp::spawn().await;
|
||||
@@ -131,7 +99,7 @@ async fn subscribe_fails_if_there_is_a_fatal_database_error() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let email = "alphonse.paix@outlook.com";
|
||||
let body = format!("name=Alphonse&email={0}&email_check={0}", email);
|
||||
let body = format!("name=Alphonse&email={}", email);
|
||||
|
||||
sqlx::query!("ALTER TABLE subscriptions DROP COLUMN email")
|
||||
.execute(&app.connection_pool)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use wiremock::ResponseTemplate;
|
||||
|
||||
use crate::helpers::{TestApp, fake_newsletter_body, fake_post_body, when_sending_an_email};
|
||||
use wiremock::ResponseTemplate;
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscriber_can_unsubscribe() {
|
||||
@@ -88,7 +87,7 @@ async fn a_valid_unsubscribe_link_is_present_in_new_post_email_notifications() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_valid_unsubscribe_link_is_present_in_emails_manually_sent() {
|
||||
async fn a_valid_unsubscribe_link_is_present_in_standalone_emails() {
|
||||
let app = TestApp::spawn().await;
|
||||
app.create_confirmed_subscriber().await;
|
||||
app.admin_login().await;
|
||||
@@ -127,3 +126,64 @@ async fn an_invalid_unsubscribe_token_is_rejected() {
|
||||
|
||||
assert_eq!(response.status().as_u16(), 404);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscription_works_after_unsubscribe() {
|
||||
let app = TestApp::spawn().await;
|
||||
app.create_confirmed_subscriber().await;
|
||||
|
||||
let record = sqlx::query!("SELECT email, unsubscribe_token FROM subscriptions")
|
||||
.fetch_one(&app.connection_pool)
|
||||
.await
|
||||
.expect("Failed to fetch saved email and token");
|
||||
let email = record.email;
|
||||
|
||||
let response = app
|
||||
.get_unsubscribe(
|
||||
record
|
||||
.unsubscribe_token
|
||||
.expect("Confirmed subscriber should have a valid unsubscribe token"),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status().as_u16(), 200);
|
||||
let html_fragment = response.text().await.unwrap();
|
||||
assert!(html_fragment.contains("Good bye, old friend"));
|
||||
|
||||
let record = sqlx::query!("SELECT email, unsubscribe_token FROM subscriptions")
|
||||
.fetch_optional(&app.connection_pool)
|
||||
.await
|
||||
.expect("Failed to fetch subscriber");
|
||||
|
||||
assert!(record.is_none());
|
||||
|
||||
when_sending_an_email()
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
app.post_subscriptions(format!("email={}", email)).await;
|
||||
|
||||
let requests = app.email_server.received_requests().await.unwrap();
|
||||
let confirmation_request = requests.last().unwrap();
|
||||
let confirmation_links = app.get_confirmation_links(confirmation_request);
|
||||
dbg!(&confirmation_links.html.as_str());
|
||||
|
||||
let response = reqwest::get(confirmation_links.html).await.unwrap();
|
||||
assert_eq!(response.status().as_u16(), 200);
|
||||
assert!(
|
||||
response
|
||||
.text()
|
||||
.await
|
||||
.unwrap()
|
||||
.contains("Your email has been confirmed")
|
||||
);
|
||||
|
||||
let record = sqlx::query!("SELECT email, unsubscribe_token FROM subscriptions")
|
||||
.fetch_optional(&app.connection_pool)
|
||||
.await
|
||||
.expect("Failed to fetch subscriber");
|
||||
|
||||
assert!(record.is_some());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user