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] = "
#{@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:
+ +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)