payload-api 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,762 @@
1
+ require "payload"
2
+ require "payload/arm/object"
3
+ require 'base64'
4
+
5
+
6
+ RSpec.describe Payload::ARMRequest do
7
+
8
+ describe "#select" do
9
+
10
+ let(:instance) { described_class.new }
11
+
12
+ context "when the user selects custom fields" do
13
+ it "selects the requested fields" do
14
+ instance.select(' name', 'age ')
15
+ expect(instance.instance_variable_get(:@filters)).to eq({ "fields" => "name,age" })
16
+ instance.select('count(id)', 'sum(amount)')
17
+ expect(instance.instance_variable_get(:@filters)).to eq({ "fields" => "count(id),sum(amount)" })
18
+ end
19
+
20
+ it "builds the appropriate select request" do
21
+
22
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
23
+
24
+ Payload::api_key = 'test_key'
25
+ instance.instance_variable_set(:@cls, Payload::Customer)
26
+
27
+ expect(instance).to receive(:_execute_request) do |http, request|
28
+ expect(request.method).to eq("GET")
29
+ expect(http.address).to eq("api.payload.co")
30
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
31
+ expect(request.path).to eq("/customers?fields=name%2Cage")
32
+
33
+ class MockResponse
34
+ def initialize
35
+ end
36
+
37
+ def code
38
+ '200'
39
+ end
40
+
41
+ def body
42
+ '{
43
+ "object": "list",
44
+ "values": [
45
+ {"id": "' + $test_id + '", "object": "customer"}
46
+ ]
47
+ }'
48
+ end
49
+ end
50
+
51
+ MockResponse.new
52
+ end
53
+
54
+ custs = instance.select('name', 'age').all()
55
+
56
+ expect(custs.size).to eq(1)
57
+ expect(custs[0].object).to eq("customer")
58
+ expect(custs[0].session).to eq(instance.instance_variable_get(:@session))
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "#filter_by" do
64
+
65
+ let(:instance) { described_class.new }
66
+
67
+ context "when the user filters fields" do
68
+ it "sets the given data as filters" do
69
+ class TestObject < Payload::ARMObject
70
+ @poly = { "type" => "test" }
71
+ end
72
+ instance.instance_variable_set(:@cls, TestObject)
73
+
74
+ instance.filter_by(name: "John", age: 30)
75
+ expect(instance.instance_variable_get(:@filters)).to eq({name: "John", age: 30, "type" => "test"})
76
+ end
77
+
78
+ it "builds the appropriate filter_by request" do
79
+ Payload::api_key = 'test_key'
80
+ instance.instance_variable_set(:@cls, Payload::Customer)
81
+
82
+ expect(instance).to receive(:_execute_request) do |http, request|
83
+ expect(request.method).to eq("GET")
84
+ expect(http.address).to eq("api.payload.co")
85
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
86
+ expect(request.path).to eq("/customers?name=John&age=30")
87
+
88
+ class MockResponse
89
+ def initialize
90
+ end
91
+
92
+ def code
93
+ '200'
94
+ end
95
+
96
+ def body
97
+ '{
98
+ "object": "list",
99
+ "values": [
100
+ {
101
+ "object": "customer"
102
+ }
103
+ ]
104
+ }'
105
+ end
106
+ end
107
+
108
+ MockResponse.new
109
+ end
110
+
111
+ custs = instance.filter_by(name: "John", age: 30).all()
112
+
113
+ expect(custs.size).to eq(1)
114
+ expect(custs[0].object).to eq("customer")
115
+ expect(custs[0].session).to eq(instance.instance_variable_get(:@session))
116
+ end
117
+ end
118
+
119
+ context "when called multiple times" do
120
+ it "merges all the given data into a single hash" do
121
+ class TestObject < Payload::ARMObject
122
+ @poly = { "type" => "test" }
123
+ end
124
+ instance.instance_variable_set(:@cls, TestObject)
125
+
126
+ instance.filter_by(name: "John", city: "San Francisco")
127
+ instance.filter_by(age: ['<30', '>20'])
128
+ expect(instance.instance_variable_get(:@filters)).to eq({name: "John", age: ['<30', '>20'], city: "San Francisco", "type" => "test"})
129
+ end
130
+ end
131
+ end
132
+
133
+ describe "#create" do
134
+
135
+ let(:instance) { described_class.new }
136
+
137
+ context "when the user creates an object" do
138
+ it "executes the appropriate request and returns the appropriate object" do
139
+
140
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
141
+
142
+ Payload::api_key = 'test_key'
143
+ instance.instance_variable_set(:@cls, Payload::Customer)
144
+
145
+ expect(instance).to receive(:_execute_request) do |http, request|
146
+ expect(request.method).to eq("POST")
147
+ expect(http.address).to eq("api.payload.co")
148
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
149
+ expect(request.path).to eq("/customers?")
150
+ expect(request.body).to eq("{\"type\":\"bill\",\"processing_id\":\"acct_3bz0zU99AX06SJwfMmfn0\",\"due_date\":\"2020-01-01\",\"items\":[{\"entry_type\":\"charge\",\"type\":\"item1\",\"amount\":29.99}],\"customer_id\":\"acct_3bW9JMoGYQul5fCIa9f8q\"}")
151
+
152
+ class MockResponse
153
+ def initialize
154
+ end
155
+
156
+ def code
157
+ '200'
158
+ end
159
+
160
+ def body
161
+ '{
162
+ "id": "' + $test_id + '",
163
+ "object": "customer"
164
+ }'
165
+ end
166
+ end
167
+
168
+ MockResponse.new
169
+ end
170
+
171
+ cust = instance.create(
172
+ type: 'bill',
173
+ processing_id: 'acct_3bz0zU99AX06SJwfMmfn0',
174
+ due_date: '2020-01-01',
175
+ items: [
176
+ Payload::ChargeItem.new(
177
+ type: 'item1',
178
+ amount: 29.99
179
+ )
180
+ ],
181
+ customer_id: 'acct_3bW9JMoGYQul5fCIa9f8q'
182
+ )
183
+ expect(cust.id).to eq($test_id)
184
+ expect(cust.object).to eq("customer")
185
+ expect(cust.session).to eq(instance.instance_variable_get(:@session))
186
+ end
187
+ end
188
+
189
+ context "when the user creates multiple objects" do
190
+ it "executes the appropriate request and returns the appropriate objects" do
191
+
192
+ $test_id_a = 'acct_' + rand(9000000...9999999).to_s
193
+ $test_id_b = 'acct_' + rand(9000000...9999999).to_s
194
+ $test_id_c = 'acct_' + rand(9000000...9999999).to_s
195
+
196
+ Payload::api_key = 'test_key'
197
+ instance.instance_variable_set(:@cls, Payload::Customer)
198
+
199
+ expect(instance).to receive(:_execute_request) do |http, request|
200
+ expect(request.method).to eq("POST")
201
+ expect(http.address).to eq("api.payload.co")
202
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
203
+ expect(request.path).to eq("/customers?")
204
+ expect(request.body).to eq("{\"object\":\"list\",\"values\":[{\"name\":\"John\",\"age\":30},{\"name\":\"Alice\",\"age\":25},{\"name\":\"Bob\",\"age\":35}]}")
205
+
206
+ class MockResponse
207
+ def initialize
208
+ end
209
+
210
+ def code
211
+ '200'
212
+ end
213
+
214
+ def body
215
+ '{
216
+ "object": "list",
217
+ "values": [
218
+ {"id": "' + $test_id_a + '", "object": "customer"},
219
+ {"id": "' + $test_id_b + '", "object": "customer"},
220
+ {"id": "' + $test_id_c + '", "object": "customer"}
221
+ ]
222
+ }'
223
+ end
224
+ end
225
+
226
+ MockResponse.new
227
+ end
228
+
229
+ customers = instance.create([{ name: "John", age: 30 }, { name: "Alice", age: 25 }, { name: "Bob", age: 35 }])
230
+ expect(customers.size).to eq(3)
231
+
232
+ expect(customers[0].id).to eq($test_id_a)
233
+ expect(customers[0].object).to eq("customer")
234
+ expect(customers[0].session).to eq(instance.instance_variable_get(:@session))
235
+
236
+ expect(customers[1].id).to eq($test_id_b)
237
+ expect(customers[1].object).to eq("customer")
238
+ expect(customers[1].session).to eq(instance.instance_variable_get(:@session))
239
+
240
+ expect(customers[2].id).to eq($test_id_c)
241
+ expect(customers[2].object).to eq("customer")
242
+ expect(customers[2].session).to eq(instance.instance_variable_get(:@session))
243
+ end
244
+ end
245
+
246
+ context "when the user creates multiple objects using ARMObjects" do
247
+ it "executes the appropriate request and returns the appropriate objects" do
248
+
249
+ $test_id_a = 'acct_' + rand(9000000...9999999).to_s
250
+ $test_id_b = 'acct_' + rand(9000000...9999999).to_s
251
+ $test_id_c = 'acct_' + rand(9000000...9999999).to_s
252
+
253
+ Payload::api_key = 'test_key'
254
+ instance.instance_variable_set(:@cls, Payload::Customer)
255
+
256
+ expect(instance).to receive(:_execute_request) do |http, request|
257
+ expect(request.method).to eq("POST")
258
+ expect(http.address).to eq("api.payload.co")
259
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
260
+ expect(request.path).to eq("/customers?")
261
+ expect(request.body).to eq("{\"object\":\"list\",\"values\":[{\"name\":\"John\",\"age\":30},{\"name\":\"Alice\",\"age\":25},{\"name\":\"Bob\",\"age\":35}]}")
262
+
263
+ class MockResponse
264
+ def initialize
265
+ end
266
+
267
+ def code
268
+ '200'
269
+ end
270
+
271
+ def body
272
+ '{
273
+ "object": "list",
274
+ "values": [
275
+ {"id": "' + $test_id_a + '", "object": "customer"},
276
+ {"id": "' + $test_id_b + '", "object": "customer"},
277
+ {"id": "' + $test_id_c + '", "object": "customer"}
278
+ ]
279
+ }'
280
+ end
281
+ end
282
+
283
+ MockResponse.new
284
+ end
285
+
286
+ customers = instance.create([{ name: "John", age: 30 }, { name: "Alice", age: 25 }, { name: "Bob", age: 35 }])
287
+ expect(customers.size).to eq(3)
288
+
289
+ expect(customers[0].id).to eq($test_id_a)
290
+ expect(customers[0].object).to eq("customer")
291
+ expect(customers[0].session).to eq(instance.instance_variable_get(:@session))
292
+
293
+ expect(customers[1].id).to eq($test_id_b)
294
+ expect(customers[1].object).to eq("customer")
295
+ expect(customers[1].session).to eq(instance.instance_variable_get(:@session))
296
+
297
+ expect(customers[2].id).to eq($test_id_c)
298
+ expect(customers[2].object).to eq("customer")
299
+ expect(customers[2].session).to eq(instance.instance_variable_get(:@session))
300
+ end
301
+ end
302
+ end
303
+
304
+ describe "#get" do
305
+
306
+ let(:instance) { described_class.new }
307
+
308
+ context "when the user gets an object" do
309
+
310
+ it "executes the appropriate request and returns the appropriate object" do
311
+
312
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
313
+
314
+ Payload::api_key = 'test_key'
315
+ instance.instance_variable_set(:@cls, Payload::Customer)
316
+
317
+ expect(instance).to receive(:_execute_request) do |http, request|
318
+ expect(request.method).to eq("GET")
319
+ expect(http.address).to eq("api.payload.co")
320
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
321
+ expect(request.path).to eq("/customers/" + $test_id + "?")
322
+
323
+ class MockResponse
324
+ def initialize
325
+ end
326
+
327
+ def code
328
+ '200'
329
+ end
330
+
331
+ def body
332
+ '{
333
+ "id": "' + $test_id + '",
334
+ "object": "customer"
335
+ }'
336
+ end
337
+ end
338
+
339
+ MockResponse.new
340
+ end
341
+
342
+ cust = instance.get($test_id)
343
+ expect(cust.id).to eq($test_id)
344
+ expect(cust.object).to eq("customer")
345
+ expect(cust.session).to eq(instance.instance_variable_get(:@session))
346
+ end
347
+ end
348
+ end
349
+
350
+ describe "#update" do
351
+
352
+ let(:instance) { described_class.new }
353
+
354
+ context "when the user updates an object" do
355
+
356
+ it "executes the appropriate request and returns the appropriate object" do
357
+
358
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
359
+
360
+ Payload::api_key = 'test_key'
361
+ instance.instance_variable_set(:@cls, Payload::Customer)
362
+
363
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
364
+ expect(request.method).to eq("PUT")
365
+ expect(http.address).to eq("api.payload.co")
366
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
367
+ expect(request.path).to eq("/customers/" + $test_id + "?")
368
+ expect(request.body).to eq("{\"name\":\"John\",\"age\":30}")
369
+
370
+ class MockResponse
371
+ def initialize
372
+ end
373
+
374
+ def code
375
+ '200'
376
+ end
377
+
378
+ def body
379
+ '{
380
+ "id": "' + $test_id + '",
381
+ "object": "customer"
382
+ }'
383
+ end
384
+ end
385
+
386
+ MockResponse.new
387
+ end
388
+
389
+ cust = Payload::Customer.new(id: $test_id).update(name: "John", age: 30)
390
+ expect(cust.id).to eq($test_id)
391
+ expect(cust.object).to eq("customer")
392
+ expect(cust.session).to eq(instance.instance_variable_get(:@session))
393
+ end
394
+ end
395
+
396
+ context "when the user updates an object that is part of a session" do
397
+
398
+ it "executes the appropriate request and returns the appropriate object" do
399
+
400
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
401
+
402
+ Payload::api_key = 'test_key'
403
+ instance = Payload::Session.new('session_key', 'https://sandbox.payload.co')
404
+
405
+ cust = Payload::Customer.new({id: $test_id})
406
+ cust.set_session(instance)
407
+
408
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
409
+ expect(request.method).to eq("PUT")
410
+ expect(http.address).to eq("sandbox.payload.co")
411
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('session_key')
412
+ expect(request.path).to eq("/customers/" + $test_id + "?")
413
+ expect(request.body).to eq("{\"name\":\"John\",\"age\":30}")
414
+
415
+ class MockResponse
416
+ def initialize
417
+ end
418
+
419
+ def code
420
+ '200'
421
+ end
422
+
423
+ def body
424
+ '{
425
+ "id": "' + $test_id + '",
426
+ "object": "customer"
427
+ }'
428
+ end
429
+ end
430
+
431
+ MockResponse.new
432
+ end
433
+
434
+ cust.update(name: "John", age: 30)
435
+ end
436
+ end
437
+
438
+ context "when the user updates multiple objects" do
439
+
440
+ it "executes the appropriate request and returns the appropriate objects" do
441
+
442
+ $test_id_a = 'acct_' + rand(9000000...9999999).to_s
443
+ $test_id_b = 'acct_' + rand(9000000...9999999).to_s
444
+ $test_id_c = 'acct_' + rand(9000000...9999999).to_s
445
+
446
+ Payload::api_key = 'test_key'
447
+ instance.instance_variable_set(:@cls, Payload::Customer)
448
+
449
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
450
+ expect(request.method).to eq("PUT")
451
+ expect(http.address).to eq("api.payload.co")
452
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
453
+ expect(request.path).to eq("/customers?")
454
+ expect(request.body).to eq("{\"object\":\"list\",\"values\":[{\"name\":\"John\",\"age\":30,\"id\":\"" + $test_id_a + "\"},{\"name\":\"Alice\",\"age\":25,\"id\":\"" + $test_id_b + "\"},{\"name\":\"Bob\",\"age\":35,\"id\":\"" + $test_id_c + "\"}]}")
455
+
456
+ class MockResponse
457
+ def initialize
458
+ end
459
+
460
+ def code
461
+ '200'
462
+ end
463
+
464
+ def body
465
+ '{
466
+ "object": "list",
467
+ "values": [
468
+ {"id": "' + $test_id_a + '", "object": "customer"},
469
+ {"id": "' + $test_id_b + '", "object": "customer"},
470
+ {"id": "' + $test_id_c + '", "object": "customer"}
471
+ ]
472
+ }'
473
+ end
474
+ end
475
+
476
+ MockResponse.new
477
+ end
478
+
479
+ custs = Payload::update([
480
+ [Payload::Customer.new(id: $test_id_a), { name: "John", age: 30 }],
481
+ [Payload::Customer.new(id: $test_id_b), { name: "Alice", age: 25 }],
482
+ [Payload::Customer.new(id: $test_id_c), { name: "Bob", age: 35 }]
483
+ ])
484
+
485
+ expect(custs.size).to eq(3)
486
+
487
+ expect(custs[0].id).to eq($test_id_a)
488
+ expect(custs[0].object).to eq("customer")
489
+ expect(custs[0].session).to eq(instance.instance_variable_get(:@session))
490
+
491
+ expect(custs[1].id).to eq($test_id_b)
492
+ expect(custs[1].object).to eq("customer")
493
+ expect(custs[1].session).to eq(instance.instance_variable_get(:@session))
494
+
495
+ expect(custs[2].id).to eq($test_id_c)
496
+ expect(custs[2].object).to eq("customer")
497
+ expect(custs[2].session).to eq(instance.instance_variable_get(:@session))
498
+ end
499
+ end
500
+
501
+ context "when the user updates multiple objects via query" do
502
+
503
+ it "executes the appropriate request and returns the appropriate objects" do
504
+
505
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
506
+
507
+ Payload::api_key = 'test_key'
508
+ instance.instance_variable_set(:@cls, Payload::Customer)
509
+
510
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
511
+ expect(request.method).to eq("PUT")
512
+ expect(http.address).to eq("api.payload.co")
513
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
514
+ expect(request.path).to eq("/customers?name=John+Smith&mode=query")
515
+ expect(request.body).to eq("{\"name\":\"John\",\"age\":30}")
516
+
517
+ class MockResponse
518
+ def initialize
519
+ end
520
+
521
+ def code
522
+ '200'
523
+ end
524
+
525
+ def body
526
+ '{
527
+ "object": "list",
528
+ "values": [
529
+ {"id": "' + $test_id + '", "object": "customer"}
530
+ ]
531
+ }'
532
+ end
533
+ end
534
+
535
+ MockResponse.new
536
+ end
537
+
538
+ custs = Payload::Customer.
539
+ filter_by(name: 'John Smith').
540
+ update(name: "John", age: 30)
541
+
542
+ expect(custs.size).to eq(1)
543
+
544
+ expect(custs[0].id).to eq($test_id)
545
+ expect(custs[0].object).to eq("customer")
546
+ expect(custs[0].session).to eq(instance.instance_variable_get(:@session))
547
+ end
548
+ end
549
+ end
550
+
551
+ describe "#delete" do
552
+
553
+ let(:instance) { described_class.new }
554
+
555
+ context "when the user deletes an object" do
556
+
557
+ it "executes the appropriate request and returns the appropriate object" do
558
+
559
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
560
+
561
+ Payload::api_key = 'test_key'
562
+
563
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
564
+ expect(request.method).to eq("DELETE")
565
+ expect(http.address).to eq("api.payload.co")
566
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
567
+ expect(request.path).to eq("/customers/" + $test_id + "?")
568
+ expect(request.body).to eq(nil)
569
+
570
+ class MockResponse
571
+ def initialize
572
+ end
573
+
574
+ def code
575
+ '200'
576
+ end
577
+
578
+ def body
579
+ '{
580
+ "id": "' + $test_id + '",
581
+ "object": "customer"
582
+ }'
583
+ end
584
+ end
585
+
586
+ MockResponse.new
587
+ end
588
+
589
+ cust = Payload::Customer.new(id: $test_id).delete()
590
+ expect(cust.id).to eq($test_id)
591
+ expect(cust.object).to eq("customer")
592
+ expect(cust.session).to eq(instance.instance_variable_get(:@session))
593
+ end
594
+ end
595
+
596
+ context "when the user updates an object that is part of a session" do
597
+
598
+ it "executes the appropriate request and returns the appropriate object" do
599
+
600
+ $test_id = 'acct_' + rand(9000000...9999999).to_s
601
+
602
+ Payload::api_key = 'test_key'
603
+ instance = Payload::Session.new('session_key', 'https://sandbox.payload.co')
604
+
605
+ cust = Payload::Customer.new({id: $test_id})
606
+ cust.set_session(instance)
607
+
608
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
609
+ expect(request.method).to eq("DELETE")
610
+ expect(http.address).to eq("sandbox.payload.co")
611
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('session_key')
612
+ expect(request.path).to eq("/customers/" + $test_id + "?")
613
+ expect(request.body).to eq(nil)
614
+
615
+ class MockResponse
616
+ def initialize
617
+ end
618
+
619
+ def code
620
+ '200'
621
+ end
622
+
623
+ def body
624
+ '{
625
+ "id": "' + $test_id + '",
626
+ "object": "customer"
627
+ }'
628
+ end
629
+ end
630
+
631
+ MockResponse.new
632
+ end
633
+
634
+ cust.delete()
635
+ end
636
+ end
637
+
638
+ context "when the user deletes multiple objects" do
639
+
640
+ it "executes the appropriate request and returns the appropriate objects" do
641
+
642
+ $test_id_a = 'acct_' + rand(9000000...9999999).to_s
643
+ $test_id_b = 'acct_' + rand(9000000...9999999).to_s
644
+ $test_id_c = 'acct_' + rand(9000000...9999999).to_s
645
+
646
+ Payload::api_key = 'test_key'
647
+ instance.instance_variable_set(:@cls, Payload::Customer)
648
+
649
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
650
+ expect(request.method).to eq("DELETE")
651
+ expect(http.address).to eq("api.payload.co")
652
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
653
+ expect(request.path).to eq("/customers?")
654
+ expect(request.body).to eq("{\"object\":\"list\",\"values\":[{\"id\":\"" + $test_id_a + "\"},{\"id\":\"" + $test_id_b + "\"},{\"id\":\"" + $test_id_c + "\"}]}")
655
+
656
+ class MockResponse
657
+ def initialize
658
+ end
659
+
660
+ def code
661
+ '200'
662
+ end
663
+
664
+ def body
665
+ '{
666
+ "object": "list",
667
+ "values": [
668
+ {"id": "' + $test_id_a + '", "object": "customer"},
669
+ {"id": "' + $test_id_b + '", "object": "customer"},
670
+ {"id": "' + $test_id_c + '", "object": "customer"}
671
+ ]
672
+ }'
673
+ end
674
+ end
675
+
676
+ MockResponse.new
677
+ end
678
+
679
+ custs = Payload::delete([
680
+ Payload::Customer.new(id: $test_id_a),
681
+ Payload::Customer.new(id: $test_id_b),
682
+ Payload::Customer.new(id: $test_id_c)
683
+ ])
684
+
685
+ expect(custs.size).to eq(3)
686
+
687
+ expect(custs[0].id).to eq($test_id_a)
688
+ expect(custs[0].object).to eq("customer")
689
+ expect(custs[0].session).to eq(instance.instance_variable_get(:@session))
690
+
691
+ expect(custs[1].id).to eq($test_id_b)
692
+ expect(custs[1].object).to eq("customer")
693
+ expect(custs[1].session).to eq(instance.instance_variable_get(:@session))
694
+
695
+ expect(custs[2].id).to eq($test_id_c)
696
+ expect(custs[2].object).to eq("customer")
697
+ expect(custs[2].session).to eq(instance.instance_variable_get(:@session))
698
+ end
699
+ end
700
+
701
+ context "when the user deletes multiple objects via query" do
702
+
703
+ it "executes the appropriate request and returns the appropriate objects" do
704
+
705
+ $test_id_a = 'acct_' + rand(9000000...9999999).to_s
706
+ $test_id_b = 'acct_' + rand(9000000...9999999).to_s
707
+ $test_id_c = 'acct_' + rand(9000000...9999999).to_s
708
+
709
+ Payload::api_key = 'test_key'
710
+ instance.instance_variable_set(:@cls, Payload::Customer)
711
+
712
+ expect_any_instance_of(Payload::ARMRequest).to receive(:_execute_request) do |inst, http, request|
713
+ expect(request.method).to eq("DELETE")
714
+ expect(http.address).to eq("api.payload.co")
715
+ expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('test_key')
716
+ expect(request.path).to eq("/customers?name=John+Smith&mode=query")
717
+ expect(request.body).to eq(nil)
718
+
719
+ class MockResponse
720
+ def initialize
721
+ end
722
+
723
+ def code
724
+ '200'
725
+ end
726
+
727
+ def body
728
+ '{
729
+ "object": "list",
730
+ "values": [
731
+ {"id": "' + $test_id_a + '", "object": "customer"},
732
+ {"id": "' + $test_id_b + '", "object": "customer"},
733
+ {"id": "' + $test_id_c + '", "object": "customer"}
734
+ ]
735
+ }'
736
+ end
737
+ end
738
+
739
+ MockResponse.new
740
+ end
741
+
742
+ custs = Payload::Customer.
743
+ filter_by(name: 'John Smith').
744
+ delete()
745
+
746
+ expect(custs.size).to eq(3)
747
+
748
+ expect(custs[0].id).to eq($test_id_a)
749
+ expect(custs[0].object).to eq("customer")
750
+ expect(custs[0].session).to eq(instance.instance_variable_get(:@session))
751
+
752
+ expect(custs[1].id).to eq($test_id_b)
753
+ expect(custs[1].object).to eq("customer")
754
+ expect(custs[1].session).to eq(instance.instance_variable_get(:@session))
755
+
756
+ expect(custs[2].id).to eq($test_id_c)
757
+ expect(custs[2].object).to eq("customer")
758
+ expect(custs[2].session).to eq(instance.instance_variable_get(:@session))
759
+ end
760
+ end
761
+ end
762
+ end