This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
/
model_spec.rb
437 lines (357 loc) · 12.4 KB
/
model_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
require File.dirname(__FILE__) + '/../spec_helper'
describe Remotely::Model do
let(:app) { "http://localhost:1234" }
let(:attributes) { {id: 1, name: "Marceline Quest", type: "MATHEMATICAL!"} }
subject { Adventure.new(attributes) }
describe ".attr_savable" do
let(:attrs) { {id: 2, name: "Wishes!", type: "MATHEMATICAL!", length: 9} }
let(:saved) { to_json({name: "OMG New Name!", type: "MATHEMATICAL!", id: 2}) }
subject { Adventure.new(attrs) }
it "stores which attributes are savable" do
Adventure.savable_attributes.should == [:name, :type]
end
it "only sends the specified attributes when saving an existing record" do
stub_request(:put, "#{app}/adventures/2").to_return(body: saved)
subject.update_attribute(:name, "OMG New Name!")
a_request(:put, "#{app}/adventures/2").with(body: saved).should have_been_made
end
end
describe ".find" do
it "retreives an individual resource" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").should have_been_made
end
end
describe ".where" do
it "searches for resources" do
Adventure.where(:type => "MATHEMATICAL!")
a_request(:get, "#{app}/adventures/search?type=MATHEMATICAL!").should have_been_made
end
it "returns a collection of resources" do
Adventure.where(:type => "MATHEMATICAL!").should be_a Remotely::Collection
end
end
describe ".destroy" do
it "destroys the resource" do
Adventure.destroy(1)
a_request(:delete, "#{app}/adventures/1").should have_been_made
end
it "returns true on success" do
Adventure.destroy(1).should be_true
end
it "returns false on failure" do
stub_request(:delete, %r[/adventures/1]).to_return(status: 500)
Adventure.destroy(1).should be_false
end
end
describe ".create" do
let(:attrs) { attributes.except(:id) }
before do
stub_request(:post, "#{app}/adventures").to_return(lambda { |req| { body: req.body, status: 201 }})
end
it "creates the resource" do
Adventure.create(attrs)
a_request(:post, "#{app}/adventures").with(attrs).should have_been_made
end
it "returns the new resource on creation" do
Adventure.create(attrs).name.should == "Marceline Quest"
end
it "returns an instance with errors when the creation fails" do
body = MultiJson.dump({errors: {base: ["error"]}})
stub_request(:post, %r[/adventures]).to_return(status: 500, body: body)
Adventure.create(attrs).errors[:base].should include("error")
end
end
describe ".find_or_" do
let(:body) { MultiJson.dump([{id: 1, name: "BubbleGum"}]) }
let(:stub_success) { stub_request(:get, "#{app}/adventures/search?name=BubbleGum").to_return(body: body) }
let(:stub_failure) { stub_request(:get, "#{app}/adventures/search?name=BubbleGum").to_return(body: "[]") }
describe "initialize" do
it "tries to fetch the record" do
stub_success
Adventure.find_or_initialize(name: "BubbleGum")
a_request(:get, "#{app}/adventures/search?name=BubbleGum").should have_been_made
end
it "returns the fetched object if found" do
stub_success
Adventure.find_or_initialize(name: "BubbleGum").id.should == 1
end
it "creates a new object if one is not found" do
stub_failure
Adventure.find_or_initialize(name: "BubbleGum").should be_a_new_record
end
end
describe "create" do
it "automatically saves the new object" do
stub_failure
Adventure.should_receive(:create).with(name: "BubbleGum")
Adventure.find_or_create(name: "BubbleGum")
end
it "returns the first item from the collection" do
stub_success
Adventure.find_or_create(name: "BubbleGum").should be_an Adventure
end
end
end
describe ".find_by_*" do
it "searches by a single attribute" do
Adventure.find_by_name("Fun")
a_request(:get, "#{app}/adventures/search?name=Fun").should have_been_made
end
it "searches by multiple attributes seperated by 'and'" do
Adventure.find_by_name_and_type("Fun", "MATHEMATICAL!")
a_request(:get, "#{app}/adventures/search?name=Fun&type=MATHEMATICAL!").should have_been_made
end
end
describe ".all" do
it "fetches all resources" do
Adventure.all
a_request(:get, "#{app}/adventures").should have_been_made
end
end
describe ".update_all" do
it "request an update to all entries" do
Adventure.update_all(type: "awesome")
a_request(:put, "#{app}/adventures").with(type: "awesome").should have_been_made
end
end
describe "#save" do
let(:new_name) { "City of Thieves" }
let(:new_attributes) { attributes.merge(name: new_name) }
context "when updating" do
it "updates the resource" do
adventure = Adventure.new(attributes)
adventure.name = new_name
adventure.save
a_request(:put, "#{app}/adventures/1").with(new_attributes).should have_been_made
end
it "returns true when the save succeeds" do
Adventure.new(attributes).save.should be_true
end
it "sets errors when a save fails" do
adventure = Adventure.new(attributes)
stub_request(:put, %r[/adventures/1]).to_return(status: 409, body: to_json({errors: {base: %w{this failed}}}))
adventure.save
adventure.errors[:base].should == %w{this failed}
end
end
context "when creating" do
let!(:adventure) { Adventure.new(name: "To Be Saved...") }
it "merges in the response body to attributes on success" do
stub_request(:post, %r(/adventures)).to_return(body: to_json(attributes.merge(name: "To Be Saved...", id: 2)), status: 201)
adventure.save
adventure.id.should == 2
end
it "sets errors on a failure" do
body = MultiJson.dump({errors: {base: ["error"]}})
stub_request(:post, %r(/adventures)).to_return(status: 409, body: body)
adventure.save
adventure.errors.should_not be_empty
end
end
end
describe "#update_attribute" do
it "updates a single attribute and saves" do
subject.update_attribute(:type, "powerful")
a_request(:put, "#{app}/adventures/1").with(type: "powerful").should have_been_made
end
end
describe "#to_param" do
it "returns correct value" do
subject.to_param.should == '1'
end
end
describe "#update_attributes" do
let(:updates) { {type: "awesome"} }
let(:new_attributes) { subject.attributes.merge(updates) }
it "replaces existing attribute values" do
subject.update_attributes(updates)
subject.type.should == "awesome"
end
it "calls save" do
subject.update_attributes(updates)
a_request(:put, "#{app}/adventures/1").with(new_attributes).should have_been_made
end
it "returns true on success" do
subject.update_attributes(updates).should be_true
end
it "sets errors on failure" do
body = MultiJson.dump({errors: {base: ["error"]}})
stub_request(:put, %r[/adventures/1]).to_return(status: 500, body: body)
subject.update_attributes(updates)
subject.errors[:base].should include("error")
end
end
describe "#destroy" do
it "destroys a resource with the might of 60 jotun!!" do
Adventure.new(attributes).destroy
a_request(:delete, "#{app}/adventures/1").should have_been_made
end
end
describe "associations" do
let(:member) { Member.new(id: 2, name_id: 1) }
it "creates associations when instantiated" do
member.should respond_to :name
end
it "fetches the resource when accessed" do
Name.should_receive(:find).with(1)
member.name
end
it "doesn't fetch a resource twice" do
Name.should_receive(:find).with(1).once
member.name
member.name
end
it "reloads association objects" do
Name.should_receive(:find).with(1).twice
member.name
member.name(true)
end
end
context "basic auth" do
before do
Remotely.configure do
app :adventure_app do
url "http://localhost:3000"
basic_auth "user", "password"
end
end
end
after do
Remotely.reset!
end
it "sends Authorization headers when basic auth is configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "Basic dXNlcjpwYXNzd29yZA=="})
end
it "doesn't send Authorization headers when basic auth is not configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end
context "token auth" do
before do
Remotely.configure do
app :adventure_app do
url "http://localhost:3000"
token_auth "token", {:foo => :bar}
end
end
end
after do
Remotely.reset!
end
it "sends Authorization headers when token auth is configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "Token token=token foo=bar"})
end
it "doesn't send Authorization headers when token auth is not configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end
context "custom authorization as a string" do
before do
Remotely.configure do
app :adventure_app do
url "http://localhost:3000"
authorization "OAuth", "token=foo"
end
end
end
after do
Remotely.reset!
end
it "sends Authorization headers when custom authorization is configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "OAuth token=foo"})
end
it "doesn't send Authorization headers when custom authorization is not configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end
context "custom authorization as a hash" do
before do
Remotely.configure do
app :adventure_app do
url "http://localhost:3000"
authorization "OAuth", {:token => :foo}
end
end
end
after do
Remotely.reset!
end
it "sends Authorization headers when custom authorization is configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "OAuth token=foo"})
end
it "doesn't send Authorization headers when custom authorization is not configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end
it "sets the app it belongs to" do
Adventure.app.name.should == :adventure_app
end
it "sets the uri to itself" do
Adventure.uri.should == "/adventures"
end
it "has a connection" do
Adventure.app.connection.should be_a Faraday::Connection
end
it "supports ActiveModel::Naming methods" do
Adventure.model_name.element.should == "adventure"
end
it "is reloadable" do
subject.reload
a_request(:get, "#{app}/adventures/1")
end
it "symbolizes attribute keys" do
subject.attributes.should == attributes
end
it "can be initialized with a hash of attribute/values" do
subject.name.should == "Marceline Quest"
end
it "sets an attribute value" do
subject.name = "City of Thieves"
subject.name.should == "City of Thieves"
end
it "raises a normal NoMethodError for non-existent attributes" do
expect { subject.height }.to raise_error(NoMethodError)
end
it "is a new_record when no id exists" do
subject.id = nil
subject.should be_a_new_record
end
it "creates boolean methods for each attribute" do
subject.name?.should == true
end
it "returns id from #to_key" do
subject.id = 1
subject.to_key.should == [1]
end
it "returns id from #to_param" do
subject.id = 1
subject.to_param.should == "1"
end
it "returns itself from #to_model" do
subject.to_model.should == subject
end
context "with an app uri" do
before do
Remotely.app :uri_app, "http://localhost:3000/api"
Thing.app :uri_app
end
it "removes leading slash to work with Faraday's path_prefix" do
Thing.expand("/members").should == "members"
end
end
context "with errors" do
let(:attributes) { {'errors' => {:base => %w{totally failed dude}}} }
it "adds errors during #initialize" do
subject.errors[:base].should == %w{totally failed dude}
end
end
end