defra_ruby_mocks 2.3.3 → 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +32 -75
  3. data/app/controllers/defra_ruby_mocks/govpay_controller.rb +23 -0
  4. data/app/services/defra_ruby_mocks/govpay_refund_details_service.rb +43 -0
  5. data/app/services/defra_ruby_mocks/govpay_request_refund_service.rb +27 -0
  6. data/config/routes.rb +10 -10
  7. data/lib/defra_ruby_mocks/configuration.rb +1 -1
  8. data/lib/defra_ruby_mocks/engine.rb +0 -1
  9. data/lib/defra_ruby_mocks/version.rb +1 -1
  10. data/spec/dummy/log/test.log +1256 -2285
  11. data/spec/examples.txt +68 -130
  12. data/spec/requests/govpay_spec.rb +44 -1
  13. data/spec/services/govpay_refund_details_service_spec.rb +58 -0
  14. data/spec/services/govpay_request_refund_service_spec.rb +31 -0
  15. metadata +8 -48
  16. data/app/controllers/defra_ruby_mocks/worldpay_controller.rb +0 -57
  17. data/app/services/defra_ruby_mocks/worldpay_payment_service.rb +0 -47
  18. data/app/services/defra_ruby_mocks/worldpay_refund_service.rb +0 -37
  19. data/app/services/defra_ruby_mocks/worldpay_request_handler_service.rb +0 -40
  20. data/app/services/defra_ruby_mocks/worldpay_resource_service.rb +0 -55
  21. data/app/services/defra_ruby_mocks/worldpay_response_service.rb +0 -119
  22. data/app/views/defra_ruby_mocks/worldpay/payment_request.xml.erb +0 -4
  23. data/app/views/defra_ruby_mocks/worldpay/refund_request.xml.erb +0 -4
  24. data/app/views/defra_ruby_mocks/worldpay/stuck.html.erb +0 -37
  25. data/lib/defra_ruby_mocks/unrecognised_worldpay_request_error.rb +0 -5
  26. data/spec/fixtures/files/worldpay/payment_request_invalid.xml +0 -6
  27. data/spec/fixtures/files/worldpay/payment_request_valid.xml +0 -30
  28. data/spec/fixtures/files/worldpay/refund_request_invalid.xml +0 -6
  29. data/spec/fixtures/files/worldpay/refund_request_valid.xml +0 -11
  30. data/spec/fixtures/files/worldpay/unrecognised_request.xml +0 -6
  31. data/spec/requests/worldpay_spec.rb +0 -163
  32. data/spec/services/worldpay_payment_service_spec.rb +0 -95
  33. data/spec/services/worldpay_refund_service_spec.rb +0 -68
  34. data/spec/services/worldpay_request_handler_service_spec.rb +0 -79
  35. data/spec/services/worldpay_resource_service_spec.rb +0 -120
  36. data/spec/services/worldpay_response_service_spec.rb +0 -280
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails_helper"
4
-
5
- module DefraRubyMocks
6
- RSpec.describe WorldpayRequestHandlerService do
7
- describe ".run" do
8
- context "when a request is made" do
9
-
10
- let(:merchant_code) { "MERCHME" }
11
- let(:args) { { merchant_code: merchant_code, xml: xml } }
12
-
13
- context "and it's for a payment" do
14
- before do
15
- allow_any_instance_of(WorldpayPaymentService).to receive(:generate_id) { order_id }
16
- end
17
-
18
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/payment_request_valid.xml")) }
19
- let(:order_id) { "1234567890" }
20
- let(:request_type) { { request_type: :payment } }
21
- let(:response_values) do
22
- {
23
- merchant_code: merchant_code,
24
- order_code: "1577726052",
25
- id: order_id,
26
- url: "http://example.com"
27
- }
28
- end
29
-
30
- it "correctly determines the request service to use" do
31
- expect(WorldpayPaymentService).to receive(:run).with(args) { response_values }
32
-
33
- described_class.run(xml)
34
- end
35
-
36
- it "returns the values the controller needs to handle the request" do
37
- expect(WorldpayPaymentService).to receive(:run).with(args) { response_values }
38
-
39
- expect(described_class.run(xml)).to eq(request_type.merge(response_values))
40
- end
41
- end
42
-
43
- context "and it's for a refund" do
44
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/refund_request_valid.xml")) }
45
- let(:request_type) { { request_type: :refund } }
46
- let(:response_values) do
47
- {
48
- merchant_code: merchant_code,
49
- order_code: "1579644835",
50
- refund_value: "2500",
51
- currency_code: "GBP",
52
- exponent: "2"
53
- }
54
- end
55
-
56
- it "correctly determines the request service to use" do
57
- expect(WorldpayRefundService).to receive(:run).with(args) { response_values }
58
-
59
- described_class.run(xml)
60
- end
61
-
62
- it "returns the values the controller needs to handle the request" do
63
- expect(WorldpayRefundService).to receive(:run).with(args) { response_values }
64
-
65
- expect(described_class.run(xml)).to eq(request_type.merge(response_values))
66
- end
67
- end
68
-
69
- context "but it's not recognised" do
70
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/unrecognised_request.xml")) }
71
-
72
- it "raises a 'UnrecognisedWorldpayRequestError'" do
73
- expect { described_class.run(xml) }.to raise_error UnrecognisedWorldpayRequestError
74
- end
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails_helper"
4
-
5
- module DefraRubyMocks
6
- RSpec.describe WorldpayResourceService do
7
- before(:each) do
8
- allow(::WasteCarriersEngine::TransientRegistration).to receive(:where) { transient_relation }
9
- allow(::WasteCarriersEngine::Registration).to receive(:where) { registration_relation }
10
- end
11
-
12
- let(:reference) { "12345" }
13
- let(:company_name) { "Pay for the thing" }
14
-
15
- let(:resource) { double(:resource, finance_details: finance_details, company_name: company_name) }
16
- let(:finance_details) { double(:finance_details, orders: orders) }
17
- let(:orders) { double(:orders, order_by: sorted_orders) }
18
- let(:sorted_orders) { double(:sorted_orders, first: order) }
19
- let(:order) { double(:order) }
20
-
21
- let(:args) { { reference: reference } }
22
-
23
- describe ".run" do
24
-
25
- context "when the resource is a TransientRegistration" do
26
- let(:transient_relation) { double(:relation, first: resource) }
27
-
28
- it "will only search transient registrations" do
29
- described_class.run(args)
30
-
31
- expect(::WasteCarriersEngine::TransientRegistration).to have_received(:where).with(token: reference)
32
-
33
- expect(::WasteCarriersEngine::Registration).not_to have_received(:where).with(reg_uuid: reference)
34
- end
35
-
36
- it "returns an object with the matching resource" do
37
- expect(described_class.run(args).resource).to eq(resource)
38
- end
39
-
40
- it "returns an object with the expected order" do
41
- expect(described_class.run(args).order).to eq(order)
42
- end
43
-
44
- it "returns an object with the expected company name" do
45
- expect(described_class.run(args).company_name).to eq(company_name.downcase)
46
- end
47
-
48
- context "when the company name is not populated" do
49
- let(:company_name) { nil }
50
-
51
- it "returns the object" do
52
- expect(described_class.run(args).resource).to eq(resource)
53
- end
54
- end
55
- end
56
-
57
- context "when the resource is a Registration" do
58
- let(:transient_relation) { double(:relation, first: nil) }
59
- let(:registration_relation) { double(:relation, first: resource) }
60
-
61
- it "will search transient registrations first, then registrations" do
62
- described_class.run(args)
63
-
64
- expect(::WasteCarriersEngine::TransientRegistration).to have_received(:where).with(token: reference)
65
-
66
- expect(::WasteCarriersEngine::Registration).to have_received(:where).with(reg_uuid: reference)
67
- end
68
-
69
- it "returns an object with the matching resource" do
70
- expect(described_class.run(args).resource).to eq(resource)
71
- end
72
-
73
- it "returns an object with the expected order" do
74
- expect(described_class.run(args).order).to eq(order)
75
- end
76
-
77
- it "returns an object with the expected company name" do
78
- expect(described_class.run(args).company_name).to eq(company_name.downcase)
79
- end
80
- end
81
-
82
- context "when the resource is a OrderCopyCardsRegistration" do
83
- before do
84
- # Because we do not copy the company name to
85
- # `OrderCopyCardsRegistration` instances when we create them in WCR
86
- # we need to locate the orignal registration they are based on. We
87
- # determine in the class if the 'resource' is an instance of one by
88
- # comparing the result of resource.class.to_s to
89
- # "WasteCarriersEngine::OrderCopyCardsRegistration". The problem is
90
- # when testing 'resource' is actually an instance of
91
- # `RSpec::Mocks::Double`! So we subvert the call to class on
92
- # RSpec::Mocks::Double to return "WasteCarriersEngine::OrderCopyCardsRegistration"
93
- # just in this spec. We can then test that the service does indeed
94
- # locate the original registration for a company name
95
- allow_any_instance_of(RSpec::Mocks::Double).to receive(:class)
96
- .and_return("WasteCarriersEngine::OrderCopyCardsRegistration")
97
- end
98
-
99
- let(:copy_card_resource) { double(:resource, finance_details: finance_details, reg_identifier: "CBDU123") }
100
- let(:transient_relation) { double(:relation, first: copy_card_resource) }
101
- let(:registration_relation) { double(:relation, first: resource) }
102
-
103
- it "locates the original registration to grab the company name" do
104
- expect(described_class.run(args).company_name).to eq(company_name.downcase)
105
-
106
- expect(::WasteCarriersEngine::Registration).to have_received(:where).with(reg_identifier: "CBDU123")
107
- end
108
- end
109
-
110
- context "when the resource does not exist" do
111
- let(:transient_relation) { double(:relation, first: nil) }
112
- let(:registration_relation) { double(:relation, first: nil) }
113
-
114
- it "causes an error" do
115
- expect { described_class.run(args) }.to raise_error MissingResourceError
116
- end
117
- end
118
- end
119
- end
120
- end
@@ -1,280 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails_helper"
4
-
5
- module DefraRubyMocks
6
-
7
- RSpec.describe WorldpayResponseService do
8
- before(:each) do
9
- Helpers::Configuration.prep_for_tests
10
- DefraRubyMocks.configure do |config|
11
- config.worldpay_admin_code = admin_code
12
- config.worldpay_merchant_code = merchant_code
13
- config.worldpay_mac_secret = mac_secret
14
- end
15
-
16
- allow(WorldpayResourceService).to receive(:run) { resource }
17
- end
18
-
19
- let(:resource) { double(:resource, order: order, company_name: company_name&.downcase) }
20
-
21
- let(:admin_code) { "admincode1" }
22
- let(:merchant_code) { "merchantcode1" }
23
- let(:mac_secret) { "mac1" }
24
- let(:reference) { "12345" }
25
- let(:order_code) { "54321" }
26
- let(:order_key) { "#{admin_code}^#{merchant_code}^#{order_code}" }
27
- let(:order_value) { 105_00 }
28
- let(:payment_status) { :AUTHORISED }
29
- let(:company_name) { "Pay for the thing" }
30
-
31
- let(:order) { double(:order, order_code: order_code, total_amount: order_value) }
32
-
33
- let(:mac) { Digest::MD5.hexdigest(mac_data.join).to_s }
34
-
35
- let(:query_string) do
36
- [
37
- "orderKey=#{order_key}",
38
- "paymentStatus=#{payment_status}",
39
- "paymentAmount=#{order_value}",
40
- "paymentCurrency=GBP",
41
- "mac=#{mac}",
42
- "source=WP"
43
- ].join("&")
44
- end
45
-
46
- let(:args) do
47
- {
48
- success_url: success_url,
49
- failure_url: failure_url,
50
- pending_url: pending_url,
51
- cancel_url: cancel_url,
52
- error_url: error_url
53
- }
54
- end
55
-
56
- describe ".run" do
57
- context "when the request comes from the waste-carriers-front-office" do
58
- let(:success_url) { "http://example.com/fo/#{reference}/worldpay/success" }
59
- let(:failure_url) { "http://example.com/fo/#{reference}/worldpay/failure" }
60
- let(:pending_url) { "http://example.com/fo/#{reference}/worldpay/pending" }
61
- let(:cancel_url) { "http://example.com/fo/#{reference}/worldpay/cancel" }
62
- let(:error_url) { "http://example.com/fo/#{reference}/worldpay/error" }
63
-
64
- context "and is valid" do
65
- let(:relation) { double(:relation, first: registration) }
66
- let(:mac_data) { [order_key, order_value, "GBP", payment_status, mac_secret] }
67
-
68
- it "can extract the reference from the `success_url`" do
69
- expect(described_class.run(args).reference).to eq(reference)
70
- end
71
-
72
- it "can generate a valid order key" do
73
- expect(described_class.run(args).order_key).to eq(order_key)
74
- end
75
-
76
- context "and is for a successful payment" do
77
- it "can generate a valid mac" do
78
- expect(described_class.run(args).mac).to eq(mac)
79
- end
80
-
81
- it "returns a url in the expected format" do
82
- expected_response_url = "#{success_url}?#{query_string}"
83
-
84
- expect(described_class.run(args).url).to eq(expected_response_url)
85
- end
86
-
87
- context "and the company name is blank" do
88
- let(:company_name) { nil }
89
-
90
- it "can extract the reference from the `success_url`" do
91
- expect(described_class.run(args).reference).to eq(reference)
92
- end
93
- end
94
- end
95
-
96
- context "and is for a rejected payment" do
97
- let(:payment_status) { :REFUSED }
98
- let(:company_name) { "Reject for the thing" }
99
-
100
- it "can generate a valid mac" do
101
- expect(described_class.run(args).mac).to eq(mac)
102
- end
103
-
104
- it "returns a url in the expected format" do
105
- expected_response_url = "#{failure_url}?#{query_string}"
106
-
107
- expect(described_class.run(args).url).to eq(expected_response_url)
108
- end
109
- end
110
-
111
- context "and is for a stuck payment" do
112
- let(:payment_status) { :STUCK }
113
- let(:company_name) { "Give me a stuck thing" }
114
-
115
- it "can generate a valid mac" do
116
- expect(described_class.run(args).mac).to eq(mac)
117
- end
118
-
119
- it "returns a status of :STUCK" do
120
- expect(described_class.run(args).status).to eq(:STUCK)
121
- end
122
- end
123
-
124
- context "and is for a pending payment" do
125
- let(:payment_status) { :SENT_FOR_AUTHORISATION }
126
- let(:company_name) { "Pending for the thing" }
127
-
128
- it "can generate a valid mac" do
129
- expect(described_class.run(args).mac).to eq(mac)
130
- end
131
-
132
- it "returns a url in the expected format" do
133
- expected_response_url = "#{pending_url}?#{query_string}"
134
-
135
- expect(described_class.run(args).url).to eq(expected_response_url)
136
- end
137
- end
138
-
139
- context "and is for a cancelled payment" do
140
- let(:payment_status) { :CANCELLED }
141
- let(:company_name) { "Cancel the thing" }
142
- let(:mac_data) { [order_key, order_value, "GBP", mac_secret] }
143
-
144
- it "can generate a valid mac" do
145
- expect(described_class.run(args).mac).to eq(mac)
146
- end
147
-
148
- it "returns a url in the expected format" do
149
- expected_response_url = "#{cancel_url}?#{query_string}"
150
-
151
- expect(described_class.run(args).url).to eq(expected_response_url)
152
- end
153
- end
154
-
155
- context "and is for an errored payment" do
156
- let(:payment_status) { :ERROR }
157
- let(:company_name) { "Error the thing" }
158
-
159
- it "can generate a valid mac" do
160
- expect(described_class.run(args).mac).to eq(mac)
161
- end
162
-
163
- it "returns a url in the expected format" do
164
- expected_response_url = "#{error_url}?#{query_string}"
165
-
166
- expect(described_class.run(args).url).to eq(expected_response_url)
167
- end
168
- end
169
- end
170
- end
171
-
172
- context "when the request comes from the waste-carriers-frontend" do
173
- let(:success_url) { "http://example.com/your-registration/#{reference}/worldpay/success/54321/NEWREG?locale=en" }
174
- let(:failure_url) { "http://example.com/your-registration/#{reference}/worldpay/failure/54321/NEWREG?locale=en" }
175
- let(:pending_url) { "http://example.com/your-registration/#{reference}/worldpay/pending/54321/NEWREG?locale=en" }
176
- let(:cancel_url) { "http://example.com/your-registration/#{reference}/worldpay/cancel/54321/NEWREG?locale=en" }
177
- let(:error_url) { "http://example.com/your-registration/#{reference}/worldpay/error/54321/NEWREG?locale=en" }
178
-
179
- context "and is valid" do
180
- let(:relation) { double(:relation, first: registration) }
181
- let(:mac_data) { [order_key, order_value, "GBP", payment_status, mac_secret] }
182
-
183
- it "can extract the reference from the `success_url`" do
184
- expect(described_class.run(args).reference).to eq(reference)
185
- end
186
-
187
- it "can generate a valid order key" do
188
- expect(described_class.run(args).order_key).to eq(order_key)
189
- end
190
-
191
- context "and is for a successful payment" do
192
- it "can generate a valid mac" do
193
- expect(described_class.run(args).mac).to eq(mac)
194
- end
195
-
196
- it "returns a url in the expected format" do
197
- expected_response_url = "#{success_url}&#{query_string}"
198
-
199
- expect(described_class.run(args).url).to eq(expected_response_url)
200
- end
201
- end
202
-
203
- context "and is for a rejected payment" do
204
- let(:payment_status) { :REFUSED }
205
- let(:company_name) { "Reject for the thing" }
206
-
207
- it "can generate a valid mac" do
208
- expect(described_class.run(args).mac).to eq(mac)
209
- end
210
-
211
- it "returns a url in the expected format" do
212
- expected_response_url = "#{failure_url}&#{query_string}"
213
-
214
- expect(described_class.run(args).url).to eq(expected_response_url)
215
- end
216
- end
217
-
218
- context "and is for a stuck payment" do
219
- let(:payment_status) { :STUCK }
220
- let(:company_name) { "Give me a stuck thing" }
221
-
222
- it "can generate a valid mac" do
223
- expect(described_class.run(args).mac).to eq(mac)
224
- end
225
-
226
- it "returns a status of :STUCK" do
227
- expect(described_class.run(args).status).to eq(:STUCK)
228
- end
229
- end
230
-
231
- context "and is for a pending payment" do
232
- let(:payment_status) { :SENT_FOR_AUTHORISATION }
233
- let(:company_name) { "Pending for the thing" }
234
-
235
- it "can generate a valid mac" do
236
- expect(described_class.run(args).mac).to eq(mac)
237
- end
238
-
239
- it "returns a url in the expected format" do
240
- expected_response_url = "#{pending_url}&#{query_string}"
241
-
242
- expect(described_class.run(args).url).to eq(expected_response_url)
243
- end
244
- end
245
-
246
- context "and is for a cancelled payment" do
247
- let(:payment_status) { :CANCELLED }
248
- let(:company_name) { "Cancel the thing" }
249
- let(:mac_data) { [order_key, order_value, "GBP", mac_secret] }
250
-
251
- it "can generate a valid mac" do
252
- expect(described_class.run(args).mac).to eq(mac)
253
- end
254
-
255
- it "returns a url in the expected format" do
256
- expected_response_url = "#{cancel_url}&#{query_string}"
257
-
258
- expect(described_class.run(args).url).to eq(expected_response_url)
259
- end
260
- end
261
-
262
- context "and is for an errored payment" do
263
- let(:payment_status) { :ERROR }
264
- let(:company_name) { "Error the thing" }
265
-
266
- it "can generate a valid mac" do
267
- expect(described_class.run(args).mac).to eq(mac)
268
- end
269
-
270
- it "returns a url in the expected format" do
271
- expected_response_url = "#{error_url}&#{query_string}"
272
-
273
- expect(described_class.run(args).url).to eq(expected_response_url)
274
- end
275
- end
276
- end
277
- end
278
- end
279
- end
280
- end