ryanwood-mousetrap 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +7 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +25 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/mousetrap/customer.rb +190 -0
- data/lib/mousetrap/invoice.rb +41 -0
- data/lib/mousetrap/plan.rb +34 -0
- data/lib/mousetrap/resource.rb +147 -0
- data/lib/mousetrap/subscription.rb +160 -0
- data/lib/mousetrap.rb +25 -0
- data/mousetrap.gemspec +90 -0
- data/script/authenticate.rb +3 -0
- data/script/cheddar_getter.example.yml +3 -0
- data/script/console +1 -0
- data/spec/factories.rb +34 -0
- data/spec/integration/settings.example.yml +6 -0
- data/spec/integration/smoke_test.rb +355 -0
- data/spec/integration/spike.rb +108 -0
- data/spec/mousetrap/customer_spec.rb +437 -0
- data/spec/mousetrap/plan_spec.rb +41 -0
- data/spec/mousetrap/resource_spec.rb +188 -0
- data/spec/mousetrap/subscription_spec.rb +152 -0
- data/spec/mousetrap_spec.rb +17 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/fixtures.rb +189 -0
- data/spec/support/random_data.rb +21 -0
- metadata +173 -0
@@ -0,0 +1,437 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Mousetrap::Customer do
|
4
|
+
include Fixtures
|
5
|
+
|
6
|
+
def customer_attributes_for_api(customer)
|
7
|
+
{
|
8
|
+
:firstName => customer.first_name,
|
9
|
+
:lastName => customer.last_name,
|
10
|
+
:email => customer.email,
|
11
|
+
:company => customer.company,
|
12
|
+
:code => customer.code,
|
13
|
+
:subscription => {
|
14
|
+
:planCode => customer.subscription.plan_code,
|
15
|
+
:ccFirstName => customer.subscription.billing_first_name,
|
16
|
+
:ccLastName => customer.subscription.billing_last_name,
|
17
|
+
:ccNumber => customer.subscription.credit_card_number,
|
18
|
+
:ccExpMonth => customer.subscription.credit_card_expiration_month,
|
19
|
+
:ccExpYear => customer.subscription.credit_card_expiration_year,
|
20
|
+
:ccZip => customer.subscription.billing_zip_code,
|
21
|
+
:ccCountry => customer.subscription.billing_country,
|
22
|
+
:ccAddress => customer.subscription.billing_address,
|
23
|
+
:ccCity => customer.subscription.billing_city,
|
24
|
+
:ccState => customer.subscription.billing_state
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "when having multiple subscriptions" do
|
30
|
+
it "returns the latest one" do
|
31
|
+
Mousetrap::Customer.new_from_api(full_customer).subscription.should_not be_nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.all' do
|
36
|
+
before do
|
37
|
+
Mousetrap::Customer.stub :build_resources_from
|
38
|
+
end
|
39
|
+
|
40
|
+
it "gets all customers" do
|
41
|
+
Mousetrap::Customer.should_receive(:get_resources).with('customers').and_return('some hash')
|
42
|
+
Mousetrap::Customer.all
|
43
|
+
end
|
44
|
+
|
45
|
+
it "handles kludgy 'no customers found' response" do
|
46
|
+
Mousetrap::Customer.stub :get_resources => {
|
47
|
+
'error' => 'Resource not found: No customers found.'
|
48
|
+
}
|
49
|
+
Mousetrap::Customer.all.should == []
|
50
|
+
end
|
51
|
+
|
52
|
+
it "raises error if response has one" do
|
53
|
+
expect do
|
54
|
+
Mousetrap::Customer.stub :get_resources => { 'error' => "some other error" }
|
55
|
+
Mousetrap::Customer.all
|
56
|
+
end.to raise_error(RuntimeError, "some other error")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "builds resources from the response" do
|
60
|
+
Mousetrap::Customer.stub :get_resources => 'some hash'
|
61
|
+
Mousetrap::Customer.should_receive(:build_resources_from).with('some hash')
|
62
|
+
Mousetrap::Customer.all
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '.create' do
|
67
|
+
before do
|
68
|
+
@customer_hash = Factory.attributes_for :new_customer
|
69
|
+
@customer = Mousetrap::Customer.new @customer_hash
|
70
|
+
@customer.stub :create
|
71
|
+
Mousetrap::Customer.stub(:new => @customer)
|
72
|
+
Mousetrap::Customer.stub(:build_resource_from => stub(:id => 0))
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'instantiates a customer with a hash of attributes' do
|
76
|
+
Mousetrap::Customer.should_receive(:new).with(@customer_hash).and_return(@customer)
|
77
|
+
Mousetrap::Customer.create(@customer_hash)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'creates the new customer instance' do
|
81
|
+
@customer.should_receive :create
|
82
|
+
Mousetrap::Customer.create(@customer_hash)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'returns an instance of Mousetrap::Customer' do
|
86
|
+
Mousetrap::Customer.create(@customer_hash).should be_instance_of(Mousetrap::Customer)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe ".new" do
|
91
|
+
subject do
|
92
|
+
Mousetrap::Customer.new \
|
93
|
+
:first_name => 'Jon',
|
94
|
+
:last_name => 'Larkowski',
|
95
|
+
:email => 'lark@example.com',
|
96
|
+
:code => 'asfkhw0'
|
97
|
+
end
|
98
|
+
|
99
|
+
it { should be_instance_of(Mousetrap::Customer) }
|
100
|
+
it { should be_new_record }
|
101
|
+
|
102
|
+
describe "sets" do
|
103
|
+
it 'first_name' do
|
104
|
+
subject.first_name.should == 'Jon'
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'last_name' do
|
108
|
+
subject.last_name.should == 'Larkowski'
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'email' do
|
112
|
+
subject.email.should == 'lark@example.com'
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'code' do
|
116
|
+
subject.code.should == 'asfkhw0'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '.update' do
|
122
|
+
def do_update
|
123
|
+
Mousetrap::Customer.update('some customer code', 'some attributes')
|
124
|
+
end
|
125
|
+
|
126
|
+
it "makes a new customer from the attributes" do
|
127
|
+
Mousetrap::Customer.should_receive(:new).with('some attributes').and_return(stub(:null_object => true))
|
128
|
+
do_update
|
129
|
+
end
|
130
|
+
|
131
|
+
it "sets the new customer code to the argument" do
|
132
|
+
customer = mock
|
133
|
+
customer.stub :update
|
134
|
+
Mousetrap::Customer.stub :new => customer
|
135
|
+
customer.should_receive(:code=).with('some customer code')
|
136
|
+
do_update
|
137
|
+
end
|
138
|
+
|
139
|
+
it "calls #update" do
|
140
|
+
customer = mock(:null_object => true)
|
141
|
+
Mousetrap::Customer.stub :new => customer
|
142
|
+
customer.should_receive :update
|
143
|
+
do_update
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe '#cancel' do
|
148
|
+
context "for existing records" do
|
149
|
+
it 'cancels' do
|
150
|
+
customer = Factory :existing_customer
|
151
|
+
customer.should_receive(:member_action).with('cancel')
|
152
|
+
customer.cancel
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context "for new records" do
|
157
|
+
it "does nothing" do
|
158
|
+
customer = Factory.build :new_customer
|
159
|
+
customer.should_not_receive(:member_action).with('cancel')
|
160
|
+
customer.cancel
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "#new?" do
|
166
|
+
it "looks up the customer on CheddarGetter" do
|
167
|
+
c = Mousetrap::Customer.new :code => 'some_customer_code'
|
168
|
+
Mousetrap::Customer.should_receive(:[]).with('some_customer_code')
|
169
|
+
c.new?
|
170
|
+
end
|
171
|
+
|
172
|
+
context "with an existing CheddarGetter record" do
|
173
|
+
before do
|
174
|
+
Mousetrap::Customer.stub(:[] => stub(:id => 'some_customer_id'))
|
175
|
+
end
|
176
|
+
|
177
|
+
it "grabs the id from CheddarGetter and assigns it locally" do
|
178
|
+
c = Mousetrap::Customer.new :code => 'some_customer_code'
|
179
|
+
c.should_receive(:id=).with('some_customer_id')
|
180
|
+
c.new?
|
181
|
+
end
|
182
|
+
|
183
|
+
it "is false" do
|
184
|
+
c = Mousetrap::Customer.new
|
185
|
+
c.should_not be_new
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context "without a CheddarGetter record" do
|
190
|
+
before do
|
191
|
+
Mousetrap::Customer.stub :[] => nil
|
192
|
+
end
|
193
|
+
|
194
|
+
it "is true" do
|
195
|
+
c = Mousetrap::Customer.new
|
196
|
+
c.should be_new
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe '#save' do
|
202
|
+
context "for existing records" do
|
203
|
+
before do
|
204
|
+
@customer = Factory :existing_customer
|
205
|
+
@customer.stub :new? => false
|
206
|
+
end
|
207
|
+
|
208
|
+
context "with subscription association set up" do
|
209
|
+
it 'posts to edit action' do
|
210
|
+
attributes_for_api = customer_attributes_for_api(@customer)
|
211
|
+
|
212
|
+
# We don't send code for existing API resources.
|
213
|
+
attributes_for_api.delete(:code)
|
214
|
+
|
215
|
+
@customer.class.should_receive(:put_resource).with('customers', 'edit', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
|
216
|
+
@customer.save
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context "with no subscription association" do
|
221
|
+
it 'posts to edit action' do
|
222
|
+
attributes_for_api = customer_attributes_for_api(@customer)
|
223
|
+
|
224
|
+
# We don't send code for existing API resources.
|
225
|
+
attributes_for_api.delete(:code)
|
226
|
+
|
227
|
+
attributes_for_api.delete(:subscription)
|
228
|
+
@customer.subscription = nil
|
229
|
+
|
230
|
+
@customer.class.should_receive(:put_resource).with('customers', 'edit-customer', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
|
231
|
+
@customer.save
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
context "for new records" do
|
237
|
+
it 'calls create' do
|
238
|
+
customer = Factory :new_customer
|
239
|
+
customer.stub :new? => true
|
240
|
+
Mousetrap::Customer.stub :exists? => false
|
241
|
+
customer.should_receive(:create)
|
242
|
+
customer.save
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "#switch_to_plan" do
|
248
|
+
it "raises an error if not existing CheddarGetter customer" do
|
249
|
+
c = Mousetrap::Customer.new :code => 'some_customer_code'
|
250
|
+
c.stub :exists? => false
|
251
|
+
expect { c.switch_to_plan 'some_plan_code' }.to raise_error(/existing/)
|
252
|
+
end
|
253
|
+
|
254
|
+
it "puts a subscription with a plan code" do
|
255
|
+
c = Mousetrap::Customer.new :code => 'some_customer_code'
|
256
|
+
c.stub :exists? => true
|
257
|
+
c.class.should_receive(:put_resource).with(
|
258
|
+
'customers', 'edit-subscription', 'some_customer_code', { :planCode => 'some_plan_code' })
|
259
|
+
c.switch_to_plan 'some_plan_code'
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe "protected methods" do
|
264
|
+
describe "#create" do
|
265
|
+
before do
|
266
|
+
@customer = Mousetrap::Customer.new
|
267
|
+
@customer.stub :attributes_for_api_with_subscription => 'some_attributes'
|
268
|
+
end
|
269
|
+
|
270
|
+
it "posts a new customer" do
|
271
|
+
@customer.class.should_receive(:post_resource).with('customers', 'new', 'some_attributes').and_return({:id => 'some_id'})
|
272
|
+
@customer.class.stub :build_resource_from => stub(:id => 'some_id')
|
273
|
+
@customer.send :create
|
274
|
+
end
|
275
|
+
|
276
|
+
it "raises error if CheddarGetter reports one" do
|
277
|
+
@customer.class.stub :post_resource => {'error' => 'some error message'}
|
278
|
+
expect { @customer.send(:create) }.to raise_error('some error message')
|
279
|
+
end
|
280
|
+
|
281
|
+
it "builds a customer from the CheddarGetter return values" do
|
282
|
+
@customer.class.stub :post_resource => 'some response'
|
283
|
+
@customer.class.should_receive(:build_resource_from).with('some response').and_return(stub(:id => 'some_id'))
|
284
|
+
@customer.send :create
|
285
|
+
end
|
286
|
+
|
287
|
+
it "grabs the id from CheddarGetter and assigns it locally" do
|
288
|
+
@customer.class.stub :post_resource => {}
|
289
|
+
@customer.class.stub :build_resource_from => stub(:id => 'some_id')
|
290
|
+
@customer.should_receive(:id=).with('some_id')
|
291
|
+
@customer.send :create
|
292
|
+
end
|
293
|
+
|
294
|
+
it "returns the response" do
|
295
|
+
@customer.class.stub :post_resource => { :some => :response }
|
296
|
+
@customer.class.stub :build_resource_from => stub(:id => 'some_id')
|
297
|
+
@customer.send(:create).should == { :some => :response }
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
describe "#update" do
|
302
|
+
context "when there's a subscription instance" do
|
303
|
+
let(:customer) { Mousetrap::Customer.new :code => 'some code' }
|
304
|
+
|
305
|
+
it "puts the customer with subscription when there's a subscription instance" do
|
306
|
+
customer.stub :subscription => stub
|
307
|
+
customer.stub :attributes_for_api_with_subscription => 'some attributes with subscription'
|
308
|
+
customer.class.should_receive(:put_resource).with('customers', 'edit', 'some code', 'some attributes with subscription').and_return({:id => 'some_id'})
|
309
|
+
customer.send :update
|
310
|
+
end
|
311
|
+
|
312
|
+
it "puts just the customer when no subscription instance" do
|
313
|
+
customer.stub :subscription => nil
|
314
|
+
customer.stub :attributes_for_api => 'some attributes'
|
315
|
+
customer.class.should_receive(:put_resource).with('customers', 'edit-customer', 'some code', 'some attributes').and_return({:id => 'some_id'})
|
316
|
+
customer.send :update
|
317
|
+
end
|
318
|
+
|
319
|
+
it "raises error if CheddarGetter reports one" do
|
320
|
+
customer.class.stub :put_resource => {'error' => 'some error message'}
|
321
|
+
expect { customer.send(:update) }.to raise_error('some error message')
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
describe '#add_custom_charge' do
|
328
|
+
context "when there's a subscription instance" do
|
329
|
+
before :all do
|
330
|
+
@customer = Factory(:new_customer)
|
331
|
+
end
|
332
|
+
|
333
|
+
it "should not raise an error with CheddarGetter" do
|
334
|
+
@customer.class.should_receive(:put_resource).with('customers', 'add-charge', @customer.code, { :eachAmount => 45.00, :chargeCode => 'BOGUS', :quantity => 1, :description => nil }).and_return({ :id => 'some_id' })
|
335
|
+
@customer.add_custom_charge('BOGUS', 45.00, 1, nil)
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
context "with there is not a subscription" do
|
340
|
+
before :all do
|
341
|
+
@customer = Mousetrap::Customer.new
|
342
|
+
end
|
343
|
+
|
344
|
+
it "should raise an error with CheddarGetter" do
|
345
|
+
@customer.class.stub :put_resource => { 'error' => 'some error message' }
|
346
|
+
expect { @customer.add_custom_charge('BOGUS') }.to raise_error('some error message')
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
# describe '#update_tracked_item_quantity' do
|
352
|
+
# context "when there's a subscription instance" do
|
353
|
+
# before do
|
354
|
+
# @customer = Factory(:new_customer)
|
355
|
+
# end
|
356
|
+
#
|
357
|
+
# it "should not raise an error with CheddarGetter" do
|
358
|
+
# @customer.class.should_receive(:put_resource).with('customers', 'add-item-quantity', @customer.code, { :quantity => 1, :itemCode => 'BOGUS' }).and_return({ :id => 'some_id' })
|
359
|
+
# @customer.update_tracked_item_quantity('BOGUS', 1).should_not raise_error
|
360
|
+
# end
|
361
|
+
# end
|
362
|
+
# end
|
363
|
+
end
|
364
|
+
|
365
|
+
|
366
|
+
__END__
|
367
|
+
|
368
|
+
customers:
|
369
|
+
customer:
|
370
|
+
company:
|
371
|
+
lastName: cgejerpkyw
|
372
|
+
code: krylmrreef@example.com
|
373
|
+
subscriptions:
|
374
|
+
subscription:
|
375
|
+
plans:
|
376
|
+
plan:
|
377
|
+
name: Test
|
378
|
+
setupChargeAmount: "42.00"
|
379
|
+
code: TEST
|
380
|
+
recurringChargeAmount: "13.00"
|
381
|
+
billingFrequencyQuantity: "1"
|
382
|
+
trialDays: "0"
|
383
|
+
id: 8e933180-08b5-102d-a92d-40402145ee8b
|
384
|
+
billingFrequency: monthly
|
385
|
+
createdDatetime: "2009-10-12T19:28:09+00:00"
|
386
|
+
recurringChargeCode: TEST_RECURRING
|
387
|
+
isActive: "1"
|
388
|
+
billingFrequencyUnit: months
|
389
|
+
description: This is my test plan. There are many like it, but this one is mine.
|
390
|
+
billingFrequencyPer: month
|
391
|
+
setupChargeCode: TEST_SETUP
|
392
|
+
gatewayToken:
|
393
|
+
id: 7ccea6de-0a4d-102d-a92d-40402145ee8b
|
394
|
+
createdDatetime: "2009-10-14T20:08:14+00:00"
|
395
|
+
ccType: visa
|
396
|
+
ccLastFour: "1111"
|
397
|
+
ccExpirationDate: "2012-12-31T00:00:00+00:00"
|
398
|
+
canceledDatetime:
|
399
|
+
invoices:
|
400
|
+
invoice:
|
401
|
+
- number: "5"
|
402
|
+
transactions:
|
403
|
+
transaction:
|
404
|
+
response: approved
|
405
|
+
code: ""
|
406
|
+
amount: "42.00"
|
407
|
+
memo: This is a simulated transaction
|
408
|
+
id: 7ce53c78-0a4d-102d-a92d-40402145ee8b
|
409
|
+
createdDatetime: "2009-10-14T20:08:14+00:00"
|
410
|
+
transactedDatetime: "2009-10-14T20:08:14+00:00"
|
411
|
+
parentId:
|
412
|
+
charges:
|
413
|
+
charge:
|
414
|
+
code: TEST_SETUP
|
415
|
+
quantity: "1"
|
416
|
+
id: 7ce2cb6e-0a4d-102d-a92d-40402145ee8b
|
417
|
+
createdDatetime: "2009-10-14T20:08:14+00:00"
|
418
|
+
type: setup
|
419
|
+
eachAmount: "42.00"
|
420
|
+
description:
|
421
|
+
gatewayAccount:
|
422
|
+
id: ""
|
423
|
+
billingDatetime: "2009-10-14T20:08:14+00:00"
|
424
|
+
id: 7cd25072-0a4d-102d-a92d-40402145ee8b
|
425
|
+
createdDatetime: "2009-10-14T20:08:14+00:00"
|
426
|
+
type: setup
|
427
|
+
- number: "6"
|
428
|
+
billingDatetime: "2009-11-14T20:08:14+00:00"
|
429
|
+
id: 7cd4253c-0a4d-102d-a92d-40402145ee8b
|
430
|
+
createdDatetime: "2009-10-14T20:08:14+00:00"
|
431
|
+
type: subscription
|
432
|
+
gatewayToken:
|
433
|
+
id: 7ccd6e5e-0a4d-102d-a92d-40402145ee8b
|
434
|
+
createdDatetime: "2009-10-14T20:08:14+00:00"
|
435
|
+
modifiedDatetime: "2009-10-14T20:08:14+00:00"
|
436
|
+
firstName: wqaqyhjdfg
|
437
|
+
email: krylmrreef@example.com
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Mousetrap::Plan do
|
4
|
+
# name: Test
|
5
|
+
# billingFrequencyQuantity: "1"
|
6
|
+
# code: TEST
|
7
|
+
# recurringChargeAmount: "42.00"
|
8
|
+
# createdDatetime: "2009-08-25T04:24:34+00:00"
|
9
|
+
# id: 5fbb9a84-e27f-102c-a92d-40402145ee8b
|
10
|
+
# isActive: "1"
|
11
|
+
# billingFrequency: monthly
|
12
|
+
# description: Test
|
13
|
+
# trialDays: "0"
|
14
|
+
# setupChargeCode: TEST_SETUP
|
15
|
+
# recurringChargeCode: TEST_RECURRING
|
16
|
+
# billingFrequencyUnit: months
|
17
|
+
# setupChargeAmount: "0.00"
|
18
|
+
# billingFrequencyPer: month
|
19
|
+
|
20
|
+
describe ".all" do
|
21
|
+
before do
|
22
|
+
Mousetrap::Plan.stub :build_resources_from
|
23
|
+
end
|
24
|
+
|
25
|
+
it "gets all plans" do
|
26
|
+
Mousetrap::Plan.should_receive(:get_resources).with('plans').and_return('some hash')
|
27
|
+
Mousetrap::Plan.all
|
28
|
+
end
|
29
|
+
|
30
|
+
it "handles no-plans case" do
|
31
|
+
Mousetrap::Plan.stub :get_resources => { 'plans' => nil }
|
32
|
+
Mousetrap::Plan.all.should == []
|
33
|
+
end
|
34
|
+
|
35
|
+
it "builds resources from the response" do
|
36
|
+
Mousetrap::Plan.stub :get_resources => { 'plans' => 'some hash' }
|
37
|
+
Mousetrap::Plan.should_receive(:build_resources_from).with({ 'plans' => 'some hash' })
|
38
|
+
Mousetrap::Plan.all
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
module Mousetrap
|
4
|
+
class Widget < Resource
|
5
|
+
attr_accessor :id
|
6
|
+
attr_accessor :code
|
7
|
+
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def self.attributes_from_api(attributes)
|
12
|
+
{
|
13
|
+
:id => attributes['id'],
|
14
|
+
:code => attributes['code'],
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.plural_resource_name
|
19
|
+
'widgets'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.singular_resource_name
|
23
|
+
'widget'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe Mousetrap::Resource do
|
29
|
+
before do
|
30
|
+
Mousetrap.product_code = 'my_product_code'
|
31
|
+
end
|
32
|
+
|
33
|
+
subject { Mousetrap::Resource }
|
34
|
+
|
35
|
+
describe "class" do
|
36
|
+
it "sets the base URI" do
|
37
|
+
subject.default_options[:base_uri].should == 'https://cheddargetter.com'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "set the header to our client name" do
|
41
|
+
subject.headers['User-Agent'].should == 'Mousetrap Ruby Client'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe ".[]" do
|
46
|
+
before do
|
47
|
+
customer_hash = Factory.attributes_for :existing_customer
|
48
|
+
customer_hash = HashWithIndifferentAccess.new customer_hash
|
49
|
+
@code = customer_hash[:code]
|
50
|
+
@server_response_hash = { 'widgets' => { 'widget' => customer_hash } }
|
51
|
+
Mousetrap::Widget.stub(:get_resource => @server_response_hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "gets a resource with widget code" do
|
55
|
+
Mousetrap::Widget.should_receive(:get_resource).with('widgets', @code).and_return(@server_response_hash)
|
56
|
+
Mousetrap::Widget[@code]
|
57
|
+
end
|
58
|
+
|
59
|
+
context "returned widget instance" do
|
60
|
+
subject { Mousetrap::Widget[@code] }
|
61
|
+
it { should be_instance_of(Mousetrap::Widget) }
|
62
|
+
it { should_not be_new_record }
|
63
|
+
it { subject.code.should == @code }
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when there's errors" do
|
67
|
+
it "handles kludgy 'Resource not found' response" do
|
68
|
+
Mousetrap::Widget.stub :get_resource => {
|
69
|
+
'error' => 'Resource not found: Customer not found for code=cantfindme within productCode=MOUSETRAP_TEST'
|
70
|
+
}
|
71
|
+
Mousetrap::Widget['cantfindme'].should be_nil
|
72
|
+
end
|
73
|
+
|
74
|
+
it "raises error if response has one" do
|
75
|
+
expect do
|
76
|
+
Mousetrap::Widget.stub :get_resource => { 'error' => 'some other error' }
|
77
|
+
Mousetrap::Widget['some_resource_code'].should be_nil
|
78
|
+
end.to raise_error(RuntimeError, 'some other error')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe ".destroy_all" do
|
84
|
+
it "destroys each resource" do
|
85
|
+
all_widgets = [stub, stub, stub]
|
86
|
+
Mousetrap::Widget.stub :all => all_widgets
|
87
|
+
all_widgets.each { |w| w.should_receive :destroy }
|
88
|
+
Mousetrap::Widget.destroy_all
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe ".exists?" do
|
93
|
+
it "gets by code" do
|
94
|
+
Mousetrap::Widget.should_receive(:[]).with('some_resource_code')
|
95
|
+
Mousetrap::Widget.exists? 'some_resource_code'
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns true when resource exists" do
|
99
|
+
Mousetrap::Widget.stub :[] => stub('some_widget')
|
100
|
+
Mousetrap::Widget.exists?('some_resource_code').should be_true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "returns false when resource doesn't exist" do
|
104
|
+
Mousetrap::Widget.stub :[] => nil
|
105
|
+
Mousetrap::Widget.exists?('some_resource_code').should be_false
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#destroy' do
|
110
|
+
context "for existing records" do
|
111
|
+
it 'destroys' do
|
112
|
+
widget = Mousetrap::Widget.new
|
113
|
+
widget.stub :new_record? => false
|
114
|
+
widget.should_receive(:member_action).with('delete')
|
115
|
+
widget.destroy
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "for new records" do
|
120
|
+
it "does nothing" do
|
121
|
+
widget = Mousetrap::Widget.new
|
122
|
+
widget.stub :new_record? => true
|
123
|
+
Mousetrap::Customer.should_not_receive(:member_action)
|
124
|
+
widget.destroy
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "#exists?" do
|
130
|
+
it "calls .exists? with code" do
|
131
|
+
Mousetrap::Widget.should_receive(:exists?).with('some_resource_code')
|
132
|
+
r = Mousetrap::Widget.new :code => 'some_resource_code'
|
133
|
+
r.exists?
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe '#new?' do
|
138
|
+
it 'is true if id is nil' do
|
139
|
+
s = Mousetrap::Widget.new
|
140
|
+
s.should be_new
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'is false if id exists' do
|
144
|
+
s = Mousetrap::Widget.new
|
145
|
+
s.stub :id => 'some_id'
|
146
|
+
s.should_not be_new
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "protected methods" do
|
151
|
+
describe ".delete_resource" do
|
152
|
+
it "gets /xml/<resource>/delete/productCode/<my_product_code>/code/<resource_code>" do
|
153
|
+
subject.should_receive(:post).with('/xml/widgets/delete/productCode/my_product_code/code/some_resource_code')
|
154
|
+
subject.delete_resource 'widgets', 'some_resource_code'
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe ".get_resource" do
|
159
|
+
it "gets /xml/<resource>/get/productCode/<my_product_code>/code/<resource_code>" do
|
160
|
+
subject.should_receive(:get).with('/xml/widgets/get/productCode/my_product_code/code/some%2Bresource%2Bcode')
|
161
|
+
subject.get_resource 'widgets', 'some+resource+code'
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe ".get_resources" do
|
166
|
+
it "gets /xml/<resource>/get/productCode/<my_product_code>" do
|
167
|
+
subject.should_receive(:get).with('/xml/widgets/get/productCode/my_product_code')
|
168
|
+
subject.get_resources 'widgets'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
describe ".post_resource" do
|
173
|
+
it "posts to /xml/<resource>/<action>/productCode/<product_code>" do
|
174
|
+
subject.should_receive(:post).with('/xml/widgets/some_action/productCode/my_product_code', :body => 'some_hash')
|
175
|
+
subject.post_resource 'widgets', 'some_action', 'some_hash'
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe ".put_resource" do
|
180
|
+
it "puts to /xml/<resource>/<action>/productCode/<product_code>/code/<resource_code>" do
|
181
|
+
subject.should_receive(:post).with(
|
182
|
+
'/xml/widgets/some_action/productCode/my_product_code/code/some_widget_code',
|
183
|
+
:body => 'some_hash')
|
184
|
+
subject.put_resource 'widgets', 'some_action', 'some_widget_code', 'some_hash'
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|