defra_ruby_mocks 2.3.2 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -75
- data/app/controllers/defra_ruby_mocks/govpay_controller.rb +28 -3
- data/app/jobs/defra_ruby_mocks/application_job.rb +6 -0
- data/app/jobs/defra_ruby_mocks/govpay_payment_callback_job.rb +17 -0
- data/app/services/defra_ruby_mocks/govpay_create_payment_service.rb +41 -13
- data/app/services/defra_ruby_mocks/govpay_get_payment_service.rb +74 -16
- data/app/services/defra_ruby_mocks/govpay_refund_details_service.rb +43 -0
- data/app/services/defra_ruby_mocks/govpay_request_refund_service.rb +27 -0
- data/config/routes.rb +10 -10
- data/lib/defra_ruby_mocks/configuration.rb +1 -1
- data/lib/defra_ruby_mocks/engine.rb +0 -1
- data/lib/defra_ruby_mocks/version.rb +1 -1
- data/spec/dummy/log/test.log +1279 -972
- data/spec/examples.txt +68 -121
- data/spec/fixtures/files/govpay/create_payment_created_response.json +24 -0
- data/spec/fixtures/files/govpay/create_payment_error_response.json +5 -0
- data/spec/fixtures/files/govpay/get_payment_response_cancelled.json +61 -0
- data/spec/fixtures/files/govpay/get_payment_response_created.json +73 -0
- data/spec/fixtures/files/govpay/get_payment_response_error.json +5 -0
- data/spec/fixtures/files/govpay/get_payment_response_failure.json +9 -0
- data/spec/fixtures/files/govpay/get_payment_response_not_found.json +4 -0
- data/spec/fixtures/files/govpay/get_payment_response_submitted.json +75 -0
- data/spec/fixtures/files/govpay/get_payment_response_success.json +62 -0
- data/spec/requests/govpay_spec.rb +64 -1
- data/spec/services/govpay_create_payment_service_spec.rb +44 -0
- data/spec/services/govpay_get_payment_service_spec.rb +30 -0
- data/spec/services/govpay_refund_details_service_spec.rb +58 -0
- data/spec/services/govpay_request_refund_service_spec.rb +31 -0
- metadata +32 -48
- data/app/controllers/defra_ruby_mocks/worldpay_controller.rb +0 -57
- data/app/services/defra_ruby_mocks/worldpay_payment_service.rb +0 -47
- data/app/services/defra_ruby_mocks/worldpay_refund_service.rb +0 -37
- data/app/services/defra_ruby_mocks/worldpay_request_handler_service.rb +0 -40
- data/app/services/defra_ruby_mocks/worldpay_resource_service.rb +0 -55
- data/app/services/defra_ruby_mocks/worldpay_response_service.rb +0 -119
- data/app/views/defra_ruby_mocks/worldpay/payment_request.xml.erb +0 -4
- data/app/views/defra_ruby_mocks/worldpay/refund_request.xml.erb +0 -4
- data/app/views/defra_ruby_mocks/worldpay/stuck.html.erb +0 -37
- data/lib/defra_ruby_mocks/unrecognised_worldpay_request_error.rb +0 -5
- data/spec/fixtures/payment_request_invalid.xml +0 -6
- data/spec/fixtures/payment_request_valid.xml +0 -30
- data/spec/fixtures/refund_request_invalid.xml +0 -6
- data/spec/fixtures/refund_request_valid.xml +0 -11
- data/spec/fixtures/unrecognised_request.xml +0 -6
- data/spec/requests/worldpay_spec.rb +0 -163
- data/spec/services/worldpay_payment_service_spec.rb +0 -95
- data/spec/services/worldpay_refund_service_spec.rb +0 -68
- data/spec/services/worldpay_request_handler_service_spec.rb +0 -79
- data/spec/services/worldpay_resource_service_spec.rb +0 -120
- data/spec/services/worldpay_response_service_spec.rb +0 -280
@@ -1,37 +0,0 @@
|
|
1
|
-
<!doctype html>
|
2
|
-
|
3
|
-
<html lang="en">
|
4
|
-
<head>
|
5
|
-
<meta charset="utf-8">
|
6
|
-
|
7
|
-
<title>You is stuck</title>
|
8
|
-
<meta name="description" content="Thw Worldpay stuck page">
|
9
|
-
<meta name="author" content="Defra">
|
10
|
-
</head>
|
11
|
-
|
12
|
-
<body>
|
13
|
-
<main>
|
14
|
-
<div id="message">
|
15
|
-
<h1>Stuck!</h1>
|
16
|
-
<p>Looks like your registration has gotten stuck whilst paying for it using Worldpay.</p>
|
17
|
-
<p>We hope that's what you expected to happen.</p>
|
18
|
-
<p>Regards, the Ruby Services Team</p>
|
19
|
-
</div>
|
20
|
-
<% if @response %>
|
21
|
-
<div id="debug">
|
22
|
-
<h2>Debug</h2>
|
23
|
-
<ul>
|
24
|
-
<li><strong>Supplied URL</strong> <code><%= @response.supplied_url %></code></li>
|
25
|
-
<li><strong>Separator</strong> <code><%= @response.separator %></code></li>
|
26
|
-
<li><strong>Order key</strong> <code><%= @response.order_key %></code></li>
|
27
|
-
<li><strong>Mac</strong> <code><%= @response.mac %></code></li>
|
28
|
-
<li><strong>Value</strong> <code><%= @response.value %></code></li>
|
29
|
-
<li><strong>Status</strong> <code><%= @response.status %></code></li>
|
30
|
-
<li><strong>Reference</strong> <code><%= @response.reference %></code></li>
|
31
|
-
<li><strong>Url</strong> <code><%= @response.url %></code></li>
|
32
|
-
</ul>
|
33
|
-
</div>
|
34
|
-
<% end %>
|
35
|
-
</main>
|
36
|
-
</body>
|
37
|
-
</html>
|
@@ -1,30 +0,0 @@
|
|
1
|
-
<?xml version="1.0"?>
|
2
|
-
<!DOCTYPE paymentService PUBLIC "-//WorldPay/DTD WorldPay PaymentService v1/EN" "http://dtd.worldpay.com/paymentService_v1.dtd">
|
3
|
-
<paymentService version="1.4" merchantCode="MERCHME">
|
4
|
-
<submit>
|
5
|
-
<order orderCode="1577726052">
|
6
|
-
<description>Your Waste Carrier Registration CBDU2</description>
|
7
|
-
<amount currencyCode="GBP" value="10500" exponent="2"/>
|
8
|
-
<orderContent>Waste Carrier Registration renewal: CBDU2 for Booth Mcdaniel Associates</orderContent>
|
9
|
-
<paymentMethodMask>
|
10
|
-
<include code="VISA-SSL"/>
|
11
|
-
<include code="MAESTRO-SSL"/>
|
12
|
-
<include code="ECMC-SSL"/>
|
13
|
-
</paymentMethodMask>
|
14
|
-
<shopper>
|
15
|
-
<shopperEmailAddress>vucij@example.com</shopperEmailAddress>
|
16
|
-
</shopper>
|
17
|
-
<billingAddress>
|
18
|
-
<address>
|
19
|
-
<firstName>Bell</firstName>
|
20
|
-
<lastName>Cruz</lastName>
|
21
|
-
<address1>HARMSEN GROUP DEANERY ROAD</address1>
|
22
|
-
<address2/>
|
23
|
-
<postalCode>BS1 5AH</postalCode>
|
24
|
-
<city>BRISTOL</city>
|
25
|
-
<countryCode>GB</countryCode>
|
26
|
-
</address>
|
27
|
-
</billingAddress>
|
28
|
-
</order>
|
29
|
-
</submit>
|
30
|
-
</paymentService>
|
@@ -1,11 +0,0 @@
|
|
1
|
-
<?xml version="1.0"?>
|
2
|
-
<!DOCTYPE paymentService PUBLIC '-//WorldPay/DTD WorldPay PaymentService v1/EN' 'http://dtd.worldpay.com/paymentService_v1.dtd'>
|
3
|
-
<paymentService version="1.4" merchantCode="MERCHME">
|
4
|
-
<modify>
|
5
|
-
<orderModification orderCode="1579644835">
|
6
|
-
<refund>
|
7
|
-
<amount value="2500" currencyCode="GBP" exponent="2"/>
|
8
|
-
</refund>
|
9
|
-
</orderModification>
|
10
|
-
</modify>
|
11
|
-
</paymentService>
|
@@ -1,163 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rails_helper"
|
4
|
-
|
5
|
-
module DefraRubyMocks
|
6
|
-
RSpec.describe "Worldpay", 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.worldpay_admin_code = "admincode1"
|
14
|
-
config.worldpay_mac_secret = "macsecret1"
|
15
|
-
config.worldpay_domain = "http://localhost:3000/defra_ruby_mocks"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context "#payments_service" do
|
20
|
-
let(:path) { "/defra_ruby_mocks/worldpay/payments-service" }
|
21
|
-
|
22
|
-
context "when a payment request is received" do
|
23
|
-
context "and the request is valid" do
|
24
|
-
let(:data) { File.read("spec/fixtures/payment_request_valid.xml") }
|
25
|
-
|
26
|
-
it "returns an XML response with a 200 code" do
|
27
|
-
post path, headers: { "RAW_POST_DATA" => data }
|
28
|
-
|
29
|
-
expect(response.media_type).to eq("application/xml")
|
30
|
-
expect(response.code).to eq("200")
|
31
|
-
expect(response.body).to be_xml
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "and the request is invalid" do
|
36
|
-
let(:data) { File.read("spec/fixtures/payment_request_invalid.xml") }
|
37
|
-
|
38
|
-
it "returns a response with a 500 code" do
|
39
|
-
post path, headers: { "RAW_POST_DATA" => data }
|
40
|
-
|
41
|
-
expect(response.code).to eq("500")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "when a refund request is received" do
|
47
|
-
context "and the request is valid" do
|
48
|
-
let(:data) { File.read("spec/fixtures/refund_request_valid.xml") }
|
49
|
-
|
50
|
-
it "returns an XML response with a 200 code" do
|
51
|
-
post path, headers: { "RAW_POST_DATA" => data }
|
52
|
-
|
53
|
-
expect(response.media_type).to eq("application/xml")
|
54
|
-
expect(response.code).to eq("200")
|
55
|
-
expect(response.body).to be_xml
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context "#dispatcher" do
|
62
|
-
let(:success_url) { "http://example.com/fo/12345/worldpay/success" }
|
63
|
-
let(:failure_url) { "http://example.com/fo/12345/worldpay/failure" }
|
64
|
-
let(:pending_url) { "http://example.com/fo/12345/worldpay/pending" }
|
65
|
-
let(:cancel_url) { "http://example.com/fo/12345/worldpay/cancel" }
|
66
|
-
let(:error_url) { "http://example.com/fo/12345/worldpay/error" }
|
67
|
-
let(:response_url) { "#{success_url}?orderKey=admincode1^^987654&paymentStatus=#{status}&paymentAmount=10500&paymentCurrency=GBP&mac=0ba5271e1ed1b26f9bb428ef7fb536a4&source=WP" }
|
68
|
-
let(:path) do
|
69
|
-
root = "/defra_ruby_mocks/worldpay/dispatcher"
|
70
|
-
escaped_success = CGI.escape(success_url)
|
71
|
-
escaped_failure = CGI.escape(failure_url)
|
72
|
-
escaped_pending = CGI.escape(pending_url)
|
73
|
-
escaped_cancel = CGI.escape(cancel_url)
|
74
|
-
escaped_error = CGI.escape(error_url)
|
75
|
-
|
76
|
-
"#{root}?successURL=#{escaped_success}&failureURL=#{escaped_failure}&pendingURL=#{escaped_pending}&cancelURL=#{escaped_cancel}&errorURL=#{escaped_error}"
|
77
|
-
end
|
78
|
-
let(:service_response) do
|
79
|
-
double(
|
80
|
-
:response,
|
81
|
-
supplied_url: success_url,
|
82
|
-
url: response_url,
|
83
|
-
status: status,
|
84
|
-
separator: "?",
|
85
|
-
order_key: "admincode1",
|
86
|
-
mac: "e5bc7ce5dfe44d2000771ac2b157f0e9",
|
87
|
-
value: 154_00,
|
88
|
-
reference: "12345"
|
89
|
-
)
|
90
|
-
end
|
91
|
-
|
92
|
-
context "and the request is valid" do
|
93
|
-
before(:each) do
|
94
|
-
allow(WorldpayResponseService).to receive(:run)
|
95
|
-
.with(
|
96
|
-
success_url: success_url,
|
97
|
-
failure_url: failure_url,
|
98
|
-
pending_url: pending_url,
|
99
|
-
cancel_url: cancel_url,
|
100
|
-
error_url: error_url
|
101
|
-
) { service_response }
|
102
|
-
end
|
103
|
-
|
104
|
-
context "and a response is expected" do
|
105
|
-
let(:status) { "AUTHORISED" }
|
106
|
-
|
107
|
-
it "redirects the user with a 300 code" do
|
108
|
-
get path
|
109
|
-
|
110
|
-
expect(response).to redirect_to(response_url)
|
111
|
-
expect(response.code).to eq("302")
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "and a response is not expected" do
|
116
|
-
let(:status) { :STUCK }
|
117
|
-
|
118
|
-
it "renders the Worldpay stuck page" do
|
119
|
-
get path
|
120
|
-
|
121
|
-
expect(response).to render_template(:stuck)
|
122
|
-
expect(response.code).to eq("200")
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context "and the request is invalid" do
|
128
|
-
before(:each) { allow(WorldpayResponseService).to receive(:run).and_raise(MissingResourceError.new("foo")) }
|
129
|
-
|
130
|
-
context "because the success url is not in a recognised format" do
|
131
|
-
let(:success_url) { "http://example.com/forthewin" }
|
132
|
-
|
133
|
-
it "returns a response with a 500 code" do
|
134
|
-
get path
|
135
|
-
|
136
|
-
expect(response.code).to eq("500")
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
context "when mocks are disabled" do
|
144
|
-
before(:each) { DefraRubyMocks.configuration.enable = false }
|
145
|
-
|
146
|
-
context "#payments_service" do
|
147
|
-
let(:path) { "/defra_ruby_mocks/worldpay/payments-service" }
|
148
|
-
|
149
|
-
it "cannot load the page" do
|
150
|
-
expect { get path }.to raise_error(ActionController::RoutingError)
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
context "#dispatcher" do
|
155
|
-
let(:path) { "/defra_ruby_mocks/worldpay/dispatcher" }
|
156
|
-
|
157
|
-
it "cannot load the page" do
|
158
|
-
expect { get path }.to raise_error(ActionController::RoutingError)
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rails_helper"
|
4
|
-
|
5
|
-
module DefraRubyMocks
|
6
|
-
RSpec.describe WorldpayPaymentService do
|
7
|
-
describe ".run" do
|
8
|
-
after(:each) { Helpers::Configuration.reset_for_tests }
|
9
|
-
|
10
|
-
let(:merchant_code) { "MERCHME" }
|
11
|
-
let(:args) { { merchant_code: merchant_code, xml: xml } }
|
12
|
-
|
13
|
-
context "when the mocks config is missing a worldpay domain" do
|
14
|
-
let(:xml) { nil }
|
15
|
-
|
16
|
-
it "raises a 'InvalidConfigError'" do
|
17
|
-
expect { described_class.run(args) }.to raise_error InvalidConfigError
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "when the XML is valid" do
|
22
|
-
before(:each) do
|
23
|
-
DefraRubyMocks.configure do |config|
|
24
|
-
config.worldpay_domain = "http://localhost:3000/defra_ruby_mocks"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
let(:xml) { Nokogiri::XML(File.read("spec/fixtures/payment_request_valid.xml")) }
|
29
|
-
|
30
|
-
context "the result it returns" do
|
31
|
-
it "is a hash" do
|
32
|
-
expect(described_class.run(args)).to be_an_instance_of(Hash)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "contains 4 values" do
|
36
|
-
result = described_class.run(args).length
|
37
|
-
expect(result).to eq(4)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "has the merchant code passed in" do
|
41
|
-
result = described_class.run(args)[:merchant_code]
|
42
|
-
|
43
|
-
expect(result).to eq(merchant_code)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "has an order code extracted from the XML" do
|
47
|
-
result = described_class.run(args)[:order_code]
|
48
|
-
|
49
|
-
expect(result).to eq("1577726052")
|
50
|
-
end
|
51
|
-
|
52
|
-
context "has a generated ID which is" do
|
53
|
-
it "10 characters long" do
|
54
|
-
result = described_class.run(args)[:id]
|
55
|
-
|
56
|
-
expect(result.length).to eq(10)
|
57
|
-
end
|
58
|
-
|
59
|
-
it "only made up of the digits 0 to 9" do
|
60
|
-
result = described_class.run(args)[:id]
|
61
|
-
|
62
|
-
expect(result.scan(/\D/).empty?).to be_truthy
|
63
|
-
end
|
64
|
-
|
65
|
-
it "different each time" do
|
66
|
-
results = []
|
67
|
-
3.times do
|
68
|
-
results << described_class.run(args)[:id]
|
69
|
-
end
|
70
|
-
|
71
|
-
expect(results.uniq.length).to eq(results.length)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context "has a url" do
|
76
|
-
it "based on the configured domain, and extracted merchant and order codes" do
|
77
|
-
result = described_class.run(args)[:url]
|
78
|
-
|
79
|
-
expect(result).to eq("http://localhost:3000/defra_ruby_mocks/worldpay/dispatcher?OrderKey=MERCHME%5E1577726052")
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
context "when the XML is invalid" do
|
87
|
-
let(:xml) { Nokogiri::XML(File.read("spec/fixtures/payment_request_invalid.xml")) }
|
88
|
-
|
89
|
-
it "raises an error" do
|
90
|
-
expect { described_class.run(args) }.to raise_error StandardError
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rails_helper"
|
4
|
-
|
5
|
-
module DefraRubyMocks
|
6
|
-
RSpec.describe WorldpayRefundService do
|
7
|
-
describe ".run" do
|
8
|
-
|
9
|
-
let(:merchant_code) { "MERCHME" }
|
10
|
-
let(:args) { { merchant_code: merchant_code, xml: xml } }
|
11
|
-
|
12
|
-
context "when the XML is valid" do
|
13
|
-
|
14
|
-
let(:xml) { Nokogiri::XML(File.read("spec/fixtures/refund_request_valid.xml")) }
|
15
|
-
|
16
|
-
context "the result it returns" do
|
17
|
-
it "is a hash" do
|
18
|
-
expect(described_class.run(args)).to be_an_instance_of(Hash)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "contains 5 values" do
|
22
|
-
result = described_class.run(args).length
|
23
|
-
expect(result).to eq(5)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "has the merchant code passed in" do
|
27
|
-
result = described_class.run(args)[:merchant_code]
|
28
|
-
|
29
|
-
expect(result).to eq(merchant_code)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "has an order code extracted from the XML" do
|
33
|
-
result = described_class.run(args)[:order_code]
|
34
|
-
|
35
|
-
expect(result).to eq("1579644835")
|
36
|
-
end
|
37
|
-
|
38
|
-
it "has the refund value extracted from the XML" do
|
39
|
-
result = described_class.run(args)[:refund_value]
|
40
|
-
|
41
|
-
expect(result).to eq("2500")
|
42
|
-
end
|
43
|
-
|
44
|
-
it "has a currency code extracted from the XML" do
|
45
|
-
result = described_class.run(args)[:currency_code]
|
46
|
-
|
47
|
-
expect(result).to eq("GBP")
|
48
|
-
end
|
49
|
-
|
50
|
-
it "has the exponent extracted from the XML" do
|
51
|
-
result = described_class.run(args)[:exponent]
|
52
|
-
|
53
|
-
expect(result).to eq("2")
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
context "when the XML is invalid" do
|
60
|
-
let(:xml) { Nokogiri::XML(File.read("spec/fixtures/refund_request_invalid.xml")) }
|
61
|
-
|
62
|
-
it "raises an error" do
|
63
|
-
expect { described_class.run(args) }.to raise_error StandardError
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -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/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/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/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
|