Skip to content

Add support for testing emails #3483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{db, Config, Env};
use std::{sync::Arc, time::Duration};

use crate::downloads_counter::DownloadsCounter;
use crate::email::Emails;
use crate::github::GitHubClient;
use diesel::r2d2;
use oauth2::basic::BasicClient;
Expand Down Expand Up @@ -36,6 +37,9 @@ pub struct App {
/// Count downloads and periodically persist them in the database
pub downloads_counter: DownloadsCounter,

/// Backend used to send emails
pub emails: Emails,

/// A configured client for outgoing HTTP requests
///
/// In production this shares a single connection pool across requests. In tests
Expand Down Expand Up @@ -136,6 +140,7 @@ impl App {
session_key: config.session_key.clone(),
config,
downloads_counter: DownloadsCounter::new(),
emails: Emails::from_environment(),
http_client,
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/controllers/user/me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashMap;
use crate::controllers::frontend_prelude::*;

use crate::controllers::helpers::*;
use crate::email;

use crate::controllers::helpers::pagination::Paginated;
use crate::models::{
Expand Down Expand Up @@ -158,7 +157,14 @@ pub fn update_user(req: &mut dyn RequestExt) -> EndpointResult {
.get_result(&*conn)
.map_err(|_| server_error("Error in creating token"))?;

crate::email::send_user_confirm_email(user_email, &user.gh_login, &token);
// This swallows any errors that occur while attempting to send the email. Some users have
// an invalid email set in their GitHub profile, and we should let them sign in even though
// we're trying to silently use their invalid address during signup and can't send them an
// email. They'll then have to provide a valid email address.
let _ = req
.app()
.emails
.send_user_confirm(user_email, &user.gh_login, &token);

Ok(())
})?;
Expand Down Expand Up @@ -207,8 +213,9 @@ pub fn regenerate_token_and_send(req: &mut dyn RequestExt) -> EndpointResult {
.get_result(&*conn)
.map_err(|_| bad_request("Email could not be found"))?;

email::try_send_user_confirm_email(&email.email, &user.gh_login, &email.token)
.map_err(|_| server_error("Error in sending email"))
req.app()
.emails
.send_user_confirm(&email.email, &user.gh_login, &email.token)
})?;

ok_true()
Expand Down
14 changes: 11 additions & 3 deletions src/controllers/user/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use conduit_cookie::RequestSession;
use oauth2::reqwest::http_client;
use oauth2::{AuthorizationCode, Scope, TokenResponse};

use crate::email::Emails;
use crate::github::GithubUser;
use crate::models::{NewUser, User};
use crate::schema::users;
Expand Down Expand Up @@ -102,7 +103,12 @@ pub fn authorize(req: &mut dyn RequestExt) -> EndpointResult {

// Fetch the user info from GitHub using the access token we just got and create a user record
let ghuser = req.app().github.current_user(token)?;
let user = save_user_to_database(&ghuser, &token.secret(), &*req.db_conn()?)?;
let user = save_user_to_database(
&ghuser,
&token.secret(),
&req.app().emails,
&*req.db_conn()?,
)?;

// Log in by setting a cookie and the middleware authentication
req.session_mut()
Expand All @@ -114,6 +120,7 @@ pub fn authorize(req: &mut dyn RequestExt) -> EndpointResult {
fn save_user_to_database(
user: &GithubUser,
access_token: &str,
emails: &Emails,
conn: &PgConnection,
) -> AppResult<User> {
NewUser::new(
Expand All @@ -123,7 +130,7 @@ fn save_user_to_database(
user.avatar_url.as_deref(),
access_token,
)
.create_or_update(user.email.as_deref(), conn)
.create_or_update(user.email.as_deref(), emails, conn)
.map_err(Into::into)
.or_else(|e: Box<dyn AppError>| {
// If we're in read only mode, we can't update their details
Expand Down Expand Up @@ -158,6 +165,7 @@ mod tests {

#[test]
fn gh_user_with_invalid_email_doesnt_fail() {
let emails = Emails::new_in_memory();
let conn = pg_connection();
let gh_user = GithubUser {
email: Some("String.Format(\"{0}.{1}@live.com\", FirstName, LastName)".into()),
Expand All @@ -166,7 +174,7 @@ mod tests {
id: -1,
avatar_url: None,
};
let result = save_user_to_database(&gh_user, "arbitrary_token", &conn);
let result = save_user_to_database(&gh_user, "arbitrary_token", &emails, &conn);

assert!(
result.is_ok(),
Expand Down
3 changes: 2 additions & 1 deletion src/downloads_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl PersistStats {
#[cfg(test)]
mod tests {
use super::*;
use crate::email::Emails;
use crate::models::{Crate, NewCrate, NewUser, NewVersion, User};
use diesel::PgConnection;
use semver::Version;
Expand Down Expand Up @@ -423,7 +424,7 @@ mod tests {
gh_login: "ghost",
..NewUser::default()
}
.create_or_update(None, conn)
.create_or_update(None, &Emails::new_in_memory(), conn)
.expect("failed to create user");

let krate = NewCrate {
Expand Down
Loading