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);
}