defra_ruby_mocks 2.3.0 → 2.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/defra_ruby_mocks/govpay_controller.rb +40 -0
  3. data/app/jobs/defra_ruby_mocks/application_job.rb +6 -0
  4. data/app/jobs/defra_ruby_mocks/govpay_payment_callback_job.rb +17 -0
  5. data/app/services/defra_ruby_mocks/govpay_create_payment_service.rb +62 -0
  6. data/app/services/defra_ruby_mocks/govpay_get_payment_service.rb +90 -0
  7. data/app/services/defra_ruby_mocks/worldpay_resource_service.rb +2 -2
  8. data/app/services/defra_ruby_mocks/worldpay_response_service.rb +2 -0
  9. data/config/routes.rb +9 -0
  10. data/lib/defra_ruby_mocks/configuration.rb +1 -1
  11. data/lib/defra_ruby_mocks/version.rb +1 -1
  12. data/spec/dummy/log/test.log +2377 -2251
  13. data/spec/examples.txt +127 -111
  14. data/spec/fixtures/files/govpay/create_payment_created_response.json +24 -0
  15. data/spec/fixtures/files/govpay/create_payment_error_response.json +5 -0
  16. data/spec/fixtures/files/govpay/get_payment_response_cancelled.json +61 -0
  17. data/spec/fixtures/files/govpay/get_payment_response_created.json +73 -0
  18. data/spec/fixtures/files/govpay/get_payment_response_error.json +5 -0
  19. data/spec/fixtures/files/govpay/get_payment_response_failure.json +9 -0
  20. data/spec/fixtures/files/govpay/get_payment_response_not_found.json +4 -0
  21. data/spec/fixtures/files/govpay/get_payment_response_submitted.json +75 -0
  22. data/spec/fixtures/files/govpay/get_payment_response_success.json +62 -0
  23. data/spec/fixtures/{payment_request_invalid.xml → files/worldpay/payment_request_invalid.xml} +0 -0
  24. data/spec/fixtures/{payment_request_valid.xml → files/worldpay/payment_request_valid.xml} +0 -0
  25. data/spec/fixtures/{refund_request_invalid.xml → files/worldpay/refund_request_invalid.xml} +0 -0
  26. data/spec/fixtures/{refund_request_valid.xml → files/worldpay/refund_request_valid.xml} +0 -0
  27. data/spec/fixtures/{unrecognised_request.xml → files/worldpay/unrecognised_request.xml} +0 -0
  28. data/spec/requests/govpay_spec.rb +127 -0
  29. data/spec/requests/worldpay_spec.rb +3 -3
  30. data/spec/services/govpay_create_payment_service_spec.rb +44 -0
  31. data/spec/services/govpay_get_payment_service_spec.rb +30 -0
  32. data/spec/services/worldpay_payment_service_spec.rb +2 -2
  33. data/spec/services/worldpay_refund_service_spec.rb +2 -2
  34. data/spec/services/worldpay_request_handler_service_spec.rb +3 -3
  35. data/spec/services/worldpay_resource_service_spec.rb +8 -0
  36. data/spec/services/worldpay_response_service_spec.rb +9 -1
  37. metadata +44 -15
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ module DefraRubyMocks
6
+ RSpec.describe "Govpay", type: :request do
7
+ after(:all) { Helpers::Configuration.reset_for_tests }
8
+
9
+ context "when mocks are enabled" do
10
+ before(:each) do
11
+ Helpers::Configuration.prep_for_tests
12
+ DefraRubyMocks.configure do |config|
13
+ config.govpay_domain = "http://localhost:3000/defra_ruby_mocks"
14
+ end
15
+
16
+ end
17
+
18
+ context "#create_payment" do
19
+ let(:path) { "/defra_ruby_mocks/govpay/v1/payments" }
20
+ # Use an example from the Govpay documentation
21
+ let(:payment_request) do
22
+ {
23
+ amount: 14_500,
24
+ reference: "12345",
25
+ description: "Pay your council tax",
26
+ return_url: "https://your.service.gov.uk/completed"
27
+ }
28
+ end
29
+
30
+ context "when the request is valid" do
31
+
32
+ it "returns a valid success response" do
33
+ post path, params: payment_request.as_json
34
+
35
+ expect(response.media_type).to eq("application/json")
36
+ expect(response.code).to eq("200")
37
+ response_json = JSON.parse(response.body)
38
+ expect(response_json["reference"]).to eq payment_request[:reference]
39
+ expect(response_json["amount"]).to eq payment_request[:amount]
40
+ expect(response_json["description"]).to eq payment_request[:description]
41
+ expect(response_json["_links"]["next_url"]["href"]).to eq payment_request[:return_url]
42
+ end
43
+
44
+ it "enqueues a job to perform the payment callback" do
45
+ ActiveJob::Base.queue_adapter = :test
46
+ expect { post path, params: payment_request.as_json }.to have_enqueued_job(GovpayPaymentCallbackJob)
47
+ end
48
+ end
49
+
50
+ context "when the request is missing a mandatory parameter" do
51
+ before { payment_request[:amount] = nil }
52
+
53
+ it "returns a HTTP 500 responseß" do
54
+ post path, params: payment_request.as_json
55
+
56
+ expect(response.code).to eq "500"
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "#payment_details" do
62
+ let(:path) { "/defra_ruby_mocks/govpay/v1/payments/#{payment_id}" }
63
+
64
+ context "when the payment id is valid" do
65
+ before do
66
+ allow(GovpayGetPaymentService).to receive(:run)
67
+ .with(payment_id)
68
+ .and_return(JSON.parse(File.read("spec/fixtures/files/govpay/get_payment_response_success.json")))
69
+ end
70
+
71
+ let(:payment_id) { "12345678901234567890123456" }
72
+
73
+ it "returns a valid success response" do
74
+ get path
75
+
76
+ expect(response.code).to eq "200"
77
+ end
78
+ end
79
+
80
+ context "when the payment id is not valid" do
81
+ before do
82
+ allow(GovpayGetPaymentService).to receive(:run)
83
+ .with(payment_id)
84
+ .and_return(JSON.parse(File.read("spec/fixtures/files/govpay/get_payment_response_error.json")))
85
+ end
86
+
87
+ let(:payment_id) { "0" }
88
+
89
+ it "returns a 422 response" do
90
+ get path
91
+
92
+ expect(response.code).to eq "422"
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ context "when mocks are disabled" do
99
+ before(:each) { DefraRubyMocks.configuration.enable = false }
100
+ let(:payment_id) { Faker::Alphanumeric.alphanumeric(number: 26) }
101
+
102
+ context "POST #govpay_payments" do
103
+ let(:path) { "/defra_ruby_mocks/govpay/payments" }
104
+
105
+ it "cannot load the page" do
106
+ expect { post path }.to raise_error(ActionController::RoutingError)
107
+ end
108
+ end
109
+
110
+ context "GET #govpay_payments" do
111
+ let(:path) { "/defra_ruby_mocks/govpay/payments/#{payment_id}" }
112
+
113
+ it "cannot load the page" do
114
+ expect { get path }.to raise_error(ActionController::RoutingError)
115
+ end
116
+ end
117
+
118
+ context "#govpay_refunds" do
119
+ let(:path) { "/defra_ruby_mocks/govpay/#{payment_id}/refunds" }
120
+
121
+ it "cannot load the page" do
122
+ expect { post path }.to raise_error(ActionController::RoutingError)
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -21,7 +21,7 @@ module DefraRubyMocks
21
21
 
22
22
  context "when a payment request is received" do
23
23
  context "and the request is valid" do
24
- let(:data) { File.read("spec/fixtures/payment_request_valid.xml") }
24
+ let(:data) { File.read("spec/fixtures/files/worldpay/payment_request_valid.xml") }
25
25
 
26
26
  it "returns an XML response with a 200 code" do
27
27
  post path, headers: { "RAW_POST_DATA" => data }
@@ -33,7 +33,7 @@ module DefraRubyMocks
33
33
  end
34
34
 
35
35
  context "and the request is invalid" do
36
- let(:data) { File.read("spec/fixtures/payment_request_invalid.xml") }
36
+ let(:data) { File.read("spec/fixtures/files/worldpay/payment_request_invalid.xml") }
37
37
 
38
38
  it "returns a response with a 500 code" do
39
39
  post path, headers: { "RAW_POST_DATA" => data }
@@ -45,7 +45,7 @@ module DefraRubyMocks
45
45
 
46
46
  context "when a refund request is received" do
47
47
  context "and the request is valid" do
48
- let(:data) { File.read("spec/fixtures/refund_request_valid.xml") }
48
+ let(:data) { File.read("spec/fixtures/files/worldpay/refund_request_valid.xml") }
49
49
 
50
50
  it "returns an XML response with a 200 code" do
51
51
  post path, headers: { "RAW_POST_DATA" => data }
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ module DefraRubyMocks
6
+
7
+ RSpec.describe GovpayCreatePaymentService do
8
+
9
+ before(:each) do
10
+ Helpers::Configuration.prep_for_tests
11
+ DefraRubyMocks.configure do |config|
12
+ config.govpay_domain = "http://localhost:3000/defra_ruby_mocks"
13
+ end
14
+ end
15
+
16
+ let(:amount) { Faker::Number.number(digits: 4) }
17
+ let(:description) { Faker::Lorem.sentence }
18
+ let(:return_url) { Faker::Internet.url }
19
+
20
+ describe ".run" do
21
+
22
+ subject { described_class.run(amount: amount, description: description, return_url: return_url).deep_symbolize_keys }
23
+
24
+ context "for a valid payment request" do
25
+
26
+ it "returns a payment response with the order amount" do
27
+ expect(subject[:amount]).to eq(amount)
28
+ end
29
+
30
+ it "returns a payment response with the description" do
31
+ expect(subject[:description]).to eq(description)
32
+ end
33
+
34
+ it "returns a payment response with the return_url" do
35
+ expect(subject[:_links][:next_url][:href]).to eq(return_url)
36
+ end
37
+
38
+ it "returns the expected status" do
39
+ expect(subject[:state][:status]).to eq("created")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ module DefraRubyMocks
6
+
7
+ RSpec.describe GovpayGetPaymentService do
8
+ before { Helpers::Configuration.prep_for_tests }
9
+
10
+ let(:order_value) { Faker::Number.number(digits: 4) }
11
+ let(:payment_id) { SecureRandom.hex(26) }
12
+
13
+ # Note that the service currently supports only success responses.
14
+ describe ".run" do
15
+
16
+ subject { described_class.run(payment_id: payment_id, amount: order_value).deep_symbolize_keys }
17
+
18
+ context "when the payment is successful" do
19
+
20
+ it "returns a payment with the order amount" do
21
+ expect(subject[:amount]).to eq(order_value)
22
+ end
23
+
24
+ it "returns the expected status" do
25
+ expect(subject[:state][:status]).to eq("success")
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -25,7 +25,7 @@ module DefraRubyMocks
25
25
  end
26
26
  end
27
27
 
28
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/payment_request_valid.xml")) }
28
+ let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/payment_request_valid.xml")) }
29
29
 
30
30
  context "the result it returns" do
31
31
  it "is a hash" do
@@ -84,7 +84,7 @@ module DefraRubyMocks
84
84
  end
85
85
 
86
86
  context "when the XML is invalid" do
87
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/payment_request_invalid.xml")) }
87
+ let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/payment_request_invalid.xml")) }
88
88
 
89
89
  it "raises an error" do
90
90
  expect { described_class.run(args) }.to raise_error StandardError
@@ -11,7 +11,7 @@ module DefraRubyMocks
11
11
 
12
12
  context "when the XML is valid" do
13
13
 
14
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/refund_request_valid.xml")) }
14
+ let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/refund_request_valid.xml")) }
15
15
 
16
16
  context "the result it returns" do
17
17
  it "is a hash" do
@@ -57,7 +57,7 @@ module DefraRubyMocks
57
57
  end
58
58
 
59
59
  context "when the XML is invalid" do
60
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/refund_request_invalid.xml")) }
60
+ let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/refund_request_invalid.xml")) }
61
61
 
62
62
  it "raises an error" do
63
63
  expect { described_class.run(args) }.to raise_error StandardError
@@ -15,7 +15,7 @@ module DefraRubyMocks
15
15
  allow_any_instance_of(WorldpayPaymentService).to receive(:generate_id) { order_id }
16
16
  end
17
17
 
18
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/payment_request_valid.xml")) }
18
+ let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/payment_request_valid.xml")) }
19
19
  let(:order_id) { "1234567890" }
20
20
  let(:request_type) { { request_type: :payment } }
21
21
  let(:response_values) do
@@ -41,7 +41,7 @@ module DefraRubyMocks
41
41
  end
42
42
 
43
43
  context "and it's for a refund" do
44
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/refund_request_valid.xml")) }
44
+ let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/refund_request_valid.xml")) }
45
45
  let(:request_type) { { request_type: :refund } }
46
46
  let(:response_values) do
47
47
  {
@@ -67,7 +67,7 @@ module DefraRubyMocks
67
67
  end
68
68
 
69
69
  context "but it's not recognised" do
70
- let(:xml) { Nokogiri::XML(File.read("spec/fixtures/unrecognised_request.xml")) }
70
+ let(:xml) { Nokogiri::XML(File.read("spec/fixtures/files/worldpay/unrecognised_request.xml")) }
71
71
 
72
72
  it "raises a 'UnrecognisedWorldpayRequestError'" do
73
73
  expect { described_class.run(xml) }.to raise_error UnrecognisedWorldpayRequestError
@@ -44,6 +44,14 @@ module DefraRubyMocks
44
44
  it "returns an object with the expected company name" do
45
45
  expect(described_class.run(args).company_name).to eq(company_name.downcase)
46
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
47
55
  end
48
56
 
49
57
  context "when the resource is a Registration" do
@@ -16,7 +16,7 @@ module DefraRubyMocks
16
16
  allow(WorldpayResourceService).to receive(:run) { resource }
17
17
  end
18
18
 
19
- let(:resource) { double(:resource, order: order, company_name: company_name.downcase) }
19
+ let(:resource) { double(:resource, order: order, company_name: company_name&.downcase) }
20
20
 
21
21
  let(:admin_code) { "admincode1" }
22
22
  let(:merchant_code) { "merchantcode1" }
@@ -83,6 +83,14 @@ module DefraRubyMocks
83
83
 
84
84
  expect(described_class.run(args).url).to eq(expected_response_url)
85
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
86
94
  end
87
95
 
88
96
  context "and is for a rejected payment" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defra_ruby_mocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-29 00:00:00.000000000 Z
11
+ date: 2022-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -149,9 +149,14 @@ files:
149
149
  - Rakefile
150
150
  - app/controllers/defra_ruby_mocks/application_controller.rb
151
151
  - app/controllers/defra_ruby_mocks/company_controller.rb
152
+ - app/controllers/defra_ruby_mocks/govpay_controller.rb
152
153
  - app/controllers/defra_ruby_mocks/worldpay_controller.rb
154
+ - app/jobs/defra_ruby_mocks/application_job.rb
155
+ - app/jobs/defra_ruby_mocks/govpay_payment_callback_job.rb
153
156
  - app/services/defra_ruby_mocks/base_service.rb
154
157
  - app/services/defra_ruby_mocks/companies_house_service.rb
158
+ - app/services/defra_ruby_mocks/govpay_create_payment_service.rb
159
+ - app/services/defra_ruby_mocks/govpay_get_payment_service.rb
155
160
  - app/services/defra_ruby_mocks/worldpay_payment_service.rb
156
161
  - app/services/defra_ruby_mocks/worldpay_refund_service.rb
157
162
  - app/services/defra_ruby_mocks/worldpay_request_handler_service.rb
@@ -210,17 +215,29 @@ files:
210
215
  - spec/dummy/public/500.html
211
216
  - spec/dummy/public/favicon.ico
212
217
  - spec/examples.txt
213
- - spec/fixtures/payment_request_invalid.xml
214
- - spec/fixtures/payment_request_valid.xml
215
- - spec/fixtures/refund_request_invalid.xml
216
- - spec/fixtures/refund_request_valid.xml
217
- - spec/fixtures/unrecognised_request.xml
218
+ - spec/fixtures/files/govpay/create_payment_created_response.json
219
+ - spec/fixtures/files/govpay/create_payment_error_response.json
220
+ - spec/fixtures/files/govpay/get_payment_response_cancelled.json
221
+ - spec/fixtures/files/govpay/get_payment_response_created.json
222
+ - spec/fixtures/files/govpay/get_payment_response_error.json
223
+ - spec/fixtures/files/govpay/get_payment_response_failure.json
224
+ - spec/fixtures/files/govpay/get_payment_response_not_found.json
225
+ - spec/fixtures/files/govpay/get_payment_response_submitted.json
226
+ - spec/fixtures/files/govpay/get_payment_response_success.json
227
+ - spec/fixtures/files/worldpay/payment_request_invalid.xml
228
+ - spec/fixtures/files/worldpay/payment_request_valid.xml
229
+ - spec/fixtures/files/worldpay/refund_request_invalid.xml
230
+ - spec/fixtures/files/worldpay/refund_request_valid.xml
231
+ - spec/fixtures/files/worldpay/unrecognised_request.xml
218
232
  - spec/lib/configuration_spec.rb
219
233
  - spec/rails_helper.rb
220
234
  - spec/requests/company_spec.rb
235
+ - spec/requests/govpay_spec.rb
221
236
  - spec/requests/officers_spec.rb
222
237
  - spec/requests/worldpay_spec.rb
223
238
  - spec/services/companies_house_service_spec.rb
239
+ - spec/services/govpay_create_payment_service_spec.rb
240
+ - spec/services/govpay_get_payment_service_spec.rb
224
241
  - spec/services/worldpay_payment_service_spec.rb
225
242
  - spec/services/worldpay_refund_service_spec.rb
226
243
  - spec/services/worldpay_request_handler_service_spec.rb
@@ -237,7 +254,7 @@ homepage: https://github.com/DEFRA/defra-ruby-mocks
237
254
  licenses:
238
255
  - The Open Government Licence (OGL) Version 3
239
256
  metadata: {}
240
- post_install_message:
257
+ post_install_message:
241
258
  rdoc_options: []
242
259
  require_paths:
243
260
  - lib
@@ -253,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
253
270
  version: '0'
254
271
  requirements: []
255
272
  rubygems_version: 3.1.2
256
- signing_key:
273
+ signing_key:
257
274
  specification_version: 4
258
275
  summary: Defra Ruby on Rails external API mocking engine
259
276
  test_files:
@@ -295,6 +312,7 @@ test_files:
295
312
  - spec/dummy/README.rdoc
296
313
  - spec/examples.txt
297
314
  - spec/defra_ruby_mocks_spec.rb
315
+ - spec/requests/govpay_spec.rb
298
316
  - spec/requests/company_spec.rb
299
317
  - spec/requests/worldpay_spec.rb
300
318
  - spec/requests/officers_spec.rb
@@ -305,15 +323,26 @@ test_files:
305
323
  - spec/support/helpers/configuration.rb
306
324
  - spec/support/helpers/waste_carriers_engine.rb
307
325
  - spec/lib/configuration_spec.rb
308
- - spec/fixtures/unrecognised_request.xml
309
- - spec/fixtures/payment_request_invalid.xml
310
- - spec/fixtures/refund_request_valid.xml
311
- - spec/fixtures/payment_request_valid.xml
312
- - spec/fixtures/refund_request_invalid.xml
326
+ - spec/fixtures/files/worldpay/unrecognised_request.xml
327
+ - spec/fixtures/files/worldpay/payment_request_invalid.xml
328
+ - spec/fixtures/files/worldpay/refund_request_valid.xml
329
+ - spec/fixtures/files/worldpay/payment_request_valid.xml
330
+ - spec/fixtures/files/worldpay/refund_request_invalid.xml
331
+ - spec/fixtures/files/govpay/get_payment_response_submitted.json
332
+ - spec/fixtures/files/govpay/get_payment_response_not_found.json
333
+ - spec/fixtures/files/govpay/create_payment_created_response.json
334
+ - spec/fixtures/files/govpay/get_payment_response_created.json
335
+ - spec/fixtures/files/govpay/create_payment_error_response.json
336
+ - spec/fixtures/files/govpay/get_payment_response_success.json
337
+ - spec/fixtures/files/govpay/get_payment_response_failure.json
338
+ - spec/fixtures/files/govpay/get_payment_response_cancelled.json
339
+ - spec/fixtures/files/govpay/get_payment_response_error.json
313
340
  - spec/rails_helper.rb
341
+ - spec/services/govpay_get_payment_service_spec.rb
314
342
  - spec/services/worldpay_response_service_spec.rb
315
343
  - spec/services/worldpay_resource_service_spec.rb
316
344
  - spec/services/companies_house_service_spec.rb
317
345
  - spec/services/worldpay_payment_service_spec.rb
318
346
  - spec/services/worldpay_refund_service_spec.rb
319
347
  - spec/services/worldpay_request_handler_service_spec.rb
348
+ - spec/services/govpay_create_payment_service_spec.rb