Email client, application startup logic and tests
This commit is contained in:
61
tests/api/subscriptions.rs
Normal file
61
tests/api/subscriptions.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use crate::helpers::TestApp;
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let body = "name=alphonse&email=alphonse.paix%40outlook.com";
|
||||
let response = app.post_subscriptions(body.into()).await;
|
||||
|
||||
assert_eq!(200, response.status().as_u16());
|
||||
|
||||
let saved = sqlx::query!("SELECT email, name FROM subscriptions")
|
||||
.fetch_one(&app.connection_pool)
|
||||
.await
|
||||
.expect("Failed to fetch saved subscription");
|
||||
|
||||
assert_eq!(saved.email, "alphonse.paix@outlook.com");
|
||||
assert_eq!(saved.name, "alphonse");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_422_when_data_is_missing() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let test_cases = [
|
||||
("name=Alphonse", "missing the email"),
|
||||
("email=alphonse.paix%40outlook.com", "missing the name"),
|
||||
("", "missing both name and email"),
|
||||
];
|
||||
for (invalid_body, error_message) in test_cases {
|
||||
let response = app.post_subscriptions(invalid_body.into()).await;
|
||||
|
||||
assert_eq!(
|
||||
422,
|
||||
response.status().as_u16(),
|
||||
"the API did not fail with 422 Unprocessable Entity when the payload was {}.",
|
||||
error_message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let test_cases = [
|
||||
("name=&email=alphonse.paix%40outlook.com", "empty name"),
|
||||
("name=Alphonse&email=", "empty email"),
|
||||
("name=Alphonse&email=not-an-email", "invalid email"),
|
||||
];
|
||||
for (body, description) in test_cases {
|
||||
let response = app.post_subscriptions(body.into()).await;
|
||||
|
||||
assert_eq!(
|
||||
400,
|
||||
response.status().as_u16(),
|
||||
"the API did not return a 400 Bad Request when the payload had an {}.",
|
||||
description
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user