tests for link in email & subscription status
This commit is contained in:
@ -1,11 +1,13 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use sqlx::{postgres::PgPoolOptions, Connection, Executor, PgConnection, Pool, Postgres};
|
||||
use uuid::Uuid;
|
||||
use wiremock::MockServer;
|
||||
use zero2prod::{configuration::{get_configuration, DatabaseSettings}, startup::{get_connection_pool, Application}, telemetry::{get_subscriber, init_subscriber}};
|
||||
|
||||
pub struct TestApp {
|
||||
pub address: String,
|
||||
pub connection_pool: Pool<Postgres>,
|
||||
pub email_server: MockServer,
|
||||
}
|
||||
|
||||
impl TestApp {
|
||||
@ -35,23 +37,27 @@ static TRACING: Lazy<()> = Lazy::new(|| {
|
||||
|
||||
pub async fn spawn_app() -> TestApp {
|
||||
Lazy::force(&TRACING);
|
||||
let email_server = MockServer::start().await;
|
||||
|
||||
let config = {
|
||||
let mut c = get_configuration().expect("Failed to read configuration");
|
||||
c.database.database_name = Uuid::new_v4().to_string();
|
||||
c.application.port = 0;
|
||||
c.email_client.base_url = email_server.uri();
|
||||
c
|
||||
};
|
||||
|
||||
configure_database(&config.database).await;
|
||||
|
||||
let app = Application::build(config.clone()).await.expect("Failed to build app.");
|
||||
println!("App built at port: {}", app.port());
|
||||
let address = format!("http://127.0.0.1:{}", app.port());
|
||||
let _fut = tokio::spawn(app.run_until_stopped());
|
||||
|
||||
TestApp {
|
||||
address,
|
||||
connection_pool: get_connection_pool(config.database),
|
||||
email_server,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,20 +1,40 @@
|
||||
use crate::helpers::spawn_app;
|
||||
use sqlx::query;
|
||||
use wiremock::{matchers::{path, method}, Mock, ResponseTemplate, http::Method};
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
let app = spawn_app().await;
|
||||
Mock::given(path("/email"))
|
||||
.and(method(Method::POST))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
let response = app.post_subscriptions("name=andre&email=andre.heber@gmx.net".to_string()).await;
|
||||
|
||||
assert_eq!(200, response.status().as_u16());
|
||||
}
|
||||
|
||||
let saved = query!("SELECT email, name FROM subscriptions",)
|
||||
#[tokio::test]
|
||||
async fn subscribe_persists_the_new_subscriber() {
|
||||
let app = spawn_app().await;
|
||||
Mock::given(path("/email"))
|
||||
.and(method(Method::POST))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
let _response = app.post_subscriptions("name=andre&email=andre.heber@gmx.net".to_string()).await;
|
||||
|
||||
let saved = query!("SELECT email, name, status FROM subscriptions",)
|
||||
.fetch_one(&app.connection_pool)
|
||||
.await
|
||||
.expect("Failed to fetch saved subscription.");
|
||||
|
||||
assert_eq!(saved.email, "andre.heber@gmx.net");
|
||||
assert_eq!(saved.name, "andre");
|
||||
assert_eq!(saved.status, "pending_confirmation");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@ -59,3 +79,55 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_sends_a_confirmation_email_for_valid_data() {
|
||||
let app = spawn_app().await;
|
||||
|
||||
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
||||
|
||||
Mock::given(path("/email"))
|
||||
.and(method(Method::POST))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
let _response = app.post_subscriptions(body.to_string()).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_sends_a_confirmation_email_with_a_link() {
|
||||
let app = spawn_app().await;
|
||||
|
||||
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
||||
|
||||
Mock::given(path("/email"))
|
||||
.and(method(Method::POST))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
let response = app.post_subscriptions(body.to_string()).await;
|
||||
|
||||
assert_eq!(200, response.status().as_u16());
|
||||
|
||||
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
||||
let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap();
|
||||
|
||||
let get_link = |s: &str| {
|
||||
let links: Vec<_> = linkify::LinkFinder::new()
|
||||
.links(s)
|
||||
.filter(|l| *l.kind() == linkify::LinkKind::Url)
|
||||
.collect();
|
||||
assert_eq!(links.len(), 1);
|
||||
links[0].as_str().to_owned()
|
||||
};
|
||||
|
||||
let html_link = get_link(body["HtmlBody"].as_str().unwrap());
|
||||
let text_link = get_link(body["TextBody"].as_str().unwrap());
|
||||
|
||||
// The two links should be identical
|
||||
assert_eq!(html_link, text_link);
|
||||
}
|
||||
Reference in New Issue
Block a user