mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-10 10:49:35 +00:00
Refactor tests.
This commit is contained in:
parent
83826d0166
commit
32c03a55c5
3 changed files with 49 additions and 14 deletions
|
@ -1,12 +1,12 @@
|
|||
use actix_web::{web, HttpResponse};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct BodyData {
|
||||
title: String,
|
||||
content: Content,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Content {
|
||||
html: String,
|
||||
text: String,
|
||||
|
|
|
@ -43,6 +43,15 @@ impl TestApp {
|
|||
.expect("Failed to execute request.")
|
||||
}
|
||||
|
||||
pub async fn post_newsletters(&self, body: serde_json::Value) -> reqwest::Response {
|
||||
reqwest::Client::new()
|
||||
.post(&format!("{}/newsletters", &self.address))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request.")
|
||||
}
|
||||
|
||||
/// Extract the confirmation links embedded in the request to the email API.
|
||||
pub fn get_confirmation_links(&self, email_request: &wiremock::Request) -> ConfirmationLinks {
|
||||
let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap();
|
||||
|
|
|
@ -56,12 +56,7 @@ async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() {
|
|||
"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.");
|
||||
let response = app.post_newsletters(newsletter_request_body).await;
|
||||
|
||||
// Assert
|
||||
assert_eq!(response.status().as_u16(), 200);
|
||||
|
@ -89,14 +84,45 @@ async fn newsletters_are_delivered_to_confirmed_subscribers() {
|
|||
"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.");
|
||||
let response = app.post_newsletters(newsletter_request_body).await;
|
||||
|
||||
// Assert
|
||||
assert_eq!(response.status().as_u16(), 200);
|
||||
// Mock verifies on Drop that we have sent the newsletter email
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn newsletters_returns_400_for_invalid_data() {
|
||||
// Arrange
|
||||
let app = spawn_app().await;
|
||||
let test_cases = vec![
|
||||
(
|
||||
serde_json::json!({
|
||||
"content": {
|
||||
"text": "Newsletter body as plain text",
|
||||
"html": "<p>Newsletter body as HTML</p>",
|
||||
}
|
||||
}),
|
||||
"missing title",
|
||||
),
|
||||
(
|
||||
serde_json::json!({
|
||||
"title": "Newsletter!"
|
||||
}),
|
||||
"missing content",
|
||||
),
|
||||
];
|
||||
|
||||
for (invalid_body, error_message) in test_cases {
|
||||
let response = app.post_newsletters(invalid_body).await;
|
||||
|
||||
// Assert
|
||||
assert_eq!(
|
||||
400,
|
||||
response.status().as_u16(),
|
||||
// Additional customised error message on test failure
|
||||
"The API did not fail with 400 Bad Request when the payload was {}.",
|
||||
error_message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue