69 lines
1.8 KiB
Rust
69 lines
1.8 KiB
Rust
use crate::helpers::{TestApp, when_sending_an_email};
|
|
use sqlx::PgPool;
|
|
use wiremock::ResponseTemplate;
|
|
|
|
#[sqlx::test]
|
|
async fn unsubscribe_form_sends_a_valid_link_if_email_is_in_database(connection_pool: PgPool) {
|
|
let app = TestApp::spawn(connection_pool).await;
|
|
app.create_confirmed_subscriber().await;
|
|
|
|
when_sending_an_email()
|
|
.respond_with(ResponseTemplate::new(200))
|
|
.expect(1)
|
|
.mount(&app.email_server)
|
|
.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 body = serde_json::json!({
|
|
"email": record.email
|
|
});
|
|
app.post_unsubscribe(&body).await;
|
|
|
|
let email_request = app
|
|
.email_server
|
|
.received_requests()
|
|
.await
|
|
.unwrap()
|
|
.pop()
|
|
.unwrap();
|
|
|
|
let unsubscribe_links = app.get_unsubscribe_links(&email_request);
|
|
|
|
assert!(
|
|
unsubscribe_links
|
|
.html
|
|
.as_str()
|
|
.contains(&record.unsubscribe_token.unwrap())
|
|
);
|
|
|
|
let response = reqwest::get(unsubscribe_links.html)
|
|
.await
|
|
.unwrap()
|
|
.error_for_status()
|
|
.unwrap();
|
|
let html_fragment = response.text().await.unwrap();
|
|
assert!(html_fragment.contains("Good bye, friend"));
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn an_invalid_email_is_ignored(connection_pool: PgPool) {
|
|
let app = TestApp::spawn(connection_pool).await;
|
|
app.create_confirmed_subscriber().await;
|
|
|
|
when_sending_an_email()
|
|
.respond_with(ResponseTemplate::new(200))
|
|
.expect(0)
|
|
.mount(&app.email_server)
|
|
.await;
|
|
|
|
let body = serde_json::json!({
|
|
"email": "invalid.email@example.com"
|
|
});
|
|
|
|
app.post_unsubscribe(&body).await;
|
|
}
|