sproutbox-mousetrap 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift << File.join(File.dirname(__FILE__), "..", "lib")
4
+
5
+ require 'mousetrap'
6
+ require 'irb'
7
+ require 'yaml'
8
+
9
+ config_file = File.join(File.dirname(__FILE__), "..", "mousetrap_config.yml")
10
+ raise "#{config_file} must exist" unless File.exists?(config_file)
11
+ config = YAML.load_file(config_file)
12
+ Mousetrap.product_code = config['product_code']
13
+ Mousetrap.authenticate(config['username'], config['password'])
14
+
15
+ IRB.start
@@ -0,0 +1,42 @@
1
+ require 'factory_girl'
2
+
3
+ Factory.define :new_customer, :class => Mousetrap::Customer, :default_strategy => :stub do |f|
4
+ f.email { random_email_address }
5
+ f.first_name { random_string }
6
+ f.last_name { random_string }
7
+ f.company { random_string }
8
+ f.code { |me| me.email }
9
+ f.add_attribute :id, nil
10
+ f.subscription_attributes { Factory.attributes_for :subscription }
11
+ end
12
+
13
+ Factory.define :existing_customer, :parent => :new_customer, :default_strategy => :stub do |f|
14
+ f.add_attribute :id, '2d1244e8-e338-102c-a92d-40402145ee8b'
15
+ end
16
+
17
+ Factory.define :alt_new_customer, :parent => :new_customer, :default_strategy => :stub do |f|
18
+ # TODO: international?
19
+ end
20
+
21
+ Factory.define :subscription, :class => Mousetrap::Subscription, :default_strategy => :stub do |f|
22
+ f.plan_code 'TEST'
23
+ f.billing_first_name { random_string }
24
+ f.billing_last_name { random_string }
25
+ f.credit_card_number '4111111111111111'
26
+ f.credit_card_expiration_month '12'
27
+ f.credit_card_expiration_year '2012'
28
+ f.credit_card_code '123'
29
+ f.billing_address "400 W 7th St\n#200"
30
+ f.billing_city 'Bloomington'
31
+ f.billing_state 'IN'
32
+ f.billing_country 'US'
33
+ f.billing_zip_code '47404'
34
+ end
35
+
36
+ Factory.define :alternate_subscription, :parent => :subscription, :default_strategy => :stub do |f|
37
+ f.credit_card_number '5555555555554444'
38
+ f.credit_card_expiration_month '7'
39
+ f.credit_card_expiration_year '2013'
40
+ f.credit_card_code '456'
41
+ f.billing_zip_code '12345'
42
+ end
@@ -0,0 +1,6 @@
1
+ # Replace user and password with your CheddarGetter credentials.
2
+ user: 'lark@example.com'
3
+ password: 'abc123'
4
+
5
+ # Go create a product in the CheddarGetter web admin interface named "mousetrap_test".
6
+ product_code: 'mousetrap_test'
@@ -0,0 +1,358 @@
1
+ require File.expand_path('../../../lib/mousetrap', __FILE__)
2
+
3
+ require "bundler"
4
+ Bundler.setup
5
+
6
+ require 'factories'
7
+
8
+ require 'rspec'
9
+ require 'active_support'
10
+ require 'yaml'
11
+
12
+ Dir["#{File.dirname(__FILE__)}/../support/**/*.rb"].each {|f| require f}
13
+
14
+ settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
15
+ Mousetrap.authenticate(settings['user'], settings['password'])
16
+ Mousetrap.product_code = settings['product_code']
17
+
18
+ RSpec.configure do |config|
19
+ config.before :suite do
20
+ begin
21
+ Mousetrap::Customer.destroy_all
22
+ rescue
23
+ end
24
+ end
25
+ end
26
+
27
+ shared_examples_for "a Customer record from CheddarGetter" do
28
+ describe "And I get the customer" do
29
+ before :all do
30
+ @api_customer = Mousetrap::Customer[@customer.code]
31
+ end
32
+
33
+ it "Then I should see first name" do
34
+ @api_customer.first_name.should == @customer.first_name
35
+ end
36
+
37
+ it "And I should see last name" do
38
+ @api_customer.last_name.should == @customer.last_name
39
+ end
40
+
41
+ it "And I should see company" do
42
+ @api_customer.company.should == @customer.company
43
+ end
44
+
45
+ it "And I should see the code" do
46
+ @api_customer.code.should == @customer.code
47
+ end
48
+
49
+ it "And I should see the ID" do
50
+ @api_customer.id.should == @customer.id
51
+ end
52
+ end
53
+ end
54
+
55
+ shared_examples_for "an active Subscription record from CheddarGetter" do
56
+ describe "And I get the subscription" do
57
+ before :all do
58
+ @api_customer = Mousetrap::Customer[@customer.code]
59
+ @api_subscription = @api_customer.subscription
60
+ end
61
+
62
+ it "Then ID is set" do
63
+ @api_subscription.id.should be
64
+ end
65
+
66
+ it "Then canceled_at is not set" do
67
+ @api_subscription.canceled_at.should be_nil
68
+ end
69
+
70
+ it "Then created_at is set" do
71
+ @api_subscription.created_at.should be
72
+ end
73
+
74
+ it "Then I should see the credit card expiration" do
75
+ expiration_date_from_api = Date.parse(@api_subscription.credit_card_expiration_date)
76
+ expiration_date = Date.parse("#{@customer.subscription.credit_card_expiration_year}-#{@customer.subscription.credit_card_expiration_month}-31")
77
+ expiration_date_from_api.should == expiration_date
78
+ end
79
+
80
+ it "Then I should see the credit card last four digits" do
81
+ @api_subscription.credit_card_last_four_digits.should == @customer.subscription.credit_card_number[-4..-1]
82
+ end
83
+
84
+ it "Then I should see the credit card type" do
85
+ @api_subscription.credit_card_type.should == (@credit_card_type || 'visa')
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "The Wrapper Gem" do
91
+ describe Mousetrap::Customer do
92
+ describe ".all" do
93
+ describe "Given a few customers on CheddarGetter" do
94
+ before :all do
95
+ 3.times { Factory(:new_customer).save }
96
+ violated "Couldn't save customers" unless Mousetrap::Customer.all.size == 3
97
+ end
98
+
99
+ describe "When I call .all" do
100
+ before :all do
101
+ @all_customers = Mousetrap::Customer.all
102
+ end
103
+
104
+ it "Then I should get all the customers" do
105
+ @all_customers.size.should == 3
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ describe ".create" do
112
+ describe "When I create a customer" do
113
+ before :all do
114
+ attributes = Factory.attributes_for :new_customer
115
+ @customer = Mousetrap::Customer.create attributes
116
+ end
117
+
118
+ it_should_behave_like "a Customer record from CheddarGetter"
119
+ it_should_behave_like "an active Subscription record from CheddarGetter"
120
+ end
121
+
122
+ describe "When I create a customer with international card" do
123
+ before :all do
124
+ attributes = Factory.attributes_for :alt_new_customer
125
+ @customer = Mousetrap::Customer.create attributes
126
+ end
127
+
128
+ it_should_behave_like "a Customer record from CheddarGetter"
129
+ it_should_behave_like "an active Subscription record from CheddarGetter"
130
+ end
131
+ end
132
+
133
+ describe ".destroy_all" do
134
+ describe "Given a few customers on CheddarGetter" do
135
+ before :all do
136
+ Mousetrap::Customer.destroy_all
137
+ 3.times { Factory(:new_customer).save }
138
+ violated "Couldn't save customers" unless Mousetrap::Customer.all.size == 3
139
+ end
140
+
141
+ describe "When I call .destroy_all" do
142
+ before :all do
143
+ Mousetrap::Customer.destroy_all
144
+ end
145
+
146
+ it "Then there should be no customers" do
147
+ Mousetrap::Customer.all.size.should == 0
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ describe ".update" do
154
+ describe "Given a customer" do
155
+ before :all do
156
+ @customer = Factory :new_customer
157
+ @api_customer = nil
158
+
159
+ # TODO: figure out why multiple records are being created even though
160
+ # we use "before :all". Until then, here's the kludge...
161
+ if Mousetrap::Customer.all.size == 1
162
+ @api_customer = Mousetrap::Customer.all.first
163
+ @customer = @api_customer
164
+ else
165
+ @customer.save
166
+ @api_customer = Mousetrap::Customer[@customer.code]
167
+ end
168
+ end
169
+
170
+ describe "When I update the customer, with only customer attributes" do
171
+ before :all do
172
+ @api_customer = Mousetrap::Customer[@customer.code]
173
+
174
+ updated_attributes = {
175
+ :first_name => 'new_first',
176
+ :last_name => 'new_last',
177
+ :email => 'new_email@example.com',
178
+ :company => 'new_company'
179
+ }
180
+
181
+ @customer = Mousetrap::Customer.new updated_attributes
182
+ @customer.code = @api_customer.code
183
+ @customer.id = @api_customer.id
184
+
185
+ Mousetrap::Customer.update(@api_customer.code, updated_attributes)
186
+ end
187
+
188
+ it_should_behave_like "a Customer record from CheddarGetter"
189
+ end
190
+
191
+ describe "When I update the customer, with customer and subscription attributes" do
192
+ before :all do
193
+ @api_customer = Mousetrap::Customer[@customer.code]
194
+
195
+ updated_attributes = {
196
+ :first_name => 'new_first',
197
+ :last_name => 'new_last',
198
+ :email => 'new_email@example.com',
199
+ :company => 'new_company',
200
+ :subscription_attributes => {
201
+ :plan_code => 'TEST',
202
+ :billing_first_name => 'new_billing_first',
203
+ :billing_last_name => 'new_billing_last',
204
+ :credit_card_number => '5555555555554444',
205
+ :credit_card_expiration_month => '01',
206
+ :credit_card_expiration_year => '2013',
207
+ :billing_zip_code => '12345'
208
+ }
209
+ }
210
+
211
+ @credit_card_type = 'mc'
212
+
213
+ @customer = Mousetrap::Customer.new updated_attributes
214
+
215
+ # set up our test comparison objects as if they came from API gets
216
+ @customer.code = @api_customer.code
217
+ @customer.id = @api_customer.id
218
+ @customer.subscription.send(:id=, @api_customer.subscription.id)
219
+
220
+ Mousetrap::Customer.update(@api_customer.code, updated_attributes)
221
+ end
222
+
223
+ it_should_behave_like "a Customer record from CheddarGetter"
224
+ it_should_behave_like "an active Subscription record from CheddarGetter"
225
+ end
226
+ end
227
+ end
228
+
229
+ describe "#cancel" do
230
+ describe "Given a customer" do
231
+ before :all do
232
+ @customer = Factory :new_customer
233
+ @api_customer = nil
234
+
235
+ if Mousetrap::Customer.all.size == 1
236
+ @api_customer = Mousetrap::Customer.all.first
237
+ @customer = @api_customer
238
+ else
239
+ @customer.save
240
+ @api_customer = Mousetrap::Customer[@customer.code]
241
+ end
242
+ end
243
+
244
+ describe "When I cancel" do
245
+ before :all do
246
+ @api_customer.cancel
247
+ end
248
+
249
+ describe "And I get the customer" do
250
+ before :all do
251
+ @api_customer = Mousetrap::Customer[@customer.code]
252
+ end
253
+
254
+ it "Then I should see a cancellation date on subscription" do
255
+ @api_customer.subscription.canceled_at.should be
256
+ end
257
+
258
+ describe "When I resubscribe them" do
259
+ before :all do
260
+ @customer = Factory :new_customer, :email => @api_customer.email, :code => @api_customer.email
261
+ @customer.save
262
+ end
263
+
264
+ it_should_behave_like "a Customer record from CheddarGetter"
265
+ it_should_behave_like "an active Subscription record from CheddarGetter"
266
+ end
267
+ end
268
+ end
269
+ end
270
+ end
271
+
272
+ describe "#save" do
273
+ describe "When I save a customer" do
274
+ before :all do
275
+ @customer = Factory :new_customer
276
+ @customer.save
277
+ end
278
+
279
+ it_should_behave_like "a Customer record from CheddarGetter"
280
+
281
+ describe "When I save it again, with different attributes" do
282
+ before :all do
283
+ attributes = Factory.attributes_for :new_customer
284
+ @customer.first_name = attributes[:first_name]
285
+ @customer.last_name = attributes[:last_name]
286
+ @customer.email = attributes[:email]
287
+ @customer.company = attributes[:company]
288
+ @customer.save
289
+ end
290
+
291
+ it_should_behave_like "a Customer record from CheddarGetter"
292
+ end
293
+
294
+ context "When I update subscription information" do
295
+ before :all do
296
+ @subscription = Mousetrap::Subscription.new(Factory.attributes_for(:alternate_subscription))
297
+ @credit_card_type = 'mc'
298
+ @customer.subscription = @subscription
299
+ @customer.save
300
+ end
301
+
302
+ it_should_behave_like "an active Subscription record from CheddarGetter"
303
+ end
304
+ end
305
+ end
306
+
307
+ describe '#switch_to_plan' do
308
+ describe "Given an existing CheddarGetter customer" do
309
+ before :all do
310
+ @customer = Factory :new_customer
311
+ @customer.save
312
+ end
313
+
314
+ describe 'When I switch plans' do
315
+ before :all do
316
+ @customer.switch_to_plan('TEST_2')
317
+ end
318
+
319
+ describe "And I get the customer" do
320
+ before :all do
321
+ @api_customer = Mousetrap::Customer[@customer.code]
322
+ end
323
+
324
+ it 'Then they should be on the new plan' do
325
+ @api_customer.subscription.plan.code.should == 'TEST_2'
326
+ end
327
+ end
328
+ end
329
+ end
330
+ end
331
+ end
332
+
333
+ describe Mousetrap::Subscription do
334
+ describe "Given a customer on CheddarGetter" do
335
+ before :all do
336
+ @customer = Factory :new_customer
337
+ violated "Use a visa for setup" unless @customer.subscription.credit_card_number == '4111111111111111'
338
+ @customer.save
339
+ end
340
+
341
+ describe "When I update a subscription field" do
342
+ before :all do
343
+ Mousetrap::Subscription.update @customer.code, :credit_card_number => '5555555555554444'
344
+ end
345
+
346
+ describe "And I get the customer" do
347
+ before :all do
348
+ @api_customer = Mousetrap::Customer[@customer.code]
349
+ end
350
+
351
+ it 'Then I should see the updated field' do
352
+ @api_customer.subscription.credit_card_last_four_digits.should == '4444'
353
+ end
354
+ end
355
+ end
356
+ end
357
+ end
358
+ end
@@ -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 'factories'
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
+