Unsubscribe option available on website
This commit is contained in:
67
tests/api/unsubscribe_confirm.rs
Normal file
67
tests/api/unsubscribe_confirm.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use crate::helpers::{TestApp, when_sending_an_email};
|
||||
use wiremock::ResponseTemplate;
|
||||
|
||||
#[tokio::test]
|
||||
async fn unsubscribe_form_sends_a_valid_link_if_email_is_in_database() {
|
||||
let app = TestApp::spawn().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"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_invalid_email_is_ignored() {
|
||||
let app = TestApp::spawn().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;
|
||||
}
|
||||
Reference in New Issue
Block a user