json-crud-api 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc410c49c2e6a7721caefeaf56701e7ffc074e51
4
- data.tar.gz: b93070f2495112662568de6719abd6a62e67a860
3
+ metadata.gz: 783895fbee97ba45427d51d8f0220e3aee4a60b9
4
+ data.tar.gz: 311de6db45e1d3db7bd3ac7108577d5f8fba1577
5
5
  SHA512:
6
- metadata.gz: c8e8b68269544a177a403737cb22fd933bc04f1ecaf2f722fa185e8e1795d5df474bafe6e0382d182c63bb5b9251f9473d2ca8ebe1c6a43bf45b5adc836163a4
7
- data.tar.gz: 3f269d7e5000223850301da05ac2fa96584aa8d83e8f7157b660f5acbaa3bc8d05d5304808d1834fa6f2b8094873556dc80ecf994834a9ba03bdedeab556602b
6
+ metadata.gz: 77dab5ab969c0a1450baa4933bddd00222cf5967cc2fd46d9ef6337d9d1935f2baccbc5fcd20f3b9e29dd6d2a1f318ce7705586cd4b430f10ef4c25ad34913af
7
+ data.tar.gz: 7d4bb5bedaaab7decf7876b8cdd846457ea669bdf820af2c889dff24c8842e20e1c541042bd5c13f2972d3e9e7a5c81e8b34ca8cb9d85357ac8abb291a3d3da4
@@ -26,8 +26,9 @@ module JsonCrudApi
26
26
  service = settings.services[key]
27
27
  presenter = settings.presenters[key]
28
28
  return fail_unauthorized unless service.user_authorized_for? :create
29
- entity = service.create(presenter.parse(@payload, :post))
30
-
29
+ post_data = presenter.parse @payload, :post
30
+ return fail_with_errors unless service.is_valid? post_data, :create, self
31
+ entity = service.create post_data
31
32
  JSON.fast_generate presenter.render(entity, :post)
32
33
  end
33
34
 
@@ -35,8 +36,10 @@ module JsonCrudApi
35
36
  service = settings.services[key]
36
37
  presenter = settings.presenters[key]
37
38
  return fail_unauthorized unless service.user_authorized_for? :update
38
- return fail_not_found unless service.update(params["id"], presenter.parse(@payload, :put))
39
- entity = service.get(params["id"])
39
+ put_data = presenter.parse @payload, :put
40
+ return fail_with_errors unless service.is_valid? put_data, :update, self
41
+ return fail_not_found unless service.update params["id"], put_data
42
+ entity = service.get params["id"]
40
43
  JSON.fast_generate presenter.render(entity, :put)
41
44
  end
42
45
 
@@ -44,7 +47,7 @@ module JsonCrudApi
44
47
  service = settings.services[key]
45
48
  presenter = settings.presenters[key]
46
49
  return fail_unauthorized unless service.user_authorized_for? :delete
47
- return fail_not_found unless service.delete(params["id"])
50
+ return fail_not_found unless service.delete params["id"]
48
51
  204
49
52
  end
50
53
  end
@@ -62,6 +62,11 @@ module JsonCrudApi
62
62
  @user_scopes = user_scopes
63
63
  end
64
64
 
65
+ # Find if the params are valid for an operation (defaults to true)
66
+ def valid_for?(params, operation, api_instance)
67
+ true
68
+ end
69
+
65
70
  # Determine if the current user is authorized for the given operation
66
71
  def user_authorized_for?(operation)
67
72
  # Auth is disabled if scope map is nil
@@ -0,0 +1,76 @@
1
+ require "spec_helper"
2
+
3
+ describe JsonCrudApi::CrudExtension do
4
+ before(:each) do
5
+ class CrudTest
6
+ attr_accessor :test_settings, :test_params, :payload
7
+ include JsonCrudApi::CrudExtension
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
+ end
@@ -143,6 +143,7 @@ describe JsonCrudApi::AuthClient do
143
143
  expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
144
144
 
145
145
  expect(@service).to receive(:user_authorized_for?).with(:create).and_return(true)
146
+ expect(@service).to receive(:is_valid?).with({ :test_output => 12398}, :create, @test).and_return(true)
146
147
  expect(@service).to receive(:create).with({ :test_output => 12398}).and_return({ :test_output => 77234})
147
148
 
148
149
  expect(@presenter).to receive(:parse).with(@test.payload, :post).and_return({ :test_output => 12398})
@@ -151,6 +152,23 @@ describe JsonCrudApi::AuthClient do
151
152
  expect(@test.send(:crud_post,'thekey')).to eq '{"test_output":12313}'
152
153
  end
153
154
 
155
+ it 'should fail with 422 if service is_valid? fails' 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(:create).and_return(true)
160
+ expect(@service).to receive(:is_valid?).with({ :test_output => 12398}, :create, @test).and_return(false)
161
+
162
+ expect(@presenter).to receive(:parse).with(@test.payload, :post).and_return({ :test_output => 12398})
163
+
164
+ expect(@test).to receive(:fail_with_errors)
165
+
166
+ expect(@service).not_to receive(:create)
167
+ expect(@presenter).not_to receive(:render)
168
+
169
+ @test.send(:crud_post,'thekey')
170
+ end
171
+
154
172
  it 'should fail_unauthorized if not authorized for create' do
155
173
  expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
156
174
  expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
@@ -184,20 +202,36 @@ describe JsonCrudApi::AuthClient do
184
202
  expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
185
203
 
186
204
  expect(@service).to receive(:user_authorized_for?).with(:update).and_return(true)
205
+ expect(@presenter).to receive(:parse).with(@test.payload, :put).and_return({ :test_output => 12398})
206
+ expect(@service).to receive(:is_valid?).with({ :test_output => 12398},:update,@test).and_return(true)
187
207
  expect(@service).to receive(:update).with(7345, { :test_output => 12398}).and_return(true)
188
208
  expect(@service).to receive(:get).with(7345).and_return({ :test_output => 77234})
189
209
 
190
- expect(@presenter).to receive(:parse).with(@test.payload, :put).and_return({ :test_output => 12398})
191
210
  expect(@presenter).to receive(:render).with({ :test_output => 77234}, :put).and_return({ :test_output => 12313})
192
211
 
193
212
  expect(@test.send(:crud_put,'thekey')).to eq '{"test_output":12313}'
194
213
  end
195
214
 
215
+ it 'should fail_with_errors if service is_valid? fails' do
216
+ expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
217
+ expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
218
+
219
+ expect(@service).to receive(:user_authorized_for?).with(:update).and_return(true)
220
+ expect(@presenter).to receive(:parse).with(@test.payload, :put).and_return({ :test_output => 12398})
221
+ expect(@service).to receive(:is_valid?).with({ :test_output => 12398},:update,@test).and_return(false)
222
+ expect(@presenter).not_to receive(:render)
223
+
224
+ expect(@test).to receive(:fail_with_errors)
225
+
226
+ expect(@test.send(:crud_put,'thekey'))
227
+ end
228
+
196
229
  it 'should fail_not_found if service update returned false' do
197
230
  expect(@test.test_settings.services).to receive(:[]).with('thekey').and_return(@service)
198
231
  expect(@test.test_settings.presenters).to receive(:[]).with('thekey').and_return(@presenter)
199
232
 
200
233
  expect(@service).to receive(:user_authorized_for?).with(:update).and_return(true)
234
+ expect(@service).to receive(:is_valid?).with({ :test_output => 12398},:update,@test).and_return(true)
201
235
  expect(@service).to receive(:update).with(7345, { :test_output => 12398}).and_return(false)
202
236
 
203
237
  expect(@test).to receive(:fail_not_found)
@@ -160,6 +160,12 @@ describe JsonCrudApi::Service do
160
160
  end
161
161
  end
162
162
 
163
+ describe '#valid_for?' do
164
+ it 'should return true' do
165
+ expect(@service.valid_for?(nil,nil,nil)).to be true
166
+ end
167
+ end
168
+
163
169
  describe '#user_authorized_for?' do
164
170
  it 'should return true if scope_map is nil' do
165
171
  @service.scope_map = nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-crud-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Cully
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-30 00:00:00.000000000 Z
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -159,6 +159,7 @@ files:
159
159
  - spec/spec_helper.rb
160
160
  - spec/unit/api_spec.rb
161
161
  - spec/unit/auth_client_spec.rb
162
+ - spec/unit/crud_extension_spec.rb
162
163
  - spec/unit/crud_spec.rb
163
164
  - spec/unit/json_errors_spec.rb
164
165
  - spec/unit/json_payload_spec.rb
@@ -193,6 +194,7 @@ test_files:
193
194
  - spec/spec_helper.rb
194
195
  - spec/unit/api_spec.rb
195
196
  - spec/unit/auth_client_spec.rb
197
+ - spec/unit/crud_extension_spec.rb
196
198
  - spec/unit/crud_spec.rb
197
199
  - spec/unit/json_errors_spec.rb
198
200
  - spec/unit/json_payload_spec.rb