Files
zero2prod/tests/api/helpers.rs
2025-10-07 23:07:16 +02:00

507 lines
15 KiB
Rust

use argon2::{
Algorithm, Argon2, Params, PasswordHasher, Version,
password_hash::{SaltString, rand_core::OsRng},
};
use fake::{
Fake,
faker::{
internet::en::SafeEmail,
lorem::en::{Paragraph, Sentence},
},
};
use linkify::{Link, LinkFinder};
use once_cell::sync::Lazy;
use sqlx::PgPool;
use uuid::Uuid;
use wiremock::{
Mock, MockBuilder, MockServer, ResponseTemplate,
matchers::{method, path},
};
use zero2prod::{
configuration::get_configuration,
email_client::EmailClient,
issue_delivery_worker::{ExecutionOutcome, try_execute_task},
startup::Application,
telemetry::init_subscriber,
};
static TRACING: Lazy<()> = Lazy::new(|| {
if std::env::var("TEST_LOG").is_ok() {
init_subscriber(std::io::stdout);
} else {
init_subscriber(std::io::sink);
}
});
pub struct ConfirmationLinks {
pub html: reqwest::Url,
pub text: reqwest::Url,
}
pub struct TestUser {
pub user_id: Uuid,
pub username: String,
pub password: String,
}
impl TestUser {
pub fn generate() -> Self {
Self {
user_id: Uuid::new_v4(),
username: Uuid::new_v4().to_string(),
password: Uuid::new_v4().to_string(),
}
}
pub async fn store(&self, connection_pool: &PgPool) {
let salt = SaltString::generate(&mut OsRng);
let password_hash = Argon2::new(
Algorithm::Argon2id,
Version::V0x13,
Params::new(1500, 2, 1, None).unwrap(),
)
.hash_password(self.password.as_bytes(), &salt)
.unwrap()
.to_string();
sqlx::query!(
"INSERT INTO users (user_id, username, password_hash, role) VALUES ($1, $2, $3, 'admin')",
self.user_id,
self.username,
password_hash
)
.execute(connection_pool)
.await
.expect("Failed to create test user");
}
}
pub struct TestApp {
pub address: String,
pub connection_pool: PgPool,
pub email_server: wiremock::MockServer,
pub port: u16,
pub test_user: TestUser,
pub api_client: reqwest::Client,
pub email_client: EmailClient,
}
impl TestApp {
pub async fn spawn(connection_pool: PgPool) -> Self {
Lazy::force(&TRACING);
let email_server = MockServer::start().await;
let configuration = {
let mut c = get_configuration().expect("Failed to read configuration");
c.application.port = 0;
c.email_client.base_url = email_server.uri();
c.database.database_name = connection_pool
.connect_options()
.get_database()
.unwrap()
.to_string();
c
};
let local_addr = configuration.application.host.clone();
let email_client = EmailClient::build(configuration.email_client.clone()).unwrap();
let application = Application::build(configuration)
.await
.expect("Failed to build application");
let port = application.port();
let address = format!("http://{}:{}", local_addr, port);
let test_user = TestUser::generate();
test_user.store(&connection_pool).await;
let api_client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.cookie_store(true)
.build()
.unwrap();
let app = TestApp {
address,
connection_pool,
email_server,
port,
test_user,
api_client,
email_client,
};
tokio::spawn(application.run_until_stopped());
app
}
pub async fn create_user(
&self,
username: &str,
password: &str,
admin: bool,
) -> reqwest::Response {
let body = serde_json::json!({
"username": username,
"password": password,
"password_check": password,
"admin": admin,
});
self.api_client
.post(format!("{}/admin/users", self.address))
.form(&body)
.send()
.await
.unwrap()
}
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 delete_subscriber(&self, subscriber_id: Uuid) -> reqwest::Response {
self.api_client
.delete(format!(
"{}/admin/subscribers/{}",
self.address, subscriber_id
))
.send()
.await
.expect("Could not delete subscriber")
}
pub async fn delete_user(&self, user_id: Uuid) -> reqwest::Response {
self.api_client
.delete(format!("{}/admin/users/{}", self.address, user_id))
.send()
.await
.expect("Could not delete user")
}
pub async fn dispatch_all_pending_emails(&self) {
loop {
if let ExecutionOutcome::EmptyQueue =
try_execute_task(&self.connection_pool, &self.email_client)
.await
.unwrap()
{
break;
}
}
}
pub fn get_post_urls(&self, request: &wiremock::Request) -> ConfirmationLinks {
let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap();
let get_link = |s: &str| {
let links = get_links(s);
assert!(!links.is_empty());
let mut confirmation_link = reqwest::Url::parse(links[0].as_str()).unwrap();
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
confirmation_link.set_port(Some(self.port)).unwrap();
confirmation_link
};
let html = get_link(body["html"].as_str().unwrap());
let text = get_link(body["text"].as_str().unwrap());
ConfirmationLinks { html, text }
}
pub fn get_unsubscribe_links(&self, request: &wiremock::Request) -> ConfirmationLinks {
let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap();
let get_link = |s: &str| {
let links = get_links(s);
assert!(!links.is_empty());
let mut confirmation_link =
reqwest::Url::parse(links.last().unwrap().as_str()).unwrap();
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
confirmation_link.set_port(Some(self.port)).unwrap();
confirmation_link
};
let html = get_link(body["html"].as_str().unwrap());
let text = get_link(body["text"].as_str().unwrap());
ConfirmationLinks { html, text }
}
pub fn get_confirmation_links(&self, request: &wiremock::Request) -> ConfirmationLinks {
let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap();
let get_link = |s: &str| {
let links = get_links(s);
assert_eq!(links.len(), 1);
let mut confirmation_link = reqwest::Url::parse(links[0].as_str()).unwrap();
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
confirmation_link.set_port(Some(self.port)).unwrap();
confirmation_link
};
let html = get_link(body["html"].as_str().unwrap());
let text = get_link(body["text"].as_str().unwrap());
ConfirmationLinks { html, text }
}
pub async fn get_unsubscribe_confirm(&self, unsubscribe_token: &str) -> reqwest::Response {
self.api_client
.get(format!(
"{}/unsubscribe/confirm?token={}",
&self.address, unsubscribe_token
))
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_admin_dashboard(&self) -> reqwest::Response {
self.api_client
.get(format!("{}/dashboard", &self.address))
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_admin_dashboard_html(&self) -> String {
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))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn post_newsletters<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/newsletters", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn post_login<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/login", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn admin_login(&self) {
let login_body = serde_json::json!({
"username": self.test_user.username,
"password": self.test_user.password
});
self.post_login(&login_body).await;
}
pub async fn logout(&self) -> reqwest::Response {
self.api_client
.get(format!("{}/logout", self.address))
.send()
.await
.expect("Failed to execute request")
}
pub async fn post_change_password<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/password", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn edit_profile<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.put(format!("{}/users/edit", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_profile(&self, username: &str) -> reqwest::Response {
self.api_client
.get(format!("{}/users/{}", self.address, username))
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_profile_html(&self, username: &str) -> String {
self.get_profile(username).await.text().await.unwrap()
}
pub async fn post_create_post<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/posts", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn post_comment<Body>(&self, post_id: &Uuid, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/posts/{post_id}/comments", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn delete_post(&self, post_id: Uuid) -> reqwest::Response {
self.api_client
.delete(format!("{}/admin/posts/{}", self.address, post_id))
.send()
.await
.expect("Could not delete post")
}
pub async fn delete_comment(&self, comment_id: Uuid) -> reqwest::Response {
self.api_client
.delete(format!("{}/admin/comments/{}", self.address, comment_id))
.send()
.await
.expect("Could not delete comment")
}
pub async fn post_unsubscribe<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/unsubscribe", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_user_id(&self, username: &str) -> Uuid {
let record = sqlx::query!("SELECT user_id FROM users WHERE username = $1", username)
.fetch_one(&self.connection_pool)
.await
.unwrap();
record.user_id
}
}
pub fn assert_is_redirect_to(response: &reqwest::Response, location: &str) {
assert!(
response.status().as_u16() == 303
|| response.status().as_u16() == 200
&& response.headers().get("hx-redirect").unwrap() == location
);
}
pub fn when_sending_an_email() -> MockBuilder {
Mock::given(path("/email")).and(method("POST"))
}
pub fn fake_newsletter_body() -> serde_json::Value {
serde_json::json!({
"title": "Newsletter title",
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>",
"idempotency_key": Uuid::new_v4().to_string(),
})
}
pub fn fake_post_body() -> serde_json::Value {
serde_json::json!({
"title": "Post title",
"content": "Post content",
"idempotency_key": Uuid::new_v4().to_string()
})
}
pub fn get_links(s: &'_ str) -> Vec<Link<'_>> {
LinkFinder::new()
.links(s)
.filter(|l| *l.kind() == linkify::LinkKind::Url)
.collect()
}
pub fn subject() -> String {
Sentence(1..2).fake()
}
pub fn content() -> String {
Paragraph(1..10).fake()
}