test: move logic to helpers
Some checks failed
Rust / Test (push) Has been cancelled
Rust / Rustfmt (push) Has been cancelled
Rust / Clippy (push) Has been cancelled
Rust / Code coverage (push) Has been cancelled

This commit is contained in:
Sandro Eiler 2024-04-18 23:48:34 +02:00
parent efd1137c71
commit 6e07d9c33a
3 changed files with 38 additions and 31 deletions

View file

@ -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())