From 8424292c39cb62afc7bf386af5f8bf1ced4e3e71 Mon Sep 17 00:00:00 2001 From: Matt Bernhardt Date: Mon, 12 Apr 2021 16:37:00 -0400 Subject: [PATCH] Implements transfer receipt email ** Why are these changes being introduced: * Staff who submit batches of thesis files to the Libraries as a bulk Transfer record should receive an email confirming their receipt. ** Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/ETD-79 ** How does this address that need: * This adds a transfer_receipt_email method to the ReceiptMailer class, with an associated view template and tests. ** Document any side effects to this change: * It may be a good idea to update the name of the thesis receipt method to something specific to theses. Right now the ReceiptMailer class has two methods: receipt_email (for theses) and transfer_receipt_email (for transfers). * The testing approach for this email is slightly different than what we use for the thesis email. This is intentional, and the result of some difficulty. Future-us may take another pass at it, perhaps. --- app/controllers/transfer_controller.rb | 1 + app/mailers/receipt_mailer.rb | 9 ++++++ .../transfer_receipt_email.html.erb | 23 +++++++++++++ test/integration/admin/admin_transfer_test.rb | 16 ++++++++++ test/integration/transfer_test.rb | 11 +++++++ test/mailers/receipt_mailer_test.rb | 32 ++++++++++++++++++- 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 app/views/receipt_mailer/transfer_receipt_email.html.erb diff --git a/app/controllers/transfer_controller.rb b/app/controllers/transfer_controller.rb index 102f456e..24b8547f 100644 --- a/app/controllers/transfer_controller.rb +++ b/app/controllers/transfer_controller.rb @@ -18,6 +18,7 @@ def create end if @transfer.save flash[:success] = "

Success!

#{@transfer.files.count} files have been transferred. You will receive an email confirmation with a list of the files you transferred.

" + ReceiptMailer.transfer_receipt_email(@transfer, current_user).deliver_later redirect_to transfer_confirm_path else flash[:error] = "Error saving transfer: #{@transfer.errors.full_messages}" diff --git a/app/mailers/receipt_mailer.rb b/app/mailers/receipt_mailer.rb index f4cb65b7..db5e9f10 100644 --- a/app/mailers/receipt_mailer.rb +++ b/app/mailers/receipt_mailer.rb @@ -7,4 +7,13 @@ def receipt_email(thesis, user) to: @user.email, subject: 'Your thesis information submission') end + + def transfer_receipt_email(transfer, user) + return unless ENV.fetch('DISABLE_ALL_EMAIL', 'true') == 'false' # allows PR builds to disable emails + @user = user + @transfer = transfer + mail(from: "MIT Libraries <#{ENV['THESIS_ADMIN_EMAIL']}>", + to: @user.email, + subject: 'Thesis files transferred') + end end diff --git a/app/views/receipt_mailer/transfer_receipt_email.html.erb b/app/views/receipt_mailer/transfer_receipt_email.html.erb new file mode 100644 index 00000000..ccd9d6bb --- /dev/null +++ b/app/views/receipt_mailer/transfer_receipt_email.html.erb @@ -0,0 +1,23 @@ +

Hello <%= @user.given_name %>,

+ +

Below is a record of the files you submitted on <%= @transfer.created_at.strftime('%b %-d, %Y at %l:%M %p') %>.

+ +

A couple reminders:

+ + + +

Files transferred:

+ +
    + <% @transfer.files.each do |file| %> +
  1. <%= file.filename %>
  2. + <% end %> +
+ +

Let us know if you have any questions.

+ +

Thanks,

+

The Thesis team at the MIT Libraries

diff --git a/test/integration/admin/admin_transfer_test.rb b/test/integration/admin/admin_transfer_test.rb index 937e16f9..f70741a4 100644 --- a/test/integration/admin/admin_transfer_test.rb +++ b/test/integration/admin/admin_transfer_test.rb @@ -60,4 +60,20 @@ def teardown get "/admin/transfers/#{transfers(:valid).id}" assert_response :success end + + test 'updating transfer through admin panel does not send emails' do + mock_auth(users(:thesis_admin)) + transfer = Transfer.first + ClimateControl.modify DISABLE_ALL_EMAIL: 'false' do + assert_emails 0 do + patch admin_transfer_path(transfer), + params: { + transfer: { + user_ids: [ User.first.id ], + note: 'I hope we do not use this in the fixture' + } + } + end + end + end end diff --git a/test/integration/transfer_test.rb b/test/integration/transfer_test.rb index 6fb36149..33be4b73 100644 --- a/test/integration/transfer_test.rb +++ b/test/integration/transfer_test.rb @@ -60,4 +60,15 @@ def teardown post transfer_index_path, params: { transfer: @transfer_params } assert_select 'span.error', text: Transfer::VALIDATION_MSGS[:generic] end + + test 'a confirmation email is sent when a transfer is created' do + mock_auth(users(:transfer_submitter)) + ClimateControl.modify DISABLE_ALL_EMAIL: 'false' do + assert_emails 1 do + post transfer_index_path, params: { + transfer: @transfer_params + } + end + end + end end diff --git a/test/mailers/receipt_mailer_test.rb b/test/mailers/receipt_mailer_test.rb index 2311b55e..04b38763 100644 --- a/test/mailers/receipt_mailer_test.rb +++ b/test/mailers/receipt_mailer_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class ReceiptMailerTest < ActionMailer::TestCase - test 'sends confirmation emails' do + test 'sends confirmation emails for thesis records' do ClimateControl.modify DISABLE_ALL_EMAIL: 'false' do thesis = theses(:one) user = users(:admin) @@ -20,6 +20,36 @@ class ReceiptMailerTest < ActionMailer::TestCase end end + test 'sends confirmation emails for transfer records' do + ClimateControl.modify DISABLE_ALL_EMAIL: 'false' do + transfer = transfers(:valid) + f = Rails.root.join('test','fixtures','files','a_pdf.pdf') + transfer.files.attach(io: File.open(f), filename: 'a_pdf.pdf') + user = users(:transfer_submitter) + email = ReceiptMailer.transfer_receipt_email(transfer, user) + + # Send the email, then test that it got queued + assert_emails 1 do + email.deliver_now + end + + # Test the body of the sent email contains what we expect it to + assert_equal ['test@example.com'], email.from + assert_equal ['transfer@example.com'], email.to + assert_equal 'Thesis files transferred', email.subject + # Please note: we are not attempting to assert_equal on the entire + # message because this email currently includes a reference to the + # transfer.created_at value, which puts us into dealing with timezones. + # I have lost more hours testing timezones than I care to calculate. + # Instead, we test for the presence of values we actually care about in + # the email body (filenames, and a greeting). + # Test that we are greeting the submitter by name + assert_match 'Hello Terry,', email.body.to_s + # Test that the filename we transferred appears in the body of the email + assert_match 'a_pdf.pdf', email.body.to_s + end + end + test 'does not send emails if DISABLE_ALL_EMAIL is not false' do ClimateControl.modify DISABLE_ALL_EMAIL: 'true' do thesis = theses(:one)