sproutbox-mousetrap 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,491 @@
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
+ :notes => customer.notes,
14
+ :subscription => {
15
+ :planCode => customer.subscription.plan_code,
16
+ :ccFirstName => customer.subscription.billing_first_name,
17
+ :ccLastName => customer.subscription.billing_last_name,
18
+ :ccNumber => customer.subscription.credit_card_number,
19
+ :ccExpMonth => customer.subscription.credit_card_expiration_month,
20
+ :ccExpYear => customer.subscription.credit_card_expiration_year,
21
+ :ccCardCode => customer.subscription.credit_card_code,
22
+ :ccZip => customer.subscription.billing_zip_code,
23
+ :ccCountry => customer.subscription.billing_country,
24
+ :ccAddress => customer.subscription.billing_address,
25
+ :ccCity => customer.subscription.billing_city,
26
+ :ccState => customer.subscription.billing_state,
27
+ :changeBillDate => customer.subscription.billing_date
28
+ }
29
+ }
30
+ end
31
+
32
+ describe "when having multiple subscriptions" do
33
+ it "returns the latest one" do
34
+ Mousetrap::Customer.new_from_api(full_customer).subscription.should_not be_nil
35
+ end
36
+ end
37
+
38
+ describe '.all' do
39
+ before do
40
+ Mousetrap::Customer.stub :build_resources_from
41
+ end
42
+
43
+ it "gets all customers" do
44
+ Mousetrap::Customer.should_receive(:get_resources).with('customers').and_return('some hash')
45
+ Mousetrap::Customer.all
46
+ end
47
+
48
+ it "handles kludgy 'no customers found' response" do
49
+ Mousetrap::Customer.stub :get_resources => {
50
+ 'error' => 'Resource not found: No customers found.'
51
+ }
52
+ Mousetrap::Customer.all.should == []
53
+ end
54
+
55
+ it "raises error if response has one" do
56
+ expect do
57
+ Mousetrap::Customer.stub :get_resources => { 'error' => "some other error" }
58
+ Mousetrap::Customer.all
59
+ end.to raise_error(RuntimeError, "some other error")
60
+ end
61
+
62
+ it "builds resources from the response" do
63
+ Mousetrap::Customer.stub :get_resources => 'some hash'
64
+ Mousetrap::Customer.should_receive(:build_resources_from).with('some hash')
65
+ Mousetrap::Customer.all
66
+ end
67
+ end
68
+
69
+ describe '.create' do
70
+ before do
71
+ @customer_hash = Factory.attributes_for :new_customer
72
+ @customer = Mousetrap::Customer.new @customer_hash
73
+ @customer.stub :create
74
+ Mousetrap::Customer.stub(:new => @customer)
75
+ Mousetrap::Customer.stub(:build_resource_from => stub(:id => 0))
76
+ end
77
+
78
+ it 'instantiates a customer with a hash of attributes' do
79
+ Mousetrap::Customer.should_receive(:new).with(@customer_hash).and_return(@customer)
80
+ Mousetrap::Customer.create(@customer_hash)
81
+ end
82
+
83
+ it 'creates the new customer instance' do
84
+ @customer.should_receive :create
85
+ Mousetrap::Customer.create(@customer_hash)
86
+ end
87
+
88
+ it 'returns an instance of Mousetrap::Customer' do
89
+ Mousetrap::Customer.create(@customer_hash).should be_instance_of(Mousetrap::Customer)
90
+ end
91
+ end
92
+
93
+ describe ".new" do
94
+ subject do
95
+ Mousetrap::Customer.new \
96
+ :first_name => 'Jon',
97
+ :last_name => 'Larkowski',
98
+ :email => 'lark@example.com',
99
+ :code => 'asfkhw0',
100
+ :notes => 'Lorem ipsum dolor'
101
+ end
102
+
103
+ it { should be_instance_of(Mousetrap::Customer) }
104
+ it { should be_new_record }
105
+
106
+ describe "sets" do
107
+ it 'first_name' do
108
+ subject.first_name.should == 'Jon'
109
+ end
110
+
111
+ it 'last_name' do
112
+ subject.last_name.should == 'Larkowski'
113
+ end
114
+
115
+ it 'email' do
116
+ subject.email.should == 'lark@example.com'
117
+ end
118
+
119
+ it 'code' do
120
+ subject.code.should == 'asfkhw0'
121
+ end
122
+
123
+ it 'notes' do
124
+ subject.notes.should == 'Lorem ipsum dolor'
125
+ end
126
+ end
127
+ end
128
+
129
+ describe '.update' do
130
+ def do_update
131
+ Mousetrap::Customer.update('some customer code', 'some attributes')
132
+ end
133
+
134
+ it "makes a new customer from the attributes" do
135
+ customer = mock
136
+ customer.stub :update
137
+ customer.should_receive(:code=).with("some customer code")
138
+ Mousetrap::Customer.should_receive(:new).with('some attributes').and_return(customer)
139
+ do_update
140
+ end
141
+
142
+ it "sets the new customer code to the argument" do
143
+ customer = mock
144
+ customer.stub :update
145
+ Mousetrap::Customer.stub :new => customer
146
+ customer.should_receive(:code=).with('some customer code')
147
+ do_update
148
+ end
149
+
150
+ it "calls #update" do
151
+ customer = mock
152
+ customer.should_receive(:code=).with("some customer code")
153
+ Mousetrap::Customer.stub :new => customer
154
+ customer.should_receive :update
155
+ do_update
156
+ end
157
+ end
158
+
159
+ describe '#cancel' do
160
+ context "for existing records" do
161
+ it 'cancels' do
162
+ customer = Factory :existing_customer
163
+ customer.should_receive(:member_action).with('cancel')
164
+ customer.cancel
165
+ end
166
+ end
167
+
168
+ context "for new records" do
169
+ it "does nothing" do
170
+ customer = Factory.build :new_customer
171
+ customer.should_not_receive(:member_action).with('cancel')
172
+ customer.cancel
173
+ end
174
+ end
175
+ end
176
+
177
+ describe "#new?" do
178
+ it "looks up the customer on CheddarGetter" do
179
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
180
+ Mousetrap::Customer.should_receive(:[]).with('some_customer_code')
181
+ c.new?
182
+ end
183
+
184
+ context "with an existing CheddarGetter record" do
185
+ before do
186
+ Mousetrap::Customer.stub(:[] => stub(:id => 'some_customer_id'))
187
+ end
188
+
189
+ it "grabs the id from CheddarGetter and assigns it locally" do
190
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
191
+ c.should_receive(:id=).with('some_customer_id')
192
+ c.new?
193
+ end
194
+
195
+ it "is false" do
196
+ c = Mousetrap::Customer.new
197
+ c.should_not be_new
198
+ end
199
+ end
200
+
201
+ context "without a CheddarGetter record" do
202
+ before do
203
+ Mousetrap::Customer.stub :[] => nil
204
+ end
205
+
206
+ it "is true" do
207
+ c = Mousetrap::Customer.new
208
+ c.should be_new
209
+ end
210
+ end
211
+ end
212
+
213
+ describe '#save' do
214
+ context "for existing records" do
215
+ before do
216
+ @customer = Factory :existing_customer
217
+ @customer.stub :new? => false
218
+ end
219
+
220
+ context "with subscription association set up" 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
+ @customer.class.should_receive(:put_resource).with('customers', 'edit', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
228
+ @customer.save
229
+ end
230
+ end
231
+
232
+ context "with no subscription association" do
233
+ it 'posts to edit action' do
234
+ attributes_for_api = customer_attributes_for_api(@customer)
235
+
236
+ # We don't send code for existing API resources.
237
+ attributes_for_api.delete(:code)
238
+
239
+ attributes_for_api.delete(:subscription)
240
+ @customer.subscription = nil
241
+
242
+ @customer.class.should_receive(:put_resource).with('customers', 'edit-customer', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
243
+ @customer.save
244
+ end
245
+ end
246
+ end
247
+
248
+ context "for new records" do
249
+ it 'calls create' do
250
+ customer = Factory :new_customer
251
+ customer.stub :new? => true
252
+ Mousetrap::Customer.stub :exists? => false
253
+ customer.should_receive(:create)
254
+ customer.save
255
+ end
256
+ end
257
+ end
258
+
259
+ describe "#switch_to_plan" do
260
+ it "raises an error if not existing CheddarGetter customer" do
261
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
262
+ c.stub :exists? => false
263
+ expect { c.switch_to_plan 'some_plan_code' }.to raise_error(/existing/)
264
+ end
265
+
266
+ it "puts a subscription with a plan code" do
267
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
268
+ c.stub :exists? => true
269
+ c.class.should_receive(:put_resource).with(
270
+ 'customers', 'edit-subscription', 'some_customer_code', { :planCode => 'some_plan_code' })
271
+ c.switch_to_plan 'some_plan_code'
272
+ end
273
+ end
274
+
275
+ describe "#bill_now" do
276
+ it "raises an error if not existing CheddarGetter customer" do
277
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
278
+ c.stub :exists? => false
279
+ expect { c.bill_now }.to raise_error(/existing/)
280
+ end
281
+
282
+ it "puts a billing date with 'now'" do
283
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
284
+ c.stub :exists? => true
285
+ c.class.should_receive(:put_resource).with(
286
+ 'customers', 'edit-subscription', 'some_customer_code', { :changeBillDate => 'now' })
287
+ c.bill_now
288
+ end
289
+ end
290
+
291
+ describe "protected methods" do
292
+ describe "#create" do
293
+ before do
294
+ @customer = Mousetrap::Customer.new
295
+ @customer.stub :attributes_for_api_with_subscription => 'some_attributes'
296
+ end
297
+
298
+ it "posts a new customer" do
299
+ @customer.class.should_receive(:post_resource).with('customers', 'new', 'some_attributes').and_return({:id => 'some_id'})
300
+ @customer.class.stub :build_resource_from => stub(:id => 'some_id')
301
+ @customer.send :create
302
+ end
303
+
304
+ it "raises error if CheddarGetter reports one" do
305
+ @customer.class.stub :post_resource => {'error' => 'some error message'}
306
+ expect { @customer.send(:create) }.to raise_error('some error message')
307
+ end
308
+
309
+ it "builds a customer from the CheddarGetter return values" do
310
+ @customer.class.stub :post_resource => 'some response'
311
+ @customer.class.should_receive(:build_resource_from).with('some response').and_return(stub(:id => 'some_id'))
312
+ @customer.send :create
313
+ end
314
+
315
+ it "grabs the id from CheddarGetter and assigns it locally" do
316
+ @customer.class.stub :post_resource => {}
317
+ @customer.class.stub :build_resource_from => stub(:id => 'some_id')
318
+ @customer.should_receive(:id=).with('some_id')
319
+ @customer.send :create
320
+ end
321
+
322
+ it "returns the response" do
323
+ @customer.class.stub :post_resource => { :some => :response }
324
+ @customer.class.stub :build_resource_from => stub(:id => 'some_id')
325
+ @customer.send(:create).should == { :some => :response }
326
+ end
327
+ end
328
+
329
+ describe "#update" do
330
+ context "when there's a subscription instance" do
331
+ let(:customer) { Mousetrap::Customer.new :code => 'some code' }
332
+
333
+ it "puts the customer with subscription when there's a subscription instance" do
334
+ customer.stub :subscription => stub
335
+ customer.stub :attributes_for_api_with_subscription => 'some attributes with subscription'
336
+ customer.class.should_receive(:put_resource).with('customers', 'edit', 'some code', 'some attributes with subscription').and_return({:id => 'some_id'})
337
+ customer.send :update
338
+ end
339
+
340
+ it "puts just the customer when no subscription instance" do
341
+ customer.stub :subscription => nil
342
+ customer.stub :attributes_for_api => 'some attributes'
343
+ customer.class.should_receive(:put_resource).with('customers', 'edit-customer', 'some code', 'some attributes').and_return({:id => 'some_id'})
344
+ customer.send :update
345
+ end
346
+
347
+ it "raises error if CheddarGetter reports one" do
348
+ customer.class.stub :put_resource => {'error' => 'some error message'}
349
+ expect { customer.send(:update) }.to raise_error('some error message')
350
+ end
351
+ end
352
+ end
353
+ end
354
+
355
+ describe '#add_custom_charge' do
356
+ context "when there's a subscription instance" do
357
+ before :all do
358
+ @customer = Factory(:new_customer)
359
+ end
360
+
361
+ it "should not raise an error with CheddarGetter" do
362
+ @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' })
363
+ @customer.add_custom_charge('BOGUS', 45.00, 1, nil)
364
+ end
365
+ end
366
+
367
+ context "with there is not a subscription" do
368
+ before :all do
369
+ @customer = Mousetrap::Customer.new
370
+ end
371
+
372
+ it "should raise an error with CheddarGetter" do
373
+ @customer.class.stub :put_resource => { 'error' => 'some error message' }
374
+ expect { @customer.add_custom_charge('BOGUS') }.to raise_error('some error message')
375
+ end
376
+ end
377
+ end
378
+
379
+ describe '#instant_bill_custom_charge' do
380
+ context "when there's a subscription instance" do
381
+ before :all do
382
+ @customer = Factory(:new_customer)
383
+ end
384
+
385
+ it "should not raise an error with CheddarGetter" do
386
+ @customer.should_receive(:add_custom_charge).with('BOGUS', 45.00, 1, nil)
387
+
388
+ @customer.should_receive(:bill_now)
389
+ @customer.instant_bill_custom_charge('BOGUS', 45.00, 1, nil)
390
+ end
391
+ end
392
+
393
+ context "with there is not a subscription" do
394
+ before :all do
395
+ @customer = Mousetrap::Customer.new
396
+ end
397
+
398
+ it "should raise an error with CheddarGetter" do
399
+ @customer.class.stub :put_resource => { 'error' => 'some error message' }
400
+ expect { @customer.add_custom_charge('BOGUS') }.to raise_error('some error message')
401
+ end
402
+ end
403
+ end
404
+
405
+ # describe '#update_tracked_item_quantity' do
406
+ # context "when there's a subscription instance" do
407
+ # before do
408
+ # @customer = Factory(:new_customer)
409
+ # end
410
+ #
411
+ # it "should not raise an error with CheddarGetter" do
412
+ # @customer.class.should_receive(:put_resource).with('customers', 'add-item-quantity', @customer.code, { :quantity => 1, :itemCode => 'BOGUS' }).and_return({ :id => 'some_id' })
413
+ # @customer.update_tracked_item_quantity('BOGUS', 1).should_not raise_error
414
+ # end
415
+ # end
416
+ # end
417
+ end
418
+
419
+
420
+ __END__
421
+
422
+ customers:
423
+ customer:
424
+ company:
425
+ lastName: cgejerpkyw
426
+ code: krylmrreef@example.com
427
+ subscriptions:
428
+ subscription:
429
+ plans:
430
+ plan:
431
+ name: Test
432
+ setupChargeAmount: "42.00"
433
+ code: TEST
434
+ recurringChargeAmount: "13.00"
435
+ billingFrequencyQuantity: "1"
436
+ trialDays: "0"
437
+ id: 8e933180-08b5-102d-a92d-40402145ee8b
438
+ billingFrequency: monthly
439
+ createdDatetime: "2009-10-12T19:28:09+00:00"
440
+ recurringChargeCode: TEST_RECURRING
441
+ isActive: "1"
442
+ billingFrequencyUnit: months
443
+ description: This is my test plan. There are many like it, but this one is mine.
444
+ billingFrequencyPer: month
445
+ setupChargeCode: TEST_SETUP
446
+ gatewayToken:
447
+ id: 7ccea6de-0a4d-102d-a92d-40402145ee8b
448
+ createdDatetime: "2009-10-14T20:08:14+00:00"
449
+ ccType: visa
450
+ ccLastFour: "1111"
451
+ ccExpirationDate: "2012-12-31T00:00:00+00:00"
452
+ canceledDatetime:
453
+ invoices:
454
+ invoice:
455
+ - number: "5"
456
+ transactions:
457
+ transaction:
458
+ response: approved
459
+ code: ""
460
+ amount: "42.00"
461
+ memo: This is a simulated transaction
462
+ id: 7ce53c78-0a4d-102d-a92d-40402145ee8b
463
+ createdDatetime: "2009-10-14T20:08:14+00:00"
464
+ transactedDatetime: "2009-10-14T20:08:14+00:00"
465
+ parentId:
466
+ charges:
467
+ charge:
468
+ code: TEST_SETUP
469
+ quantity: "1"
470
+ id: 7ce2cb6e-0a4d-102d-a92d-40402145ee8b
471
+ createdDatetime: "2009-10-14T20:08:14+00:00"
472
+ type: setup
473
+ eachAmount: "42.00"
474
+ description:
475
+ gatewayAccount:
476
+ id: ""
477
+ billingDatetime: "2009-10-14T20:08:14+00:00"
478
+ id: 7cd25072-0a4d-102d-a92d-40402145ee8b
479
+ createdDatetime: "2009-10-14T20:08:14+00:00"
480
+ type: setup
481
+ - number: "6"
482
+ billingDatetime: "2009-11-14T20:08:14+00:00"
483
+ id: 7cd4253c-0a4d-102d-a92d-40402145ee8b
484
+ createdDatetime: "2009-10-14T20:08:14+00:00"
485
+ type: subscription
486
+ gatewayToken:
487
+ id: 7ccd6e5e-0a4d-102d-a92d-40402145ee8b
488
+ createdDatetime: "2009-10-14T20:08:14+00:00"
489
+ modifiedDatetime: "2009-10-14T20:08:14+00:00"
490
+ firstName: wqaqyhjdfg
491
+ email: krylmrreef@example.com