More tests, not found page and dashboard fixes
Some checks failed
Rust / Test (push) Has been cancelled
Rust / Rustfmt (push) Has been cancelled
Rust / Clippy (push) Has been cancelled
Rust / Code coverage (push) Has been cancelled

When post was deleted, website shows a 404 page insead of an 500 page.
Also made the dashboard empty page message more explicit.
This commit is contained in:
Alphonse Paix
2025-09-26 20:31:30 +02:00
parent 0f6b479af9
commit f9ae3f42a6
10 changed files with 233 additions and 48 deletions

View File

@@ -25,9 +25,32 @@ async fn logout_clears_session_state(connection_pool: PgPool) {
assert!(html_page.contains("Connected as"));
assert!(html_page.contains(&app.test_user.username));
let response = app.post_logout().await;
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));
}

View File

@@ -22,11 +22,7 @@ async fn you_must_be_logged_in_to_change_your_password(connection_pool: PgPool)
async fn new_password_fields_must_match(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.post_login(&serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password,
}))
.await;
app.admin_login().await;
let new_password = Uuid::new_v4().to_string();
let another_new_password = Uuid::new_v4().to_string();
@@ -47,11 +43,7 @@ async fn new_password_fields_must_match(connection_pool: PgPool) {
async fn current_password_is_invalid(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
app.post_login(&serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password,
}))
.await;
app.admin_login().await;
let new_password = Uuid::new_v4().to_string();
let response = app
@@ -70,13 +62,7 @@ async fn current_password_is_invalid(connection_pool: PgPool) {
#[sqlx::test]
async fn changing_password_works(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");
app.admin_login().await;
let new_password = Uuid::new_v4().to_string();
let response = app
@@ -91,7 +77,7 @@ async fn changing_password_works(connection_pool: PgPool) {
let html_page_fragment = response.text().await.unwrap();
assert!(html_page_fragment.contains("Your password has been changed"));
let response = app.post_logout().await;
let response = app.logout().await;
assert_is_redirect_to(&response, "/login");
let login_body = &serde_json::json!({

View File

@@ -160,6 +160,17 @@ impl TestApp {
.unwrap();
}
pub async fn delete_subscriber(&self, subscriber_id: Uuid) {
self.api_client
.delete(format!(
"{}/admin/subscribers/{}",
self.address, subscriber_id
))
.send()
.await
.expect("Could not delete subscriber");
}
pub async fn dispatch_all_pending_emails(&self) {
loop {
if let ExecutionOutcome::EmptyQueue =
@@ -228,6 +239,30 @@ impl TestApp {
self.get_admin_dashboard().await.text().await.unwrap()
}
pub async fn get_posts(&self) -> reqwest::Response {
self.api_client
.get(format!("{}/posts", &self.address))
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_posts_html(&self) -> String {
self.get_posts().await.text().await.unwrap()
}
pub async fn get_post(&self, post_id: Uuid) -> reqwest::Response {
self.api_client
.get(format!("{}/posts/{}", &self.address, post_id))
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_post_html(&self, post_id: Uuid) -> String {
self.get_post(post_id).await.text().await.unwrap()
}
pub async fn post_subscriptions(&self, body: String) -> reqwest::Response {
self.api_client
.post(format!("{}/subscriptions", self.address))
@@ -270,9 +305,9 @@ impl TestApp {
self.post_login(&login_body).await;
}
pub async fn post_logout(&self) -> reqwest::Response {
pub async fn logout(&self) -> reqwest::Response {
self.api_client
.post(format!("{}/admin/logout", self.address))
.get(format!("{}/admin/logout", self.address))
.send()
.await
.expect("Failed to execute request")
@@ -302,6 +337,14 @@ impl TestApp {
.expect("Failed to execute request")
}
pub async fn delete_post(&self, post_id: Uuid) {
self.api_client
.delete(format!("{}/admin/posts/{}", self.address, post_id))
.send()
.await
.expect("Could not delete post");
}
pub async fn post_unsubscribe<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,

View File

@@ -82,3 +82,86 @@ async fn confirmed_subscribers_are_notified_when_a_new_post_is_published(connect
app.dispatch_all_pending_emails().await;
}
#[sqlx::test]
async fn new_posts_are_visible_on_the_website(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
let html = app.get_posts_html().await;
assert!(html.contains("No posts yet"));
let title = subject();
let content = content();
let body = serde_json::json!({
"title": title,
"content": content,
"idempotency_key": Uuid::new_v4(),
});
app.admin_login().await;
app.post_create_post(&body).await;
app.logout().await;
let html = app.get_posts_html().await;
assert!(html.contains(&title));
let post = sqlx::query!("SELECT post_id FROM posts")
.fetch_one(&app.connection_pool)
.await
.unwrap();
let html = app.get_post_html(post.post_id).await;
assert!(html.contains(&title));
}
#[sqlx::test]
async fn visitor_can_read_a_blog_post(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
let title = subject();
let content = content();
let body = serde_json::json!({
"title": title,
"content": content,
"idempotency_key": Uuid::new_v4(),
});
app.admin_login().await;
app.post_create_post(&body).await;
app.logout().await;
let html = app.get_posts_html().await;
assert!(html.contains(&title));
let post = sqlx::query!("SELECT post_id FROM posts")
.fetch_one(&app.connection_pool)
.await
.unwrap();
let html = app.get_post_html(post.post_id).await;
assert!(html.contains(&title));
}
#[sqlx::test]
async fn a_deleted_blog_post_returns_404(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).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;
let post = sqlx::query!("SELECT post_id FROM posts")
.fetch_one(&app.connection_pool)
.await
.unwrap();
app.delete_post(post.post_id).await;
let html = app.get_post_html(post.post_id).await;
assert!(html.contains("Not Found"));
}