rspreedly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.document +5 -0
  2. data/.gitignore +5 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +78 -0
  5. data/Rakefile +48 -0
  6. data/VERSION +1 -0
  7. data/lib/rspreedly/base.rb +76 -0
  8. data/lib/rspreedly/complimentary_subscription.rb +5 -0
  9. data/lib/rspreedly/complimentary_time_extension.rb +5 -0
  10. data/lib/rspreedly/config.rb +84 -0
  11. data/lib/rspreedly/error.rb +9 -0
  12. data/lib/rspreedly/invoice.rb +61 -0
  13. data/lib/rspreedly/line_item.rb +5 -0
  14. data/lib/rspreedly/payment_method.rb +7 -0
  15. data/lib/rspreedly/subscriber.rb +153 -0
  16. data/lib/rspreedly/subscription_plan.rb +40 -0
  17. data/lib/rspreedly.rb +29 -0
  18. data/spec/config_spec.rb +144 -0
  19. data/spec/fixtures/complimentary_failed_active.xml +1 -0
  20. data/spec/fixtures/complimentary_failed_inactive.xml +1 -0
  21. data/spec/fixtures/complimentary_not_valid.xml +3 -0
  22. data/spec/fixtures/complimentary_success.xml +25 -0
  23. data/spec/fixtures/create_subscriber.xml +25 -0
  24. data/spec/fixtures/existing_subscriber.xml +1 -0
  25. data/spec/fixtures/free_plan_not_elligable.xml +1 -0
  26. data/spec/fixtures/free_plan_not_free.xml +1 -0
  27. data/spec/fixtures/free_plan_not_set.xml +1 -0
  28. data/spec/fixtures/free_plan_success.xml +25 -0
  29. data/spec/fixtures/invalid_subscriber.xml +1 -0
  30. data/spec/fixtures/invalid_update.xml +1 -0
  31. data/spec/fixtures/invoice_created.xml +43 -0
  32. data/spec/fixtures/invoice_invalid.xml +1 -0
  33. data/spec/fixtures/no_plans.xml +2 -0
  34. data/spec/fixtures/no_subscribers.xml +2 -0
  35. data/spec/fixtures/payment_already_paid.xml +1 -0
  36. data/spec/fixtures/payment_invalid.xml +1 -0
  37. data/spec/fixtures/payment_not_found.xml +1 -0
  38. data/spec/fixtures/payment_success.xml +43 -0
  39. data/spec/fixtures/plan_disabled.xml +1 -0
  40. data/spec/fixtures/plan_not_found.xml +1 -0
  41. data/spec/fixtures/subscriber.xml +25 -0
  42. data/spec/fixtures/subscriber_not_found.xml +1 -0
  43. data/spec/fixtures/subscribers.xml +75 -0
  44. data/spec/fixtures/subscription_plan_list.xml +91 -0
  45. data/spec/invoice_spec.rb +129 -0
  46. data/spec/spec_helper.rb +44 -0
  47. data/spec/subscriber_spec.rb +442 -0
  48. data/spec/subscription_plan_spec.rb +36 -0
  49. metadata +107 -0
@@ -0,0 +1,129 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe RSpreedly::Invoice do
4
+
5
+ describe "#create!" do
6
+
7
+ before(:each) do
8
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob", :email => "test@example.com")
9
+ @invoice = RSpreedly::Invoice.new(:subscriber => @subscriber, :subscription_plan_id => 2502)
10
+ end
11
+
12
+ it "should return true if successful" do
13
+ stub_http_with_fixture("invoice_created.xml", 201)
14
+ @invoice.create.should be_true
15
+ end
16
+
17
+ it "should update the invoice if successful" do
18
+ stub_http_with_fixture("invoice_created.xml", 200)
19
+ lambda{
20
+ @invoice.create
21
+ }.should change(@invoice, :price).to("$0.00")
22
+ end
23
+
24
+ it "should setup line items in the invoice if successful" do
25
+ stub_http_with_fixture("invoice_created.xml", 200)
26
+ @invoice.create
27
+ @invoice.line_items.size.should == 1
28
+ @invoice.line_items[0].should be_a(RSpreedly::LineItem)
29
+ end
30
+
31
+ it "should raise NotFound if the plan doesn't exist" do
32
+ stub_http_with_fixture("plan_not_found.xml", 404)
33
+ lambda{
34
+ @invoice.create!
35
+ }.should raise_error(RSpreedly::Error::NotFound)
36
+ end
37
+
38
+ it "should raise BadRequest if the invoice is invalid" do
39
+ stub_http_with_fixture("invoice_invalid.xml", 422)
40
+ lambda{
41
+ @invoice.create!
42
+ }.should raise_error(RSpreedly::Error::BadRequest)
43
+ end
44
+
45
+ it "should raise Forbidden if the plan is disabled" do
46
+ stub_http_with_fixture("plan_disabled.xml", 403)
47
+ lambda{
48
+ @invoice.create!
49
+ }.should raise_error(RSpreedly::Error::Forbidden)
50
+ end
51
+ end
52
+
53
+ describe "#create" do
54
+
55
+ before(:each) do
56
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob", :email => "test@example.com")
57
+ @invoice = RSpreedly::Invoice.new(:subscriber => @subscriber, :subscription_plan_id => 2502)
58
+ end
59
+
60
+ it "should return true if successful" do
61
+ stub_http_with_fixture("invoice_created.xml", 201)
62
+ @invoice.create.should be_true
63
+ end
64
+
65
+ it "should return nil if the plan doesn't exist" do
66
+ stub_http_with_fixture("plan_not_found.xml", 404)
67
+ @invoice.create.should be_nil
68
+ end
69
+
70
+ it "should return nil if the invoice is invalid" do
71
+ stub_http_with_fixture("invoice_invalid.xml", 422)
72
+ @invoice.create.should be_nil
73
+ end
74
+
75
+ it "should return nil if the plan is disabled" do
76
+ stub_http_with_fixture("plan_disabled.xml", 403)
77
+ @invoice.create.should be_nil
78
+ end
79
+ end
80
+
81
+
82
+ describe "#pay" do
83
+
84
+ before(:each) do
85
+ @invoice = RSpreedly::Invoice.new(:token => "5b1f186651dd988865c6573921ec87fa4bec23b8")
86
+ @payment = RSpreedly::PaymentMethod::CreditCard.new(:number => "4222222222222",
87
+ :card_type => "visa",
88
+ :verification_value => "234",
89
+ :month => 1,
90
+ :year => 2011,
91
+ :first_name => "Joe",
92
+ :last_name => "Bob")
93
+ end
94
+
95
+ it "should return true if successful" do
96
+ stub_http_with_fixture("payment_success.xml", 200)
97
+ @invoice.pay(@payment).should be_true
98
+ end
99
+
100
+ it "should update the Invoice if successful" do
101
+ stub_http_with_fixture("payment_success.xml", 200)
102
+
103
+ lambda{
104
+ @invoice.pay(@payment)
105
+ }.should change(@invoice, :closed).to(true)
106
+ end
107
+
108
+ it "should raise NotFound if the invoice doesn't exist" do
109
+ stub_http_with_fixture("payment_not_found.xml", 404)
110
+ lambda{
111
+ @invoice.pay(@payment)
112
+ }.should raise_error(RSpreedly::Error::NotFound)
113
+ end
114
+
115
+ it "should raise BadRequest if the payment method is invalid" do
116
+ stub_http_with_fixture("payment_invalid.xml", 422)
117
+ lambda{
118
+ @invoice.pay(@payment)
119
+ }.should raise_error(RSpreedly::Error::BadRequest)
120
+ end
121
+
122
+ it "should raise Forbidden if the invoice is already paid" do
123
+ stub_http_with_fixture("payment_already_paid.xml", 403)
124
+ lambda{
125
+ @invoice.pay(@payment)
126
+ }.should raise_error(RSpreedly::Error::Forbidden)
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'rspreedly'
7
+
8
+ # setup a config with dummy data
9
+ RSpreedly::Config.setup do |config|
10
+ config.api_key = "your-api-key-here"
11
+ config.site_name = "you-site-name-here"
12
+ end
13
+
14
+ Spec::Runner.configure do |config|
15
+
16
+ end
17
+
18
+ def load_fixture(file)
19
+ response_file = File.join(File.dirname(__FILE__), 'fixtures', file)
20
+ File.read(response_file)
21
+ end
22
+
23
+ def stub_http_with_fixture(fixture, status = 200)
24
+ stub_http_response(:body => load_fixture(fixture), :code => status)
25
+ end
26
+
27
+ def stub_http_with_code(status = 200)
28
+ stub_http_response(:code => status)
29
+ end
30
+
31
+ def stub_http_response(opts={})
32
+ http_response = mock(:response)
33
+
34
+ http_response.stub!(:body).and_return(opts[:body] || "")
35
+ http_response.stub!(:message).and_return("")
36
+ http_response.stub!(:kind_of?).and_return(true)
37
+ http_response.stub!(:code).and_return(opts[:code] || 200)
38
+ http_response.stub!(:to_hash).and_return({})
39
+
40
+ mock_http = mock(:net_http, :null_object => true)
41
+ mock_http.stub!(:request).and_return(http_response)
42
+
43
+ Net::HTTP.stub!(:new).and_return(mock_http)
44
+ end
@@ -0,0 +1,442 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ describe RSpreedly::Subscriber do
3
+
4
+ describe ".find" do
5
+ describe "with the symbol :all" do
6
+ it "should pass to RSpreedly::Subscriber.all" do
7
+ RSpreedly::Subscriber.should_receive(:all)
8
+ RSpreedly::Subscriber.find(:all)
9
+ end
10
+ end
11
+
12
+ it "should return nil with an id for a subscriber who doesn't exist" do
13
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
14
+ RSpreedly::Subscriber.find(42).should be_nil
15
+ end
16
+
17
+ it "should return a Subscriber with an id for an existing subscriber" do
18
+ stub_http_with_fixture("subscriber.xml", 200)
19
+ RSpreedly::Subscriber.find(42).should be_a(RSpreedly::Subscriber)
20
+ end
21
+ end
22
+
23
+ describe ".all" do
24
+ it "should return an empty array if there are no subscribers" do
25
+ stub_http_with_fixture("no_subscribers.xml", 200)
26
+ RSpreedly::Subscriber.all.should == []
27
+ end
28
+
29
+ it "should return an array of subscribers if there are any to find" do
30
+ stub_http_with_fixture("subscribers.xml", 200)
31
+ RSpreedly::Subscriber.all.size.should == 3 # there are 3 in the fixture
32
+ RSpreedly::Subscriber.all.select{|x| x.is_a?(RSpreedly::Subscriber )}.size.should == 3
33
+ end
34
+ end
35
+
36
+ describe ".destroy_all" do
37
+ it "should return true if successful" do
38
+ stub_http_with_code(200)
39
+ RSpreedly::Subscriber.destroy_all.should be_true
40
+ end
41
+ end
42
+
43
+ describe "#new_record?" do
44
+ before(:each) do
45
+ @subscriber = RSpreedly::Subscriber.new
46
+ end
47
+
48
+ it "should return true if the subscriber doesn't have a token" do
49
+ @subscriber.new_record?.should be_true
50
+ end
51
+
52
+ it "should return false if the subscriber has a token" do
53
+ @subscriber.token = "something"
54
+ @subscriber.new_record?.should be_false
55
+ end
56
+ end
57
+
58
+ describe "#save" do
59
+ before(:each) do
60
+ @subscriber = RSpreedly::Subscriber.new
61
+ end
62
+
63
+ it "should call #create for a non existing subscriber" do
64
+ @subscriber.should_receive(:create)
65
+ @subscriber.save
66
+ end
67
+
68
+ it "should call update for an existing subscriber" do
69
+ @subscriber.token = "something"
70
+ @subscriber.should_receive(:update)
71
+ @subscriber.save
72
+ end
73
+ end
74
+
75
+ describe "#create!" do
76
+ it "should return true if successful" do
77
+ stub_http_with_fixture("create_subscriber.xml", 201)
78
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob")
79
+ @subscriber.create!.should be_true
80
+ end
81
+
82
+ it "should raise a BadRequest error if the data is invalid" do
83
+ stub_http_with_fixture("invalid_subscriber.xml", 422)
84
+ @subscriber = RSpreedly::Subscriber.new
85
+ lambda{
86
+ @subscriber.create!
87
+ }.should raise_error(RSpreedly::Error::BadRequest)
88
+ end
89
+
90
+ it "should update the subscriber if successful" do
91
+ stub_http_with_fixture("create_subscriber.xml", 200)
92
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob")
93
+ lambda{
94
+ @subscriber.create!
95
+ }.should change(@subscriber, :token).to("6af9994a57e420345897b1abb4c27a9db27fa4d0")
96
+ end
97
+
98
+ it "should raise a Forbidden error if there is already a subscriber with that ID" do
99
+ stub_http_with_fixture("existing_subscriber.xml", 403)
100
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob")
101
+ lambda{
102
+ @subscriber.create!
103
+ }.should raise_error(RSpreedly::Error::Forbidden)
104
+ end
105
+ end
106
+
107
+ describe "#create" do
108
+ it "should return true if successful" do
109
+ stub_http_with_fixture("create_subscriber.xml", 201)
110
+ @subscriber = RSpreedly::Subscriber.new
111
+ @subscriber.customer_id = 42
112
+ @subscriber.screen_name = "bob"
113
+ @subscriber.create.should be_true
114
+ end
115
+
116
+ it "should return nil if the data is invalid" do
117
+ stub_http_with_fixture("invalid_subscriber.xml", 422)
118
+ @subscriber = RSpreedly::Subscriber.new
119
+ @subscriber.create.should be_nil
120
+ end
121
+
122
+ it "should return nil if there is already a subscriber with that ID" do
123
+ stub_http_with_fixture("existing_subscriber.xml", 403)
124
+ @subscriber = RSpreedly::Subscriber.new
125
+ @subscriber.customer_id = 42
126
+ @subscriber.screen_name = "bob"
127
+ @subscriber.create.should be_nil
128
+ end
129
+ end
130
+
131
+ describe "#update!" do
132
+ before(:each) do
133
+ @subscriber = RSpreedly::Subscriber.new
134
+ end
135
+
136
+ describe "with basic information" do
137
+ it "should return true if successful" do
138
+ stub_http_with_code(200)
139
+ @subscriber.customer_id = 42
140
+ @subscriber.email = "new@email.com"
141
+ @subscriber.update!.should be_true
142
+ end
143
+
144
+ it "should raise NotFound if the subscriber doesn't exist" do
145
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
146
+ @subscriber.customer_id = 400
147
+ lambda{
148
+ @subscriber.update!
149
+ }.should raise_error(RSpreedly::Error::NotFound)
150
+ end
151
+
152
+ it "should raise BadRequest if the data is invalid" do
153
+ stub_http_with_fixture("invalid_update.xml", 422)
154
+ @subscriber.customer_id = 42
155
+ lambda{
156
+ @subscriber.update!
157
+ }.should raise_error(RSpreedly::Error::BadRequest)
158
+ end
159
+ end
160
+
161
+ describe "with new_customer_id" do
162
+ it "should return true if successful" do
163
+ stub_http_with_code(200)
164
+ @subscriber.customer_id = 42
165
+ @subscriber.new_customer_id = 43
166
+ @subscriber.email = "new@email.com"
167
+ @subscriber.update!.should be_true
168
+ end
169
+
170
+ it "should raise NotFound if the subscriber doesn't exist" do
171
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
172
+ @subscriber.customer_id = 400
173
+ @subscriber.new_customer_id = 401
174
+ lambda{
175
+ @subscriber.update!
176
+ }.should raise_error(RSpreedly::Error::NotFound)
177
+ end
178
+
179
+ it "should raise BadRequest if the data is invalid" do
180
+ stub_http_with_fixture("invalid_update.xml", 422)
181
+ @subscriber.new_customer_id = 43
182
+ lambda{
183
+ @subscriber.update!
184
+ }.should raise_error(RSpreedly::Error::BadRequest)
185
+ end
186
+ end
187
+
188
+ describe "with payment information" do
189
+ before(:each) do
190
+ @subscriber.customer_id = 42
191
+ @subscriber.payment_method = RSpreedly::PaymentMethod::CreditCard.new(:number => "1233445", :verification_value => "234")
192
+ end
193
+
194
+ it "should return true if successful" do
195
+ stub_http_with_code(200)
196
+ @subscriber.update!.should be_true
197
+ end
198
+
199
+ it "should raise NotFound if the subscriber doesn't exist" do
200
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
201
+ lambda{
202
+ @subscriber.update!
203
+ }.should raise_error(RSpreedly::Error::NotFound)
204
+ end
205
+
206
+ it "should raise BadRequest if the data is invalid" do
207
+ stub_http_with_fixture("invalid_update.xml", 422)
208
+ lambda{
209
+ @subscriber.update!
210
+ }.should raise_error(RSpreedly::Error::BadRequest)
211
+ end
212
+
213
+ it "should raise Forbidden if the data is invalid" do
214
+ stub_http_with_fixture("invalid_update.xml", 403)
215
+ lambda{
216
+ @subscriber.update!
217
+ }.should raise_error(RSpreedly::Error::Forbidden)
218
+ end
219
+ end
220
+ end
221
+
222
+ describe "#update" do
223
+ before(:each) do
224
+ @subscriber = RSpreedly::Subscriber.new
225
+ end
226
+
227
+ describe "with basic information" do
228
+ it "should return true if successful" do
229
+ stub_http_with_code(200)
230
+ @subscriber.customer_id = 42
231
+ @subscriber.email = "new@email.com"
232
+ @subscriber.update!.should be_true
233
+ end
234
+
235
+ it "should return nil if the subscriber doesn't exist" do
236
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
237
+ @subscriber.customer_id = 400
238
+ @subscriber.update.should be_nil
239
+ end
240
+
241
+ it "should return nil if the data is invalid" do
242
+ stub_http_with_fixture("invalid_update.xml", 422)
243
+ @subscriber.customer_id = 42
244
+ @subscriber.update.should be_nil
245
+ end
246
+ end
247
+ end
248
+
249
+ describe "#destroy" do
250
+ before(:each) do
251
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
252
+ end
253
+
254
+ it "should return nil if the subscriber doesn't exist" do
255
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
256
+ @subscriber.destroy.should be_nil
257
+ end
258
+
259
+ it "should return true if successful" do
260
+ stub_http_with_code(200)
261
+ @subscriber.destroy.should be_true
262
+ end
263
+ end
264
+
265
+ describe "#comp_subscription" do
266
+ before(:each) do
267
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :feature_level => "Lowly")
268
+ @subscription = RSpreedly::ComplimentarySubscription.new(:duration_quantity => 42, :duration_units => "months", :feature_level => "Pro")
269
+ end
270
+
271
+ it "should return true if successful" do
272
+ stub_http_with_fixture("complimentary_success.xml", 201)
273
+ @subscriber.comp_subscription(@subscription).should be_true
274
+ end
275
+
276
+ it "should update the subscriber if successful" do
277
+ stub_http_with_fixture("complimentary_success.xml", 201)
278
+ lambda{
279
+ @subscriber.comp_subscription(@subscription)
280
+ }.should change(@subscriber, :feature_level).to("Pro")
281
+ end
282
+
283
+ it "should raise NotFound if the subscriber doesn't exist" do
284
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
285
+ lambda{
286
+ @subscriber.comp_subscription(@subscription)
287
+ }.should raise_error(RSpreedly::Error::NotFound)
288
+ end
289
+
290
+ it "should raise BadRequest if validation fails on the subscription" do
291
+ stub_http_with_fixture("complimentary_not_valid.xml", 422)
292
+ lambda{
293
+ @subscriber.comp_subscription(@subscription)
294
+ }.should raise_error(RSpreedly::Error::BadRequest)
295
+ end
296
+
297
+ it "should raise Forbidden if the subscriber is active" do
298
+ stub_http_with_fixture("complimentary_failed_active.xml", 403)
299
+ lambda{
300
+ @subscriber.comp_subscription(@subscription)
301
+ }.should raise_error(RSpreedly::Error::Forbidden)
302
+ end
303
+ end
304
+
305
+ describe "#comp_time_extension" do
306
+ before(:each) do
307
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :feature_level => "Lowly")
308
+ @subscription = RSpreedly::ComplimentaryTimeExtension.new(:duration_quantity => 42, :duration_units => "months")
309
+ end
310
+
311
+ it "should return true if successful" do
312
+ stub_http_with_fixture("complimentary_success.xml", 201)
313
+ @subscriber.comp_time_extension(@subscription).should be_true
314
+ end
315
+
316
+ it "should update the subscriber if successful" do
317
+ stub_http_with_fixture("complimentary_success.xml", 201)
318
+ lambda{
319
+ @subscriber.comp_time_extension(@subscription)
320
+ }.should change(@subscriber, :active_until).to(Time.parse("Sun Feb 21 19:04:28 UTC 2010"))
321
+ end
322
+
323
+ it "should raise NotFound if the subscriber doesn't exist" do
324
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
325
+ lambda{
326
+ @subscriber.comp_time_extension(@subscription)
327
+ }.should raise_error(RSpreedly::Error::NotFound)
328
+ end
329
+
330
+ it "should raise BadRequest if validation fails on the subscription" do
331
+ stub_http_with_fixture("complimentary_not_valid.xml", 422)
332
+ lambda{
333
+ @subscriber.comp_time_extension(@subscription)
334
+ }.should raise_error(RSpreedly::Error::BadRequest)
335
+ end
336
+
337
+ it "should raise Forbidden if the subscriber is inactive" do
338
+ stub_http_with_fixture("complimentary_failed_inactive.xml", 403)
339
+ lambda{
340
+ @subscriber.comp_time_extension(@subscription)
341
+ }.should raise_error(RSpreedly::Error::Forbidden)
342
+ end
343
+ end
344
+
345
+ describe "#stop_auto_renew" do
346
+ before(:each) do
347
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
348
+ end
349
+
350
+ it "should return true if successful" do
351
+ stub_http_with_code(200)
352
+ @subscriber.stop_auto_renew.should be_true
353
+ end
354
+
355
+ it "should raise NotFound if the subscriber doesn't exist" do
356
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
357
+ lambda{
358
+ @subscriber.stop_auto_renew
359
+ }.should raise_error(RSpreedly::Error::NotFound)
360
+ end
361
+ end
362
+
363
+ describe "#subscribe_to_free_trial" do
364
+ before(:each) do
365
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
366
+ @plan = RSpreedly::SubscriptionPlan.new(:id => 2533)
367
+ end
368
+
369
+ it "should return true if successful" do
370
+ stub_http_with_fixture("free_plan_success.xml", 200)
371
+ @subscriber.subscribe_to_free_trial(@plan).should be_true
372
+ end
373
+
374
+ it "should update the subscriber if successful" do
375
+ stub_http_with_fixture("free_plan_success.xml", 200)
376
+ lambda{
377
+ @subscriber.subscribe_to_free_trial(@plan)
378
+ }.should change(@subscriber, :grace_until).to(Time.parse("2013-05-02T00:07:37Z"))
379
+ end
380
+
381
+ it "should raise NotFound if the subscriber doesn't exist" do
382
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
383
+ lambda{
384
+ @subscriber.subscribe_to_free_trial(@plan)
385
+ }.should raise_error(RSpreedly::Error::NotFound)
386
+ end
387
+
388
+ it "should raise NotFound if the plan doesn't exist" do
389
+ stub_http_with_fixture("plan_not_found.xml", 404)
390
+ lambda{
391
+ @subscriber.subscribe_to_free_trial(@plan)
392
+ }.should raise_error(RSpreedly::Error::NotFound)
393
+ end
394
+
395
+ it "should raise BadRequest if no plan is specified" do
396
+ stub_http_with_fixture("free_plan_not_set.xml", 422)
397
+ @plan.id = nil
398
+ lambda{
399
+ @subscriber.subscribe_to_free_trial(@plan)
400
+ }.should raise_error(RSpreedly::Error::BadRequest)
401
+ end
402
+
403
+ it "should raise Forbidden if the subscriber isn't elligable" do
404
+ stub_http_with_fixture("free_plan_not_elligable.xml", 403)
405
+ lambda{
406
+ @subscriber.subscribe_to_free_trial(@plan)
407
+ }.should raise_error(RSpreedly::Error::Forbidden)
408
+ end
409
+
410
+ it "should raise Forbidden if the plan isn't a free trial" do
411
+ stub_http_with_fixture("free_plan_not_free.xml", 403)
412
+ lambda{
413
+ @subscriber.subscribe_to_free_trial(@plan)
414
+ }.should raise_error(RSpreedly::Error::Forbidden)
415
+ end
416
+ end
417
+
418
+ describe "#allow_free_trial" do
419
+ before(:each) do
420
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
421
+ end
422
+
423
+ it "should return true if successful" do
424
+ stub_http_with_fixture("free_plan_success.xml", 200)
425
+ @subscriber.allow_free_trial.should be_true
426
+ end
427
+
428
+ it "should update the subscriber if successful" do
429
+ stub_http_with_fixture("free_plan_success.xml", 200)
430
+ lambda{
431
+ @subscriber.allow_free_trial
432
+ }.should change(@subscriber, :grace_until).to(Time.parse("2013-05-02T00:07:37Z"))
433
+ end
434
+
435
+ it "should raise NotFound if the subscriber doesn't exist" do
436
+ stub_http_with_fixture("subscriber_not_found.xml", 404)
437
+ lambda{
438
+ @subscriber.allow_free_trial
439
+ }.should raise_error(RSpreedly::Error::NotFound)
440
+ end
441
+ end
442
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe RSpreedly::SubscriptionPlan do
4
+
5
+ describe ".find" do
6
+ describe "with the symbol :all" do
7
+ it "should pass to RSpreedly::SubscriptionPlan.all" do
8
+ RSpreedly::SubscriptionPlan.should_receive(:all)
9
+ RSpreedly::SubscriptionPlan.find(:all)
10
+ end
11
+ end
12
+
13
+ it "should return nil with an id for a plan which doesn't exist" do
14
+ stub_http_with_fixture("subscription_plan_list.xml", 200)
15
+ RSpreedly::SubscriptionPlan.find(99).should be_nil
16
+ end
17
+
18
+ it "should return a SubscriptionPlan with an id for an existing plan" do
19
+ stub_http_with_fixture("subscription_plan_list.xml", 200)
20
+ RSpreedly::SubscriptionPlan.find(42).should be_a(RSpreedly::SubscriptionPlan)
21
+ end
22
+ end
23
+
24
+ describe ".all" do
25
+ it "should return an empty array if there are no plans" do
26
+ stub_http_with_fixture("no_plans.xml", 200)
27
+ RSpreedly::SubscriptionPlan.all.should == []
28
+ end
29
+
30
+ it "should return an array of SubscriptionPlans if there are any to find" do
31
+ stub_http_with_fixture("subscription_plan_list.xml", 200)
32
+ RSpreedly::SubscriptionPlan.all.size.should == 4 # there are 4 in the fixture
33
+ RSpreedly::SubscriptionPlan.all.select{|x| x.is_a?(RSpreedly::SubscriptionPlan )}.size.should == 4
34
+ end
35
+ end
36
+ end