124 lines
4.1 KiB
Rust
124 lines
4.1 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, "/admin/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 data available"));
|
|
|
|
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 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);
|
|
}
|