customerio 5.6.0 → 6.0.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.
@@ -1,432 +0,0 @@
1
- require 'spec_helper'
2
- require 'multi_json'
3
- require 'base64'
4
- require 'tempfile'
5
-
6
- describe Customerio::APIClient do
7
- let(:app_key) { "appkey" }
8
-
9
- let(:client) { Customerio::APIClient.new(app_key) }
10
- let(:response) { double("Response", code: 200) }
11
-
12
- def api_uri(path)
13
- "https://api.customer.io#{path}"
14
- end
15
-
16
- def request_headers
17
- { 'Authorization': "Bearer #{app_key}", 'Content-Type': 'application/json', 'User-Agent': 'Customer.io Ruby Client/' + Customerio::VERSION }
18
- end
19
-
20
- def json(data)
21
- MultiJson.dump(data)
22
- end
23
-
24
- it "the base client is initialised with the correct values when no region is passed in" do
25
- app_key = "appkey"
26
-
27
- expect(Customerio::BaseClient).to(
28
- receive(:new)
29
- .with(
30
- { app_key: app_key },
31
- {
32
- region: Customerio::Regions::US,
33
- url: Customerio::Regions::US.api_url
34
- }
35
- )
36
- )
37
-
38
- client = Customerio::APIClient.new(app_key)
39
- end
40
-
41
- it "raises an error when an incorrect region is passed in" do
42
- expect {
43
- Customerio::APIClient.new("appkey", region: :au)
44
- }.to raise_error /region must be an instance of Customerio::Regions::Region/
45
- end
46
-
47
- [Customerio::Regions::US, Customerio::Regions::EU].each do |region|
48
- it "the base client is initialised with the correct values when the region \"#{region}\" is passed in" do
49
- app_key = "appkey"
50
-
51
- expect(Customerio::BaseClient).to(
52
- receive(:new)
53
- .with(
54
- { app_key: app_key },
55
- {
56
- region: region,
57
- url: region.api_url
58
- }
59
- )
60
- )
61
-
62
- client = Customerio::APIClient.new(app_key, { region: region })
63
- end
64
- end
65
-
66
- describe "#send_email" do
67
- it "sends a POST request to the /api/send/email path" do
68
- req = Customerio::SendEmailRequest.new(
69
- identifiers: {
70
- id: 'c1',
71
- },
72
- transactional_message_id: 1,
73
- )
74
-
75
- stub_request(:post, api_uri('/v1/send/email'))
76
- .with(headers: request_headers, body: req.message)
77
- .to_return(status: 200, body: { delivery_id: 1 }.to_json, headers: {})
78
-
79
- client.send_email(req).should eq({ "delivery_id" => 1 })
80
- end
81
-
82
- it "handles validation failures (400)" do
83
- req = Customerio::SendEmailRequest.new(
84
- identifiers: {
85
- id: 'c1',
86
- },
87
- transactional_message_id: 1,
88
- )
89
-
90
- err_json = { meta: { error: "example error" } }.to_json
91
-
92
- stub_request(:post, api_uri('/v1/send/email'))
93
- .with(headers: request_headers, body: req.message)
94
- .to_return(status: 400, body: err_json, headers: {})
95
-
96
- lambda { client.send_email(req) }.should(
97
- raise_error(Customerio::InvalidResponse) { |error|
98
- error.message.should eq "example error"
99
- error.code.should eq "400"
100
- }
101
- )
102
- end
103
-
104
- it "handles other failures (5xx)" do
105
- req = Customerio::SendEmailRequest.new(
106
- identifiers: {
107
- id: 'c1',
108
- },
109
- transactional_message_id: 1,
110
- )
111
-
112
- stub_request(:post, api_uri('/v1/send/email'))
113
- .with(headers: request_headers, body: req.message)
114
- .to_return(status: 500, body: "Server unavailable", headers: {})
115
-
116
- lambda { client.send_email(req) }.should(
117
- raise_error(Customerio::InvalidResponse) { |error|
118
- error.message.should eq "Server unavailable"
119
- error.code.should eq "500"
120
- }
121
- )
122
- end
123
-
124
- it "allows attaching file content without encoding" do
125
- content = 'sample content'
126
-
127
- req = Customerio::SendEmailRequest.new(
128
- customer_id: 'c1',
129
- transactional_message_id: 1,
130
- )
131
-
132
- req.attach('test', content, encode: false)
133
- req.message[:attachments]['test'].should eq content
134
-
135
- stub_request(:post, api_uri('/v1/send/email'))
136
- .with(headers: request_headers, body: req.message)
137
- .to_return(status: 200, body: { delivery_id: 1 }.to_json, headers: {})
138
-
139
- client.send_email(req)
140
- end
141
-
142
- it "allows attaching files with encoding (default)" do
143
- content = 'sample content'
144
-
145
- req = Customerio::SendEmailRequest.new(
146
- customer_id: 'c1',
147
- transactional_message_id: 1,
148
- )
149
-
150
- req.attach('test', content)
151
- req.message[:attachments]['test'].should eq Base64.strict_encode64(content)
152
-
153
- stub_request(:post, api_uri('/v1/send/email'))
154
- .with(headers: request_headers, body: req.message)
155
- .to_return(status: 200, body: { delivery_id: 1 }.to_json, headers: {})
156
-
157
- client.send_email(req)
158
- end
159
-
160
- it "raises error when attaching the same key again" do
161
- req = Customerio::SendEmailRequest.new(
162
- customer_id: 'c1',
163
- transactional_message_id: 1,
164
- )
165
-
166
- req.attach('test', 'test-content')
167
-
168
- lambda { req.attach('test', '') }.should raise_error(/attachment test already exists/)
169
- req.message[:attachments].should eq({ "test" => Base64.strict_encode64("test-content") })
170
- end
171
- end
172
-
173
- describe "#send_push" do
174
- it "sends a POST request to the /api/send/push path" do
175
- req = Customerio::SendPushRequest.new(
176
- identifiers: {
177
- id: 'c1',
178
- },
179
- transactional_message_id: 1,
180
- )
181
-
182
- stub_request(:post, api_uri('/v1/send/push'))
183
- .with(headers: request_headers, body: req.message)
184
- .to_return(status: 200, body: { delivery_id: 1 }.to_json, headers: {})
185
-
186
- client.send_push(req).should eq({ "delivery_id" => 1 })
187
- end
188
-
189
- it "handles validation failures (400)" do
190
- req = Customerio::SendPushRequest.new(
191
- identifiers: {
192
- id: 'c1',
193
- },
194
- transactional_message_id: 1,
195
- )
196
-
197
- err_json = { meta: { error: "example error" } }.to_json
198
-
199
- stub_request(:post, api_uri('/v1/send/push'))
200
- .with(headers: request_headers, body: req.message)
201
- .to_return(status: 400, body: err_json, headers: {})
202
-
203
- lambda { client.send_push(req) }.should(
204
- raise_error(Customerio::InvalidResponse) { |error|
205
- error.message.should eq "example error"
206
- error.code.should eq "400"
207
- }
208
- )
209
- end
210
-
211
- it "handles other failures (5xx)" do
212
- req = Customerio::SendPushRequest.new(
213
- identifiers: {
214
- id: 'c1',
215
- },
216
- transactional_message_id: 1,
217
- )
218
-
219
- stub_request(:post, api_uri('/v1/send/push'))
220
- .with(headers: request_headers, body: req.message)
221
- .to_return(status: 500, body: "Server unavailable", headers: {})
222
-
223
- lambda { client.send_push(req) }.should(
224
- raise_error(Customerio::InvalidResponse) { |error|
225
- error.message.should eq "Server unavailable"
226
- error.code.should eq "500"
227
- }
228
- )
229
- end
230
-
231
- it "sets custom_device correctly if device present in req" do
232
- req = Customerio::SendPushRequest.new(
233
- identifiers: {
234
- id: 'c1',
235
- },
236
- transactional_message_id: 1,
237
- device: {
238
- platform: 'ios',
239
- token: 'sample-token',
240
- }
241
- )
242
-
243
- req.message[:custom_device].should eq({
244
- platform: 'ios',
245
- token: 'sample-token',
246
- })
247
-
248
- stub_request(:post, api_uri('/v1/send/push'))
249
- .with(headers: request_headers, body: req.message)
250
- .to_return(status: 200, body: { delivery_id: 2 }.to_json, headers: {})
251
-
252
- client.send_push(req).should eq({ "delivery_id" => 2 })
253
- end
254
- end
255
-
256
- describe "#send_sms" do
257
- it "sends a POST request to the /api/send/sms path" do
258
- req = Customerio::SendSMSRequest.new(
259
- identifiers: {
260
- id: 'c1',
261
- },
262
- transactional_message_id: 1,
263
- )
264
-
265
- stub_request(:post, api_uri('/v1/send/sms'))
266
- .with(headers: request_headers, body: req.message)
267
- .to_return(status: 200, body: { delivery_id: 1 }.to_json, headers: {})
268
-
269
- client.send_sms(req).should eq({ "delivery_id" => 1 })
270
- end
271
-
272
- it "handles validation failures (400)" do
273
- req = Customerio::SendSMSRequest.new(
274
- identifiers: {
275
- id: 'c1',
276
- },
277
- transactional_message_id: 1,
278
- )
279
-
280
- err_json = { meta: { error: "example error" } }.to_json
281
-
282
- stub_request(:post, api_uri('/v1/send/sms'))
283
- .with(headers: request_headers, body: req.message)
284
- .to_return(status: 400, body: err_json, headers: {})
285
-
286
- lambda { client.send_sms(req) }.should(
287
- raise_error(Customerio::InvalidResponse) { |error|
288
- error.message.should eq "example error"
289
- error.code.should eq "400"
290
- }
291
- )
292
- end
293
-
294
- it "handles other failures (5xx)" do
295
- req = Customerio::SendSMSRequest.new(
296
- identifiers: {
297
- id: 'c1',
298
- },
299
- transactional_message_id: 1,
300
- )
301
-
302
- stub_request(:post, api_uri('/v1/send/sms'))
303
- .with(headers: request_headers, body: req.message)
304
- .to_return(status: 500, body: "Server unavailable", headers: {})
305
-
306
- lambda { client.send_sms(req) }.should(
307
- raise_error(Customerio::InvalidResponse) { |error|
308
- error.message.should eq "Server unavailable"
309
- error.code.should eq "500"
310
- }
311
- )
312
- end
313
- end
314
-
315
- describe "#send_inbox_message" do
316
- it "sends a POST request to the /api/send/inbox_message path" do
317
- req = Customerio::SendInboxMessageRequest.new(
318
- identifiers: {
319
- id: 'c1',
320
- },
321
- transactional_message_id: 1,
322
- )
323
-
324
- stub_request(:post, api_uri('/v1/send/inbox_message'))
325
- .with(headers: request_headers, body: req.message)
326
- .to_return(status: 200, body: { delivery_id: 1 }.to_json, headers: {})
327
-
328
- client.send_inbox_message(req).should eq({ "delivery_id" => 1 })
329
- end
330
-
331
- it "handles validation failures (400)" do
332
- req = Customerio::SendInboxMessageRequest.new(
333
- identifiers: {
334
- id: 'c1',
335
- },
336
- transactional_message_id: 1,
337
- )
338
-
339
- err_json = { meta: { error: "example error" } }.to_json
340
-
341
- stub_request(:post, api_uri('/v1/send/inbox_message'))
342
- .with(headers: request_headers, body: req.message)
343
- .to_return(status: 400, body: err_json, headers: {})
344
-
345
- lambda { client.send_inbox_message(req) }.should(
346
- raise_error(Customerio::InvalidResponse) { |error|
347
- error.message.should eq "example error"
348
- error.code.should eq "400"
349
- }
350
- )
351
- end
352
-
353
- it "handles other failures (5xx)" do
354
- req = Customerio::SendInboxMessageRequest.new(
355
- identifiers: {
356
- id: 'c1',
357
- },
358
- transactional_message_id: 1,
359
- )
360
-
361
- stub_request(:post, api_uri('/v1/send/inbox_message'))
362
- .with(headers: request_headers, body: req.message)
363
- .to_return(status: 500, body: "Server unavailable", headers: {})
364
-
365
- lambda { client.send_inbox_message(req) }.should(
366
- raise_error(Customerio::InvalidResponse) { |error|
367
- error.message.should eq "Server unavailable"
368
- error.code.should eq "500"
369
- }
370
- )
371
- end
372
- end
373
-
374
- describe "#send_in_app" do
375
- it "sends a POST request to the /api/send/in_app path" do
376
- req = Customerio::SendInAppRequest.new(
377
- identifiers: {
378
- id: 'c1',
379
- },
380
- transactional_message_id: 1,
381
- )
382
-
383
- stub_request(:post, api_uri('/v1/send/in_app'))
384
- .with(headers: request_headers, body: req.message)
385
- .to_return(status: 200, body: { delivery_id: 1 }.to_json, headers: {})
386
-
387
- client.send_in_app(req).should eq({ "delivery_id" => 1 })
388
- end
389
-
390
- it "handles validation failures (400)" do
391
- req = Customerio::SendInAppRequest.new(
392
- identifiers: {
393
- id: 'c1',
394
- },
395
- transactional_message_id: 1,
396
- )
397
-
398
- err_json = { meta: { error: "example error" } }.to_json
399
-
400
- stub_request(:post, api_uri('/v1/send/in_app'))
401
- .with(headers: request_headers, body: req.message)
402
- .to_return(status: 400, body: err_json, headers: {})
403
-
404
- lambda { client.send_in_app(req) }.should(
405
- raise_error(Customerio::InvalidResponse) { |error|
406
- error.message.should eq "example error"
407
- error.code.should eq "400"
408
- }
409
- )
410
- end
411
-
412
- it "handles other failures (5xx)" do
413
- req = Customerio::SendInAppRequest.new(
414
- identifiers: {
415
- id: 'c1',
416
- },
417
- transactional_message_id: 1,
418
- )
419
-
420
- stub_request(:post, api_uri('/v1/send/in_app'))
421
- .with(headers: request_headers, body: req.message)
422
- .to_return(status: 500, body: "Server unavailable", headers: {})
423
-
424
- lambda { client.send_in_app(req) }.should(
425
- raise_error(Customerio::InvalidResponse) { |error|
426
- error.message.should eq "Server unavailable"
427
- error.code.should eq "500"
428
- }
429
- )
430
- end
431
- end
432
- end
@@ -1,67 +0,0 @@
1
- require 'spec_helper'
2
- require 'multi_json'
3
- require 'base64'
4
-
5
- describe Customerio::BaseClient do
6
- let(:url) { "https://test.customer.io" }
7
-
8
- let(:site_id) { "SITE_ID" }
9
- let(:api_key) { "API_KEY" }
10
- let(:track_client) { Customerio::BaseClient.new({ site_id: site_id, api_key: api_key }, { url: url }) }
11
-
12
- let(:app_key) { "APP_KEY" }
13
- let(:api_client) { Customerio::BaseClient.new({ app_key: app_key }, { url: url }) }
14
-
15
- def api_uri(path)
16
- "#{url}#{path}"
17
- end
18
-
19
- def track_client_request_headers
20
- token = Base64.strict_encode64("#{site_id}:#{api_key}")
21
- { 'Authorization': "Basic #{token}", 'Content-Type': 'application/json', 'User-Agent': 'Customer.io Ruby Client/' + Customerio::VERSION }
22
- end
23
-
24
- def api_client_request_headers
25
- { 'Authorization': "Bearer #{app_key}", 'Content-Type': 'application/json', 'User-Agent': 'Customer.io Ruby Client/' + Customerio::VERSION }
26
- end
27
-
28
- describe "with a site ID and API key" do
29
- it "uses the correct basic auth" do
30
- stub_request(:put, api_uri('/some/path')).
31
- with(headers: track_client_request_headers).
32
- to_return(status: 200, body: "", headers: {})
33
-
34
- track_client.request(:put, '/some/path', "")
35
- end
36
- end
37
-
38
- describe "with an app key" do
39
- it "uses the correct bearer token" do
40
- stub_request(:put, api_uri('/some/path')).
41
- with(headers: api_client_request_headers).
42
- to_return(status: 200, body: "", headers: {})
43
-
44
- api_client.request(:put, '/some/path', "")
45
- end
46
- end
47
-
48
- describe "#verify_response" do
49
- it "throws an error when the response isn't between 200 and 300" do
50
- stub_request(:put, api_uri('/some/path')).
51
- with(headers: api_client_request_headers).
52
- to_return(status: 400, body: "", headers: {})
53
-
54
- lambda { api_client.request_and_verify_response(:put, '/some/path', "") }.should(
55
- raise_error(Customerio::InvalidResponse)
56
- )
57
- end
58
-
59
- it "returns the response when the status is 200" do
60
- stub_request(:put, api_uri('/some/path')).
61
- with(headers: api_client_request_headers).
62
- to_return(status: 200, body: "Test", headers: {})
63
-
64
- api_client.request_and_verify_response(:put, '/some/path', "").body.should eq("Test")
65
- end
66
- end
67
- end