oel-brenner-mousetrap 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,108 @@
1
+ # This script is for hacking and playing around.
2
+
3
+ require File.dirname(__FILE__) + '/../../lib/mousetrap'
4
+ require 'activesupport'
5
+ require 'factory_girl'
6
+ require 'ruby-debug'
7
+ require 'yaml'
8
+
9
+ Dir["#{File.dirname(__FILE__)}/../support/**/*.rb"].each {|f| require f}
10
+
11
+ settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
12
+ Mousetrap.authenticate(settings['user'], settings['password'])
13
+ Mousetrap.product_code = settings['product_code']
14
+
15
+
16
+ def plans
17
+ all_plans = Mousetrap::Plan.all
18
+ puts all_plans.to_yaml
19
+
20
+ test_plan = Mousetrap::Plan['TEST']
21
+ puts test_plan.to_yaml
22
+ end
23
+
24
+ def all_customers
25
+ all_customers = Mousetrap::Customer.all
26
+ puts all_customers.to_yaml
27
+ end
28
+
29
+ def create_customer
30
+ customer = Factory :new_customer
31
+ customer.save
32
+
33
+ api_customer = Mousetrap::Customer[customer.code]
34
+ puts api_customer.to_yaml
35
+ end
36
+
37
+ def destroy_all_customers
38
+ Mousetrap::Customer.destroy_all
39
+ end
40
+ #create_customer
41
+
42
+ destroy_all_customers
43
+ customer = Factory :new_customer
44
+ customer.save
45
+
46
+ api_customer = Mousetrap::Customer[customer.code]
47
+ puts api_customer.to_yaml
48
+ puts '-' * 80
49
+
50
+ Mousetrap::Subscription.update customer.code, {
51
+ :billing_first_name => 'x',
52
+ :billing_last_name => 'y',
53
+ :credit_card_number => '5555555555554444',
54
+ :credit_card_expiration_month => '05',
55
+ :credit_card_expiration_year => '2013',
56
+ :billing_zip_code => '31415'
57
+ }
58
+
59
+ api_customer = Mousetrap::Customer[customer.code]
60
+ puts api_customer.to_yaml
61
+ puts '-' * 80
62
+
63
+ #customer_only_fields = Mousetrap::Customer.new \
64
+ #:first_name => 'first',
65
+ #:last_name => 'last',
66
+ #:company => 'company',
67
+ #:email => 'random@example.com',
68
+ #:code => customer.code
69
+
70
+ #customer_only_fields.save
71
+
72
+ #api_customer = Mousetrap::Customer[customer.code]
73
+ #puts api_customer.to_yaml
74
+
75
+
76
+ #code = "rvhljmvenp@example.com"
77
+ #api_customer = Mousetrap::Customer[code]
78
+ ##puts api_customer.to_yaml
79
+
80
+ #customer = Factory :new_customer
81
+ #customer.save
82
+ #api_customer = Mousetrap::Customer[customer.code]
83
+
84
+ #puts '-' * 80
85
+ #p Mousetrap::Customer[customer.code]
86
+
87
+ #puts '-' * 80
88
+ #p Mousetrap::Customer.exists? customer.code
89
+
90
+ #puts '-' * 80
91
+ #p Mousetrap::Customer['cantfindme']
92
+
93
+ #puts '-' * 80
94
+ #p Mousetrap::Customer.exists? 'cantfindme'
95
+
96
+ # create_customer
97
+
98
+ #code = 'igcuvfehrc@example.com'
99
+ #api_customer = Mousetrap::Customer[code]
100
+ #puts api_customer.to_yaml
101
+ #puts '-' * 80
102
+ #puts 'cancel'
103
+ #puts api_customer.cancel
104
+ #puts '-' * 80
105
+ #api_customer = Mousetrap::Customer[code]
106
+ #puts api_customer.to_yaml
107
+
108
+
@@ -0,0 +1,397 @@
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
+ :initialBillDate => customer.subscription.initial_bill_date,
22
+ }
23
+ }
24
+ end
25
+
26
+ describe "when having multiple subscriptions" do
27
+ it "returns the latest one" do
28
+ Mousetrap::Customer.new_from_api(full_customer).subscription.should_not be_nil
29
+ end
30
+ end
31
+
32
+ describe '.all' do
33
+ before do
34
+ Mousetrap::Customer.stub :build_resources_from
35
+ end
36
+
37
+ it "gets all customers" do
38
+ Mousetrap::Customer.should_receive(:get_resources).with('customers').and_return('some hash')
39
+ Mousetrap::Customer.all
40
+ end
41
+
42
+ it "handles kludgy 'no customers found' response" do
43
+ Mousetrap::Customer.stub :get_resources => {
44
+ 'error' => 'Resource not found: No customers found.'
45
+ }
46
+ Mousetrap::Customer.all.should == []
47
+ end
48
+
49
+ it "raises error if response has one" do
50
+ expect do
51
+ Mousetrap::Customer.stub :get_resources => { 'error' => "some other error" }
52
+ Mousetrap::Customer.all
53
+ end.to raise_error(RuntimeError, "some other error")
54
+ end
55
+
56
+ it "builds resources from the response" do
57
+ Mousetrap::Customer.stub :get_resources => 'some hash'
58
+ Mousetrap::Customer.should_receive(:build_resources_from).with('some hash')
59
+ Mousetrap::Customer.all
60
+ end
61
+ end
62
+
63
+ describe '.create' do
64
+ before do
65
+ @customer_hash = Factory.attributes_for :new_customer
66
+ @customer = Mousetrap::Customer.new @customer_hash
67
+ @customer.stub :create
68
+ Mousetrap::Customer.stub(:new => @customer)
69
+ Mousetrap::Customer.stub(:build_resource_from => stub(:id => 0))
70
+ end
71
+
72
+ it 'instantiates a customer with a hash of attributes' do
73
+ Mousetrap::Customer.should_receive(:new).with(@customer_hash).and_return(@customer)
74
+ Mousetrap::Customer.create(@customer_hash)
75
+ end
76
+
77
+ it 'creates the new customer instance' do
78
+ @customer.should_receive :create
79
+ Mousetrap::Customer.create(@customer_hash)
80
+ end
81
+
82
+ it 'returns an instance of Mousetrap::Customer' do
83
+ Mousetrap::Customer.create(@customer_hash).should be_instance_of(Mousetrap::Customer)
84
+ end
85
+ end
86
+
87
+ describe ".new" do
88
+ subject do
89
+ Mousetrap::Customer.new \
90
+ :first_name => 'Jon',
91
+ :last_name => 'Larkowski',
92
+ :email => 'lark@example.com',
93
+ :code => 'asfkhw0'
94
+ end
95
+
96
+ it { should be_instance_of(Mousetrap::Customer) }
97
+ it { should be_new_record }
98
+
99
+ describe "sets" do
100
+ it 'first_name' do
101
+ subject.first_name.should == 'Jon'
102
+ end
103
+
104
+ it 'last_name' do
105
+ subject.last_name.should == 'Larkowski'
106
+ end
107
+
108
+ it 'email' do
109
+ subject.email.should == 'lark@example.com'
110
+ end
111
+
112
+ it 'code' do
113
+ subject.code.should == 'asfkhw0'
114
+ end
115
+ end
116
+ end
117
+
118
+ describe '.update' do
119
+ def do_update
120
+ Mousetrap::Customer.update('some customer code', 'some attributes')
121
+ end
122
+
123
+ it "makes a new customer from the attributes" do
124
+ Mousetrap::Customer.should_receive(:new).with('some attributes').and_return(stub(:null_object => true))
125
+ do_update
126
+ end
127
+
128
+ it "sets the new customer code to the argument" do
129
+ customer = mock
130
+ customer.stub :update
131
+ Mousetrap::Customer.stub :new => customer
132
+ customer.should_receive(:code=).with('some customer code')
133
+ do_update
134
+ end
135
+
136
+ it "calls #update" do
137
+ customer = mock(:null_object => true)
138
+ Mousetrap::Customer.stub :new => customer
139
+ customer.should_receive :update
140
+ do_update
141
+ end
142
+ end
143
+
144
+ describe '#cancel' do
145
+ context "for existing records" do
146
+ it 'cancels' do
147
+ customer = Factory :existing_customer
148
+ customer.should_receive(:member_action).with('cancel')
149
+ customer.cancel
150
+ end
151
+ end
152
+
153
+ context "for new records" do
154
+ it "does nothing" do
155
+ customer = Factory.build :new_customer
156
+ customer.should_not_receive(:member_action).with('cancel')
157
+ customer.cancel
158
+ end
159
+ end
160
+ end
161
+
162
+ describe "#new?" do
163
+ it "looks up the customer on CheddarGetter" do
164
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
165
+ Mousetrap::Customer.should_receive(:[]).with('some_customer_code')
166
+ c.new?
167
+ end
168
+
169
+ context "with an existing CheddarGetter record" do
170
+ before do
171
+ Mousetrap::Customer.stub(:[] => stub(:id => 'some_customer_id'))
172
+ end
173
+
174
+ it "grabs the id from CheddarGetter and assigns it locally" do
175
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
176
+ c.should_receive(:id=).with('some_customer_id')
177
+ c.new?
178
+ end
179
+
180
+ it "is false" do
181
+ c = Mousetrap::Customer.new
182
+ c.should_not be_new
183
+ end
184
+ end
185
+
186
+ context "without a CheddarGetter record" do
187
+ before do
188
+ Mousetrap::Customer.stub :[] => nil
189
+ end
190
+
191
+ it "is true" do
192
+ c = Mousetrap::Customer.new
193
+ c.should be_new
194
+ end
195
+ end
196
+ end
197
+
198
+ describe '#save' do
199
+ context "for existing records" do
200
+ before do
201
+ @customer = Factory :existing_customer
202
+ @customer.stub :new? => false
203
+ end
204
+
205
+ context "with subscription association set up" do
206
+ it 'posts to edit action' do
207
+ attributes_for_api = customer_attributes_for_api(@customer)
208
+
209
+ # We don't send code for existing API resources.
210
+ attributes_for_api.delete(:code)
211
+
212
+ @customer.class.should_receive(:put_resource).with('customers', 'edit', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
213
+ @customer.save
214
+ end
215
+ end
216
+
217
+ context "with no subscription association" do
218
+ it 'posts to edit action' do
219
+ attributes_for_api = customer_attributes_for_api(@customer)
220
+
221
+ # We don't send code for existing API resources.
222
+ attributes_for_api.delete(:code)
223
+
224
+ attributes_for_api.delete(:subscription)
225
+ @customer.subscription = nil
226
+
227
+ @customer.class.should_receive(:put_resource).with('customers', 'edit-customer', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
228
+ @customer.save
229
+ end
230
+ end
231
+ end
232
+
233
+ context "for new records" do
234
+ it 'calls create' do
235
+ customer = Factory :new_customer
236
+ customer.stub :new? => true
237
+ Mousetrap::Customer.stub :exists? => false
238
+ customer.should_receive(:create)
239
+ customer.save
240
+ end
241
+ end
242
+ end
243
+
244
+ describe "#switch_to_plan" do
245
+ it "raises an error if not existing CheddarGetter customer" do
246
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
247
+ c.stub :exists? => false
248
+ expect { c.switch_to_plan 'some_plan_code' }.to raise_error(/existing/)
249
+ end
250
+
251
+ it "puts a subscription with a plan code" do
252
+ c = Mousetrap::Customer.new :code => 'some_customer_code'
253
+ c.stub :exists? => true
254
+ c.class.should_receive(:put_resource).with(
255
+ 'customers', 'edit-subscription', 'some_customer_code', { :planCode => 'some_plan_code' })
256
+ c.switch_to_plan 'some_plan_code'
257
+ end
258
+ end
259
+
260
+ describe "protected methods" do
261
+ describe "#create" do
262
+ before do
263
+ @customer = Mousetrap::Customer.new
264
+ @customer.stub :attributes_for_api_with_subscription => 'some_attributes'
265
+ end
266
+
267
+ it "posts a new customer" do
268
+ @customer.class.should_receive(:post_resource).with('customers', 'new', 'some_attributes').and_return({:id => 'some_id'})
269
+ @customer.class.stub :build_resource_from => stub(:id => 'some_id')
270
+ @customer.send :create
271
+ end
272
+
273
+ it "raises error if CheddarGetter reports one" do
274
+ @customer.class.stub :post_resource => {'error' => 'some error message'}
275
+ expect { @customer.send(:create) }.to raise_error('some error message')
276
+ end
277
+
278
+ it "builds a customer from the CheddarGetter return values" do
279
+ @customer.class.stub :post_resource => 'some response'
280
+ @customer.class.should_receive(:build_resource_from).with('some response').and_return(stub(:id => 'some_id'))
281
+ @customer.send :create
282
+ end
283
+
284
+ it "grabs the id from CheddarGetter and assigns it locally" do
285
+ @customer.class.stub :post_resource => {}
286
+ @customer.class.stub :build_resource_from => stub(:id => 'some_id')
287
+ @customer.should_receive(:id=).with('some_id')
288
+ @customer.send :create
289
+ end
290
+
291
+ it "returns the response" do
292
+ @customer.class.stub :post_resource => { :some => :response }
293
+ @customer.class.stub :build_resource_from => stub(:id => 'some_id')
294
+ @customer.send(:create).should == { :some => :response }
295
+ end
296
+ end
297
+
298
+ describe "#update" do
299
+ context "when there's a subscription instance" do
300
+ let(:customer) { Mousetrap::Customer.new :code => 'some code' }
301
+
302
+ it "puts the customer with subscription when there's a subscription instance" do
303
+ customer.stub :subscription => stub
304
+ customer.stub :attributes_for_api_with_subscription => 'some attributes with subscription'
305
+ customer.class.should_receive(:put_resource).with('customers', 'edit', 'some code', 'some attributes with subscription').and_return({:id => 'some_id'})
306
+ customer.send :update
307
+ end
308
+
309
+ it "puts just the customer when no subscription instance" do
310
+ customer.stub :subscription => nil
311
+ customer.stub :attributes_for_api => 'some attributes'
312
+ customer.class.should_receive(:put_resource).with('customers', 'edit-customer', 'some code', 'some attributes').and_return({:id => 'some_id'})
313
+ customer.send :update
314
+ end
315
+
316
+ it "raises error if CheddarGetter reports one" do
317
+ customer.class.stub :put_resource => {'error' => 'some error message'}
318
+ expect { customer.send(:update) }.to raise_error('some error message')
319
+ end
320
+ end
321
+ end
322
+ end
323
+ end
324
+
325
+
326
+ __END__
327
+
328
+ customers:
329
+ customer:
330
+ company:
331
+ lastName: cgejerpkyw
332
+ code: krylmrreef@example.com
333
+ subscriptions:
334
+ subscription:
335
+ plans:
336
+ plan:
337
+ name: Test
338
+ setupChargeAmount: "42.00"
339
+ code: TEST
340
+ recurringChargeAmount: "13.00"
341
+ billingFrequencyQuantity: "1"
342
+ trialDays: "0"
343
+ id: 8e933180-08b5-102d-a92d-40402145ee8b
344
+ billingFrequency: monthly
345
+ createdDatetime: "2009-10-12T19:28:09+00:00"
346
+ recurringChargeCode: TEST_RECURRING
347
+ isActive: "1"
348
+ billingFrequencyUnit: months
349
+ description: This is my test plan. There are many like it, but this one is mine.
350
+ billingFrequencyPer: month
351
+ setupChargeCode: TEST_SETUP
352
+ gatewayToken:
353
+ id: 7ccea6de-0a4d-102d-a92d-40402145ee8b
354
+ createdDatetime: "2009-10-14T20:08:14+00:00"
355
+ ccType: visa
356
+ ccLastFour: "1111"
357
+ ccExpirationDate: "2012-12-31T00:00:00+00:00"
358
+ canceledDatetime:
359
+ invoices:
360
+ invoice:
361
+ - number: "5"
362
+ transactions:
363
+ transaction:
364
+ response: approved
365
+ code: ""
366
+ amount: "42.00"
367
+ memo: This is a simulated transaction
368
+ id: 7ce53c78-0a4d-102d-a92d-40402145ee8b
369
+ createdDatetime: "2009-10-14T20:08:14+00:00"
370
+ transactedDatetime: "2009-10-14T20:08:14+00:00"
371
+ parentId:
372
+ charges:
373
+ charge:
374
+ code: TEST_SETUP
375
+ quantity: "1"
376
+ id: 7ce2cb6e-0a4d-102d-a92d-40402145ee8b
377
+ createdDatetime: "2009-10-14T20:08:14+00:00"
378
+ type: setup
379
+ eachAmount: "42.00"
380
+ description:
381
+ gatewayAccount:
382
+ id: ""
383
+ billingDatetime: "2009-10-14T20:08:14+00:00"
384
+ id: 7cd25072-0a4d-102d-a92d-40402145ee8b
385
+ createdDatetime: "2009-10-14T20:08:14+00:00"
386
+ type: setup
387
+ - number: "6"
388
+ billingDatetime: "2009-11-14T20:08:14+00:00"
389
+ id: 7cd4253c-0a4d-102d-a92d-40402145ee8b
390
+ createdDatetime: "2009-10-14T20:08:14+00:00"
391
+ type: subscription
392
+ gatewayToken:
393
+ id: 7ccd6e5e-0a4d-102d-a92d-40402145ee8b
394
+ createdDatetime: "2009-10-14T20:08:14+00:00"
395
+ modifiedDatetime: "2009-10-14T20:08:14+00:00"
396
+ firstName: wqaqyhjdfg
397
+ email: krylmrreef@example.com