|
| 1 | +use crate::helpers::spawn_app; |
| 2 | + |
| 3 | +#[tokio::test] |
| 4 | +async fn subscribe_returns_a_200_for_valid_form_data() { |
| 5 | + // Arrange |
| 6 | + let app_details = spawn_app().await; |
| 7 | + let connection = &app_details.db_pool; |
| 8 | + let client = reqwest::Client::new(); |
| 9 | + |
| 10 | + // Act |
| 11 | + let body = "name=le%20guid&email=ursula_le_guin%40gmail.com"; |
| 12 | + let response = client |
| 13 | + .post(&format!("{}/subscriptions", &app_details.address)) |
| 14 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 15 | + .body(body) |
| 16 | + .send() |
| 17 | + .await |
| 18 | + .expect("Failed to execute request."); |
| 19 | + // Assert |
| 20 | + assert_eq!(200, response.status().as_u16()); |
| 21 | + |
| 22 | + let saved = sqlx::query!("SELECT email, name FROM subscriptions",) |
| 23 | + .fetch_one(connection) |
| 24 | + .await |
| 25 | + .expect("Failed to fetch saved subscription."); |
| 26 | + println!("saved: {:?}", saved); |
| 27 | + assert_eq!(saved .email , "[email protected]"); |
| 28 | + assert_eq!(saved.name, "le guid"); |
| 29 | +} |
| 30 | + |
| 31 | +#[tokio::test] |
| 32 | +async fn subscribe_returns_a_400_when_data_is_missing() { |
| 33 | + // Arrange |
| 34 | + let app_details = spawn_app().await; |
| 35 | + let client = reqwest::Client::new(); |
| 36 | + let test_cases = vec![ |
| 37 | + ("name=le%20guin", "missing the email"), |
| 38 | + ("email=ursula_le_guin%40gmail.com", "missing the name"), |
| 39 | + ("", "missing both name and email"), |
| 40 | + ]; |
| 41 | + for (invalid_body, error_message) in test_cases { |
| 42 | + // Act |
| 43 | + let response = client |
| 44 | + .post(&format!("{}/subscriptions", &app_details.address)) |
| 45 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 46 | + .body(invalid_body) |
| 47 | + .send() |
| 48 | + .await |
| 49 | + .expect("Failed to execute request."); |
| 50 | + // Assert |
| 51 | + assert_eq!( |
| 52 | + 400, |
| 53 | + response.status().as_u16(), |
| 54 | + // Additional customised error message on test failure |
| 55 | + "The API did not fail with 400 Bad Request when the payload was {}.", |
| 56 | + error_message |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[tokio::test] |
| 62 | +async fn subscribe_returns_a_400_when_fields_are_present_but_empty() { |
| 63 | + // Arrange |
| 64 | + let app_details = spawn_app().await; |
| 65 | + let client = reqwest::Client::new(); |
| 66 | + let test_cases = vec![ |
| 67 | + ("name=&email=ursula_le_guin%40gmail.com", "empty name"), |
| 68 | + ("name=le%20guin&email=", "empty email"), |
| 69 | + ( |
| 70 | + "name=le%20guin&email=definitely_not_an_email", |
| 71 | + "invalid email", |
| 72 | + ), |
| 73 | + ]; |
| 74 | + for (invalid_body, error_message) in test_cases { |
| 75 | + // Act |
| 76 | + let response = client |
| 77 | + .post(&format!("{}/subscriptions", &app_details.address)) |
| 78 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 79 | + .body(invalid_body) |
| 80 | + .send() |
| 81 | + .await |
| 82 | + .expect("Failed to execute request."); |
| 83 | + // Assert |
| 84 | + assert_eq!( |
| 85 | + 400, |
| 86 | + response.status().as_u16(), |
| 87 | + // Additional customised error message on test failure |
| 88 | + "The API did not return a 400 bad request when the payload was {}.", |
| 89 | + error_message |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments