From d5a469d736561f35c7686ce0ab6ada090a5be137 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Wed, 24 Aug 2016 16:26:07 -0500 Subject: [PATCH] user mailers completes #25 --- app/controllers/admin/auth_controller.rb | 2 +- app/controllers/admin/user_controller.rb | 2 +- app/mailers/user_mailer.rb | 12 +++++++++++ app/views/admin/auth/reset_request.html.erb | 3 --- .../user_mailer/password_reset.html.inky | 13 ++++++++++++ app/views/user_mailer/password_reset.text.erb | 7 +++++++ app/views/user_mailer/welcome.html.inky | 15 ++++++++++++++ app/views/user_mailer/welcome.text.erb | 8 ++++++++ .../controllers/admin/auth_controller_test.rb | 5 +++-- .../controllers/admin/user_controller_test.rb | 8 +++++--- test/mailers/previews/user_mailer_preview.rb | 12 +++++++++++ test/mailers/user_mailer_test.rb | 20 +++++++++++++++++++ 12 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 app/mailers/user_mailer.rb create mode 100644 app/views/user_mailer/password_reset.html.inky create mode 100644 app/views/user_mailer/password_reset.text.erb create mode 100644 app/views/user_mailer/welcome.html.inky create mode 100644 app/views/user_mailer/welcome.text.erb create mode 100644 test/mailers/previews/user_mailer_preview.rb create mode 100644 test/mailers/user_mailer_test.rb diff --git a/app/controllers/admin/auth_controller.rb b/app/controllers/admin/auth_controller.rb index fa19122..70a02a3 100644 --- a/app/controllers/admin/auth_controller.rb +++ b/app/controllers/admin/auth_controller.rb @@ -30,7 +30,7 @@ module Admin redirect_to(admin_reset_request_path) and return if user.nil? user.setup_reset - # TODO: user mailer deliver_now + UserMailer.password_reset(user).deliver_now redirect_to admin_reset_request_path, success: "Reset request sent! Please check your email for instructions." end diff --git a/app/controllers/admin/user_controller.rb b/app/controllers/admin/user_controller.rb index 0d3d38e..85b4e71 100644 --- a/app/controllers/admin/user_controller.rb +++ b/app/controllers/admin/user_controller.rb @@ -13,7 +13,7 @@ module Admin @user = User.create({ password: default_passwd }.merge(user_params.to_h)) if @user.persisted? - # TODO: UserMailer.welcome(@user, default_passwd).deliver_now + UserMailer.welcome(@user, default_passwd).deliver_now redirect_to admin_users_path, flash: { success: "Sucessfully created user #{@user.name}" } else flash[:error] = "Failed to save user." diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb new file mode 100644 index 0000000..3ea1484 --- /dev/null +++ b/app/mailers/user_mailer.rb @@ -0,0 +1,12 @@ +class UserMailer < ApplicationMailer + def password_reset user + @user = user + mail to: user.email, subject: 'Password Reset' + end + + def welcome user, default_password + @user = user + @password = default_password + mail to: user.email, subject: "Skill Assesment Acount" + end +end diff --git a/app/views/admin/auth/reset_request.html.erb b/app/views/admin/auth/reset_request.html.erb index 7e3975b..bb23fb8 100644 --- a/app/views/admin/auth/reset_request.html.erb +++ b/app/views/admin/auth/reset_request.html.erb @@ -3,9 +3,6 @@ %>

Password Reset

- -<%= raw(ap User.find_by(email: 'alan.admin@mailinator.com')) %> - <%= form_for :auth, url: admin_send_reset_path do |form| %>
diff --git a/app/views/user_mailer/password_reset.html.inky b/app/views/user_mailer/password_reset.html.inky new file mode 100644 index 0000000..38b913c --- /dev/null +++ b/app/views/user_mailer/password_reset.html.inky @@ -0,0 +1,13 @@ + + + diff --git a/app/views/user_mailer/password_reset.text.erb b/app/views/user_mailer/password_reset.text.erb new file mode 100644 index 0000000..5eacf4b --- /dev/null +++ b/app/views/user_mailer/password_reset.text.erb @@ -0,0 +1,7 @@ +Hey there <%= @user.name %>, + +It looks like you want to reset your password? +If not, it is safe to ignore this email. +Otherwise go to the following link to enter a new password: + +<%= admin_reset_url(reset_token: @user.reset_token) %> diff --git a/app/views/user_mailer/welcome.html.inky b/app/views/user_mailer/welcome.html.inky new file mode 100644 index 0000000..a9f7e31 --- /dev/null +++ b/app/views/user_mailer/welcome.html.inky @@ -0,0 +1,15 @@ + + + diff --git a/app/views/user_mailer/welcome.text.erb b/app/views/user_mailer/welcome.text.erb new file mode 100644 index 0000000..7743f45 --- /dev/null +++ b/app/views/user_mailer/welcome.text.erb @@ -0,0 +1,8 @@ +Hey there <%= @user.name %>, + +Looks like you now have access to the skills assessment app. +We've given you a temporary password below. Please sign in an personalize it asap. + +Password: <%= @password %> + +<%= admin_url %>. diff --git a/test/controllers/admin/auth_controller_test.rb b/test/controllers/admin/auth_controller_test.rb index d482c2c..d338bd5 100644 --- a/test/controllers/admin/auth_controller_test.rb +++ b/test/controllers/admin/auth_controller_test.rb @@ -44,8 +44,9 @@ module Admin test "should process a reset request" do user = users(:admin) - post admin_send_reset_url, params: { auth: { email: user.email } } - + assert_difference("ActionMailer::Base.deliveries.size", 1) do + post admin_send_reset_url, params: { auth: { email: user.email } } + end refute_equal user.reset_token, User.find(user.id).reset_token assert_redirected_to admin_reset_request_url assert_match(/request.*sent/i, flash[:success]) diff --git a/test/controllers/admin/user_controller_test.rb b/test/controllers/admin/user_controller_test.rb index 8416cc8..6f0a311 100644 --- a/test/controllers/admin/user_controller_test.rb +++ b/test/controllers/admin/user_controller_test.rb @@ -28,9 +28,11 @@ module Admin end test "should post create" do - assert_difference("User.count", 1) do - post admin_create_user_url, params: { user: - { email: 'new.user@mailinator.com', name: 'New User', role: 'reviewer' } } + assert_difference("ActionMailer::Base.deliveries.size", 1) do + assert_difference("User.count", 1) do + post admin_create_user_url, params: { user: + { email: 'new.user@mailinator.com', name: 'New User', role: 'reviewer' } } + end end assert_redirected_to admin_users_url end diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb new file mode 100644 index 0000000..b303156 --- /dev/null +++ b/test/mailers/previews/user_mailer_preview.rb @@ -0,0 +1,12 @@ +# Preview all emails at http://localhost:3000/rails/mailers/user_mailer +class UserMailerPreview < ActionMailer::Preview + def password_reset + user = User.find_by(email: 'alan.admin@mailinator.com') + UserMailer.password_reset user + end + + def welcome + user = User.find_by(email: 'alan.admin@mailinator.com') + UserMailer.welcome user, '[default-password]' + end +end diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb new file mode 100644 index 0000000..de58eb7 --- /dev/null +++ b/test/mailers/user_mailer_test.rb @@ -0,0 +1,20 @@ +require 'test_helper' + +class UserMailerTest < ActionMailer::TestCase + test "password_reset" do + user = users(:admin) + user.setup_reset + mail = UserMailer.password_reset user + + assert_equal [user.email], mail.to + assert_match user.reset_token, mail.body.encoded + end + + test "welcome" do + user = users(:admin) + mail = UserMailer.welcome user, 'p0o9i8u7' + + assert_equal [user.email], mail.to + assert_match 'p0o9i8u7', mail.body.encoded + end +end