62 lines
1.9 KiB
Rust
62 lines
1.9 KiB
Rust
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
|
|
);
|
|
}
|
|
}
|