diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index a7c598e..6d230b8 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -23,6 +23,12 @@ static TRACING: Lazy<()> = Lazy::new(|| { }; }); +/// Confirmation links embedded in the request to the email API. +pub struct ConfirmationLinks { + pub html: reqwest::Url, + pub plain_text: reqwest::Url, +} + pub struct TestApp { pub address: String, pub port: u16, @@ -47,6 +53,28 @@ impl TestApp { .await .expect("Failed to execute request.") } + pub fn get_confirmation_links(&self, email_request: &wiremock::Request) -> ConfirmationLinks { + let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap(); + + // Extract the link from one of the request fields. + 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); + let raw_link = links[0].as_str().to_owned(); + let mut confirmation_link = reqwest::Url::parse(&raw_link).unwrap(); + // Let's make sure we don't call random APIs on the web + 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["HtmlBody"].as_str().unwrap()); + let plain_text = get_link(&body["TextBody"].as_str().unwrap()); + ConfirmationLinks { html, plain_text } + } } pub async fn spawn_app() -> TestApp { @@ -93,6 +121,12 @@ pub async fn spawn_app() -> TestApp { } } +/// Create a new database and run the migrations. +/// +/// # Parameters +/// * `config`: The database configuration. +/// # Returns +/// The connection pool. async fn configure_database(config: &DatabaseSettings) -> PgPool { // Create database let mut connection = PgConnection::connect_with(&config.without_db()) diff --git a/tests/api/subscriptions.rs b/tests/api/subscriptions.rs index 7643d7c..2321645 100644 --- a/tests/api/subscriptions.rs +++ b/tests/api/subscriptions.rs @@ -18,23 +18,10 @@ async fn subscribe_sends_a_confirmation_email_with_a_link() { app.post_subscriptions(body.into()).await; // Assert - // Get the first intercepted request let email_request = &app.email_server.received_requests().await.unwrap()[0]; - // Parse the body as JSON, starting from raw bytes - let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap(); - // Extract the link from one of the request fields. - 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()); + let confirmation_links = app.get_confirmation_links(email_request); // The two links should be identical - assert_eq!(html_link, text_link); + assert_eq!(confirmation_links.html, confirmation_links.plain_text); } #[tokio::test] diff --git a/tests/api/subscriptions_confirm.rs b/tests/api/subscriptions_confirm.rs index 3ed4cfd..e339bf9 100644 --- a/tests/api/subscriptions_confirm.rs +++ b/tests/api/subscriptions_confirm.rs @@ -17,24 +17,10 @@ async fn the_link_returned_by_subscribe_returns_a_200_if_called() { app.post_subscriptions(body.into()).await; let email_request = &app.email_server.received_requests().await.unwrap()[0]; - let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap(); - // Extract the link from one of the request fields. - 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 raw_confirmation_link = &get_link(body["HtmlBody"].as_str().unwrap()); - let mut confirmation_link = Url::parse(raw_confirmation_link).unwrap(); - - assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1"); - confirmation_link.set_port(Some(app.port)).unwrap(); + let confirmation_links = app.get_confirmation_links(email_request); // Act - let response = reqwest::get(confirmation_link).await.unwrap(); + let response = reqwest::get(confirmation_links.html).await.unwrap(); // Assert assert_eq!(response.status().as_u16(), 200);