user mailers

completes #25
This commit is contained in:
Mark Moser 2016-08-24 16:26:07 -05:00
parent 7b51d26295
commit d5a469d736
12 changed files with 97 additions and 10 deletions

View File

@ -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

View File

@ -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."

View File

@ -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

View File

@ -3,9 +3,6 @@
%>
<h1>Password Reset</h1>
<%= raw(ap User.find_by(email: 'alan.admin@mailinator.com')) %>
<%= form_for :auth, url: admin_send_reset_path do |form| %>
<div class="form-group">

View File

@ -0,0 +1,13 @@
<row>
<columns class="email-body">
<p>Hey there <%= @user.name %>,</p>
<p>
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.
</p>
<p>
<%= link_to nil, admin_reset_url(reset_token: @user.reset_token) %>.
</p>
</columns>
</row>

View File

@ -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) %>

View File

@ -0,0 +1,15 @@
<row>
<columns class="email-body">
<p>Hey there <%= @user.name %>,</p>
<p>
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.
</p>
<p>
Password: <%= @password%>
</p>
<p>
<%= link_to nil, admin_url %>.
</p>
</columns>
</row>

View File

@ -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 %>.

View File

@ -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])

View File

@ -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

View File

@ -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

View File

@ -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