minhtrannhat 5e6e9c2efe
feat(test): spin up new logical database tests
- Tests will use new database every run
- Added chrono and uuid dependencies.
- Updated documentation
2024-05-04 15:27:47 -04:00

62 lines
1.7 KiB
Rust

mod test_utils;
use test_utils::spawn_app;
#[tokio::test]
async fn subscribe_returns_a_200_for_valid_form_data() {
let test_app = spawn_app().await;
let client = reqwest::Client::new();
let body = "name=le%20test&email=le_test%40gmail.com";
let response = client
.post(&format!("{}/subscribe", &test_app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute subscribe request.");
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
let saved = sqlx::query!("SELECT email, name FROM subscriptions")
.fetch_one(&test_app.db_pool)
.await
.expect("Failed to fetch saved subscribtions");
assert_eq!(saved.email, "le_test@gmail.com");
assert_eq!(saved.name, "le test");
}
#[tokio::test]
async fn subscribe_returns_a_400_when_data_is_missing() {
let test_app = spawn_app().await;
let client = reqwest::Client::new();
let test_cases = vec![
("name=le%20guin", "missing the email"),
("email=ursula_le_guin%40gmail.com", "missing the name"),
("", "missing both name and email"),
];
for (invalid_body, error_message) in test_cases {
let response = client
.post(&format!("{}/subscribe", &test_app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body)
.send()
.await
.expect("Failed to execute subscribe request.");
assert_eq!(
400,
response.status().as_u16(),
"The API failed with 400 Bad Request when the payload was {}.",
error_message
)
}
}