diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index fc921fb..e0548ee 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -61,6 +61,14 @@ impl TestUser { .await .expect("Failed to store test user."); } + + pub async fn login(&self, app: &TestApp) { + app.post_login(&serde_json::json!({ + "username": &self.username, + "password": &self.password, + })) + .await; + } } pub struct TestApp { diff --git a/tests/api/newsletter.rs b/tests/api/newsletter.rs index a58fa0c..64d5678 100644 --- a/tests/api/newsletter.rs +++ b/tests/api/newsletter.rs @@ -9,8 +9,8 @@ use crate::helpers::{assert_is_redirect_to, spawn_app, ConfirmationLinks, TestAp async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() { // Arrange let app = spawn_app().await; - create_uncorfirmed_subscriber(&app).await; + app.test_user.login(&app).await; Mock::given(any()) .respond_with(ResponseTemplate::new(200)) @@ -18,23 +18,15 @@ async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() { .mount(&app.email_server) .await; - app.post_login(&serde_json::json!({ - "username": &app.test_user.username, - "password": &app.test_user.password, - })) - .await; - // Act let newsletter_request_body = serde_json::json!({ "title": "Newsletter Title", "text_content": "Newsletter body as plain text", "html_content": "
Newsletter body as HTML
" }); - let response = app.post_newsletters(&newsletter_request_body).await; + app.post_newsletters(&newsletter_request_body).await; // Assert - assert_eq!(response.status().as_u16(), 200); - // Mock verifies on drop that we haven't sent the newsletter email. } @@ -42,8 +34,8 @@ async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() { async fn newsletters_are_delivered_to_confirmed_subscribers() { // Arrange let app = spawn_app().await; - create_confirmed_subscriber(&app).await; + app.test_user.login(&app).await; Mock::given(path("/emails")) .and(method("POST")) @@ -52,23 +44,15 @@ async fn newsletters_are_delivered_to_confirmed_subscribers() { .mount(&app.email_server) .await; - app.post_login(&serde_json::json!({ - "username": &app.test_user.username, - "password": &app.test_user.password, - })) - .await; - // Act let newsletter_request_body = serde_json::json!({ "title": "Newsletter Title", "text_content": "Newsletter body as plain text", "html_content": "Newsletter body as HTML
" }); - let response = app.post_newsletters(&newsletter_request_body).await; + 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. } @@ -76,6 +60,7 @@ async fn newsletters_are_delivered_to_confirmed_subscribers() { async fn newsletters_returns_400_for_invalid_data() { // Arrange let app = spawn_app().await; + app.test_user.login(&app).await; let test_cases = vec![ ( serde_json::json!({ @@ -100,12 +85,6 @@ async fn newsletters_returns_400_for_invalid_data() { ), ]; - app.post_login(&serde_json::json!({ - "username": &app.test_user.username, - "password": &app.test_user.password, - })) - .await; - for (invalid_body, error_message) in test_cases { // Act let response = app.post_newsletters(&invalid_body).await;