json-crud-api 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json-crud-api/api.rb +5 -65
- data/lib/json-crud-api/crud.rb +49 -51
- data/lib/json-crud-api/json_errors.rb +45 -0
- data/lib/json-crud-api/json_payload.rb +21 -0
- data/lib/json-crud-api/presenter.rb +26 -18
- data/lib/json-crud-api/session.rb +24 -0
- data/lib/json-crud-api.rb +5 -2
- data/spec/spec_helper.rb +9 -0
- data/spec/unit/api_spec.rb +11 -0
- data/spec/unit/auth_client_spec.rb +30 -30
- data/spec/unit/crud_spec.rb +331 -0
- data/spec/unit/json_errors_spec.rb +120 -0
- data/spec/unit/json_payload_spec.rb +65 -0
- data/spec/unit/presenter_spec.rb +88 -67
- data/spec/unit/service_spec.rb +54 -54
- data/spec/unit/session_spec.rb +120 -0
- metadata +22 -65
- data/spec/helper.rb +0 -12
- data/spec/helpers_spec.rb +0 -3
@@ -0,0 +1,331 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe JsonCrudApi::AuthClient do
|
4
|
+
before(:each) do
|
5
|
+
class CrudTest
|
6
|
+
attr_accessor :test_settings, :test_params, :payload
|
7
|
+
include JsonCrudApi::Crud
|
8
|
+
|
9
|
+
def settings
|
10
|
+
@test_settings
|
11
|
+
end
|
12
|
+
|
13
|
+
def params
|
14
|
+
@test_params
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
@test = CrudTest.new
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#crud_api' do
|
22
|
+
it 'should enable get_all if configured' do
|
23
|
+
expect(@test).to receive(:get).with('/test')
|
24
|
+
|
25
|
+
expect(@test).not_to receive(:get)
|
26
|
+
expect(@test).not_to receive(:post)
|
27
|
+
expect(@test).not_to receive(:put)
|
28
|
+
expect(@test).not_to receive(:delete)
|
29
|
+
|
30
|
+
@test.crud_api('/test',:one, [:disable_write,:disable_get])
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should enable get if configured' do
|
34
|
+
expect(@test).to receive(:get).with('/test/:id')
|
35
|
+
|
36
|
+
expect(@test).not_to receive(:get).with('/test')
|
37
|
+
expect(@test).not_to receive(:post)
|
38
|
+
expect(@test).not_to receive(:put)
|
39
|
+
expect(@test).not_to receive(:delete)
|
40
|
+
|
41
|
+
@test.crud_api('/test',:one, [:disable_write,:disable_get_all])
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should enable post if configured' do
|
45
|
+
expect(@test).to receive(:post).with('/test')
|
46
|
+
|
47
|
+
expect(@test).not_to receive(:get)
|
48
|
+
expect(@test).not_to receive(:post)
|
49
|
+
expect(@test).not_to receive(:put)
|
50
|
+
expect(@test).not_to receive(:delete)
|
51
|
+
|
52
|
+
@test.crud_api('/test',:one, [:disable_put,:disable_delete,:disable_read])
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should enable put if configured' do
|
56
|
+
expect(@test).to receive(:put).with('/test/:id')
|
57
|
+
|
58
|
+
expect(@test).not_to receive(:get)
|
59
|
+
expect(@test).not_to receive(:post)
|
60
|
+
expect(@test).not_to receive(:delete)
|
61
|
+
|
62
|
+
@test.crud_api('/test',:one, [:disable_post,:disable_delete,:disable_read])
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should enable delete if configured' do
|
66
|
+
expect(@test).to receive(:delete).with('/test/:id')
|
67
|
+
|
68
|
+
expect(@test).not_to receive(:get)
|
69
|
+
expect(@test).not_to receive(:post)
|
70
|
+
expect(@test).not_to receive(:put)
|
71
|
+
|
72
|
+
@test.crud_api('/test',:one, [:disable_post,:disable_put,:disable_read])
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#crud_get_all' do
|
78
|
+
|
79
|
+
before do
|
80
|
+
@service = double('service')
|
81
|
+
@presenter = double('presenter')
|
82
|
+
|
83
|
+
@test.test_settings = OpenStruct.new({
|
84
|
+
:services=>OpenStruct.new,
|
85
|
+
:presenters=>OpenStruct.new
|
86
|
+
})
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should call get_all on service, render on the presenter, and return JSON' do
|
90
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
91
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
92
|
+
|
93
|
+
expect(@service).to receive(:user_authorized_for?).with(:get_all).and_return(true)
|
94
|
+
expect(@service).to receive(:get_all).and_return([])
|
95
|
+
|
96
|
+
expect(@presenter).to receive(:render).with([], :get_all).and_return({ :test_output => 1})
|
97
|
+
|
98
|
+
expect(@test.send(:crud_get_all,'thekey')).to eq '{"test_output":1}'
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should fail_unauthorized if not authorized for get_all' do
|
102
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
103
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
104
|
+
|
105
|
+
expect(@service).to receive(:user_authorized_for?).with(:get_all).and_return(false)
|
106
|
+
|
107
|
+
expect(@test).to receive(:fail_unauthorized)
|
108
|
+
|
109
|
+
expect(@service).not_to receive(:get_all)
|
110
|
+
expect(@presenter).not_to receive(:render)
|
111
|
+
|
112
|
+
@test.send(:crud_get_all,'thekey')
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should fail_not_found if service returned nil' do
|
116
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
117
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
118
|
+
|
119
|
+
expect(@service).to receive(:user_authorized_for?).with(:get_all).and_return(true)
|
120
|
+
expect(@service).to receive(:get_all).and_return(nil)
|
121
|
+
|
122
|
+
expect(@test).to receive(:fail_not_found)
|
123
|
+
|
124
|
+
expect(@service).not_to receive(:get_all)
|
125
|
+
expect(@presenter).not_to receive(:render)
|
126
|
+
|
127
|
+
@test.send(:crud_get_all,'thekey')
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe '#crud_get' do
|
132
|
+
before do
|
133
|
+
@service = double('service')
|
134
|
+
@presenter = double('presenter')
|
135
|
+
|
136
|
+
@test.test_settings = OpenStruct.new({
|
137
|
+
:services=>OpenStruct.new,
|
138
|
+
:presenters=>OpenStruct.new
|
139
|
+
})
|
140
|
+
@test.test_params = { "id" => 234 }
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should call get on service, render on the presenter, and return JSON' do
|
144
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
145
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
146
|
+
|
147
|
+
expect(@service).to receive(:user_authorized_for?).with(:get).and_return(true)
|
148
|
+
expect(@service).to receive(:get).with(234).and_return([])
|
149
|
+
|
150
|
+
expect(@presenter).to receive(:render).with([], :get).and_return({ :test_output => 56})
|
151
|
+
|
152
|
+
expect(@test.send(:crud_get,'thekey')).to eq '{"test_output":56}'
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'should fail_unauthorized if not authorized for get' do
|
156
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
157
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
158
|
+
|
159
|
+
expect(@service).to receive(:user_authorized_for?).with(:get).and_return(false)
|
160
|
+
|
161
|
+
expect(@test).to receive(:fail_unauthorized)
|
162
|
+
|
163
|
+
expect(@service).not_to receive(:get)
|
164
|
+
expect(@presenter).not_to receive(:render)
|
165
|
+
|
166
|
+
@test.send(:crud_get,'thekey')
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'should fail_not_found if service returned nil' do
|
170
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
171
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
172
|
+
|
173
|
+
expect(@service).to receive(:user_authorized_for?).with(:get).and_return(true)
|
174
|
+
expect(@service).to receive(:get).with(234).and_return(nil)
|
175
|
+
|
176
|
+
expect(@test).to receive(:fail_not_found)
|
177
|
+
|
178
|
+
expect(@service).not_to receive(:get)
|
179
|
+
expect(@presenter).not_to receive(:render)
|
180
|
+
|
181
|
+
@test.send(:crud_get,'thekey')
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
describe '#crud_post' do
|
186
|
+
before do
|
187
|
+
@service = double('service')
|
188
|
+
@presenter = double('presenter')
|
189
|
+
|
190
|
+
@test.test_settings = OpenStruct.new({
|
191
|
+
:services=>OpenStruct.new,
|
192
|
+
:presenters=>OpenStruct.new
|
193
|
+
})
|
194
|
+
@test.payload = { :test_payload_int => 12313 }
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'should call create on service, parse on the presenter, render on the presenter, and return JSON' do
|
198
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
199
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
200
|
+
|
201
|
+
expect(@service).to receive(:user_authorized_for?).with(:create).and_return(true)
|
202
|
+
expect(@service).to receive(:create).with({ :test_output => 12398}).and_return({ :test_output => 77234})
|
203
|
+
|
204
|
+
expect(@presenter).to receive(:parse).with(@test.payload, :post).and_return({ :test_output => 12398})
|
205
|
+
expect(@presenter).to receive(:render).with({ :test_output => 77234}, :post).and_return({ :test_output => 12313})
|
206
|
+
|
207
|
+
expect(@test.send(:crud_post,'thekey')).to eq '{"test_output":12313}'
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'should fail_unauthorized if not authorized for create' do
|
211
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
212
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
213
|
+
|
214
|
+
expect(@service).to receive(:user_authorized_for?).with(:create).and_return(false)
|
215
|
+
|
216
|
+
expect(@test).to receive(:fail_unauthorized)
|
217
|
+
|
218
|
+
expect(@service).not_to receive(:create)
|
219
|
+
expect(@presenter).not_to receive(:render)
|
220
|
+
|
221
|
+
@test.send(:crud_post,'thekey')
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
describe '#crud_put' do
|
226
|
+
before do
|
227
|
+
@service = double('service')
|
228
|
+
@presenter = double('presenter')
|
229
|
+
|
230
|
+
@test.test_settings = OpenStruct.new({
|
231
|
+
:services=>OpenStruct.new,
|
232
|
+
:presenters=>OpenStruct.new
|
233
|
+
})
|
234
|
+
@test.test_params = { "id" => 7345 }
|
235
|
+
@test.payload = { :test_payload_int => 12313 }
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'should call update on service, get on the service, parse on the presenter, render on the presenter, and return JSON' do
|
239
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
240
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
241
|
+
|
242
|
+
expect(@service).to receive(:user_authorized_for?).with(:update).and_return(true)
|
243
|
+
expect(@service).to receive(:update).with(7345, { :test_output => 12398}).and_return(true)
|
244
|
+
expect(@service).to receive(:get).with(7345).and_return({ :test_output => 77234})
|
245
|
+
|
246
|
+
expect(@presenter).to receive(:parse).with(@test.payload, :put).and_return({ :test_output => 12398})
|
247
|
+
expect(@presenter).to receive(:render).with({ :test_output => 77234}, :put).and_return({ :test_output => 12313})
|
248
|
+
|
249
|
+
expect(@test.send(:crud_put,'thekey')).to eq '{"test_output":12313}'
|
250
|
+
end
|
251
|
+
|
252
|
+
it 'should fail_not_found if service update returned false' do
|
253
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
254
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
255
|
+
|
256
|
+
expect(@service).to receive(:user_authorized_for?).with(:update).and_return(true)
|
257
|
+
expect(@service).to receive(:update).with(7345, { :test_output => 12398}).and_return(false)
|
258
|
+
|
259
|
+
expect(@test).to receive(:fail_not_found)
|
260
|
+
|
261
|
+
expect(@presenter).to receive(:parse).with(@test.payload, :put).and_return({ :test_output => 12398})
|
262
|
+
expect(@presenter).not_to receive(:render)
|
263
|
+
|
264
|
+
@test.send(:crud_put,'thekey')
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'should fail_unauthorized if not authorized for update' do
|
268
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
269
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
270
|
+
|
271
|
+
expect(@service).to receive(:user_authorized_for?).with(:update).and_return(false)
|
272
|
+
|
273
|
+
expect(@test).to receive(:fail_unauthorized)
|
274
|
+
|
275
|
+
expect(@service).not_to receive(:update)
|
276
|
+
expect(@service).not_to receive(:get)
|
277
|
+
expect(@presenter).not_to receive(:render)
|
278
|
+
|
279
|
+
@test.send(:crud_put,'thekey')
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
describe '#crud_delete' do
|
284
|
+
before do
|
285
|
+
@service = double('service')
|
286
|
+
@presenter = double('presenter')
|
287
|
+
|
288
|
+
@test.test_settings = OpenStruct.new({
|
289
|
+
:services=>OpenStruct.new,
|
290
|
+
:presenters=>OpenStruct.new
|
291
|
+
})
|
292
|
+
@test.test_params = { "id" => 234 }
|
293
|
+
end
|
294
|
+
|
295
|
+
it 'should call delete on service and return 204' do
|
296
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
297
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
298
|
+
|
299
|
+
expect(@service).to receive(:user_authorized_for?).with(:delete).and_return(true)
|
300
|
+
expect(@service).to receive(:delete).with(234).and_return(true)
|
301
|
+
|
302
|
+
expect(@test.send(:crud_delete,'thekey')).to eq 204
|
303
|
+
end
|
304
|
+
|
305
|
+
it 'should fail_unauthorized if not authorized for delete' do
|
306
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
307
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
308
|
+
|
309
|
+
expect(@service).to receive(:user_authorized_for?).with(:delete).and_return(false)
|
310
|
+
|
311
|
+
expect(@test).to receive(:fail_unauthorized)
|
312
|
+
|
313
|
+
expect(@service).not_to receive(:delete)
|
314
|
+
|
315
|
+
@test.send(:crud_delete,'thekey')
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'should fail_not_found if service delete returned false' do
|
319
|
+
expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
|
320
|
+
expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
|
321
|
+
|
322
|
+
expect(@service).to receive(:user_authorized_for?).with(:delete).and_return(true)
|
323
|
+
expect(@service).to receive(:delete).with(234).and_return(false)
|
324
|
+
|
325
|
+
expect(@test).to receive(:fail_not_found)
|
326
|
+
|
327
|
+
@test.send(:crud_delete,'thekey')
|
328
|
+
end
|
329
|
+
|
330
|
+
end
|
331
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe JsonCrudApi::JsonErrors do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class TestClass
|
7
|
+
include JsonCrudApi::JsonErrors
|
8
|
+
|
9
|
+
attr_accessor :errors
|
10
|
+
|
11
|
+
def env
|
12
|
+
@test_env
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
@test = TestClass.new
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#clear_errors' do
|
20
|
+
it 'should set @errors to []' do
|
21
|
+
|
22
|
+
@test.errors = 9123878123
|
23
|
+
@test.clear_errors
|
24
|
+
|
25
|
+
expect(@test.errors).to eq []
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#add_error' do
|
30
|
+
it 'should initialise @errors if not set' do
|
31
|
+
@test.errors = nil
|
32
|
+
@test.add_error(1,2)
|
33
|
+
|
34
|
+
expect(@test.errors).not_to be nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should add an error with correct structure' do
|
38
|
+
@test.add_error(1,2)
|
39
|
+
|
40
|
+
expect(@test.errors).to eq([{ :code =>1, :message =>2}])
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should add an error with a reference if specified' do
|
44
|
+
@test.add_error(1,2,3)
|
45
|
+
|
46
|
+
expect(@test.errors).to eq([{ :code =>1, :message =>2 ,:reference =>3}])
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should add multiple errors' do
|
50
|
+
@test.add_error(1,2,3)
|
51
|
+
@test.add_error(4,5,6)
|
52
|
+
|
53
|
+
expect(@test.errors).to eq([
|
54
|
+
{ :code =>1, :message =>2 ,:reference =>3},
|
55
|
+
{ :code =>4, :message =>5 ,:reference =>6}
|
56
|
+
])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#fail_with_error' do
|
61
|
+
it 'should call add_error and fail_with_errors correctly' do
|
62
|
+
expect(@test).to receive(:add_error).with(2,3,4)
|
63
|
+
expect(@test).to receive(:fail_with_errors).with(422)
|
64
|
+
|
65
|
+
@test.fail_with_error(422,2,3,4)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should respect existing errors' do
|
69
|
+
@test.add_error(1,2,3)
|
70
|
+
|
71
|
+
expect(@test).to receive(:fail_with_errors).with(422)
|
72
|
+
|
73
|
+
@test.fail_with_error(422,2,3,4)
|
74
|
+
|
75
|
+
expect(@test.errors).to eq([
|
76
|
+
{ :code =>1, :message =>2 ,:reference =>3},
|
77
|
+
{ :code =>2, :message =>3 ,:reference =>4}
|
78
|
+
])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#fail_with_errors' do
|
83
|
+
it 'should call halt with correct status and JSON' do
|
84
|
+
@test.errors = { :one =>1, :two =>2 }
|
85
|
+
|
86
|
+
expect(@test).to receive(:halt).with(504, '{"success":false,"errors":{"one":1,"two":2}}')
|
87
|
+
|
88
|
+
@test.fail_with_errors(504)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should call halt with default 422 status and JSON' do
|
92
|
+
@test.errors = { :one =>1, :two =>2 }
|
93
|
+
|
94
|
+
expect(@test).to receive(:halt).with(422, '{"success":false,"errors":{"one":1,"two":2}}')
|
95
|
+
|
96
|
+
@test.fail_with_errors
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#fail_not_found' do
|
101
|
+
it 'should call fail_not_found with 404 and message' do
|
102
|
+
expect(@test).to receive(:fail_with_error).with(404, 'NOT_FOUND','The resource cannot be found.')
|
103
|
+
@test.fail_not_found
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#fail_unauthorized' do
|
108
|
+
it 'should call fail_not_found with 404 and message' do
|
109
|
+
expect(@test).to receive(:fail_with_error).with(401, 'UNAUTHORIZED','Authorization is required to perform this operation on the resource.')
|
110
|
+
@test.fail_unauthorized
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#fail_forbidden' do
|
115
|
+
it 'should call fail_not_found with 404 and message' do
|
116
|
+
expect(@test).to receive(:fail_with_error).with(403, 'FORBIDDEN','The user is not allowed to perform this operation on the resource.')
|
117
|
+
@test.fail_forbidden
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe JsonCrudApi::JsonPayload do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class TestClass
|
7
|
+
attr_accessor :request, :payload
|
8
|
+
|
9
|
+
include JsonCrudApi::JsonPayload
|
10
|
+
end
|
11
|
+
@test = TestClass.new
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#process_json_payload' do
|
15
|
+
it 'should call rewind on request.body' do
|
16
|
+
|
17
|
+
@test.request = OpenStruct.new({
|
18
|
+
:body => OpenStruct.new({
|
19
|
+
:read => OpenStruct.new({:length => 0})
|
20
|
+
})
|
21
|
+
})
|
22
|
+
expect(@test.request.body).to receive(:rewind)
|
23
|
+
|
24
|
+
@test.process_json_payload
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should call read and return with set payload nil if body length is less than 2' do
|
28
|
+
@test.request = OpenStruct.new({
|
29
|
+
:body => OpenStruct.new
|
30
|
+
})
|
31
|
+
expect(@test.request.body).to receive(:rewind)
|
32
|
+
expect(@test.request.body).to receive(:read).and_return('')
|
33
|
+
|
34
|
+
@test.process_json_payload
|
35
|
+
|
36
|
+
expect(@test.payload).to be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should parse the body json into @payload, symbolizing names' do
|
40
|
+
@test.request = OpenStruct.new({
|
41
|
+
:body => OpenStruct.new
|
42
|
+
})
|
43
|
+
expect(@test.request.body).to receive(:rewind)
|
44
|
+
expect(@test.request.body).to receive(:read).and_return('{"one":1,"two":2}')
|
45
|
+
|
46
|
+
@test.process_json_payload
|
47
|
+
|
48
|
+
expect(@test.payload).to eq({ :one=>1, :two =>2})
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should set @payload nil and call fail_with_error is JSON parse fails' do
|
52
|
+
@test.request = OpenStruct.new({
|
53
|
+
:body => OpenStruct.new
|
54
|
+
})
|
55
|
+
expect(@test.request.body).to receive(:rewind)
|
56
|
+
expect(@test.request.body).to receive(:read).and_return('fw4oihwafosaf8')
|
57
|
+
|
58
|
+
expect(@test).to receive(:fail_with_error).with(422, 'JSON_PARSE_ERROR', 'The JSON payload cannot be parsed')
|
59
|
+
|
60
|
+
@test.process_json_payload
|
61
|
+
|
62
|
+
expect(@test.payload).to be_nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|