diff --git a/app/controllers/admin/auth_controller.rb b/app/controllers/admin/auth_controller.rb index ef611a0..f063302 100644 --- a/app/controllers/admin/auth_controller.rb +++ b/app/controllers/admin/auth_controller.rb @@ -31,7 +31,7 @@ module Admin redirect_to(admin_reset_request_path) and return if user.nil? user.setup_reset - UserMailer.password_reset(user).deliver_now + UserMailer.password_reset(user).deliver_later 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 850e053..eea95ab 100644 --- a/app/controllers/admin/user_controller.rb +++ b/app/controllers/admin/user_controller.rb @@ -14,7 +14,7 @@ module Admin @user = User.create({ password: default_passwd }.merge(user_params.to_h)) if @user.persisted? - UserMailer.welcome(@user, default_passwd).deliver_now + UserMailer.welcome(@user, default_passwd).deliver_later redirect_to admin_users_path, flash: { success: "Sucessfully created user #{@user.name}" } else flash[:error] = "Failed to save user." diff --git a/app/controllers/quiz_controller.rb b/app/controllers/quiz_controller.rb index 74c191e..4d2a8db 100644 --- a/app/controllers/quiz_controller.rb +++ b/app/controllers/quiz_controller.rb @@ -26,14 +26,23 @@ class QuizController < ApplicationController def submit_summary not_completed_error = 'You must complete all questions to submit your test.' - record_error = 'There was a problem with your submission. Please try again later.' redirect_to :summary, flash: { error: not_completed_error } and return unless prep_status.can_submit - redirect_to :thankyou and return if current_candidate.complete! - redirect_to :summary, flash: { error: record_error } + redirect_to :thankyou and return if complete_and_email + redirect_to :summary, flash: { error: 'Sorry, there was a problem. Please try again.' } end private + def complete_and_email + if current_candidate.update_attributes(completed: true) + CandidateMailer.submitted(current_candidate).deliver_later + RecruiterMailer.candidate_submitted(current_candidate).deliver_later + ReviewerMailer.candidate_submission(current_candidate).deliver_later + return true + end + false + end + def prep_question qid @question = current_candidate.fetch_question(qid) end diff --git a/app/controllers/recruiter_controller.rb b/app/controllers/recruiter_controller.rb index c157fc8..c736c29 100644 --- a/app/controllers/recruiter_controller.rb +++ b/app/controllers/recruiter_controller.rb @@ -15,8 +15,8 @@ class RecruiterController < ApplicationController @candidate = Candidate.create(candidate_params.merge(recruiter_id: current_recruiter.id)) if @candidate.persisted? - CandidateMailer.welcome(@candidate).deliver_now - RecruiterMailer.candidate_created(@candidate).deliver_now + CandidateMailer.welcome(@candidate).deliver_later + RecruiterMailer.candidate_created(@candidate).deliver_later redirect_to recruiter_path, flash: { success: "Sucessfully created candidate #{@candidate.name}" } else flash[:error] = "Failed to save candidate." diff --git a/app/models/candidate.rb b/app/models/candidate.rb index dea8c95..a216c37 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -40,19 +40,6 @@ class Candidate < ApplicationRecord "#{my_status.progress}%" end - # FIXME: This feels wrong here. Mail deliveries should be in controller. - # Privatize in QuizController - # also, bang methods in ruby do an action and replace, or return nil - def complete! - if update_attributes(completed: true) - CandidateMailer.submitted(self).deliver_now - RecruiterMailer.candidate_submitted(self).deliver_now - ReviewerMailer.candidate_submission(self).deliver_now - return true - end - false - end - private def generate_test_hash diff --git a/test/controllers/admin/auth_controller_test.rb b/test/controllers/admin/auth_controller_test.rb index 6ac928f..671f42a 100644 --- a/test/controllers/admin/auth_controller_test.rb +++ b/test/controllers/admin/auth_controller_test.rb @@ -3,6 +3,8 @@ require 'test_helper' module Admin class AuthControllerTest < ActionDispatch::IntegrationTest + include ActiveJob::TestHelper + test "should get login" do get admin_login_url assert_response :success @@ -45,7 +47,7 @@ module Admin test "should process a reset request" do user = users(:admin) - assert_difference("ActionMailer::Base.deliveries.size", 1) do + assert_enqueued_jobs 1 do post admin_send_reset_url, params: { auth: { email: user.email } } end refute_equal user.reset_token, User.find(user.id).reset_token diff --git a/test/controllers/admin/user_controller_test.rb b/test/controllers/admin/user_controller_test.rb index 4d034c6..c11ffbb 100644 --- a/test/controllers/admin/user_controller_test.rb +++ b/test/controllers/admin/user_controller_test.rb @@ -3,6 +3,8 @@ require 'test_helper' module Admin class UserControllerTest < ActionDispatch::IntegrationTest + include ActiveJob::TestHelper + def setup post admin_auth_url, params: { auth: { email: 'alan.admin@mailinator.com', password: 'password' } } @@ -29,7 +31,7 @@ module Admin end test "should post create" do - assert_difference("ActionMailer::Base.deliveries.size", 1) do + assert_enqueued_jobs 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' } } diff --git a/test/controllers/candidate_controller_test.rb b/test/controllers/candidate_controller_test.rb index f9128bc..14348c5 100644 --- a/test/controllers/candidate_controller_test.rb +++ b/test/controllers/candidate_controller_test.rb @@ -2,6 +2,8 @@ require 'test_helper' class CandidateControllerTest < ActionDispatch::IntegrationTest + include ActiveJob::TestHelper + def setup_auth candidate post validate_candidate_url, params: { test_id: candidate.test_hash } end @@ -74,7 +76,7 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest test "should NOT send mailers on submission" do setup_auth candidates(:dawn) - assert_difference("ActionMailer::Base.deliveries.size", 0) do + assert_enqueued_jobs 0 do post post_summary_path end assert_redirected_to summary_path @@ -84,7 +86,7 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest test "should send mailers on submission" do setup_auth candidates(:peggy) - assert_difference("ActionMailer::Base.deliveries.size", 3) do + assert_enqueued_jobs 3 do post post_summary_path end assert_redirected_to thankyou_path diff --git a/test/controllers/quiz_controller_test.rb b/test/controllers/quiz_controller_test.rb index ec11003..72d820b 100644 --- a/test/controllers/quiz_controller_test.rb +++ b/test/controllers/quiz_controller_test.rb @@ -17,11 +17,6 @@ class QuizControllerTest < ActionDispatch::IntegrationTest assert_redirected_to login_path end - # TODO: test models/candidate.complete! == false - # test 'should gracefully fail on summary submit' do - # # if the mailers fail, should go to thank you still - # end - test "should redirect to saved on save" do setup_auth candidates(:dawn) qid = questions(:fed5).id diff --git a/test/controllers/recruiter_controller_test.rb b/test/controllers/recruiter_controller_test.rb index 1f8828f..5c6a98c 100644 --- a/test/controllers/recruiter_controller_test.rb +++ b/test/controllers/recruiter_controller_test.rb @@ -2,6 +2,8 @@ require 'test_helper' class RecruiterControllerTest < ActionDispatch::IntegrationTest + include ActiveJob::TestHelper + def setup_auth post recruiter_auth_url, params: { auth: { email: 'pdr.recruiter@mailinator.com', password: 'password' } } @@ -68,7 +70,7 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest test "should create new candidate" do setup_auth - assert_difference("ActionMailer::Base.deliveries.size", 2) do + assert_enqueued_jobs 2 do assert_difference("Candidate.count") do post create_candidate_path, params: { candidate: { name: 'new name', email: 'test@mailinator.com', experience: '0-3', quiz_id: quizzes(:fed).id } } @@ -81,7 +83,7 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest test "should fail creation with improper email format" do setup_auth - assert_difference("ActionMailer::Base.deliveries.size", 0) do + assert_enqueued_jobs 0 do assert_difference("Candidate.count", 0) do post create_candidate_path, params: { candidate: { name: 'new name', email: 'test@mailinatorcom', experience: '0-3', quiz_id: quizzes(:fed).id } } @@ -95,7 +97,7 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest test "should fail creation gracefully with empty email" do setup_auth - assert_difference("ActionMailer::Base.deliveries.size", 0) do + assert_enqueued_jobs 0 do assert_difference("Candidate.count", 0) do post create_candidate_path, params: { candidate: { name: 'new name', email: "", experience: '0-3', quiz_id: quizzes(:fed).id } }