Admin can now write posts

Posts can be displayed on the website. Subscribers are automatically
notified by email. This gives the opportunity to track explicitly how
many people followed the link provided in the emails sent without being
intrusive (no invisible image).
This commit is contained in:
Alphonse Paix
2025-09-18 17:22:33 +02:00
parent 848fd621b7
commit 066c2b8252
11 changed files with 284 additions and 69 deletions

View File

@@ -2,12 +2,13 @@ use argon2::{
Algorithm, Argon2, Params, PasswordHasher, Version,
password_hash::{SaltString, rand_core::OsRng},
};
use fake::{Fake, faker::internet::en::SafeEmail};
use linkify::LinkFinder;
use once_cell::sync::Lazy;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use uuid::Uuid;
use wiremock::{
Mock, MockBuilder, MockServer,
Mock, MockBuilder, MockServer, ResponseTemplate,
matchers::{method, path},
};
use zero2prod::{
@@ -120,6 +121,42 @@ impl TestApp {
app
}
pub async fn create_unconfirmed_subscriber(&self) -> ConfirmationLinks {
let email: String = SafeEmail().fake();
let body = format!("email={email}");
let _mock_guard = when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.named("Create unconfirmed subscriber")
.expect(1)
.mount_as_scoped(&self.email_server)
.await;
self.post_subscriptions(body)
.await
.error_for_status()
.unwrap();
let email_request = &self
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
self.get_confirmation_links(email_request)
}
pub async fn create_confirmed_subscriber(&self) {
let confirmation_links = self.create_unconfirmed_subscriber().await;
reqwest::get(confirmation_links.html)
.await
.unwrap()
.error_for_status()
.unwrap();
}
pub async fn dispatch_all_pending_emails(&self) {
loop {
if let ExecutionOutcome::EmptyQueue =
@@ -195,7 +232,7 @@ impl TestApp {
.form(body)
.send()
.await
.expect("failed to execute request")
.expect("Failed to execute request")
}
pub async fn admin_login(&self) {
@@ -211,7 +248,7 @@ impl TestApp {
.post(format!("{}/admin/logout", self.address))
.send()
.await
.expect("failed to execute request")
.expect("Failed to execute request")
}
pub async fn post_change_password<Body>(&self, body: &Body) -> reqwest::Response
@@ -223,7 +260,19 @@ impl TestApp {
.form(body)
.send()
.await
.expect("failed to execute request")
.expect("Failed to execute request")
}
pub async fn post_create_post<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/admin/posts", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
}