Skip to content

Commit

Permalink
formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcclung committed Mar 20, 2024
1 parent 40f1a81 commit 7419912
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/routes/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ use actix_web::HttpResponse;

pub async fn confirm() -> HttpResponse {
HttpResponse::Ok().finish()
}
}
5 changes: 2 additions & 3 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
mod confirm;
mod health_check;
mod subscriptions;
mod confirm;

pub use confirm::*;
pub use health_check::*;
pub use subscriptions::*;
pub use confirm::*;

24 changes: 12 additions & 12 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn subscribe<'a, T: EmailSender + Debug>(
info!("New subscriber details has been saved");

println!("sub_record.id {}", sub_record.id);

let subscription_token = Uuid::new_v4().to_string();
println!("new token {}", subscription_token);
let result = sqlx::query!(
Expand All @@ -76,15 +76,19 @@ pub async fn subscribe<'a, T: EmailSender + Debug>(
VALUES ($1, $2)
"#,
subscription_token,
sub_record.id
sub_record.id
)
.execute(pool.get_ref())
.instrument(tracing::info_span!("add subscription token query"))
.await;

match result {
Ok(_) => {
match send_confirmation_email(&sub_record.email, &subscription_token, email_service) {
match send_confirmation_email(
&sub_record.email,
&subscription_token,
email_service,
) {
Ok(_) => {
info!("Email sent");
HttpResponse::Ok().finish()
Expand All @@ -94,7 +98,7 @@ pub async fn subscribe<'a, T: EmailSender + Debug>(
HttpResponse::InternalServerError().finish()
}
}
},
}
Err(e) => {
error!("Failed to insert subscription token query: {:?}", e);
println!("{}", e);
Expand All @@ -112,13 +116,13 @@ pub async fn subscribe<'a, T: EmailSender + Debug>(
#[derive(Template)]
#[template(path = "confirmation/email.html")]
struct ConfirmationEmailHtmlTemplate<'a> {
token: &'a str
token: &'a str,
}

#[derive(Template)]
#[template(path = "confirmation/email.txt")]
struct ConfirmationEmailTxtTemplate<'a> {
token: &'a str
token: &'a str,
}

#[derive(Template)]
Expand All @@ -131,12 +135,8 @@ fn send_confirmation_email<T: EmailSender>(
email_service: web::Data<Mutex<EmailService<'_, T>>>,
) -> Result<()> {
println!("token string passed into send_confirmation_email {}", token);
let confirm_email_html = ConfirmationEmailHtmlTemplate {
token
};
let confirm_email_plaintext = ConfirmationEmailTxtTemplate {
token
};
let confirm_email_html = ConfirmationEmailHtmlTemplate { token };
let confirm_email_plaintext = ConfirmationEmailTxtTemplate { token };
let confirm_subject = ConfirmationEmailSubject {};

let email = Email {
Expand Down
13 changes: 7 additions & 6 deletions tests/api/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::test_app::spawn;

#[tokio::test]
async fn subscribe_returns_a_200_for_valid_form_data() {
let test_app = spawn().await.unwrap();
let test_app = spawn().await.unwrap();

let name: String = faker::name::en::FirstName().fake();
let email: String = faker::internet::en::SafeEmail().fake();
Expand All @@ -18,20 +18,21 @@ async fn subscribe_returns_a_200_for_valid_form_data() {

let subscriber_id = test_app.get_subscription(&name, &email).await;
let subscription_token = test_app.get_subscription_token(subscriber_id).await;

assert_eq!(test_app.get_emails_sent(), 1);

assert_eq!(
test_app.email_body_contains(
&format!("https://zero2prod.xyz/con=\r\nfirm?token=3D{}", subscription_token)
),
test_app.email_body_contains(&format!(
"https://zero2prod.xyz/con=\r\nfirm?token=3D{}",
subscription_token
)),
true
);
}

#[tokio::test]
async fn subscribe_returns_a_400_when_data_is_missing() {
let test_app = spawn().await.unwrap();
let test_app = spawn().await.unwrap();

struct TestCase<'a> {
name: &'a str,
Expand Down
32 changes: 19 additions & 13 deletions tests/api/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,33 @@ pub struct TestApp<'a> {
impl<'a> TestApp<'a> {
pub fn address(&self) -> &str {
&self.address
}
}

pub async fn get_subscription(&self, subscriber_name: &String, subscriber_email: &String) -> Uuid {
pub async fn get_subscription(
&self,
subscriber_name: &String,
subscriber_email: &String,
) -> Uuid {
let subscriber = sqlx::query!(
"SELECT id FROM subscriptions WHERE name = $1 AND email = $2",
subscriber_name,
"SELECT id FROM subscriptions WHERE name = $1 AND email = $2",
subscriber_name,
subscriber_email
)
.fetch_one(&self.pool)
.await
.expect("Failed to fetch saved subscription");
)
.fetch_one(&self.pool)
.await
.expect("Failed to fetch saved subscription");

subscriber.id
}

pub async fn get_subscription_token(&self, subscriber_id: Uuid) -> String {
let subscription_token = sqlx::query!(
"SELECT subscription_token FROM subscription_tokens WHERE subscriber_id = $1", subscriber_id)
.fetch_one(&self.pool)
.await
.expect("Failed to fetch subscription token");
"SELECT subscription_token FROM subscription_tokens WHERE subscriber_id = $1",
subscriber_id
)
.fetch_one(&self.pool)
.await
.expect("Failed to fetch subscription token");

subscription_token.subscription_token
}
Expand Down Expand Up @@ -70,7 +76,7 @@ impl<'a> TestApp<'a> {

if let Some(message) = sent_messages.get(0) {
let message_formatted = message.formatted();
let message_body = std::str::from_utf8(&message_formatted).unwrap();
let message_body = std::str::from_utf8(&message_formatted).unwrap();
message_body.contains(substr)
} else {
panic!("Message not sent");
Expand Down

0 comments on commit 7419912

Please sign in to comment.