Skip to content

Commit

Permalink
test: clean up newsletter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
migueloller committed Feb 11, 2024
1 parent 82b3255 commit 35df464
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
8 changes: 8 additions & 0 deletions tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 5 additions & 26 deletions tests/api/newsletter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,33 @@ 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))
.expect(0)
.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": "<p>Newsletter body as HTML</p>"
});
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.
}

#[tokio::test]
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"))
Expand All @@ -52,30 +44,23 @@ 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": "<p>Newsletter body as HTML</p>"
});
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.
}

#[tokio::test]
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!({
Expand All @@ -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;
Expand Down

0 comments on commit 35df464

Please sign in to comment.