Skip to content

Commit

Permalink
feat: add confirmation link in welcome email
Browse files Browse the repository at this point in the history
  • Loading branch information
migueloller committed Jan 21, 2024
1 parent 35b77c7 commit edc035b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ features = ["json", "rustls-tls"]
[dev-dependencies]
claims = "0.7"
fake = "~2.3"
linkify = "0.9"
once_cell = "1"
quickcheck = "0.9.2"
quickcheck_macros = "0.9.1"
Expand Down
12 changes: 10 additions & 2 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ pub async fn subscribe(
return HttpResponse::InternalServerError().finish();
}

let confirmation_link = "https://there-is-no-such-domain.com/subscriptions/confirm";
if email_client
.send_email(
new_subscriber.email,
"Welcome!",
"Welcome to our newsletter!",
"Welcome to our newsletter!",
&format!(
"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
.is_err()
Expand Down
36 changes: 36 additions & 0 deletions tests/api/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,39 @@ async fn subscribe_sends_a_confirmation_email_for_valid_data() {
// Assert
// Mock expectations are checked on drop.
}

#[tokio::test]
async fn subscribe_sends_a_confirmation_email_with_a_link() {
// Arrange
let app = spawn_app().await;
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";

Mock::given(path("/emails"))
.and(method("POST"))
.respond_with(ResponseTemplate::new(200))
.mount(&app.email_server)
.await;

// Act
app.post_subscriptions(body.into()).await;

// Assert
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["html"].as_str().unwrap());
let text_link = get_link(body["text"].as_str().unwrap());

// The two links should be identical.
assert_eq!(html_link, text_link);
}

0 comments on commit edc035b

Please sign in to comment.