defra_ruby_mocks 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/Rakefile +0 -2
- data/app/controllers/defra_ruby_mocks/worldpay_controller.rb +7 -1
- data/app/services/defra_ruby_mocks/worldpay_resource_service.rb +55 -0
- data/app/services/defra_ruby_mocks/worldpay_response_service.rb +41 -57
- data/app/views/defra_ruby_mocks/worldpay/stuck.html.erb +37 -0
- data/lib/defra_ruby_mocks/engine.rb +1 -1
- data/lib/defra_ruby_mocks/missing_resource_error.rb +9 -0
- data/lib/defra_ruby_mocks/version.rb +1 -1
- data/spec/requests/worldpay_spec.rb +36 -13
- data/spec/services/worldpay_resource_service_spec.rb +112 -0
- data/spec/services/worldpay_response_service_spec.rb +32 -63
- metadata +21 -5
- data/lib/defra_ruby_mocks/missing_registration_error.rb +0 -9
- data/spec/dummy/log/test.log +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fb698051adcf4eb4ef61921f8b493b181a9c731
|
4
|
+
data.tar.gz: 72d8f8e70614b1c8a7c9f95bbf65f7388e468a1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd5da27f28650a3a638c8bdd0a05095797fe65a5043737dba4891cf0bcef7451832c08122ca148c022e04b34f3314baac9e9f6605058961399d792491667ab19
|
7
|
+
data.tar.gz: 9cd9847c9b72135dd27fe2ef81f1132998dbe39291882020f447dd40195fc12804f6a2de9543e5c3ef98493cd4435837dcc88d4cd73c31299eaa7264c4d1b61a
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Defra Ruby Mocks
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.com/DEFRA/defra-ruby-mocks.svg?branch=master)](https://travis-ci.com/DEFRA/defra-ruby-mocks)
|
4
|
-
[![Maintainability](https://
|
5
|
-
[![
|
4
|
+
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_defra-ruby-mocks&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=DEFRA_defra-ruby-mocks)
|
5
|
+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_defra-ruby-mocks&metric=coverage)](https://sonarcloud.io/dashboard?id=DEFRA_defra-ruby-mocks)
|
6
6
|
[![security](https://hakiri.io/github/DEFRA/defra-ruby-mocks/master.svg)](https://hakiri.io/github/DEFRA/defra-ruby-mocks/master)
|
7
7
|
[![Licence](https://img.shields.io/badge/Licence-OGLv3-blue.svg)](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3)
|
8
8
|
|
@@ -130,6 +130,14 @@ If it does the engine will redirect back to the failure url instead of the succe
|
|
130
130
|
|
131
131
|
This allows us to test how the application handles both successful and unsucessful Worldpay payments.
|
132
132
|
|
133
|
+
##### Stuck payments
|
134
|
+
|
135
|
+
The engine has the ability to also mock Worldpay not redirecting back to the service. This is the equivalent of a registration getting 'stuck at Worldpay'. To have the mock not respond just ensure the registration's company name includes the word `stuck` (case doesn't matter).
|
136
|
+
|
137
|
+
If it does the engine will not redirect back to the service, but instead render a 'You're stuck!' page.
|
138
|
+
|
139
|
+
This allows us to test how the application handles Worldpay not returning after we redirect a user to them.
|
140
|
+
|
133
141
|
#### Refunds
|
134
142
|
|
135
143
|
Requesting a refund from Worldpay is a single step process.
|
data/Rakefile
CHANGED
@@ -25,7 +25,6 @@ Bundler::GemHelper.install_tasks
|
|
25
25
|
# This is wrapped to prevent an error when rake is called in environments where
|
26
26
|
# rspec may not be available, e.g. production. As such we don't need to handle
|
27
27
|
# the error.
|
28
|
-
# rubocop:disable Lint/SuppressedException
|
29
28
|
begin
|
30
29
|
require "rspec/core/rake_task"
|
31
30
|
|
@@ -35,4 +34,3 @@ begin
|
|
35
34
|
rescue LoadError
|
36
35
|
# no rspec available
|
37
36
|
end
|
38
|
-
# rubocop:enable Lint/SuppressedException
|
@@ -15,10 +15,16 @@ module DefraRubyMocks
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def dispatcher
|
18
|
-
|
18
|
+
@response = WorldpayResponseService.run(
|
19
19
|
success_url: params[:successURL],
|
20
20
|
failure_url: params[:failureURL]
|
21
21
|
)
|
22
|
+
|
23
|
+
if @response.status == :STUCK
|
24
|
+
render formats: :html, action: "stuck", layout: false
|
25
|
+
else
|
26
|
+
redirect_to @response.url
|
27
|
+
end
|
22
28
|
rescue StandardError
|
23
29
|
head 500
|
24
30
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRubyMocks
|
4
|
+
class WorldpayResourceService < BaseService
|
5
|
+
|
6
|
+
def run(reference:)
|
7
|
+
@reference = reference
|
8
|
+
|
9
|
+
raise(MissingResourceError, @reference) if resource.nil?
|
10
|
+
|
11
|
+
WorldpayResource.new(resource, order, company_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
WorldpayResource = Struct.new(:resource, :order, :company_name)
|
17
|
+
|
18
|
+
def resource
|
19
|
+
@_resource ||= locate_transient_registration || locate_completed_registration
|
20
|
+
end
|
21
|
+
|
22
|
+
def locate_transient_registration
|
23
|
+
"WasteCarriersEngine::TransientRegistration"
|
24
|
+
.constantize
|
25
|
+
.where(token: @reference)
|
26
|
+
.first
|
27
|
+
end
|
28
|
+
|
29
|
+
def locate_completed_registration
|
30
|
+
"WasteCarriersEngine::Registration"
|
31
|
+
.constantize
|
32
|
+
.where(reg_uuid: @reference)
|
33
|
+
.first
|
34
|
+
end
|
35
|
+
|
36
|
+
def locate_original_registration(reg_identifier)
|
37
|
+
"WasteCarriersEngine::Registration"
|
38
|
+
.constantize
|
39
|
+
.where(reg_identifier: reg_identifier)
|
40
|
+
.first
|
41
|
+
end
|
42
|
+
|
43
|
+
def order
|
44
|
+
@_order ||= resource.finance_details&.orders&.order_by(dateCreated: :desc)&.first
|
45
|
+
end
|
46
|
+
|
47
|
+
def company_name
|
48
|
+
if resource.class.to_s == "WasteCarriersEngine::OrderCopyCardsRegistration"
|
49
|
+
locate_original_registration(resource.reg_identifier).company_name.downcase
|
50
|
+
else
|
51
|
+
resource.company_name.downcase
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -5,14 +5,30 @@ module DefraRubyMocks
|
|
5
5
|
|
6
6
|
def run(success_url:, failure_url:)
|
7
7
|
parse_reference(success_url)
|
8
|
-
|
9
|
-
@order = last_order
|
8
|
+
@resource = WorldpayResourceService.run(reference: @reference)
|
10
9
|
|
11
|
-
|
10
|
+
generate_response(success_url, failure_url)
|
12
11
|
end
|
13
12
|
|
14
13
|
private
|
15
14
|
|
15
|
+
WorldpayResponse = Struct.new(:supplied_url, :separator, :order_key, :mac, :value, :status, :reference) do
|
16
|
+
def url
|
17
|
+
[supplied_url, separator, params].join
|
18
|
+
end
|
19
|
+
|
20
|
+
def params
|
21
|
+
[
|
22
|
+
"orderKey=#{order_key}",
|
23
|
+
"paymentStatus=#{status}",
|
24
|
+
"paymentAmount=#{value}",
|
25
|
+
"paymentCurrency=GBP",
|
26
|
+
"mac=#{mac}",
|
27
|
+
"source=WP"
|
28
|
+
].join("&")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
16
32
|
def parse_reference(url)
|
17
33
|
path = URI.parse(url).path
|
18
34
|
parts = path.split("/")
|
@@ -28,81 +44,49 @@ module DefraRubyMocks
|
|
28
44
|
end
|
29
45
|
end
|
30
46
|
|
31
|
-
def locate_registration
|
32
|
-
@registration = locate_transient_registration || locate_completed_registration
|
33
|
-
|
34
|
-
raise(MissingRegistrationError, @reference) if @registration.nil?
|
35
|
-
end
|
36
|
-
|
37
|
-
def locate_transient_registration
|
38
|
-
"WasteCarriersEngine::TransientRegistration"
|
39
|
-
.constantize
|
40
|
-
.where(token: @reference)
|
41
|
-
.first
|
42
|
-
end
|
43
|
-
|
44
|
-
def locate_completed_registration
|
45
|
-
"WasteCarriersEngine::Registration"
|
46
|
-
.constantize
|
47
|
-
.where(reg_uuid: @reference)
|
48
|
-
.first
|
49
|
-
end
|
50
|
-
|
51
|
-
def last_order
|
52
|
-
@registration.finance_details&.orders&.order_by(dateCreated: :desc)&.first
|
53
|
-
end
|
54
|
-
|
55
|
-
def reject_payment?
|
56
|
-
@registration.company_name.downcase.include?("reject")
|
57
|
-
end
|
58
|
-
|
59
47
|
def order_key
|
60
48
|
[
|
61
49
|
DefraRubyMocks.configuration.worldpay_admin_code,
|
62
50
|
DefraRubyMocks.configuration.worldpay_merchant_code,
|
63
|
-
@order.order_code
|
51
|
+
@resource.order.order_code
|
64
52
|
].join("^")
|
65
53
|
end
|
66
54
|
|
67
55
|
def order_value
|
68
|
-
@order.total_amount.to_s
|
56
|
+
@resource.order.total_amount.to_s
|
69
57
|
end
|
70
58
|
|
71
|
-
def
|
59
|
+
def payment_status
|
60
|
+
return :REFUSED if @resource.company_name.include?("reject")
|
61
|
+
return :STUCK if @resource.company_name.include?("stuck")
|
62
|
+
|
63
|
+
:AUTHORISED
|
64
|
+
end
|
65
|
+
|
66
|
+
def generate_mac(status)
|
72
67
|
data = [
|
73
68
|
order_key,
|
74
69
|
order_value,
|
75
70
|
"GBP",
|
76
|
-
|
71
|
+
status,
|
77
72
|
DefraRubyMocks.configuration.worldpay_mac_secret
|
78
73
|
]
|
79
74
|
|
80
75
|
Digest::MD5.hexdigest(data.join).to_s
|
81
76
|
end
|
82
77
|
|
83
|
-
def
|
84
|
-
|
85
|
-
"orderKey=#{order_key}",
|
86
|
-
"paymentStatus=#{status}",
|
87
|
-
"paymentAmount=#{order_value}",
|
88
|
-
"paymentCurrency=GBP",
|
89
|
-
"mac=#{generate_mac}",
|
90
|
-
"source=WP"
|
91
|
-
].join("&")
|
92
|
-
end
|
78
|
+
def generate_response(success_url, failure_url)
|
79
|
+
status = payment_status
|
93
80
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
[url, separator, query_string(status)].join
|
81
|
+
WorldpayResponse.new(
|
82
|
+
status == :AUTHORISED ? success_url : failure_url,
|
83
|
+
@url_format == :new ? "?" : "&",
|
84
|
+
order_key,
|
85
|
+
generate_mac(status),
|
86
|
+
order_value,
|
87
|
+
status,
|
88
|
+
@reference
|
89
|
+
)
|
106
90
|
end
|
107
91
|
end
|
108
92
|
end
|
@@ -0,0 +1,37 @@
|
|
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>
|
@@ -59,30 +59,53 @@ module DefraRubyMocks
|
|
59
59
|
end
|
60
60
|
|
61
61
|
context "#dispatcher" do
|
62
|
-
let(:
|
63
|
-
let(:registration) { double(:registration, finance_details: finance_details, company_name: "What a waste") }
|
64
|
-
let(:finance_details) { double(:finance_details, orders: orders) }
|
65
|
-
let(:orders) { double(:orders, order_by: sorted_orders) }
|
66
|
-
let(:sorted_orders) { double(:sorted_orders, first: order) }
|
67
|
-
let(:order) { double(:order, order_code: "987654", total_amount: 105_00) }
|
68
|
-
|
62
|
+
let(:response_url) { "#{success_url}?orderKey=admincode1^^987654&paymentStatus=#{status}&paymentAmount=10500&paymentCurrency=GBP&mac=0ba5271e1ed1b26f9bb428ef7fb536a4&source=WP" }
|
69
63
|
let(:path) { "/defra_ruby_mocks/worldpay/dispatcher?successURL=#{CGI.escape(success_url)}" }
|
64
|
+
let(:service_response) do
|
65
|
+
double(
|
66
|
+
:response,
|
67
|
+
supplied_url: success_url,
|
68
|
+
url: response_url,
|
69
|
+
status: status,
|
70
|
+
separator: "?",
|
71
|
+
order_key: "admincode1",
|
72
|
+
mac: "e5bc7ce5dfe44d2000771ac2b157f0e9",
|
73
|
+
value: 154_00,
|
74
|
+
reference: "12345"
|
75
|
+
)
|
76
|
+
end
|
70
77
|
|
71
78
|
context "and the request is valid" do
|
72
|
-
|
79
|
+
before(:each) { allow(WorldpayResponseService).to receive(:run) { service_response } }
|
80
|
+
|
73
81
|
let(:success_url) { "http://example.com/fo/12345/worldpay/success" }
|
74
82
|
|
75
|
-
|
76
|
-
|
83
|
+
context "and a response is expected" do
|
84
|
+
let(:status) { "AUTHORISED" }
|
77
85
|
|
78
|
-
|
86
|
+
it "redirects the user with a 300 code" do
|
87
|
+
get path
|
79
88
|
|
80
|
-
|
81
|
-
|
89
|
+
expect(response).to redirect_to(response_url)
|
90
|
+
expect(response.code).to eq("302")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "and a response is not expected" do
|
95
|
+
let(:status) { :STUCK }
|
96
|
+
|
97
|
+
it "renders the Worldpay stuck page" do
|
98
|
+
get path
|
99
|
+
|
100
|
+
expect(response).to render_template(:stuck)
|
101
|
+
expect(response.code).to eq("200")
|
102
|
+
end
|
82
103
|
end
|
83
104
|
end
|
84
105
|
|
85
106
|
context "and the request is invalid" do
|
107
|
+
before(:each) { allow(WorldpayResponseService).to receive(:run).and_raise(MissingResourceError.new("foo")) }
|
108
|
+
|
86
109
|
context "because the success url is not in a recognised format" do
|
87
110
|
let(:success_url) { "http://example.com/forthewin" }
|
88
111
|
|
@@ -0,0 +1,112 @@
|
|
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
|
+
end
|
48
|
+
|
49
|
+
context "when the resource is a Registration" do
|
50
|
+
let(:transient_relation) { double(:relation, first: nil) }
|
51
|
+
let(:registration_relation) { double(:relation, first: resource) }
|
52
|
+
|
53
|
+
it "will search transient registrations first, then registrations" do
|
54
|
+
described_class.run(args)
|
55
|
+
|
56
|
+
expect(::WasteCarriersEngine::TransientRegistration).to have_received(:where).with(token: reference)
|
57
|
+
|
58
|
+
expect(::WasteCarriersEngine::Registration).to have_received(:where).with(reg_uuid: reference)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns an object with the matching resource" do
|
62
|
+
expect(described_class.run(args).resource).to eq(resource)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns an object with the expected order" do
|
66
|
+
expect(described_class.run(args).order).to eq(order)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns an object with the expected company name" do
|
70
|
+
expect(described_class.run(args).company_name).to eq(company_name.downcase)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when the resource is a OrderCopyCardsRegistration" do
|
75
|
+
before do
|
76
|
+
# Because we do not copy the company name to
|
77
|
+
# `OrderCopyCardsRegistration` instances when we create them in WCR
|
78
|
+
# we need to locate the orignal registration they are based on. We
|
79
|
+
# determine in the class if the 'resource' is an instance of one by
|
80
|
+
# comparing the result of resource.class.to_s to
|
81
|
+
# "WasteCarriersEngine::OrderCopyCardsRegistration". The problem is
|
82
|
+
# when testing 'resource' is actually an instance of
|
83
|
+
# `RSpec::Mocks::Double`! So we subvert the call to class on
|
84
|
+
# RSpec::Mocks::Double to return "WasteCarriersEngine::OrderCopyCardsRegistration"
|
85
|
+
# just in this spec. We can then test that the service does indeed
|
86
|
+
# locate the original registration for a company name
|
87
|
+
allow_any_instance_of(RSpec::Mocks::Double).to receive(:class)
|
88
|
+
.and_return("WasteCarriersEngine::OrderCopyCardsRegistration")
|
89
|
+
end
|
90
|
+
|
91
|
+
let(:copy_card_resource) { double(:resource, finance_details: finance_details, reg_identifier: "CBDU123") }
|
92
|
+
let(:transient_relation) { double(:relation, first: copy_card_resource) }
|
93
|
+
let(:registration_relation) { double(:relation, first: resource) }
|
94
|
+
|
95
|
+
it "locates the original registration to grab the company name" do
|
96
|
+
expect(described_class.run(args).company_name).to eq(company_name.downcase)
|
97
|
+
|
98
|
+
expect(::WasteCarriersEngine::Registration).to have_received(:where).with(reg_identifier: "CBDU123")
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "when the resource does not exist" do
|
103
|
+
let(:transient_relation) { double(:relation, first: nil) }
|
104
|
+
let(:registration_relation) { double(:relation, first: nil) }
|
105
|
+
|
106
|
+
it "causes an error" do
|
107
|
+
expect { described_class.run(args) }.to raise_error MissingResourceError
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -3,6 +3,7 @@
|
|
3
3
|
require "rails_helper"
|
4
4
|
|
5
5
|
module DefraRubyMocks
|
6
|
+
|
6
7
|
RSpec.describe WorldpayResponseService do
|
7
8
|
before(:each) do
|
8
9
|
Helpers::Configuration.prep_for_tests
|
@@ -12,10 +13,11 @@ module DefraRubyMocks
|
|
12
13
|
config.worldpay_mac_secret = mac_secret
|
13
14
|
end
|
14
15
|
|
15
|
-
allow(
|
16
|
-
allow(::WasteCarriersEngine::Registration).to receive(:where) { relation }
|
16
|
+
allow(WorldpayResourceService).to receive(:run) { resource }
|
17
17
|
end
|
18
18
|
|
19
|
+
let(:resource) { double(:resource, order: order, company_name: company_name.downcase) }
|
20
|
+
|
19
21
|
let(:admin_code) { "admincode1" }
|
20
22
|
let(:merchant_code) { "merchantcode1" }
|
21
23
|
let(:mac_secret) { "mac1" }
|
@@ -23,12 +25,9 @@ module DefraRubyMocks
|
|
23
25
|
let(:order_code) { "54321" }
|
24
26
|
let(:order_key) { "#{admin_code}^#{merchant_code}^#{order_code}" }
|
25
27
|
let(:order_value) { 105_00 }
|
28
|
+
let(:payment_status) { :AUTHORISED }
|
26
29
|
let(:company_name) { "Pay for the thing" }
|
27
30
|
|
28
|
-
let(:registration) { double(:registration, finance_details: finance_details, company_name: company_name) }
|
29
|
-
let(:finance_details) { double(:finance_details, orders: orders) }
|
30
|
-
let(:orders) { double(:orders, order_by: sorted_orders) }
|
31
|
-
let(:sorted_orders) { double(:sorted_orders, first: order) }
|
32
31
|
let(:order) { double(:order, order_code: order_code, total_amount: order_value) }
|
33
32
|
|
34
33
|
let(:mac) do
|
@@ -36,15 +35,13 @@ module DefraRubyMocks
|
|
36
35
|
order_key,
|
37
36
|
order_value,
|
38
37
|
"GBP",
|
39
|
-
|
38
|
+
payment_status,
|
40
39
|
mac_secret
|
41
40
|
]
|
42
41
|
|
43
42
|
Digest::MD5.hexdigest(data.join).to_s
|
44
43
|
end
|
45
44
|
|
46
|
-
let(:payment_status) { "AUTHORISED" }
|
47
|
-
|
48
45
|
let(:query_string) do
|
49
46
|
[
|
50
47
|
"orderKey=#{order_key}",
|
@@ -67,69 +64,47 @@ module DefraRubyMocks
|
|
67
64
|
let(:relation) { double(:relation, first: registration) }
|
68
65
|
|
69
66
|
it "can extract the reference from the `success_url`" do
|
70
|
-
described_class.run(args)
|
71
|
-
|
72
|
-
expect(::WasteCarriersEngine::TransientRegistration).to have_received(:where).with(token: reference)
|
67
|
+
expect(described_class.run(args).reference).to eq(reference)
|
73
68
|
end
|
74
69
|
|
75
70
|
it "can generate a valid order key" do
|
76
|
-
|
77
|
-
|
78
|
-
expect(params["orderKey"]).to eq(order_key)
|
71
|
+
expect(described_class.run(args).order_key).to eq(order_key)
|
79
72
|
end
|
80
73
|
|
81
74
|
it "can generate a valid mac" do
|
82
|
-
|
83
|
-
|
84
|
-
expect(params["mac"]).to eq(mac)
|
75
|
+
expect(described_class.run(args).mac).to eq(mac)
|
85
76
|
end
|
86
77
|
|
87
78
|
context "and is for a successful payment" do
|
88
79
|
it "returns a url in the expected format" do
|
89
|
-
|
80
|
+
expected_response_url = "#{success_url}?#{query_string}"
|
90
81
|
|
91
|
-
expect(described_class.run(args)).to eq(
|
82
|
+
expect(described_class.run(args).url).to eq(expected_response_url)
|
92
83
|
end
|
93
84
|
end
|
94
85
|
|
95
86
|
context "and is for a rejected payment" do
|
96
|
-
let(:payment_status) {
|
87
|
+
let(:payment_status) { :REFUSED }
|
97
88
|
let(:company_name) { "Reject for the thing" }
|
98
89
|
|
99
90
|
it "returns a url in the expected format" do
|
100
|
-
|
91
|
+
expected_response_url = "#{failure_url}?#{query_string}"
|
101
92
|
|
102
|
-
expect(described_class.run(args)).to eq(
|
93
|
+
expect(described_class.run(args).url).to eq(expected_response_url)
|
103
94
|
end
|
104
95
|
end
|
105
|
-
end
|
106
|
-
|
107
|
-
context "but the registration does not exist" do
|
108
|
-
let(:relation) { double(:relation, first: nil) }
|
109
96
|
|
110
|
-
|
111
|
-
|
112
|
-
end
|
113
|
-
end
|
97
|
+
context "and is for a stuck payment" do
|
98
|
+
let(:company_name) { "Give me a stuck thing" }
|
114
99
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
it "causes an error" do
|
119
|
-
expect { described_class.run(args) }.to raise_error MissingRegistrationError
|
100
|
+
it "returns a status of :STUCK" do
|
101
|
+
expect(described_class.run(args).status).to eq(:STUCK)
|
102
|
+
end
|
120
103
|
end
|
121
104
|
end
|
122
105
|
end
|
123
106
|
|
124
107
|
context "when the request comes from the waste-carriers-frontend" do
|
125
|
-
before do
|
126
|
-
# The service will search transient registrations for a match first
|
127
|
-
# before then searching for the registration. Hence we need to stub
|
128
|
-
# `locate_transient_registration()` to allow the service to then
|
129
|
-
# call `locate_registration()`
|
130
|
-
allow_any_instance_of(described_class).to receive(:locate_transient_registration).and_return(nil)
|
131
|
-
end
|
132
|
-
|
133
108
|
let(:success_url) { "http://example.com/your-registration/#{reference}/worldpay/success/54321/NEWREG?locale=en" }
|
134
109
|
let(:failure_url) { "http://example.com/your-registration/#{reference}/worldpay/failure/54321/NEWREG?locale=en" }
|
135
110
|
|
@@ -137,48 +112,42 @@ module DefraRubyMocks
|
|
137
112
|
let(:relation) { double(:relation, first: registration) }
|
138
113
|
|
139
114
|
it "can extract the reference from the `success_url`" do
|
140
|
-
described_class.run(args)
|
141
|
-
|
142
|
-
expect(::WasteCarriersEngine::Registration).to have_received(:where).with(reg_uuid: reference)
|
115
|
+
expect(described_class.run(args).reference).to eq(reference)
|
143
116
|
end
|
144
117
|
|
145
118
|
it "can generate a valid order key" do
|
146
|
-
|
147
|
-
|
148
|
-
expect(params["orderKey"]).to eq(order_key)
|
119
|
+
expect(described_class.run(args).order_key).to eq(order_key)
|
149
120
|
end
|
150
121
|
|
151
122
|
it "can generate a valid mac" do
|
152
|
-
|
153
|
-
|
154
|
-
expect(params["mac"]).to eq(mac)
|
123
|
+
expect(described_class.run(args).mac).to eq(mac)
|
155
124
|
end
|
156
125
|
|
157
126
|
context "and is for a successful payment" do
|
158
127
|
it "returns a url in the expected format" do
|
159
|
-
|
128
|
+
expected_response_url = "#{success_url}&#{query_string}"
|
160
129
|
|
161
|
-
expect(described_class.run(args)).to eq(
|
130
|
+
expect(described_class.run(args).url).to eq(expected_response_url)
|
162
131
|
end
|
163
132
|
end
|
164
133
|
|
165
134
|
context "and is for a rejected payment" do
|
166
|
-
let(:payment_status) {
|
135
|
+
let(:payment_status) { :REFUSED }
|
167
136
|
let(:company_name) { "Reject for the thing" }
|
168
137
|
|
169
138
|
it "returns a url in the expected format" do
|
170
|
-
|
139
|
+
expected_response_url = "#{failure_url}&#{query_string}"
|
171
140
|
|
172
|
-
expect(described_class.run(args)).to eq(
|
141
|
+
expect(described_class.run(args).url).to eq(expected_response_url)
|
173
142
|
end
|
174
143
|
end
|
175
|
-
end
|
176
144
|
|
177
|
-
|
178
|
-
|
145
|
+
context "and is for a stuck payment" do
|
146
|
+
let(:company_name) { "Give me a stuck thing" }
|
179
147
|
|
180
|
-
|
181
|
-
|
148
|
+
it "returns a status of :STUCK" do
|
149
|
+
expect(described_class.run(args).status).to eq(:STUCK)
|
150
|
+
end
|
182
151
|
end
|
183
152
|
end
|
184
153
|
end
|
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: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Defra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 4.2.11.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sprockets
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.7.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.7.2
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: nokogiri
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,17 +141,19 @@ files:
|
|
127
141
|
- app/services/defra_ruby_mocks/worldpay_payment_service.rb
|
128
142
|
- app/services/defra_ruby_mocks/worldpay_refund_service.rb
|
129
143
|
- app/services/defra_ruby_mocks/worldpay_request_handler_service.rb
|
144
|
+
- app/services/defra_ruby_mocks/worldpay_resource_service.rb
|
130
145
|
- app/services/defra_ruby_mocks/worldpay_response_service.rb
|
131
146
|
- app/views/defra_ruby_mocks/company/not_found.json.erb
|
132
147
|
- app/views/defra_ruby_mocks/company/show.json.erb
|
133
148
|
- app/views/defra_ruby_mocks/worldpay/payment_request.xml.erb
|
134
149
|
- app/views/defra_ruby_mocks/worldpay/refund_request.xml.erb
|
150
|
+
- app/views/defra_ruby_mocks/worldpay/stuck.html.erb
|
135
151
|
- config/routes.rb
|
136
152
|
- lib/defra_ruby_mocks.rb
|
137
153
|
- lib/defra_ruby_mocks/configuration.rb
|
138
154
|
- lib/defra_ruby_mocks/engine.rb
|
139
155
|
- lib/defra_ruby_mocks/invalid_config_error.rb
|
140
|
-
- lib/defra_ruby_mocks/
|
156
|
+
- lib/defra_ruby_mocks/missing_resource_error.rb
|
141
157
|
- lib/defra_ruby_mocks/unrecognised_worldpay_request_error.rb
|
142
158
|
- lib/defra_ruby_mocks/version.rb
|
143
159
|
- lib/tasks/changelog.rake
|
@@ -173,7 +189,6 @@ files:
|
|
173
189
|
- spec/dummy/config/locales/en.yml
|
174
190
|
- spec/dummy/config/routes.rb
|
175
191
|
- spec/dummy/config/secrets.yml
|
176
|
-
- spec/dummy/log/test.log
|
177
192
|
- spec/dummy/public/404.html
|
178
193
|
- spec/dummy/public/422.html
|
179
194
|
- spec/dummy/public/500.html
|
@@ -191,6 +206,7 @@ files:
|
|
191
206
|
- spec/services/worldpay_payment_service_spec.rb
|
192
207
|
- spec/services/worldpay_refund_service_spec.rb
|
193
208
|
- spec/services/worldpay_request_handler_service_spec.rb
|
209
|
+
- spec/services/worldpay_resource_service_spec.rb
|
194
210
|
- spec/services/worldpay_response_service_spec.rb
|
195
211
|
- spec/spec_helper.rb
|
196
212
|
- spec/support/helpers/configuration.rb
|
@@ -258,7 +274,6 @@ test_files:
|
|
258
274
|
- spec/dummy/public/422.html
|
259
275
|
- spec/dummy/public/500.html
|
260
276
|
- spec/dummy/public/404.html
|
261
|
-
- spec/dummy/log/test.log
|
262
277
|
- spec/dummy/README.rdoc
|
263
278
|
- spec/defra_ruby_mocks_spec.rb
|
264
279
|
- spec/requests/company_spec.rb
|
@@ -277,6 +292,7 @@ test_files:
|
|
277
292
|
- spec/fixtures/refund_request_invalid.xml
|
278
293
|
- spec/rails_helper.rb
|
279
294
|
- spec/services/worldpay_response_service_spec.rb
|
295
|
+
- spec/services/worldpay_resource_service_spec.rb
|
280
296
|
- spec/services/companies_house_service_spec.rb
|
281
297
|
- spec/services/worldpay_payment_service_spec.rb
|
282
298
|
- spec/services/worldpay_refund_service_spec.rb
|
data/spec/dummy/log/test.log
DELETED
File without changes
|