beyonic 0.0.1

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,393 @@
1
+ require 'spec_helper'
2
+
3
+ describe Beyonic::Webhook do
4
+ describe ".crate" do
5
+ let(:payload) {
6
+ {
7
+ event: "payment.status.changed",
8
+ target: "https://my.callback.url/"
9
+ }
10
+ }
11
+
12
+ subject {
13
+ Beyonic.api_key = "my-authorization-token"
14
+ Beyonic::Webhook.create(payload)
15
+ }
16
+
17
+ context 'Success response' do
18
+ before {
19
+ stub_request(:post, "https://staging.beyonic.com/api/webhooks").to_return(
20
+ body: File.new('spec/examples/webhooks/create_response.json'))
21
+ }
22
+
23
+ it {
24
+ is_expected.to have_requested(:post, "https://staging.beyonic.com/api/webhooks").with(
25
+ headers: {"Authorization" => "Token my-authorization-token", "Beyonic-Version" => "v1"}
26
+ )
27
+ }
28
+ it { is_expected.to be_an(Beyonic::Webhook) }
29
+
30
+ it { is_expected.to have_attributes(id: 2, user: 2) }
31
+ end
32
+
33
+ context 'Bad request' do
34
+ before {
35
+ stub_request(:post, "https://staging.beyonic.com/api/webhooks").to_return(
36
+ body: File.new('spec/examples/webhooks/create_invalid_response.json'),
37
+ status: 400
38
+ )
39
+ }
40
+
41
+ subject {
42
+ -> {
43
+ Beyonic.api_key = "my-authorization-token"
44
+ Beyonic::Webhook.create(payload)
45
+ }
46
+ }
47
+ it {
48
+ is_expected.to raise_error(Beyonic::AbstractApi::ApiError)
49
+ }
50
+ end
51
+
52
+ context 'Unauthorized' do
53
+ before {
54
+ stub_request(:post, "https://staging.beyonic.com/api/webhooks").to_return(
55
+ body: File.new('spec/examples/invalid_token_error.json'),
56
+ status: 401
57
+ )
58
+ }
59
+
60
+ subject {
61
+ -> {
62
+ Beyonic.api_key = "my-authorization-token"
63
+ Beyonic::Webhook.create(payload)
64
+ }
65
+ }
66
+ it {
67
+ is_expected.to raise_error
68
+ }
69
+ end
70
+
71
+ end
72
+
73
+ describe ".list" do
74
+ subject {
75
+ Beyonic.api_key = "my-authorization-token"
76
+ Beyonic::Webhook.list
77
+ }
78
+
79
+ context 'Success response' do
80
+ before {
81
+ stub_request(:get, "https://staging.beyonic.com/api/webhooks").to_return(
82
+ body: File.new('spec/examples/webhooks/list_response.json'))
83
+ }
84
+
85
+ it {
86
+ is_expected.to have_requested(:get, "https://staging.beyonic.com/api/webhooks").with(
87
+ headers: {"Authorization" => "Token my-authorization-token", "Beyonic-Version" => "v1"}
88
+ )
89
+ }
90
+ it { is_expected.to be_an(Array) }
91
+ it { is_expected.to have(2).items }
92
+
93
+ it { is_expected.to all(be_an(Beyonic::Webhook)) }
94
+ end
95
+
96
+ context 'Unauthorized' do
97
+ before {
98
+ stub_request(:get, "https://staging.beyonic.com/api/webhooks").to_return(
99
+ body: File.new('spec/examples/invalid_token_error.json'),
100
+ status: 401
101
+ )
102
+ }
103
+
104
+ subject {
105
+ -> {
106
+ Beyonic.api_key = "my-authorization-token"
107
+ Beyonic::Webhook.list
108
+ }
109
+ }
110
+ it {
111
+ is_expected.to raise_error
112
+ }
113
+ end
114
+ end
115
+
116
+ describe ".get" do
117
+ subject {
118
+ Beyonic.api_key = "my-authorization-token"
119
+ Beyonic::Webhook.get(2)
120
+ }
121
+
122
+ context 'Success response' do
123
+ before {
124
+ stub_request(:get, "https://staging.beyonic.com/api/webhooks/2").to_return(
125
+ body: File.new('spec/examples/webhooks/get_response.json'))
126
+ }
127
+
128
+ it {
129
+ is_expected.to have_requested(:get, "https://staging.beyonic.com/api/webhooks/2").with(
130
+ headers: {"Authorization" => "Token my-authorization-token", "Beyonic-Version" => "v1"}
131
+ )
132
+ }
133
+ it { is_expected.to be_an(Beyonic::Webhook) }
134
+
135
+ it { is_expected.to have_attributes(id: 2, user: 2) }
136
+ end
137
+
138
+ context 'Unauthorized' do
139
+ before {
140
+ stub_request(:get, "https://staging.beyonic.com/api/webhooks/23").to_return(
141
+ body: File.new('spec/examples/invalid_token_error.json'),
142
+ status: 401
143
+ )
144
+ }
145
+
146
+ subject {
147
+ -> {
148
+ Beyonic.api_key = "my-authorization-token"
149
+ Beyonic::Webhook.get(2)
150
+ }
151
+ }
152
+ it {
153
+ is_expected.to raise_error
154
+ }
155
+ end
156
+
157
+ context 'Forbidden' do
158
+ before {
159
+ stub_request(:get, "https://staging.beyonic.com/api/webhooks/666").to_return(
160
+ body: File.new('spec/examples/no_permissions_error.json'),
161
+ status: 403
162
+ )
163
+ }
164
+
165
+ subject {
166
+ -> {
167
+ Beyonic.api_key = "my-authorization-token"
168
+ Beyonic::Webhook.get(666)
169
+ }
170
+ }
171
+ it {
172
+ is_expected.to raise_error
173
+ }
174
+ end
175
+ end
176
+
177
+ describe ".update" do
178
+ let(:payload) {
179
+ {
180
+ event: "payment.status.changed",
181
+ target: "https://my.callback2.url/"
182
+ }
183
+ }
184
+
185
+ subject {
186
+ Beyonic.api_key = "my-authorization-token"
187
+ Beyonic::Webhook.update(2, target: "https://my.callback2.url/")
188
+ }
189
+
190
+ context 'Success response' do
191
+ before {
192
+ stub_request(:patch, "https://staging.beyonic.com/api/webhooks/2").to_return(
193
+ body: File.new('spec/examples/webhooks/update_response.json'))
194
+ }
195
+
196
+ it {
197
+ is_expected.to have_requested(:patch, "https://staging.beyonic.com/api/webhooks/2").with(
198
+ headers: {"Authorization" => "Token my-authorization-token", "Beyonic-Version" => "v1"}
199
+ )
200
+ }
201
+ it { is_expected.to be_an(Beyonic::Webhook) }
202
+
203
+ it { is_expected.to have_attributes(id: 2, target: "https://my.callback2.url/") }
204
+ end
205
+
206
+
207
+ context 'Bad request' do
208
+ before {
209
+ stub_request(:patch, "https://staging.beyonic.com/api/webhooks/3").to_return(
210
+ body: File.new('spec/examples/webhooks/create_invalid_response.json'),
211
+ status: 400
212
+ )
213
+ }
214
+
215
+ subject {
216
+ -> {
217
+ Beyonic.api_key = "my-authorization-token"
218
+ Beyonic::Webhook.update(3, event: "wrongevent")
219
+ }
220
+ }
221
+ it {
222
+ is_expected.to raise_error(Beyonic::AbstractApi::ApiError)
223
+ }
224
+ end
225
+
226
+
227
+ context 'Forbidden' do
228
+ before {
229
+ stub_request(:patch, "https://staging.beyonic.com/api/webhooks/666").to_return(
230
+ body: File.new('spec/examples/no_permissions_error.json'),
231
+ status: 403
232
+ )
233
+ }
234
+
235
+ subject {
236
+ -> {
237
+ Beyonic.api_key = "my-authorization-token"
238
+ Beyonic::Webhook.update(666, target: "https://my.callback2.url/")
239
+ }
240
+ }
241
+ it {
242
+ is_expected.to raise_error
243
+ }
244
+ end
245
+
246
+ context 'Unauthorized' do
247
+ before {
248
+ stub_request(:patch, "https://staging.beyonic.com/api/webhooks/23").to_return(
249
+ body: File.new('spec/examples/invalid_token_error.json'),
250
+ status: 401
251
+ )
252
+ }
253
+
254
+ subject {
255
+ -> {
256
+ Beyonic.api_key = "my-authorization-token2"
257
+ Beyonic::Webhook.update(2, target: "https://my.callback2.url/")
258
+ }
259
+ }
260
+ it {
261
+ is_expected.to raise_error
262
+ }
263
+ end
264
+ end
265
+
266
+
267
+ describe ".delete" do
268
+
269
+ subject {
270
+ Beyonic.api_key = "my-authorization-token"
271
+ Beyonic::Webhook.delete(2)
272
+ }
273
+
274
+ context 'Success response' do
275
+ before {
276
+ stub_request(:delete, "https://staging.beyonic.com/api/webhooks/2").to_return(
277
+ status: 204)
278
+ }
279
+
280
+ it {
281
+ is_expected.to have_requested(:delete, "https://staging.beyonic.com/api/webhooks/2").with(
282
+ headers: {"Authorization" => "Token my-authorization-token", "Beyonic-Version" => "v1"}
283
+ )
284
+ }
285
+ it { is_expected.to be_truthy }
286
+
287
+ end
288
+
289
+ context 'Forbidden' do
290
+ before {
291
+ stub_request(:delete, "https://staging.beyonic.com/api/webhooks/666").to_return(
292
+ body: File.new('spec/examples/no_permissions_error.json'),
293
+ status: 403
294
+ )
295
+ }
296
+
297
+ subject {
298
+ -> {
299
+ Beyonic.api_key = "my-authorization-token"
300
+ Beyonic::Webhook.delete(666)
301
+ }
302
+ }
303
+ it {
304
+ is_expected.to raise_error
305
+ }
306
+ end
307
+
308
+ context 'Unauthorized' do
309
+ before {
310
+ stub_request(:delete, "https://staging.beyonic.com/api/webhooks/23").to_return(
311
+ body: File.new('spec/examples/invalid_token_error.json'),
312
+ status: 401
313
+ )
314
+ }
315
+
316
+ subject {
317
+ -> {
318
+ Beyonic.api_key = "my-authorization-token2"
319
+ Beyonic::Webhook.delete(2)
320
+ }
321
+ }
322
+ it {
323
+ is_expected.to raise_error
324
+ }
325
+ end
326
+ end
327
+
328
+ describe "#save" do
329
+ context 'new object' do
330
+ subject { Beyonic::Webhook }
331
+ let(:payload) {
332
+ {
333
+ event: "payment.status.changed",
334
+ target: "https://my.callback.url/"
335
+ }
336
+ }
337
+
338
+ before {
339
+ allow(subject).to receive(:create)
340
+ subject.new(payload).save
341
+ }
342
+
343
+ it {
344
+ is_expected.to have_received(:create).with(payload)
345
+ }
346
+ end
347
+
348
+ context 'loaded object' do
349
+ subject { Beyonic::Webhook }
350
+ before {
351
+ stub_request(:get, "https://staging.beyonic.com/api/webhooks/2").to_return(
352
+ body: File.new('spec/examples/webhooks/get_response.json'))
353
+ allow(subject).to receive(:update)
354
+
355
+ temp = subject.get(2)
356
+ temp.target = "https://google.com/"
357
+ temp.save
358
+ }
359
+
360
+ it {
361
+ is_expected.to have_received(:update).with(2, hash_including(target: "https://google.com/"))
362
+ }
363
+ end
364
+ end
365
+
366
+ describe "#id=" do
367
+ before {
368
+ stub_request(:get, "https://staging.beyonic.com/api/webhooks/2").to_return(
369
+ body: File.new('spec/examples/webhooks/get_response.json'))
370
+ }
371
+
372
+ let(:loaded_webhook) { Beyonic::Webhook.get(2) }
373
+
374
+ it {
375
+ expect{
376
+ loaded_webhook.id=(4)
377
+ }.to raise_error "Can't change id of existing Beyonic::Webhook"
378
+ }
379
+
380
+ it {
381
+ expect {
382
+ loaded_webhook[:id]=(4)
383
+ }.to raise_error "Can't change id of existing Beyonic::Webhook"
384
+ }
385
+
386
+ it {
387
+ expect {
388
+ loaded_webhook[:target]="foo"
389
+ }.to_not raise_error
390
+ }
391
+ end
392
+
393
+ end
@@ -0,0 +1,3 @@
1
+ {
2
+ detail: "Invalid token"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "detail": "You do not have permission to perform this action."
3
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "target": [
3
+ "This field is required."
4
+ ]
5
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "id": 3607,
3
+ "organization": 2,
4
+ "amount": "100.2",
5
+ "currency": "UGX",
6
+ "payment_type": "money",
7
+ "metadata": {
8
+ "id": "1234",
9
+ "name": "Lucy"
10
+ },
11
+ "description": "Per diem payment",
12
+ "phone_nos": [
13
+ "+256773712831"
14
+ ],
15
+ "state": "new",
16
+ "last_error": null,
17
+ "rejected_reason": null,
18
+ "rejected_by": null,
19
+ "rejected_time": null,
20
+ "cancelled_reason": null,
21
+ "cancelled_by": null,
22
+ "cancelled_time": null,
23
+ "created": "2014-11-30T16:08:25.421Z",
24
+ "author": 2,
25
+ "modified": "2014-11-30T16:08:25.422Z",
26
+ "updated_by": null,
27
+ "start_date": "2014-11-30T16:08:25.422Z"
28
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "id": 23,
3
+ "organization": 2,
4
+ "amount": "0.0000",
5
+ "currency": "UGX",
6
+ "payment_type": "money",
7
+ "metadata": null,
8
+ "description": "Test Payments",
9
+ "phone_nos": [],
10
+ "state": "processed_with_errors",
11
+ "last_error": "Error with payment 21. Error: (1062, \"Duplicate entry '6-2013-08-29 14:27:43-5000.0000' for key 'account_id_2'\") Type: IntegrityError",
12
+ "rejected_reason": null,
13
+ "rejected_by": null,
14
+ "rejected_time": null,
15
+ "cancelled_reason": null,
16
+ "cancelled_by": null,
17
+ "cancelled_time": null,
18
+ "created": "2013-08-29T14:27:42Z",
19
+ "author": 2,
20
+ "modified": "2013-09-29T23:26:02Z",
21
+ "updated_by": 1,
22
+ "start_date": "2013-08-29T00:00:00Z"
23
+ }