signifyd 0.1.5

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.
@@ -0,0 +1,503 @@
1
+ require 'spec_helper'
2
+
3
+ describe Signifyd do
4
+ let(:hash) { SignifydRequests.valid_case }
5
+ let(:json) { JSON.dump(hash) }
6
+
7
+ context '.verify_ssl_certs' do
8
+ context 'when calling for the verify_ssl_certs' do
9
+ before {
10
+ Signifyd.api_key = SIGNIFYD_API_KEY
11
+ }
12
+
13
+ after {
14
+ Signifyd.api_key = nil
15
+ }
16
+
17
+ subject {
18
+ Signifyd.verify_ssl_certs = true
19
+ }
20
+
21
+ it { should be_true }
22
+ it { should_not be_nil }
23
+ it { expect(subject).to eq(true) }
24
+ end
25
+ end
26
+
27
+ context '.verify_ssl_certs=' do
28
+ context 'when setting the verify_ssl_certs' do
29
+ before {
30
+ Signifyd.api_key = SIGNIFYD_API_KEY
31
+ }
32
+
33
+ after {
34
+ Signifyd.api_key = nil
35
+ Signifyd.verify_ssl_certs = true
36
+ }
37
+
38
+ subject {
39
+ Signifyd.verify_ssl_certs = false
40
+ }
41
+
42
+ it { should be_false }
43
+ it { should_not be_nil }
44
+ it { expect(subject).to eq(false) }
45
+ end
46
+ end
47
+
48
+ context '.api_base=' do
49
+ context 'when setting the api_base' do
50
+ before {
51
+ Signifyd.api_key = SIGNIFYD_API_KEY
52
+ }
53
+
54
+ after {
55
+ Signifyd.api_key = nil
56
+ Signifyd.api_base = 'https://api.signifyd.com'
57
+ }
58
+
59
+ subject {
60
+ Signifyd.api_base = 'https://signifyd.com'
61
+ }
62
+
63
+ it { should be_true }
64
+ it { should_not be_nil }
65
+ it { expect(subject).to eq('https://signifyd.com') }
66
+ end
67
+ end
68
+
69
+ context '.api_base' do
70
+ context 'when calling for the api_base' do
71
+ before {
72
+ Signifyd.api_key = SIGNIFYD_API_KEY
73
+ }
74
+
75
+ after {
76
+ Signifyd.api_key = nil
77
+ }
78
+
79
+ subject {
80
+ Signifyd.api_base
81
+ }
82
+
83
+ it { should be_true }
84
+ it { should_not be_nil }
85
+ it { expect(subject).to eq('https://api.signifyd.com') }
86
+ end
87
+ end
88
+
89
+ context '.ssl_bundle_path' do
90
+ context 'when calling for the ssl_bundle_path' do
91
+ before {
92
+ Signifyd.api_key = SIGNIFYD_API_KEY
93
+ }
94
+
95
+ after {
96
+ Signifyd.api_key = nil
97
+ }
98
+
99
+ subject {
100
+ Signifyd.ssl_bundle_path
101
+ }
102
+
103
+ it { should be_true }
104
+ it { should_not be_nil }
105
+ it { expect(subject).to include('data/ca-certificates.crt') }
106
+ end
107
+ end
108
+
109
+ context '.verify_ssl_certs' do
110
+ context 'when calling for the verify_ssl_certs' do
111
+ before {
112
+ Signifyd.api_key = SIGNIFYD_API_KEY
113
+ }
114
+
115
+ after {
116
+ Signifyd.api_key = nil
117
+ }
118
+
119
+ subject {
120
+ Signifyd.verify_ssl_certs
121
+ }
122
+
123
+ it { should be_true }
124
+ it { should_not be_nil }
125
+ it { expect(subject).to be_true }
126
+ end
127
+ end
128
+
129
+ context '.test_mode' do
130
+ context 'when calling for the test_mode' do
131
+ before {
132
+ Signifyd.api_key = SIGNIFYD_API_KEY
133
+ }
134
+
135
+ after {
136
+ Signifyd.api_key = nil
137
+ }
138
+
139
+ subject {
140
+ Signifyd.test_mode
141
+ }
142
+
143
+ it { should be_false }
144
+ it { should_not be_nil }
145
+ it { expect(subject).to be_false }
146
+ end
147
+ end
148
+
149
+ context '.test_mode=' do
150
+ context 'when setting the test_mode' do
151
+ before {
152
+ Signifyd.api_key = SIGNIFYD_API_KEY
153
+ }
154
+
155
+ after {
156
+ Signifyd.api_key = nil
157
+ Signifyd.api_version = '/v2'
158
+ }
159
+
160
+ subject {
161
+ Signifyd.test_mode = false
162
+ }
163
+
164
+ it { should be_false }
165
+ it { should_not be_nil }
166
+ it { expect(subject).to be_false }
167
+ end
168
+ end
169
+
170
+
171
+ context '.local_mode' do
172
+ context 'when calling for the local_mode' do
173
+ before {
174
+ Signifyd.api_key = SIGNIFYD_API_KEY
175
+ }
176
+
177
+ after {
178
+ Signifyd.api_key = nil
179
+ }
180
+
181
+ subject {
182
+ Signifyd.local_mode
183
+ }
184
+
185
+ it { should be_false }
186
+ it { should_not be_nil }
187
+ it { expect(subject).to be_false }
188
+ end
189
+ end
190
+
191
+ context '.local_mode=' do
192
+ context 'when setting the local_mode' do
193
+ before {
194
+ Signifyd.api_key = SIGNIFYD_API_KEY
195
+ }
196
+
197
+ after {
198
+ Signifyd.api_key = nil
199
+ Signifyd.api_version = '/v2'
200
+ }
201
+
202
+ subject {
203
+ Signifyd.local_mode = false
204
+ }
205
+
206
+ it { should be_false }
207
+ it { should_not be_nil }
208
+ it { expect(subject).to be_false }
209
+ end
210
+ end
211
+
212
+ context '.api_version=' do
213
+ context 'when setting the api_version' do
214
+ before {
215
+ Signifyd.api_key = SIGNIFYD_API_KEY
216
+ }
217
+
218
+ after {
219
+ Signifyd.api_key = nil
220
+ Signifyd.api_version = '/v2'
221
+ }
222
+
223
+ subject {
224
+ Signifyd.api_version = '/v2'
225
+ }
226
+
227
+ it { should be_true }
228
+ it { should_not be_nil }
229
+ it {
230
+ expect(subject).to eq('/v2')
231
+ }
232
+ end
233
+ end
234
+
235
+ context '.api_version' do
236
+ context 'when calling for the api_version' do
237
+ before {
238
+ Signifyd.api_key = SIGNIFYD_API_KEY
239
+ }
240
+
241
+ after {
242
+ Signifyd.api_key = nil
243
+ }
244
+
245
+ subject {
246
+ Signifyd.api_version
247
+ }
248
+
249
+ it { should be_true }
250
+ it { should_not be_nil }
251
+ it {
252
+ expect(subject).to eq('/v2')
253
+ }
254
+ end
255
+ end
256
+
257
+ context '.api_key' do
258
+ context 'when setting an invalid API key' do
259
+ context 'and when checking with the .configured? method' do
260
+ subject {
261
+ Signifyd.configured?
262
+ }
263
+
264
+ it { should_not be_nil }
265
+ it { should be_false }
266
+ end
267
+
268
+ context 'and when checking the actual value' do
269
+ subject {
270
+ Signifyd.api_key
271
+ }
272
+
273
+ it { should be_nil }
274
+ it { should be_false }
275
+ end
276
+ end
277
+
278
+ context 'when setting a valid API key' do
279
+ before {
280
+ Signifyd.api_key = SIGNIFYD_API_KEY
281
+ }
282
+
283
+ after {
284
+ Signifyd.api_key = nil
285
+ }
286
+
287
+ subject {
288
+ Signifyd.configured?
289
+ }
290
+
291
+ it { should_not be_nil }
292
+ it { should be_true }
293
+ end
294
+ end
295
+
296
+ context '.request' do
297
+ context 'with a valid API key set' do
298
+ before {
299
+ Signifyd.api_key = SIGNIFYD_API_KEY
300
+
301
+ stub_request(:post, "https://#{Signifyd.api_key}@api.signifyd.com/v2/cases").
302
+ with(:body => json, :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>json.size, 'Content-Type'=>'application/json', 'User-Agent'=>'Signifyd Ruby v2'}).
303
+ to_return(:status => 201, :body => "{\"investigationId\":14065}", :headers => {})
304
+ }
305
+
306
+ after {
307
+ Signifyd.api_key = nil
308
+ }
309
+
310
+ subject {
311
+ Signifyd.request(:post, '/v2/cases', hash)
312
+ }
313
+
314
+ it { should_not be_nil }
315
+ it { should be_true }
316
+ end
317
+
318
+ context 'with a non valid or nil API key' do
319
+ before {
320
+ Signifyd.api_key = nil
321
+ }
322
+
323
+ subject {
324
+ Signifyd.request(:post, '/v2/cases', hash)
325
+ }
326
+
327
+ it { lambda { subject }.should raise_error(Signifyd::AuthenticationError) }
328
+ end
329
+
330
+ context 'when VERIFY_NONE is set to false' do
331
+ before {
332
+ Signifyd.api_key = SIGNIFYD_API_KEY
333
+ Signifyd.verify_ssl_certs = false
334
+
335
+ stub_request(:post, "https://#{Signifyd.api_key}@api.signifyd.com/v2/cases").
336
+ with(:body => json, :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>json.size, 'Content-Type'=>'application/json', 'User-Agent'=>'Signifyd Ruby v2'}).
337
+ to_return(:status => 201, :body => "{\"investigationId\":14065}", :headers => {})
338
+ }
339
+
340
+ after {
341
+ Signifyd.api_key = nil
342
+ Signifyd.verify_ssl_certs = true
343
+ }
344
+
345
+ subject {
346
+ Signifyd.request(:post, '/v2/cases', hash)
347
+ }
348
+
349
+ it { should_not be_nil }
350
+ it { should be_true }
351
+ end
352
+
353
+ context 'when there is an authentication on invalid request error' do
354
+ context 'and returns a 400' do
355
+ before {
356
+ Signifyd.api_key = nil
357
+
358
+ stub_request(:post, "https://#{Signifyd.api_key}@api.signifyd.com/v2/cases").
359
+ with(:body => json, :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>json.size, 'Content-Type'=>'application/json', 'User-Agent'=>'Signifyd Ruby v2'}).
360
+ to_return(:status => 400, :body => "{\"investigationId\":14065}", :headers => {})
361
+ }
362
+
363
+ subject {
364
+ Signifyd.request(:post, '/v2/cases', hash)
365
+ }
366
+
367
+ it { lambda { subject }.should raise_error(Signifyd::AuthenticationError) }
368
+ end
369
+
370
+ context 'and returns a 404' do
371
+ before {
372
+ Signifyd.api_key = SIGNIFYD_API_KEY
373
+
374
+ stub_request(:post, "https://#{Signifyd.api_key}@api.signifyd.com/v2/cases").
375
+ with(:body => json, :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>json.size, 'Content-Type'=>'application/json', 'User-Agent'=>'Signifyd Ruby v2'}).
376
+ to_return(:status => 404, :body => "{\"investigationId\":14065}", :headers => {})
377
+ }
378
+
379
+ subject {
380
+ Signifyd.request(:post, '/v2/cases', hash)
381
+ }
382
+
383
+ it { lambda { subject }.should raise_error(Signifyd::InvalidRequestError) }
384
+ end
385
+ end
386
+ end
387
+
388
+ context '.handle_restclient_error' do
389
+ before {
390
+ Signifyd.api_key = nil
391
+ }
392
+
393
+ context 'when RestClient::ServerBrokeConnection OR RestClient::RequestTimeout' do
394
+ subject {
395
+ Signifyd.handle_restclient_error RestClient::ServerBrokeConnection.new
396
+ }
397
+
398
+ it { lambda { subject }.should raise_error(Signifyd::APIConnectionError) }
399
+ end
400
+
401
+ context 'when RestClient::SSLCertificateNotVerified' do
402
+ subject {
403
+ Signifyd.handle_restclient_error RestClient::SSLCertificateNotVerified.new('error')
404
+ }
405
+
406
+ it { lambda { subject }.should raise_error(Signifyd::APIConnectionError) }
407
+ end
408
+
409
+ context 'when SocketError' do
410
+ subject {
411
+ Signifyd.handle_restclient_error SocketError.new
412
+ }
413
+
414
+ it { lambda { subject }.should raise_error(Signifyd::APIConnectionError) }
415
+ end
416
+
417
+ context 'when StandardError' do
418
+ subject {
419
+ Signifyd.handle_restclient_error StandardError.new
420
+ }
421
+
422
+ it { lambda { subject }.should raise_error(Signifyd::APIConnectionError) }
423
+ end
424
+ end
425
+
426
+ context '.handle_api_error' do
427
+ before {
428
+ Signifyd.api_key = nil
429
+ }
430
+
431
+ context 'when 400 or 404' do
432
+ subject {
433
+ Signifyd.handle_api_error(400, "{\"error\":true}")
434
+ }
435
+
436
+ it { lambda { subject }.should raise_error(Signifyd::InvalidRequestError) }
437
+ end
438
+
439
+ context 'when 401' do
440
+ subject {
441
+ Signifyd.handle_api_error(401, "{\"error\":true}")
442
+ }
443
+
444
+ it { lambda { subject }.should raise_error(Signifyd::AuthenticationError) }
445
+ end
446
+
447
+ context 'when 500' do
448
+ subject {
449
+ Signifyd.handle_api_error(500, "{\"error\":true}")
450
+ }
451
+
452
+ it { lambda { subject }.should raise_error(Signifyd::APIError) }
453
+ end
454
+ end
455
+
456
+ context '.invalid_request_error' do
457
+ before {
458
+ @error = {}
459
+ @error[:message] = "Invalid request error"
460
+ @error[:param] = "Left out json body"
461
+ @rcode = 400
462
+ @rbody = "{\"error\":true}"
463
+ }
464
+
465
+ subject {
466
+ Signifyd.invalid_request_error @error, @rcode, @rbody
467
+ }
468
+
469
+ it { lambda { subject }.should raise_error(Signifyd::InvalidRequestError) }
470
+ end
471
+
472
+ context '.authentication_error' do
473
+ before {
474
+ @error = {}
475
+ @error[:message] = "Authentication Error"
476
+ @error[:param] = "Left out api key"
477
+ @rcode = 404
478
+ @rbody = "{\"error\":true}"
479
+ }
480
+
481
+ subject {
482
+ Signifyd.authentication_error @error, @rcode, @rbody
483
+ }
484
+
485
+ it { lambda { subject }.should raise_error(Signifyd::AuthenticationError) }
486
+ end
487
+
488
+ context '.general_api_error' do
489
+ before {
490
+ @error = {}
491
+ @error[:message] = "Api Error"
492
+ @error[:param] = "Left out api key"
493
+ @rcode = 404
494
+ @rbody = "{\"error\":true}"
495
+ }
496
+
497
+ subject {
498
+ Signifyd.general_api_error @rcode, @rbody
499
+ }
500
+
501
+ it { lambda { subject }.should raise_error(Signifyd::APIError) }
502
+ end
503
+ end