mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2025-01-31 19:12:28 +00:00
Confirmation link (static).
This commit is contained in:
parent
2ac9ccbe97
commit
bdddc29ac1
2 changed files with 24 additions and 10 deletions
|
@ -43,12 +43,19 @@ pub async fn subscribe(
|
||||||
.await
|
.await
|
||||||
.map_err(|_| HttpResponse::InternalServerError().finish())?;
|
.map_err(|_| HttpResponse::InternalServerError().finish())?;
|
||||||
// We are swallowing the error for the time being.
|
// We are swallowing the error for the time being.
|
||||||
|
let confirmation_link = "https://my-api.com/subscriptions/confirm";
|
||||||
let _ = email_client
|
let _ = email_client
|
||||||
.send_email(
|
.send_email(
|
||||||
new_subscriber.email,
|
new_subscriber.email,
|
||||||
"Welcome!",
|
"Welcome!",
|
||||||
"Welcome to our newsletter!",
|
&format!(
|
||||||
"Welcome to our newsletter!",
|
"Welcome to our newsletter!<br />Click <a href=\"{}\">here</a> to confirm your subscription.",
|
||||||
|
confirmation_link
|
||||||
|
),
|
||||||
|
&format!(
|
||||||
|
"Welcome to our newsletter!\nVisit {} to confirm your subscription.",
|
||||||
|
confirmation_link
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
Ok(HttpResponse::Ok().finish())
|
Ok(HttpResponse::Ok().finish())
|
||||||
|
|
|
@ -60,15 +60,22 @@ async fn subscribe_sends_a_confirmation_email_with_a_link() {
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
||||||
let body = std::str::from_utf8(&email_request.body).unwrap();
|
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()
|
let links: Vec<_> = linkify::LinkFinder::new()
|
||||||
.links(body)
|
.links(s)
|
||||||
.filter(|l| *l.kind() == linkify::LinkKind::Url)
|
.filter(|l| *l.kind() == linkify::LinkKind::Url)
|
||||||
.collect();
|
.collect();
|
||||||
// One in the HTML version, one in the plain text version
|
assert_eq!(links.len(), 1);
|
||||||
assert_eq!(links.len(), 2);
|
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
|
// The two links should be identical
|
||||||
assert_eq!(links[0].as_str(), links[1].as_str());
|
assert_eq!(html_link, text_link);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
|
Loading…
Reference in a new issue