Skip to content

Commit

Permalink
add job run mailer and start hooking up mailer to jobrun model
Browse files Browse the repository at this point in the history
  • Loading branch information
peetucket committed Sep 18, 2018
1 parent e2f868e commit b46a622
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
default from: 'no-reply-preassembly@.stanford.edu'
layout 'mailer'
end
10 changes: 10 additions & 0 deletions app/mailers/job_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class JobMailer < ApplicationMailer
default from: 'no-reply-preassembly-job@stanford.edu'

def completion_email
@job = params[:job_run]
@user = @job.bundle_context.user
mail(to: @user.email, subject: 'Your pre-assembly job has completed')
end

end
7 changes: 7 additions & 0 deletions app/models/job_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class JobRun < ApplicationRecord
belongs_to :bundle_context
validates :job_type, presence: true
after_create :enqueue!
after_update :send_notification, if: -> { saved_change_to_output_location? }

enum job_type: {
"discovery_report" => 0,
Expand All @@ -14,4 +15,10 @@ def enqueue!
return nil unless persisted?
"#{job_type.camelize}Job".constantize.perform_later(self)
end

def send_notification
return unless output_location
JobMailer.with(job_run: self).completion_email.deliver_later
end

end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ class User < ApplicationRecord
devise :remote_user_authenticatable # We don't want other (default) Devise modules
has_many :bundle_contexts
validates :sunet_id, presence: true, uniqueness: true, null: false

def email
"#{sunet_id}@stanford.edu"
end

end
1 change: 1 addition & 0 deletions app/views/job_mailer/completion_email.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%=@user.sunet_id%>, your <%=@job.job_type%> job #<%=@job.id%> completed. View the job details here: "=url_for(@job) will go here".
37 changes: 37 additions & 0 deletions spec/mailers/job_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "rails_helper"

RSpec.describe JobMailer, type: :mailer do
let(:user) do
User.new(sunet_id: "Jdoe")
end

let(:bc) do
BundleContext.new(id: 1,
project_name: "SmokeTest",
content_structure: 1,
bundle_dir: "spec/test_data/images_jp2_tif",
staging_style_symlink: false,
content_metadata_creation: 1,
user: user)
end

let(:discovery_report_run) do
JobRun.new(id: 1,
output_location: "/path/to/report",
bundle_context: bc,
job_type: "discovery_report")
end

let(:job_notification) { JobMailer.with(job_run: discovery_report_run).completion_email }

it 'renders the headers' do
expect(job_notification.subject).to eq("Your pre-assembly job has completed")
expect(job_notification.to).to eq(["Jdoe@stanford.edu"])
expect(job_notification.from).to eq(["no-reply-preassembly-job@stanford.edu"])
end

it 'renders the body' do
expect(job_notification.body.encoded).to include("Jdoe, your discovery_report job #1 completed")
end

end
27 changes: 26 additions & 1 deletion spec/models/job_run_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
RSpec.describe JobRun, type: :model do
let(:user) { User.new(sunet_id: 'Jdoe') }
let(:bc) do
BundleContext.new(project_name: 'SmokeTest',
BundleContext.new(id: 1,
project_name: 'SmokeTest',
content_structure: 1,
bundle_dir: 'spec/test_data/bundle_input_g',
staging_style_symlink: false,
Expand Down Expand Up @@ -30,6 +31,30 @@
end
end

describe 'send_notification' do
let(:mock_mailer) { double JobMailer }
let(:mock_delivery) { double ActionMailer::MessageDelivery }
before { job_run.save }
it 'does nothing if output_location not saved' do
expect(job_run).not_to receive(:send_notification)
job_run.save
end
it 'does not actually send the email if output_location is nil' do
expect(job_run).to receive(:send_notification).and_call_original
expect(JobMailer).not_to receive(:with)
job_run.output_location = nil
job_run.save
end
it 'sends a notification if output_location is updated a new non-nil value' do
expect(job_run).to receive(:send_notification).and_call_original
expect(JobMailer).to receive(:with).with(job_run: job_run).and_return(mock_mailer)
expect(mock_mailer).to receive(:completion_email).and_return(mock_delivery)
expect(mock_delivery).to receive(:deliver_later)
job_run.output_location += '/tmp'
job_run.save
end
end

describe '#job_type enum' do
it 'defines expected values' do
is_expected.to define_enum_for(:job_type).with(
Expand Down
9 changes: 8 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
it { is_expected.to validate_uniqueness_of(:sunet_id) }
it { is_expected.to validate_presence_of(:sunet_id) }
it { is_expected.to have_many(:bundle_contexts) }

describe 'enforces unique constraint on sunet_id' do
let(:required_attributes) do
{ sunet_id: "tempdoe" }
Expand All @@ -28,4 +27,12 @@
end
end
end

context "email address" do
subject(:user) { User.new(sunet_id: "jdoe") }

it "returns the user's email address based on sunet" do
expect(user.email).to eq('jdoe@stanford.edu')
end
end
end

0 comments on commit b46a622

Please sign in to comment.