-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathvoid_service_spec.rb
More file actions
128 lines (98 loc) · 4.33 KB
/
Copy pathvoid_service_spec.rb
File metadata and controls
128 lines (98 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true
require "rails_helper"
RSpec.describe OrderForms::VoidService do
subject(:service) { described_class.new(order_form:) }
let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) }
let_it_be(:customer) { create(:customer, organization:) }
let(:quote) { create(:quote, organization:, customer:) }
let(:quote_version) { create(:quote_version, :approved, organization:, quote:) }
let(:order_form) { create(:order_form, customer:, organization:, quote_version:) }
describe "#call" do
context "when license is not premium" do
it "returns a forbidden failure" do
result = service.call
expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ForbiddenFailure)
expect(result.error.code).to eq("feature_unavailable")
end
end
context "with a premium license", :premium do
context "when the order form does not exist" do
let(:order_form) { nil }
it "returns a not found failure" do
result = service.call
expect(result).not_to be_success
expect(result.error).to be_a(BaseService::NotFoundFailure)
expect(result.error.resource).to eq("order_form")
end
end
context "with concurrent mutations" do
it "wraps the work in a per-quote lock" do
allow(Quotes::LockService).to receive(:call).and_call_original
service.call
expect(Quotes::LockService).to have_received(:call).with(quote: order_form.quote_version.quote).at_least(:once)
end
it "re-checks the status under the lock and refuses voiding an order form signed concurrently" do
order_form
OrderForm.find(order_form.id).update!(status: :signed, signed_at: Time.current)
result = service.call
expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.messages).to eq({status: ["not_voidable"]})
end
end
context "when the order form is not generated" do
let(:order_form) { create(:order_form, :signed, customer:, organization:) }
it "returns a validation failure" do
result = service.call
expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.messages).to eq({status: ["not_voidable"]})
end
end
context "when the order form is generated" do
it "transitions the order form to voided" do
result = service.call
expect(result).to be_success
expect(result.order_form).to be_voided
expect(result.order_form.voided_at).to be_present
expect(result.order_form.void_reason).to eq("manual")
end
it "cascades the void to the parent quote version" do
service.call
expect(quote_version.reload).to be_voided
expect(quote_version.void_reason).to eq("cascade_of_voided")
end
it "enqueues an order_form.voided webhook" do
expect { service.call }
.to have_enqueued_job_after_commit(SendWebhookJob)
.with("order_form.voided", order_form)
end
it "enqueues a cascaded quote.voided webhook" do
expect { service.call }
.to have_enqueued_job_after_commit(SendWebhookJob)
.with("quote.voided", quote_version)
end
it "produces an order_form.voided activity log" do
service.call
expect(Utils::ActivityLog).to have_produced("order_form.voided").after_commit.with(order_form)
end
it "produces a cascaded quote.voided activity log" do
service.call
expect(Utils::ActivityLog).to have_produced("quote.voided").after_commit.with(quote_version)
end
end
end
context "when the quote lock cannot be acquired", :premium do
before do
allow(Quotes::LockService).to receive(:call).and_raise(BaseLockService::FailedToAcquireLock)
end
it "returns a concurrency conflict instead of raising" do
result = service.call
expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.messages).to eq(base: ["concurrency_conflict"])
end
end
end
end