This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
braintree_controller_spec.rb
221 lines (170 loc) · 6.9 KB
/
braintree_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require 'rails_helper'
describe Api::Payment::BraintreeController do
before do
allow(Page).to receive(:find){ page }
end
let(:page) { instance_double("Page") }
let(:action) { instance_double("Action", member_id: 79) }
describe "GET token" do
before do
allow(::Braintree::ClientToken).to receive(:generate){ '1234' }
get :token
end
it 'fetches token from braintree' do
expect(::Braintree::ClientToken).to have_received(:generate)
end
it 'renders json' do
expect(response.body).to eq( {token: '1234'}.to_json )
end
end
describe "POST transaction" do
let(:client) { PaymentProcessor::Clients::Braintree }
let(:params) do
{
payment_method_nonce: 'wqeuinv-50238-FIERN',
amount: '40.19',
user: { email: 'snake@hips.com', name: 'Snake Hips' },
currency: 'NZD',
page_id: '12'
}
end
let(:payment_options) do
{
nonce: params[:payment_method_nonce],
amount: params[:amount].to_f,
user: params[:user],
currency: params[:currency],
page_id: params[:page_id]
}
end
before :each do
request.accept = "application/json" # ask for json
end
describe 'successfully' do
describe 'with recurring: true' do
let(:subscription) { instance_double('Braintree::Subscription', id: 's1234')}
let(:builder){ instance_double('PaymentProcessor::Clients::Braintree::Subscription', action: action, success?: true, subscription_id: 's1234') }
before do
allow(client::Subscription).to receive(:make_subscription).and_return(builder)
post :transaction, params.merge(recurring: true)
end
it 'calls Subscription.make_subscription' do
expect(client::Subscription).to have_received(:make_subscription).with(payment_options)
end
it 'has status 200' do
expect(response.status).to eq 200
end
it 'responds with subscription_id in JSON' do
expect(response.body).to eq( { success: true, subscription_id: 's1234' }.to_json )
end
it 'sets the member cookie' do
expect(cookies.signed['member_id']).to eq action.member_id
end
end
describe 'without recurring' do
let(:transaction) { instance_double('Braintree::Transaction', id: 't1234')}
let(:builder){ instance_double('PaymentProcessor::Clients::Braintree::Transaction', action: action, success?: true, transaction_id: 't1234') }
before :each do
allow(client::Transaction).to receive(:make_transaction).and_return(builder)
post :transaction, params
end
it 'calls Transaction.make_transaction' do
expect(client::Transaction).to have_received(:make_transaction).with(payment_options)
end
it 'has status 200' do
expect(response.status).to eq 200
end
it 'responds with transaction_id in JSON' do
expect(response.body).to eq( { success: true, transaction_id: 't1234' }.to_json )
end
it 'sets the member cookie' do
expect(cookies.signed['member_id']).to eq action.member_id
end
end
end
describe 'unsuccessfully' do
let(:errors) { instance_double('PaymentProcessor::Clients::Braintree::ErrorProcessing', process: {my_error: 'foo'}) }
before :each do
allow(client::ErrorProcessing).to receive(:new).and_return(errors)
end
describe 'with recurring: true' do
let(:builder){ instance_double('PaymentProcessor::Clients::Braintree::Subscription', success?: false, error_container: {}) }
before do
allow(client::Subscription).to receive(:make_subscription).and_return(builder)
post :transaction, params.merge(recurring: true)
end
it 'calls Subscription.make_subscription' do
expect(client::Subscription).to have_received(:make_subscription).with(payment_options)
end
it 'calls the error processor' do
expect(client::ErrorProcessing).to have_received(:new)
expect(errors).to have_received(:process)
end
it 'has status 422' do
expect(response.status).to eq 422
end
it 'responds with the error messages' do
expect(response.body).to eq( { success: false, errors: {my_error: 'foo'} }.to_json )
end
it 'does not set the member cookie' do
expect(cookies.signed['member_id']).to eq nil
end
end
describe 'without recurring' do
let(:transaction) { instance_double('Braintree::Transaction', id: 't1234')}
let(:builder){ instance_double('PaymentProcessor::Clients::Braintree::Transaction', success?: false, error_container: {}) }
before :each do
allow(client::Transaction).to receive(:make_transaction).and_return(builder)
post :transaction, params
end
it 'calls Transaction.make_transaction' do
expect(client::Transaction).to have_received(:make_transaction).with(payment_options)
end
it 'calls the error processor' do
expect(client::ErrorProcessing).to have_received(:new)
expect(errors).to have_received(:process)
end
it 'has status 422' do
expect(response.status).to eq 422
end
it 'responds with the error messages' do
expect(response.body).to eq( { success: false, errors: {my_error: 'foo'} }.to_json )
end
it 'does not set the member cookie' do
expect(cookies.signed['member_id']).to eq nil
end
end
end
end
describe 'POST webhook' do
let(:supported_webhook) { Braintree::WebhookTesting.sample_notification(Braintree::WebhookNotification::Kind::SubscriptionChargedSuccessfully, 'test_id') }
let(:unsupported_webhook) { Braintree::WebhookTesting.sample_notification(Braintree::WebhookNotification::Kind::SubscriptionCanceled, 'test_id') }
before :each do
allow(PaymentProcessor::Clients::Braintree::WebhookHandler).to receive(:handle)
end
it 'parses a supported webhook and passes it to the Webhook handler' do
post :webhook, supported_webhook
expect(
PaymentProcessor::Clients::Braintree::WebhookHandler
).to have_received(:handle).with(
an_instance_of(Braintree::WebhookNotification)
)
end
it 'parse an unsupported_webhook and passes it to the Webhook handler' do
post :webhook, unsupported_webhook
expect(
PaymentProcessor::Clients::Braintree::WebhookHandler
).to have_received(:handle).with(
an_instance_of(Braintree::WebhookNotification)
)
end
it 'responds 200 to supported_webhook' do
post :webhook, supported_webhook
expect(response.status).to eq 200
end
it 'responds 200 to unsupported_webhook' do
post :webhook, unsupported_webhook
expect(response.status).to eq 200
end
end
end