Confirm subscription endpoint
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use linkify::LinkFinder;
|
||||
use once_cell::sync::Lazy;
|
||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||
use uuid::Uuid;
|
||||
use wiremock::MockServer;
|
||||
use zero2prod::{
|
||||
configuration::{DatabaseSettings, get_configuration},
|
||||
startup::Application,
|
||||
@@ -15,26 +17,67 @@ static TRACING: Lazy<()> = Lazy::new(|| {
|
||||
}
|
||||
});
|
||||
|
||||
pub struct ConfirmationLinks {
|
||||
pub html: reqwest::Url,
|
||||
pub text: reqwest::Url,
|
||||
}
|
||||
|
||||
pub struct TestApp {
|
||||
pub address: String,
|
||||
pub connection_pool: PgPool,
|
||||
pub email_server: wiremock::MockServer,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl TestApp {
|
||||
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: Vec<_> = LinkFinder::new()
|
||||
.links(s)
|
||||
.filter(|l| *l.kind() == linkify::LinkKind::Url)
|
||||
.collect();
|
||||
assert_eq!(links.len(), 1);
|
||||
let raw_link = links[0].as_str();
|
||||
let mut confirmation_link = reqwest::Url::parse(raw_link).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 spawn() -> Self {
|
||||
Lazy::force(&TRACING);
|
||||
|
||||
let mut configuration = get_configuration().expect("Failed to read configuration");
|
||||
configuration.database.database_name = Uuid::new_v4().to_string();
|
||||
configuration.application.port = 0;
|
||||
let email_server = MockServer::start().await;
|
||||
let configuration = {
|
||||
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
|
||||
};
|
||||
let connection_pool = configure_database(&configuration.database).await;
|
||||
let application = Application::build(configuration)
|
||||
.await
|
||||
.expect("Failed to build application");
|
||||
let address = application.address();
|
||||
let local_addr = application.local_addr();
|
||||
let port = local_addr
|
||||
.split(":")
|
||||
.last()
|
||||
.unwrap()
|
||||
.parse::<u16>()
|
||||
.unwrap();
|
||||
let address = format!("http://{}", application.local_addr());
|
||||
let app = TestApp {
|
||||
address,
|
||||
connection_pool,
|
||||
email_server,
|
||||
port,
|
||||
};
|
||||
|
||||
tokio::spawn(application.run_until_stopped());
|
||||
@@ -44,7 +87,7 @@ impl TestApp {
|
||||
|
||||
pub async fn post_subscriptions(&self, body: String) -> reqwest::Response {
|
||||
reqwest::Client::new()
|
||||
.post(format!("http://{}/subscriptions", self.address))
|
||||
.post(format!("{}/subscriptions", self.address))
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(body)
|
||||
.send()
|
||||
|
||||
Reference in New Issue
Block a user