Files
zero2prod/tests/api/admin_dashboard.rs
Alphonse Paix fb3e0e12c2
Some checks failed
Rust / Test (push) Failing after 4m44s
Rust / Rustfmt (push) Successful in 21s
Rust / Clippy (push) Failing after 1m34s
Rust / Code coverage (push) Failing after 3m8s
Edit profile and templates update
2025-10-06 19:13:51 +02:00

204 lines
6.4 KiB
Rust

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) {
let app = TestApp::spawn(connection_pool).await;
let response = app.get_admin_dashboard().await;
assert_is_redirect_to(&response, "/login");
}
#[sqlx::test]
async fn logout_clears_session_state(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
let login_body = serde_json::json!({
"username": &app.test_user.username,
"password": &app.test_user.password,
});
let response = app.post_login(&login_body).await;
assert_is_redirect_to(&response, "/dashboard");
let html_page = app.get_admin_dashboard_html().await;
assert!(html_page.contains("Connected as"));
assert!(html_page.contains(&app.test_user.username));
let response = app.logout().await;
assert_is_redirect_to(&response, "/login");
let response = app.get_admin_dashboard().await;
assert_is_redirect_to(&response, "/login");
}
#[sqlx::test]
async fn subscribers_are_visible_on_the_dashboard(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.admin_login().await;
let response = app.get_admin_dashboard_html().await;
assert!(response.contains("No subscribers to display"));
app.create_confirmed_subscriber().await;
let subscriber = sqlx::query!("SELECT id, email FROM subscriptions")
.fetch_one(&app.connection_pool)
.await
.unwrap();
let response = app.get_admin_dashboard_html().await;
assert!(response.contains(&subscriber.email));
app.delete_subscriber(subscriber.id).await;
let response = app.get_admin_dashboard_html().await;
assert!(response.contains("No subscribers to display"));
assert!(!response.contains(&subscriber.email));
}
#[sqlx::test]
async fn posts_are_visible_on_the_dashboard(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.admin_login().await;
let response = app.get_admin_dashboard_html().await;
assert!(response.contains("No posts to display"));
let response = app.post_create_post(&fake_post_body()).await;
assert!(
response
.text()
.await
.unwrap()
.contains("Your new post has been published")
);
let (post_id, post_title) = {
let record = sqlx::query!("SELECT post_id, title FROM posts")
.fetch_one(&app.connection_pool)
.await
.unwrap();
(record.post_id, record.title)
};
let html = app.get_admin_dashboard_html().await;
assert!(html.contains(&post_title));
app.delete_post(post_id).await;
let response = app.get_admin_dashboard_html().await;
assert!(response.contains("No posts to display"));
}
#[sqlx::test]
async fn comments_are_visible_on_the_dashboard(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.admin_login().await;
let response = app.get_admin_dashboard_html().await;
assert!(response.contains("No comments to display"));
app.post_create_post(&fake_post_body()).await;
let (post_id, post_title) = {
let record = sqlx::query!("SELECT post_id, title FROM posts")
.fetch_one(&app.connection_pool)
.await
.unwrap();
(record.post_id, record.title)
};
let author = "author";
let content = "comment";
let comment_body = serde_json::json!({
"author": author,
"content": content,
"idempotency_key": "key"
});
app.post_comment(&post_id, &comment_body).await;
let response = app.get_admin_dashboard_html().await;
assert!(response.contains(author));
assert!(response.contains(content));
let html = app.get_admin_dashboard_html().await;
assert!(html.contains(&post_title));
let comment_id = {
let record = sqlx::query!("SELECT comment_id FROM comments")
.fetch_one(&app.connection_pool)
.await
.unwrap();
record.comment_id
};
app.delete_comment(comment_id).await;
let response = app.get_admin_dashboard_html().await;
assert!(response.contains("No comments to display"));
}
#[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);
}