mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 05:56:35 +00:00
First integration tests for newsletter delivery.
This commit is contained in:
parent
c1caed5404
commit
5299ef1f1d
2 changed files with 103 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
mod health_check;
|
mod health_check;
|
||||||
mod helpers;
|
mod helpers;
|
||||||
|
mod newsletter;
|
||||||
mod subscriptions;
|
mod subscriptions;
|
||||||
mod subscriptions_confirm;
|
mod subscriptions_confirm;
|
||||||
|
|
102
tests/api/newsletter.rs
Normal file
102
tests/api/newsletter.rs
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
use crate::helpers::{spawn_app, ConfirmationLinks, TestApp};
|
||||||
|
use wiremock::matchers::{method, path};
|
||||||
|
use wiremock::{Mock, ResponseTemplate};
|
||||||
|
|
||||||
|
async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
|
||||||
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
||||||
|
|
||||||
|
Mock::given(path("/email"))
|
||||||
|
.and(method("POST"))
|
||||||
|
.respond_with(ResponseTemplate::new(200))
|
||||||
|
.up_to_n_times(1)
|
||||||
|
.mount(&app.email_server)
|
||||||
|
.await;
|
||||||
|
app.post_subscriptions(body.into())
|
||||||
|
.await
|
||||||
|
.error_for_status()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let email_request = &app
|
||||||
|
.email_server
|
||||||
|
.received_requests()
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.pop()
|
||||||
|
.unwrap();
|
||||||
|
app.get_confirmation_links(&email_request)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_confirmed_subscriber(app: &TestApp) {
|
||||||
|
let confirmation_link = create_unconfirmed_subscriber(app).await.html;
|
||||||
|
reqwest::get(confirmation_link)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.error_for_status()
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() {
|
||||||
|
// Arrange
|
||||||
|
let app = spawn_app().await;
|
||||||
|
create_unconfirmed_subscriber(&app).await;
|
||||||
|
|
||||||
|
Mock::given(path("/email"))
|
||||||
|
.and(method("POST"))
|
||||||
|
.respond_with(ResponseTemplate::new(200))
|
||||||
|
.expect(0)
|
||||||
|
.mount(&app.email_server)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let newsletter_request_body = serde_json::json!({
|
||||||
|
"title": "Newsletter title",
|
||||||
|
"body": {
|
||||||
|
"text": "Newsletter body as plain text",
|
||||||
|
"html": "<p>Newsletter body as HTML</p>",
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let response = reqwest::Client::new()
|
||||||
|
.post(&format!("{}/newsletters", &app.address))
|
||||||
|
.json(&newsletter_request_body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request.");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert_eq!(response.status().as_u16(), 200);
|
||||||
|
// Mock verifies on Drop that we haven't sent the newsletter email
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn newsletters_are_delivered_to_confirmed_subscribers() {
|
||||||
|
// Arrange
|
||||||
|
let app = spawn_app().await;
|
||||||
|
create_confirmed_subscriber(&app).await;
|
||||||
|
|
||||||
|
Mock::given(path("/email"))
|
||||||
|
.and(method("POST"))
|
||||||
|
.respond_with(ResponseTemplate::new(200))
|
||||||
|
.expect(1)
|
||||||
|
.mount(&app.email_server)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let newsletter_request_body = serde_json::json!({
|
||||||
|
"title": "Newsletter title",
|
||||||
|
"body": {
|
||||||
|
"text": "Newsletter body as plain text",
|
||||||
|
"html": "<p>Newsletter body as HTML</p>",
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let response = reqwest::Client::new()
|
||||||
|
.post(&format!("{}/newsletters", &app.address))
|
||||||
|
.json(&newsletter_request_body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request.");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert_eq!(response.status().as_u16(), 200);
|
||||||
|
// Mock verifies on Drop that we have sent the newsletter email
|
||||||
|
}
|
Loading…
Reference in a new issue