gocardless_pro 2.7.0 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,5 +4,5 @@ end
4
4
 
5
5
  module GoCardlessPro
6
6
  # Current version of the GC gem
7
- VERSION = '2.7.0'.freeze
7
+ VERSION = '2.8.0'.freeze
8
8
  end
@@ -0,0 +1,206 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Resources::MandateImportEntry do
4
+ let(:client) do
5
+ GoCardlessPro::Client.new(
6
+ access_token: 'SECRET_TOKEN'
7
+ )
8
+ end
9
+
10
+ let(:response_headers) { { 'Content-Type' => 'application/json' } }
11
+
12
+ describe '#create' do
13
+ subject(:post_create_response) { client.mandate_import_entries.create(params: new_resource) }
14
+ context 'with a valid request' do
15
+ let(:new_resource) do
16
+ {
17
+
18
+ 'created_at' => 'created_at-input',
19
+ 'links' => 'links-input',
20
+ 'record_identifier' => 'record_identifier-input',
21
+ }
22
+ end
23
+
24
+ before do
25
+ stub_request(:post, %r{.*api.gocardless.com/mandate_import_entries}).
26
+ with(
27
+ body: {
28
+ 'mandate_import_entries' => {
29
+
30
+ 'created_at' => 'created_at-input',
31
+ 'links' => 'links-input',
32
+ 'record_identifier' => 'record_identifier-input',
33
+ },
34
+ }
35
+ ).
36
+ to_return(
37
+ body: {
38
+ 'mandate_import_entries' =>
39
+
40
+ {
41
+
42
+ 'created_at' => 'created_at-input',
43
+ 'links' => 'links-input',
44
+ 'record_identifier' => 'record_identifier-input',
45
+ },
46
+
47
+ }.to_json,
48
+ headers: response_headers
49
+ )
50
+ end
51
+
52
+ it 'creates and returns the resource' do
53
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::MandateImportEntry)
54
+ end
55
+ end
56
+
57
+ context 'with a request that returns a validation error' do
58
+ let(:new_resource) { {} }
59
+
60
+ before do
61
+ stub_request(:post, %r{.*api.gocardless.com/mandate_import_entries}).to_return(
62
+ body: {
63
+ error: {
64
+ type: 'validation_failed',
65
+ code: 422,
66
+ errors: [
67
+ { message: 'test error message', field: 'test_field' },
68
+ ],
69
+ },
70
+ }.to_json,
71
+ headers: response_headers,
72
+ status: 422
73
+ )
74
+ end
75
+
76
+ it 'throws the correct error' do
77
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
78
+ end
79
+ end
80
+
81
+ context 'with a request that returns an idempotent creation conflict error' do
82
+ let(:id) { 'ID123' }
83
+
84
+ let(:new_resource) do
85
+ {
86
+
87
+ 'created_at' => 'created_at-input',
88
+ 'links' => 'links-input',
89
+ 'record_identifier' => 'record_identifier-input',
90
+ }
91
+ end
92
+
93
+ let!(:post_stub) do
94
+ stub_request(:post, %r{.*api.gocardless.com/mandate_import_entries}).to_return(
95
+ body: {
96
+ error: {
97
+ type: 'invalid_state',
98
+ code: 409,
99
+ errors: [
100
+ {
101
+ message: 'A resource has already been created with this idempotency key',
102
+ reason: 'idempotent_creation_conflict',
103
+ links: {
104
+ conflicting_resource_id: id,
105
+ },
106
+ },
107
+ ],
108
+ },
109
+ }.to_json,
110
+ headers: response_headers,
111
+ status: 409
112
+ )
113
+ end
114
+
115
+ it 'raises an InvalidStateError' do
116
+ expect { post_create_response }.to raise_error(GoCardlessPro::InvalidStateError)
117
+ expect(post_stub).to have_been_requested
118
+ end
119
+ end
120
+ end
121
+
122
+ describe '#list' do
123
+ describe 'with no filters' do
124
+ subject(:get_list_response) { client.mandate_import_entries.list }
125
+
126
+ before do
127
+ stub_request(:get, %r{.*api.gocardless.com/mandate_import_entries}).to_return(
128
+ body: {
129
+ 'mandate_import_entries' => [{
130
+
131
+ 'created_at' => 'created_at-input',
132
+ 'links' => 'links-input',
133
+ 'record_identifier' => 'record_identifier-input',
134
+ }],
135
+ meta: {
136
+ cursors: {
137
+ before: nil,
138
+ after: 'ABC123',
139
+ },
140
+ },
141
+ }.to_json,
142
+ headers: response_headers
143
+ )
144
+ end
145
+
146
+ it 'wraps each item in the resource class' do
147
+ expect(get_list_response.records.map(&:class).uniq.first).to eq(GoCardlessPro::Resources::MandateImportEntry)
148
+
149
+ expect(get_list_response.records.first.created_at).to eq('created_at-input')
150
+
151
+ expect(get_list_response.records.first.record_identifier).to eq('record_identifier-input')
152
+ end
153
+
154
+ it 'exposes the cursors for before and after' do
155
+ expect(get_list_response.before).to eq(nil)
156
+ expect(get_list_response.after).to eq('ABC123')
157
+ end
158
+
159
+ specify { expect(get_list_response.api_response.headers).to eql('content-type' => 'application/json') }
160
+ end
161
+ end
162
+
163
+ describe '#all' do
164
+ let!(:first_response_stub) do
165
+ stub_request(:get, %r{.*api.gocardless.com/mandate_import_entries$}).to_return(
166
+ body: {
167
+ 'mandate_import_entries' => [{
168
+
169
+ 'created_at' => 'created_at-input',
170
+ 'links' => 'links-input',
171
+ 'record_identifier' => 'record_identifier-input',
172
+ }],
173
+ meta: {
174
+ cursors: { after: 'AB345' },
175
+ limit: 1,
176
+ },
177
+ }.to_json,
178
+ headers: response_headers
179
+ )
180
+ end
181
+
182
+ let!(:second_response_stub) do
183
+ stub_request(:get, %r{.*api.gocardless.com/mandate_import_entries\?after=AB345}).to_return(
184
+ body: {
185
+ 'mandate_import_entries' => [{
186
+
187
+ 'created_at' => 'created_at-input',
188
+ 'links' => 'links-input',
189
+ 'record_identifier' => 'record_identifier-input',
190
+ }],
191
+ meta: {
192
+ limit: 2,
193
+ cursors: {},
194
+ },
195
+ }.to_json,
196
+ headers: response_headers
197
+ )
198
+ end
199
+
200
+ it 'automatically makes the extra requests' do
201
+ expect(client.mandate_import_entries.all.to_a.length).to eq(2)
202
+ expect(first_response_stub).to have_been_requested
203
+ expect(second_response_stub).to have_been_requested
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,340 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Resources::MandateImport do
4
+ let(:client) do
5
+ GoCardlessPro::Client.new(
6
+ access_token: 'SECRET_TOKEN'
7
+ )
8
+ end
9
+
10
+ let(:response_headers) { { 'Content-Type' => 'application/json' } }
11
+
12
+ describe '#create' do
13
+ subject(:post_create_response) { client.mandate_imports.create(params: new_resource) }
14
+ context 'with a valid request' do
15
+ let(:new_resource) do
16
+ {
17
+
18
+ 'created_at' => 'created_at-input',
19
+ 'id' => 'id-input',
20
+ 'scheme' => 'scheme-input',
21
+ 'status' => 'status-input',
22
+ }
23
+ end
24
+
25
+ before do
26
+ stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).
27
+ with(
28
+ body: {
29
+ 'mandate_imports' => {
30
+
31
+ 'created_at' => 'created_at-input',
32
+ 'id' => 'id-input',
33
+ 'scheme' => 'scheme-input',
34
+ 'status' => 'status-input',
35
+ },
36
+ }
37
+ ).
38
+ to_return(
39
+ body: {
40
+ 'mandate_imports' =>
41
+
42
+ {
43
+
44
+ 'created_at' => 'created_at-input',
45
+ 'id' => 'id-input',
46
+ 'scheme' => 'scheme-input',
47
+ 'status' => 'status-input',
48
+ },
49
+
50
+ }.to_json,
51
+ headers: response_headers
52
+ )
53
+ end
54
+
55
+ it 'creates and returns the resource' do
56
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::MandateImport)
57
+ end
58
+ end
59
+
60
+ context 'with a request that returns a validation error' do
61
+ let(:new_resource) { {} }
62
+
63
+ before do
64
+ stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).to_return(
65
+ body: {
66
+ error: {
67
+ type: 'validation_failed',
68
+ code: 422,
69
+ errors: [
70
+ { message: 'test error message', field: 'test_field' },
71
+ ],
72
+ },
73
+ }.to_json,
74
+ headers: response_headers,
75
+ status: 422
76
+ )
77
+ end
78
+
79
+ it 'throws the correct error' do
80
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
81
+ end
82
+ end
83
+
84
+ context 'with a request that returns an idempotent creation conflict error' do
85
+ let(:id) { 'ID123' }
86
+
87
+ let(:new_resource) do
88
+ {
89
+
90
+ 'created_at' => 'created_at-input',
91
+ 'id' => 'id-input',
92
+ 'scheme' => 'scheme-input',
93
+ 'status' => 'status-input',
94
+ }
95
+ end
96
+
97
+ let!(:post_stub) do
98
+ stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).to_return(
99
+ body: {
100
+ error: {
101
+ type: 'invalid_state',
102
+ code: 409,
103
+ errors: [
104
+ {
105
+ message: 'A resource has already been created with this idempotency key',
106
+ reason: 'idempotent_creation_conflict',
107
+ links: {
108
+ conflicting_resource_id: id,
109
+ },
110
+ },
111
+ ],
112
+ },
113
+ }.to_json,
114
+ headers: response_headers,
115
+ status: 409
116
+ )
117
+ end
118
+
119
+ let!(:get_stub) do
120
+ stub_url = "/mandate_imports/#{id}"
121
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
122
+ to_return(
123
+ body: {
124
+ 'mandate_imports' => {
125
+
126
+ 'created_at' => 'created_at-input',
127
+ 'id' => 'id-input',
128
+ 'scheme' => 'scheme-input',
129
+ 'status' => 'status-input',
130
+ },
131
+ }.to_json,
132
+ headers: response_headers
133
+ )
134
+ end
135
+
136
+ it 'fetches the already-created resource' do
137
+ post_create_response
138
+ expect(post_stub).to have_been_requested
139
+ expect(get_stub).to have_been_requested
140
+ end
141
+ end
142
+ end
143
+
144
+ describe '#get' do
145
+ let(:id) { 'ID123' }
146
+
147
+ subject(:get_response) { client.mandate_imports.get(id) }
148
+
149
+ context 'passing in a custom header' do
150
+ let!(:stub) do
151
+ stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
152
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
153
+ with(headers: { 'Foo' => 'Bar' }).
154
+ to_return(
155
+ body: {
156
+ 'mandate_imports' => {
157
+
158
+ 'created_at' => 'created_at-input',
159
+ 'id' => 'id-input',
160
+ 'scheme' => 'scheme-input',
161
+ 'status' => 'status-input',
162
+ },
163
+ }.to_json,
164
+ headers: response_headers
165
+ )
166
+ end
167
+
168
+ subject(:get_response) do
169
+ client.mandate_imports.get(id, headers: {
170
+ 'Foo' => 'Bar',
171
+ })
172
+ end
173
+
174
+ it 'includes the header' do
175
+ get_response
176
+ expect(stub).to have_been_requested
177
+ end
178
+ end
179
+
180
+ context 'when there is a mandate_import to return' do
181
+ before do
182
+ stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
183
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
184
+ body: {
185
+ 'mandate_imports' => {
186
+
187
+ 'created_at' => 'created_at-input',
188
+ 'id' => 'id-input',
189
+ 'scheme' => 'scheme-input',
190
+ 'status' => 'status-input',
191
+ },
192
+ }.to_json,
193
+ headers: response_headers
194
+ )
195
+ end
196
+
197
+ it 'wraps the response in a resource' do
198
+ expect(get_response).to be_a(GoCardlessPro::Resources::MandateImport)
199
+ end
200
+ end
201
+
202
+ context 'when nothing is returned' do
203
+ before do
204
+ stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
205
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
206
+ body: '',
207
+ headers: response_headers
208
+ )
209
+ end
210
+
211
+ it 'returns nil' do
212
+ expect(get_response).to be_nil
213
+ end
214
+ end
215
+
216
+ context "when an ID is specified which can't be included in a valid URI" do
217
+ let(:id) { '`' }
218
+
219
+ it "doesn't raise an error" do
220
+ expect { get_response }.to_not raise_error(/bad URI/)
221
+ end
222
+ end
223
+ end
224
+
225
+ describe '#submit' do
226
+ subject(:post_response) { client.mandate_imports.submit(resource_id) }
227
+
228
+ let(:resource_id) { 'ABC123' }
229
+
230
+ let!(:stub) do
231
+ # /mandate_imports/%v/actions/submit
232
+ stub_url = '/mandate_imports/:identity/actions/submit'.gsub(':identity', resource_id)
233
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
234
+ body: {
235
+ 'mandate_imports' => {
236
+
237
+ 'created_at' => 'created_at-input',
238
+ 'id' => 'id-input',
239
+ 'scheme' => 'scheme-input',
240
+ 'status' => 'status-input',
241
+ },
242
+ }.to_json,
243
+ headers: response_headers
244
+ )
245
+ end
246
+
247
+ it 'wraps the response and calls the right endpoint' do
248
+ expect(post_response).to be_a(GoCardlessPro::Resources::MandateImport)
249
+
250
+ expect(stub).to have_been_requested
251
+ end
252
+
253
+ context 'when the request needs a body and custom header' do
254
+ let(:body) { { foo: 'bar' } }
255
+ let(:headers) { { 'Foo' => 'Bar' } }
256
+ subject(:post_response) { client.mandate_imports.submit(resource_id, body, headers) }
257
+
258
+ let(:resource_id) { 'ABC123' }
259
+
260
+ let!(:stub) do
261
+ # /mandate_imports/%v/actions/submit
262
+ stub_url = '/mandate_imports/:identity/actions/submit'.gsub(':identity', resource_id)
263
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
264
+ with(
265
+ body: { foo: 'bar' },
266
+ headers: { 'Foo' => 'Bar' }
267
+ ).to_return(
268
+ body: {
269
+ 'mandate_imports' => {
270
+
271
+ 'created_at' => 'created_at-input',
272
+ 'id' => 'id-input',
273
+ 'scheme' => 'scheme-input',
274
+ 'status' => 'status-input',
275
+ },
276
+ }.to_json,
277
+ headers: response_headers
278
+ )
279
+ end
280
+ end
281
+ end
282
+
283
+ describe '#cancel' do
284
+ subject(:post_response) { client.mandate_imports.cancel(resource_id) }
285
+
286
+ let(:resource_id) { 'ABC123' }
287
+
288
+ let!(:stub) do
289
+ # /mandate_imports/%v/actions/cancel
290
+ stub_url = '/mandate_imports/:identity/actions/cancel'.gsub(':identity', resource_id)
291
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
292
+ body: {
293
+ 'mandate_imports' => {
294
+
295
+ 'created_at' => 'created_at-input',
296
+ 'id' => 'id-input',
297
+ 'scheme' => 'scheme-input',
298
+ 'status' => 'status-input',
299
+ },
300
+ }.to_json,
301
+ headers: response_headers
302
+ )
303
+ end
304
+
305
+ it 'wraps the response and calls the right endpoint' do
306
+ expect(post_response).to be_a(GoCardlessPro::Resources::MandateImport)
307
+
308
+ expect(stub).to have_been_requested
309
+ end
310
+
311
+ context 'when the request needs a body and custom header' do
312
+ let(:body) { { foo: 'bar' } }
313
+ let(:headers) { { 'Foo' => 'Bar' } }
314
+ subject(:post_response) { client.mandate_imports.cancel(resource_id, body, headers) }
315
+
316
+ let(:resource_id) { 'ABC123' }
317
+
318
+ let!(:stub) do
319
+ # /mandate_imports/%v/actions/cancel
320
+ stub_url = '/mandate_imports/:identity/actions/cancel'.gsub(':identity', resource_id)
321
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
322
+ with(
323
+ body: { foo: 'bar' },
324
+ headers: { 'Foo' => 'Bar' }
325
+ ).to_return(
326
+ body: {
327
+ 'mandate_imports' => {
328
+
329
+ 'created_at' => 'created_at-input',
330
+ 'id' => 'id-input',
331
+ 'scheme' => 'scheme-input',
332
+ 'status' => 'status-input',
333
+ },
334
+ }.to_json,
335
+ headers: response_headers
336
+ )
337
+ end
338
+ end
339
+ end
340
+ end