tests for new post notifications and dashboard stats
All checks were successful
Rust / Test (push) Successful in 3m47s
Rust / Rustfmt (push) Successful in 21s
Rust / Clippy (push) Successful in 1m14s
Rust / Code coverage (push) Successful in 3m49s

This commit is contained in:
Alphonse Paix
2025-09-29 18:22:15 +02:00
parent de44564ba0
commit 22c462fba3
8 changed files with 494 additions and 23 deletions

View File

@@ -1,5 +1,7 @@
use crate::helpers::{TestApp, assert_is_redirect_to};
use crate::helpers::{TestApp, assert_is_redirect_to, fake_post_body, when_sending_an_email};
use scraper::{Html, Selector};
use sqlx::PgPool;
use wiremock::ResponseTemplate;
#[sqlx::test]
async fn you_must_be_logged_in_to_access_the_admin_dashboard(connection_pool: PgPool) {
@@ -54,3 +56,68 @@ async fn subscribers_are_visible_on_the_dashboard(connection_pool: PgPool) {
assert!(response.contains("No data available"));
assert!(!response.contains(&subscriber.email));
}
#[sqlx::test]
async fn dashboard_shows_correct_stats(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.admin_login().await;
app.create_confirmed_subscriber().await;
app.create_confirmed_subscriber().await;
app.create_unconfirmed_subscriber().await;
app.post_create_post(&fake_post_body()).await;
app.create_confirmed_subscriber().await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.mount(&app.email_server)
.await;
app.dispatch_all_pending_emails().await;
let html = app.get_admin_dashboard_html().await;
let document = Html::parse_document(&html);
assert_element_is(&document, "p#subscribers-count", "3");
assert_element_is(&document, "p#posts-count", "1");
assert_element_is(&document, "p#notifications-sent", "2");
assert_element_is(&document, "p#open-rate", "0.0%");
let email_request = app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
let links = app.get_post_urls(&email_request);
reqwest::get(links.html).await.unwrap();
let html = app.get_admin_dashboard_html().await;
let document = Html::parse_document(&html);
assert_element_is(&document, "p#open-rate", "50.0%");
app.post_create_post(&fake_post_body()).await;
app.dispatch_all_pending_emails().await;
let email_request = app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
let links = app.get_post_urls(&email_request);
reqwest::get(links.html).await.unwrap();
let html = app.get_admin_dashboard_html().await;
let document = Html::parse_document(&html);
assert_element_is(&document, "p#posts-count", "2");
assert_element_is(&document, "p#notifications-sent", "5");
assert_element_is(&document, "p#open-rate", "40.0%");
}
fn assert_element_is(document: &Html, selectors: &str, value: &str) {
let selector = Selector::parse(selectors).unwrap();
let mut element = document.select(&selector);
assert_eq!(element.next().unwrap().text().collect::<String>(), value);
}

View File

@@ -2,7 +2,13 @@ use argon2::{
Algorithm, Argon2, Params, PasswordHasher, Version,
password_hash::{SaltString, rand_core::OsRng},
};
use fake::{Fake, faker::internet::en::SafeEmail};
use fake::{
Fake,
faker::{
internet::en::SafeEmail,
lorem::en::{Paragraph, Sentence},
},
};
use linkify::{Link, LinkFinder};
use once_cell::sync::Lazy;
use sqlx::PgPool;
@@ -183,6 +189,22 @@ impl TestApp {
}
}
pub fn get_post_urls(&self, request: &wiremock::Request) -> ConfirmationLinks {
let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap();
let get_link = |s: &str| {
let links = get_links(s);
assert!(!links.is_empty());
let mut confirmation_link = reqwest::Url::parse(links[0].as_str()).unwrap();
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
confirmation_link.set_port(Some(self.port)).unwrap();
confirmation_link
};
let html = get_link(body["html"].as_str().unwrap());
let text = get_link(body["text"].as_str().unwrap());
ConfirmationLinks { html, text }
}
pub fn get_unsubscribe_links(&self, request: &wiremock::Request) -> ConfirmationLinks {
let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap();
let get_link = |s: &str| {
@@ -395,3 +417,11 @@ pub fn get_links(s: &'_ str) -> Vec<Link<'_>> {
.filter(|l| *l.kind() == linkify::LinkKind::Url)
.collect()
}
pub fn subject() -> String {
Sentence(1..2).fake()
}
pub fn content() -> String {
Paragraph(1..10).fake()
}

View File

@@ -1,20 +1,10 @@
use crate::helpers::{TestApp, assert_is_redirect_to, when_sending_an_email};
use fake::{
Fake,
faker::lorem::en::{Paragraph, Sentence},
use crate::helpers::{
TestApp, assert_is_redirect_to, content, fake_post_body, subject, when_sending_an_email,
};
use sqlx::PgPool;
use uuid::Uuid;
use wiremock::ResponseTemplate;
fn subject() -> String {
Sentence(1..2).fake()
}
fn content() -> String {
Paragraph(1..10).fake()
}
#[sqlx::test]
async fn you_must_be_logged_in_to_create_a_new_post(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
@@ -83,6 +73,51 @@ async fn confirmed_subscribers_are_notified_when_a_new_post_is_published(connect
app.dispatch_all_pending_emails().await;
}
#[sqlx::test]
async fn notification_contains_the_blog_post_url(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.create_confirmed_subscriber().await;
app.admin_login().await;
let title = subject();
let content = content();
let body = serde_json::json!({
"title": title,
"content": content,
"idempotency_key": Uuid::new_v4(),
});
app.post_create_post(&body).await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
app.dispatch_all_pending_emails().await;
let email_request = app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
let post_id = sqlx::query!("SELECT post_id FROM posts")
.fetch_one(&app.connection_pool)
.await
.unwrap()
.post_id;
let links = app.get_post_urls(&email_request);
let text = String::from_utf8(email_request.body).unwrap();
assert!(text.contains(&title));
assert!(links.html.as_str().contains(&post_id.to_string()));
}
#[sqlx::test]
async fn new_posts_are_visible_on_the_website(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
@@ -165,3 +200,37 @@ async fn a_deleted_blog_post_returns_404(connection_pool: PgPool) {
let html = app.get_post_html(post.post_id).await;
assert!(html.contains("Not Found"));
}
#[sqlx::test]
async fn clicking_the_notification_link_marks_the_email_as_opened(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.admin_login().await;
app.create_confirmed_subscriber().await;
app.post_create_post(&fake_post_body()).await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
app.dispatch_all_pending_emails().await;
let email_request = app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
let links = app.get_post_urls(&email_request);
reqwest::get(links.html.as_str()).await.unwrap();
assert!(
sqlx::query!("SELECT opened FROM notifications_delivered")
.fetch_one(&app.connection_pool)
.await
.unwrap()
.opened
);
}

View File

@@ -34,7 +34,7 @@ async fn subscribe_persists_the_new_subscriber(connection_pool: PgPool) {
let response = app.post_subscriptions(body).await;
assert!(response.status().is_success());
let html_fragment = dbg!(response.text().await.unwrap());
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("You&#39;ll receive a confirmation email shortly"));
let saved = sqlx::query!("SELECT email, status FROM subscriptions")

View File

@@ -161,7 +161,6 @@ async fn subscription_works_after_unsubscribe(connection_pool: PgPool) {
let requests = app.email_server.received_requests().await.unwrap();
let confirmation_request = requests.last().unwrap();
let confirmation_links = app.get_confirmation_links(confirmation_request);
dbg!(&confirmation_links.html.as_str());
let response = reqwest::get(confirmation_links.html).await.unwrap();
assert_eq!(response.status().as_u16(), 200);