shutl_resource 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,329 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shutl::Resource::Rest do
4
+ let(:headers) do
5
+ {'Accept'=>'application/json', 'Authorization'=>'Bearer', 'Content-Type'=>'application/json'}
6
+ end
7
+
8
+ it 'should include the REST verb' do
9
+ TestRest.should respond_to :get
10
+ TestRest.should respond_to :post
11
+ TestRest.should respond_to :post
12
+ TestRest.should respond_to :delete
13
+ end
14
+
15
+ let(:resource) { TestRest.new(a: 'a', b: 2) }
16
+
17
+ describe '#find' do
18
+ context 'with no arguments' do
19
+ before do
20
+ @request = stub_request(:get, 'http://host/test_rests/a').
21
+ to_return(:status => 200,
22
+ :body => '{"test_rest": { "a": "a", "b": 2 }}',
23
+ :headers => headers)
24
+ end
25
+
26
+ it 'should query the endpoint' do
27
+ TestRest.find('a')
28
+
29
+ @request.should have_been_requested
30
+ end
31
+
32
+ it 'should parse the result of the body to create an object' do
33
+ resource = TestRest.find('a')
34
+
35
+ resource.should_not be_nil
36
+ resource.should be_kind_of TestRest
37
+ end
38
+
39
+ it 'should assign the attributes based on the json returned' do
40
+ resource = TestRest.find('a')
41
+
42
+ resource.instance_variable_get('@a').should == 'a'
43
+ resource.instance_variable_get('@b').should == 2
44
+ end
45
+
46
+ {
47
+ 400 => Shutl::BadRequest,
48
+ 401 => Shutl::UnauthorizedAccess,
49
+ 403 => Shutl::ForbiddenAccess,
50
+ 404 => Shutl::ResourceNotFound,
51
+ 409 => Shutl::ResourceConflict,
52
+ 410 => Shutl::ResourceGone,
53
+ 422 => Shutl::ResourceInvalid,
54
+ 503 => Shutl::ServiceUnavailable,
55
+ 501..502 => Shutl::ServerError,
56
+ 504..599 => Shutl::ServerError
57
+ }.each do |status, exception|
58
+ it "raises an #{exception} exception with a #{status}" do
59
+ if status.is_a? Range
60
+ status.each do |s|
61
+ stub_request(:get, 'http://host/test_rests/b').
62
+ to_return(status: s.to_i)
63
+
64
+ expect(->{TestRest.find('b')}).to raise_error(exception)
65
+ end
66
+ else
67
+ stub_request(:get, 'http://host/test_rests/b').
68
+ to_return(status: status)
69
+ lambda { TestRest.find('b') }.should raise_error(exception)
70
+
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ it 'should add a id based on the resource id' do
77
+ resource = TestRest.find('a')
78
+
79
+ resource.instance_variable_get('@id').should == 'a'
80
+ end
81
+ end
82
+
83
+ it 'should encode the url to support spaces' do
84
+ request = stub_request(:get, 'http://host/test_rests/new%20resource').
85
+ to_return(:status => 200, :body => '{"test_rest": {}}',
86
+ :headers => headers)
87
+
88
+ TestRest.find('new resource')
89
+
90
+ request.should have_been_requested
91
+ end
92
+
93
+ context 'with url arguments' do
94
+ before do
95
+ @request = stub_request(:get, 'http://host/test_rests/a?arg1=val1&arg2=val2').
96
+ to_return(:status => 200, :body => '{"test_rest": { "a": "a", "b": 2 }}', :headers => headers)
97
+ end
98
+
99
+ it 'should query the endpoint with the parameters' do
100
+ TestRest.find('a', arg1: 'val1', arg2: 'val2')
101
+
102
+ @request.should have_been_requested
103
+ end
104
+
105
+ end
106
+ end
107
+
108
+ describe '#all' do
109
+
110
+ context 'with no arguments' do
111
+ before do
112
+ @request = stub_request(:get, 'http://host/test_rests').
113
+ to_return(:status => 200, :body => '{"test_rests": [{ "a": "a", "b": 2 }]}', :headers => headers)
114
+ end
115
+
116
+ it 'should query the endpoint' do
117
+ TestRest.all
118
+
119
+ @request.should have_been_requested
120
+ end
121
+
122
+ it 'should parse the result of the body to create an array' do
123
+ resource = TestRest.all
124
+
125
+ resource.should have(1).item
126
+ end
127
+
128
+ it 'should assign the attributes based on the json returned' do
129
+ resource = TestRest.all
130
+
131
+ resource.first.instance_variable_get('@a').should == 'a'
132
+ resource.first.instance_variable_get('@b').should == 2
133
+ end
134
+
135
+ it 'should raise an error of the request fails' do
136
+ stub_request(:get, 'http://host/test_rests').
137
+ to_return(:status => 403)
138
+
139
+ lambda { TestRest.all}.should raise_error(Shutl::ForbiddenAccess)
140
+
141
+ end
142
+ end
143
+
144
+ context 'with no arguments' do
145
+ before do
146
+ @request = stub_request(:get, 'http://host/test_rests?arg1=val1&arg2=val2').
147
+ to_return(:status => 200, :body => '{"test_rests": [{ "a": "a", "b": 2 }]}', :headers => headers)
148
+ end
149
+
150
+ it 'should query the endpoint' do
151
+ TestRest.all(arg1: 'val1', arg2: 'val2')
152
+
153
+ @request.should have_been_requested
154
+ end
155
+ end
156
+ end
157
+
158
+ describe '.create' do
159
+
160
+ context "With the setting to not raise exceptions" do
161
+ before do
162
+ Shutl::Resource.raise_exceptions_on_validation = false
163
+ end
164
+
165
+ after do
166
+ Shutl::Resource.raise_exceptions_on_validation = true
167
+ end
168
+ specify do
169
+ errors = {"base" => "invalid", "some_field" => "some field is invalid"}
170
+ body = {"errors" => errors}.to_json
171
+
172
+ @request = stub_request(:post, 'http://host/test_rests').
173
+ to_return(:status => 422, body: body, :headers => headers)
174
+
175
+ expect{@instance = TestRest.create}.to_not raise_error Shutl::ResourceInvalid
176
+
177
+ @request.should have_been_requested
178
+ @instance.should_not be_valid
179
+ @instance.errors.should == errors
180
+ end
181
+ end
182
+
183
+
184
+ it 'should send a post request to the endpoint' do
185
+ request = stub_post 200
186
+
187
+ TestRest.create
188
+
189
+ request.should have_been_requested
190
+ end
191
+
192
+ def stub_post status
193
+ stub_request(:post, 'http://host/test_rests').
194
+ to_return(:status => status, :body => '', :headers => headers)
195
+ end
196
+
197
+ it 'should raise error if the remote server returns an error' do
198
+ request = stub_post 403
199
+ expect(->{TestRest.create}).to raise_error Shutl::ForbiddenAccess
200
+
201
+ request.should have_been_requested
202
+ end
203
+
204
+
205
+ it 'should post the header content-type: json' do
206
+ request = stub_request(:post, 'http://host/test_rests').
207
+ with(:body => "{\"test_rest\":{}}", :headers => headers )
208
+
209
+ TestRest.create
210
+
211
+ request.should have_been_requested
212
+ end
213
+
214
+ it 'should raise an exception if the create is called with the ! and it fails' do
215
+ request = stub_request(:post, 'http://host/test_rests').
216
+ to_return(:status => 400)
217
+
218
+ expect(->{ TestRest.create}).to raise_error(Shutl::BadRequest)
219
+ end
220
+
221
+
222
+ it 'shoud create a new ressource with the attributes' do
223
+ request = stub_request(:post, "http://host/test_rests").
224
+ with(body: '{"test_rest":{"a":"a","b":"b"}}',
225
+ headers: headers)
226
+
227
+ TestRest.create(a: 'a', b: 'b')
228
+
229
+ request.should have_been_requested
230
+ end
231
+
232
+ end
233
+
234
+ describe '#destroy' do
235
+
236
+ it 'should send a delete query to the endpoint' do
237
+ request = stub_request(:delete, 'http://host/test_rests/a')
238
+
239
+ TestRest.destroy id: 'a'
240
+
241
+ request.should have_been_requested
242
+ end
243
+
244
+ it 'should return true if the request succeeds' do
245
+ stub_request(:delete, 'http://host/test_rests/a').
246
+ to_return(status: 204)
247
+
248
+ TestRest.destroy(id: 'a').should eq(true)
249
+ end
250
+
251
+ it 'should return false if the request fails' do
252
+ stub_request(:delete, 'http://host/test_rests/a').
253
+ to_return(status: 400)
254
+
255
+ expect(->{TestRest.destroy(id: 'a')}).to raise_error Shutl::BadRequest
256
+ end
257
+ end
258
+
259
+ describe '#save' do
260
+ it 'should send a delete query to the endpoint' do
261
+ request = stub_request(:put, 'http://host/test_rests/a')
262
+
263
+ resource.save
264
+
265
+ request.should have_been_requested
266
+ end
267
+
268
+ it 'should return true if the request succeeds' do
269
+ stub_request(:put, 'http://host/test_rests/a').
270
+ to_return(status: 204)
271
+
272
+ resource.save.should eq(true)
273
+ end
274
+
275
+ it 'should return false if the request fails' do
276
+ stub_request(:put, 'http://host/test_rests/a').
277
+ to_return(status: 400)
278
+
279
+ ->{resource.save}.should raise_error Shutl::BadRequest
280
+ end
281
+
282
+
283
+ it 'should post in the body the json serialized resource' do
284
+ Hash.any_instance.stub(:to_json).and_return('JSON')
285
+ request = stub_request(:put, 'http://host/test_rests/a').
286
+ with(:body => 'JSON', headers: headers)
287
+
288
+ resource.save
289
+
290
+ request.should have_been_requested
291
+ end
292
+
293
+
294
+ it 'should raise an error if the update is called with the ! and it fails' do
295
+ stub_request(:put, 'http://host/test_rests/a').
296
+ to_return(status: 400)
297
+
298
+ expect(->{ resource.save }).to raise_error(Shutl::BadRequest)
299
+ end
300
+ end
301
+
302
+ describe '#update!' do
303
+ it 'should post the new json representation' do
304
+ request = stub_request(:put, "http://host/test_rests/a").
305
+ with(:body => '{"test_rest":{"a":"a","id":"a","b":"b"}}',
306
+ headers: headers)
307
+ test_resource = TestRest.new
308
+
309
+ test_resource.update!(a: 'a', b: 'b')
310
+
311
+ request.should have_been_requested
312
+ end
313
+
314
+ it 'should convert new_id to id in attributes' do
315
+ request = stub_request(:put, "http://host/test_rests/a").
316
+ with(:body => "{\"test_rest\":{\"a\":\"a\",\"id\":\"xxx\",\"b\":\"b\"}}",
317
+ :headers => headers)
318
+
319
+ test_resource = TestRest.new
320
+
321
+ test_resource.update!(a: 'a', b: 'b', new_id: 'xxx')
322
+
323
+ request.should have_been_requested
324
+ end
325
+ end
326
+
327
+
328
+
329
+ end
@@ -0,0 +1,20 @@
1
+ require 'shutl_resource'
2
+ require 'webmock/rspec'
3
+
4
+ require 'support/test_resource'
5
+ require 'support/double_logger'
6
+
7
+
8
+ require 'vcr'
9
+
10
+
11
+ VCR.configure do |c|
12
+ c.cassette_library_dir = 'spec/vcr'
13
+ c.hook_into :webmock
14
+ c.allow_http_connections_when_no_cassette = false
15
+ c.default_cassette_options = {
16
+ record: ENV['VCR_RERECORD'].present? ? :all : :once
17
+ }
18
+ end
19
+
20
+
@@ -0,0 +1,14 @@
1
+
2
+ module Shutl::Resource
3
+ class NoLogger
4
+ def debug(message) ; end
5
+ def info(message) ; end
6
+ def warn(message) ; end
7
+ def error(message) ; end
8
+ def fatal(message) ; end
9
+ end
10
+ end
11
+
12
+ Shutl::Resource.configure do |config|
13
+ config.logger = Shutl::Resource::NoLogger.new
14
+ end
@@ -0,0 +1,6 @@
1
+ class TestRest
2
+ include Shutl::Resource::Rest
3
+ base_uri 'http://host'
4
+ resource_id :a
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,248 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shutl_resource
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.8.0
6
+ platform: ruby
7
+ authors:
8
+ - David Rouchy
9
+ - Volker Pacher
10
+ - Mark Burns
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2013-01-19 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ type: :runtime
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.10.0
24
+ name: httparty
25
+ prerelease: false
26
+ requirement: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: 0.10.0
32
+ - !ruby/object:Gem::Dependency
33
+ type: :runtime
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 0.8.0
40
+ name: shutl_auth
41
+ prerelease: false
42
+ requirement: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.0
48
+ - !ruby/object:Gem::Dependency
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ name: activemodel
57
+ prerelease: false
58
+ requirement: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ - !ruby/object:Gem::Dependency
65
+ type: :development
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ name: rake
73
+ prerelease: false
74
+ requirement: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ - !ruby/object:Gem::Dependency
81
+ type: :development
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 2.11.0
88
+ name: rspec
89
+ prerelease: false
90
+ requirement: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ~>
94
+ - !ruby/object:Gem::Version
95
+ version: 2.11.0
96
+ - !ruby/object:Gem::Dependency
97
+ type: :development
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ name: debugger
105
+ prerelease: false
106
+ requirement: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ type: :development
114
+ version_requirements: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ name: vcr
121
+ prerelease: false
122
+ requirement: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ - !ruby/object:Gem::Dependency
129
+ type: :development
130
+ version_requirements: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ~>
134
+ - !ruby/object:Gem::Version
135
+ version: 1.8.7
136
+ name: webmock
137
+ prerelease: false
138
+ requirement: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ~>
142
+ - !ruby/object:Gem::Version
143
+ version: 1.8.7
144
+ description: Shutl Rest resource
145
+ email:
146
+ - davidr@shutl.co.uk
147
+ - volker@shutl.com
148
+ - mark@shutl.com
149
+ executables:
150
+ - autospec
151
+ - erubis
152
+ - htmldiff
153
+ - httparty
154
+ - httpclient
155
+ - ldiff
156
+ - rackup
157
+ - rails
158
+ - rake
159
+ - rake2thor
160
+ - rdebug
161
+ - ri
162
+ - rspec
163
+ - sprockets
164
+ - thor
165
+ - tilt
166
+ - tt
167
+ extensions: []
168
+ extra_rdoc_files: []
169
+ files:
170
+ - .gitignore
171
+ - .rbenv-version
172
+ - .rspec
173
+ - .rvmrc
174
+ - .travis.yml
175
+ - Gemfile
176
+ - LICENSE
177
+ - README.md
178
+ - Rakefile
179
+ - bin/autospec
180
+ - bin/erubis
181
+ - bin/htmldiff
182
+ - bin/httparty
183
+ - bin/httpclient
184
+ - bin/ldiff
185
+ - bin/rackup
186
+ - bin/rails
187
+ - bin/rake
188
+ - bin/rake2thor
189
+ - bin/rdebug
190
+ - bin/ri
191
+ - bin/rspec
192
+ - bin/sprockets
193
+ - bin/thor
194
+ - bin/tilt
195
+ - bin/tt
196
+ - lib/shutl/resource/configuration.rb
197
+ - lib/shutl/resource/errors.rb
198
+ - lib/shutl/resource/rest.rb
199
+ - lib/shutl/resource/rest_class_methods.rb
200
+ - lib/shutl/resource/version.rb
201
+ - lib/shutl_resource.rb
202
+ - script/ci
203
+ - shutl_resource.gemspec
204
+ - spec/configuration_spec.rb
205
+ - spec/dynamic_resource_spec.rb
206
+ - spec/remote_url_spec.rb
207
+ - spec/rest_resource_spec.rb
208
+ - spec/spec_helper.rb
209
+ - spec/support/double_logger.rb
210
+ - spec/support/test_resource.rb
211
+ homepage: ''
212
+ licenses: []
213
+ post_install_message:
214
+ rdoc_options: []
215
+ require_paths:
216
+ - lib
217
+ required_ruby_version: !ruby/object:Gem::Requirement
218
+ none: false
219
+ requirements:
220
+ - - ! '>='
221
+ - !ruby/object:Gem::Version
222
+ segments:
223
+ - 0
224
+ hash: -2260222592976093947
225
+ version: '0'
226
+ required_rubygems_version: !ruby/object:Gem::Requirement
227
+ none: false
228
+ requirements:
229
+ - - ! '>='
230
+ - !ruby/object:Gem::Version
231
+ segments:
232
+ - 0
233
+ hash: -2260222592976093947
234
+ version: '0'
235
+ requirements: []
236
+ rubyforge_project:
237
+ rubygems_version: 1.8.23
238
+ signing_key:
239
+ specification_version: 3
240
+ summary: Manage Shutl Rest resource. Parse/Serialize JSON
241
+ test_files:
242
+ - spec/configuration_spec.rb
243
+ - spec/dynamic_resource_spec.rb
244
+ - spec/remote_url_spec.rb
245
+ - spec/rest_resource_spec.rb
246
+ - spec/spec_helper.rb
247
+ - spec/support/double_logger.rb
248
+ - spec/support/test_resource.rb