Skip to content

Commit

Permalink
[CPDNPQ-2142] Ensure job only runs once if state change not possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jebw committed Nov 6, 2024
1 parent 172fe46 commit 3d2ea86
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
4 changes: 4 additions & 0 deletions app/jobs/statements/mark_as_paid_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module Statements
class MarkAsPaidJob < ApplicationJob
discard_on StandardError do |_job, exception|
Sentry.capture_exception(exception)
end

def perform(statement_id:)
return unless statement_id

Expand Down
79 changes: 58 additions & 21 deletions spec/jobs/statements/mark_as_paid_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,91 @@

require "rails_helper"

RSpec.describe Statements::MarkAsPaidJob do
RSpec.describe Statements::MarkAsPaidJob, type: :job do
subject(:job) { described_class.new.perform(statement_id:) }

before do
allow(Statements::MarkAsPaid).to receive(:new).with(statement).and_return(service)
allow(service).to receive(:mark).and_return(true)
allow(Rails.logger).to receive(:warn)

job
allow(Sentry).to receive(:capture_exception).and_return(true)
end

let(:service) { Statements::MarkAsPaid.new(statement) }
let(:statement) { create(:statement, :payable) }
let(:statement_id) { statement.id }

context "with correct params" do
context "when statement is payable" do
it "calls the correct service" do
expect(service).to have_received(:mark)
describe "#perform" do
before { job }

context "with correct params" do
context "when statement is payable" do
it "calls the correct service" do
expect(service).to have_received(:mark)
end
end

context "when statement is not payable" do
let(:statement) { create(:statement, :paid) }

it "does not call the service" do
expect(Statements::MarkAsPaid).not_to have_received(:new)
end
end
end

context "when statement is not payable" do
let(:statement) { create(:statement, :paid) }
context "with incorrect params" do
let(:statement_id) { SecureRandom.uuid }

it "does not call the service" do
expect(Statements::MarkAsPaid).not_to have_received(:new)
end

it "logs a warning" do
expect(Rails.logger)
.to have_received(:warn)
.with("Statement could not be found - statement_id: #{statement_id}")
end
end

context "with no params" do
let(:statement_id) { nil }

it { is_expected.to be_nil }
end
end

context "with incorrect params" do
let(:statement_id) { SecureRandom.uuid }
describe "#perform_later" do
subject(:job) { described_class.perform_later(statement_id:) }

it "does not call the service" do
expect(Statements::MarkAsPaid).not_to have_received(:new)
it "enqueues the job exactly once" do
expect { job }.to have_enqueued_job(described_class).exactly(:once).on_queue("default")
end

it "logs a warning" do
expect(Rails.logger)
.to have_received(:warn)
.with("Statement could not be found - statement_id: #{statement_id}")
context "with valid job" do
before do
perform_enqueued_jobs { job }
end

it { expect(Sentry).not_to have_received(:capture_exception) }
it { expect(Delayed::Job.count).to be_zero }
end
end

context "with no params" do
let(:statement_id) { nil }
context "with invalid job" do
before do
allow(service).to receive(:mark).and_raise(exception)

it { is_expected.to be_nil }
perform_enqueued_jobs { job }
end

let :exception do
StateMachines::InvalidTransition.new(statement,
Statement.state_machines[:state],
:mark_paid)
end

it { expect(Sentry).to have_received(:capture_exception) }
it { expect(Delayed::Job.count).to be_zero }
end
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
end
end

config.include ActiveJob::TestHelper, type: :job
config.include Helpers::JourneyHelper, type: :feature
config.before(:each, type: :feature) do
stub_env_variables_for_gai
Expand Down

0 comments on commit 3d2ea86

Please sign in to comment.