rspreedly 0.1.11 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -1,39 +1,22 @@
1
- require 'rubygems'
2
- require 'spec'
3
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'rspec'
6
+ require 'webmock/rspec'
5
7
  require 'rspreedly'
6
8
 
7
- Spec::Runner.configure do |config|
9
+ RSpec.configure do |config|
8
10
  config.before(:each) do
9
11
  RSpreedly::Config.reset
10
12
  end
11
13
  end
12
14
 
13
- def load_fixture(file)
14
- response_file = File.join(File.dirname(__FILE__), 'fixtures', file)
15
- File.read(response_file)
15
+ def spreedly_url(path)
16
+ "https://your-api-key:X@spreedly.com/api/v4/your-site-name#{path}"
16
17
  end
17
18
 
18
- def stub_http_with_fixture(fixture, status = 200)
19
- stub_http_response(:body => load_fixture(fixture), :code => status)
20
- end
21
-
22
- def stub_http_with_code(status = 200)
23
- stub_http_response(:code => status)
24
- end
25
-
26
- def stub_http_response(opts={})
27
- http_response = mock(:response)
28
-
29
- http_response.stub!(:body).and_return(opts[:body] || "")
30
- http_response.stub!(:message).and_return("")
31
- http_response.stub!(:kind_of?).and_return(true)
32
- http_response.stub!(:code).and_return(opts[:code] || 200)
33
- http_response.stub!(:to_hash).and_return({})
34
-
35
- mock_http = mock(:net_http, :null_object => true)
36
- mock_http.stub!(:request).and_return(http_response)
37
-
38
- Net::HTTP.stub!(:new).and_return(mock_http)
19
+ def fixture(file)
20
+ response_file = File.join(File.dirname(__FILE__), 'fixtures', file)
21
+ File.read(response_file)
39
22
  end
@@ -6,88 +6,100 @@ describe RSpreedly::Subscriber do
6
6
  it "should pass to RSpreedly::Subscriber.all" do
7
7
  RSpreedly::Subscriber.should_receive(:all)
8
8
  RSpreedly::Subscriber.find(:all)
9
- end
9
+ end
10
10
  end
11
11
 
12
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)
13
+ stub_request(:get, spreedly_url("/subscribers/42.xml")).
14
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
15
+
14
16
  RSpreedly::Subscriber.find(42).should be_nil
15
17
  end
16
18
 
17
19
  it "should return a Subscriber with an id for an existing subscriber" do
18
- stub_http_with_fixture("subscriber.xml", 200)
20
+ stub_request(:get, spreedly_url("/subscribers/42.xml")).
21
+ to_return(:body => fixture("subscriber.xml"), :status => 200)
22
+
19
23
  RSpreedly::Subscriber.find(42).should be_a(RSpreedly::Subscriber)
20
24
  end
21
25
  end
22
-
26
+
23
27
  describe ".all" do
24
28
  it "should return an empty array if there are no subscribers" do
25
- stub_http_with_fixture("no_subscribers.xml", 200)
29
+ stub_request(:get, spreedly_url("/subscribers.xml")).
30
+ to_return(:body => fixture("no_subscribers.xml"), :status => 200)
31
+
26
32
  RSpreedly::Subscriber.all.should == []
27
33
  end
28
-
34
+
29
35
  it "should return an array of subscribers if there are any to find" do
30
- stub_http_with_fixture("subscribers.xml", 200)
36
+ stub_request(:get, spreedly_url("/subscribers.xml")).
37
+ to_return(:body => fixture("subscribers.xml"), :status => 200)
38
+
31
39
  RSpreedly::Subscriber.all.size.should == 3 # there are 3 in the fixture
32
40
  RSpreedly::Subscriber.all.select{|x| x.is_a?(RSpreedly::Subscriber )}.size.should == 3
33
41
  end
34
42
  end
35
-
43
+
36
44
  describe ".destroy_all" do
37
45
  it "should return true if successful" do
38
- stub_http_with_code(200)
46
+ stub_request(:delete, spreedly_url("/subscribers.xml")).
47
+ to_return(:status => 200)
48
+
39
49
  RSpreedly::Subscriber.destroy_all.should be_true
40
- end
50
+ end
41
51
  end
42
-
52
+
43
53
  describe "#to_xml" do
44
-
54
+
45
55
  before(:each) do
46
56
  # use the XML to build a subscriber
47
- stub_http_with_fixture("subscriber.xml", 200)
57
+ stub_request(:get, spreedly_url("/subscribers/42.xml")).
58
+ to_return(:body => fixture("subscriber.xml"), :status => 200)
59
+
48
60
  @subscriber = RSpreedly::Subscriber.find(42)
49
61
  end
50
-
51
- it "should strip fields the API can't handle" do
62
+
63
+ it "should strip fields the API can't handle" do
52
64
  fields = [
53
- :active, :active_until, :card_expires_before_next_auto_renew,
54
- :created_at, :eligible_for_free_trial, :feature_level,
55
- :grace_until, :in_grace_period, :lifetime_subscription,
56
- :on_trial, :ready_to_renew, :recurring,
57
- :store_credit, :store_credit_currency_code, :subscription_plan_name,
65
+ :active, :active_until, :card_expires_before_next_auto_renew,
66
+ :created_at, :eligible_for_free_trial, :feature_level,
67
+ :grace_until, :in_grace_period, :lifetime_subscription,
68
+ :on_trial, :ready_to_renew, :recurring,
69
+ :store_credit, :store_credit_currency_code, :subscription_plan_name,
58
70
  :token, :updated_at
59
71
  ]
60
-
72
+
61
73
  xml = @subscriber.to_xml
62
74
  fields.each do |field|
63
75
  xml.should_not =~ /<#{field}>/
64
- end
76
+ end
65
77
  end
66
-
78
+
67
79
  it "should not strip fields the API can handle" do
68
80
  fields = [
69
- :billing_first_name,
70
- :billing_last_name,
71
- :customer_id, :email,
81
+ :billing_first_name,
82
+ :billing_last_name,
83
+ :customer_id, :email,
72
84
  :screen_name
73
85
  ]
74
-
86
+
75
87
  xml = @subscriber.to_xml
76
88
  fields.each do |field|
77
89
  xml.should =~ /<#{field}>/
78
- end
90
+ end
79
91
  end
80
92
  end
81
-
93
+
82
94
  describe "#new_record?" do
83
95
  before(:each) do
84
96
  @subscriber = RSpreedly::Subscriber.new
85
97
  end
86
-
98
+
87
99
  it "should return true if the subscriber doesn't have a token" do
88
100
  @subscriber.new_record?.should be_true
89
101
  end
90
-
102
+
91
103
  it "should return false if the subscriber has a token" do
92
104
  @subscriber.token = "something"
93
105
  @subscriber.new_record?.should be_false
@@ -97,86 +109,100 @@ describe RSpreedly::Subscriber do
97
109
  describe "#save" do
98
110
  before(:each) do
99
111
  @subscriber = RSpreedly::Subscriber.new
100
- end
101
-
112
+ end
113
+
102
114
  it "should call #create for a non existing subscriber" do
103
- @subscriber.should_receive(:create)
115
+ @subscriber.should_receive(:create)
104
116
  @subscriber.save
105
117
  end
106
-
118
+
107
119
  it "should call update for an existing subscriber" do
108
120
  @subscriber.token = "something"
109
121
  @subscriber.should_receive(:update)
110
- @subscriber.save
122
+ @subscriber.save
111
123
  end
112
124
  end
113
-
125
+
114
126
  describe "#save!" do
115
127
  before(:each) do
116
128
  @subscriber = RSpreedly::Subscriber.new
117
- end
118
-
129
+ end
130
+
119
131
  it "should call #create! for a non existing subscriber" do
120
- @subscriber.should_receive(:create!)
132
+ @subscriber.should_receive(:create!)
121
133
  @subscriber.save!
122
134
  end
123
-
135
+
124
136
  it "should call update! for an existing subscriber" do
125
137
  @subscriber.token = "something"
126
138
  @subscriber.should_receive(:update!)
127
- @subscriber.save!
139
+ @subscriber.save!
128
140
  end
129
- end
141
+ end
130
142
 
131
143
  describe "#create!" do
132
144
  it "should return true if successful" do
133
- stub_http_with_fixture("create_subscriber.xml", 201)
145
+ stub_request(:post, spreedly_url("/subscribers.xml")).
146
+ to_return(:body => fixture("create_subscriber.xml"), :status => 201)
147
+
134
148
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob")
135
149
  @subscriber.create!.should be_true
136
150
  end
137
-
151
+
138
152
  it "should raise a BadRequest error if the data is invalid" do
139
- stub_http_with_fixture("invalid_subscriber.xml", 422)
153
+ stub_request(:post, spreedly_url("/subscribers.xml")).
154
+ to_return(:body => fixture("invalid_subscriber.xml"), :status => 422)
155
+
140
156
  @subscriber = RSpreedly::Subscriber.new
141
157
  lambda{
142
158
  @subscriber.create!
143
159
  }.should raise_error(RSpreedly::Error::BadRequest)
144
160
  end
145
-
161
+
146
162
  it "should update the subscriber if successful" do
147
- stub_http_with_fixture("create_subscriber.xml", 200)
148
- @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob")
163
+ stub_request(:post, spreedly_url("/subscribers.xml")).
164
+ to_return(:body => fixture("create_subscriber.xml"), :status => 200)
165
+
166
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob")
149
167
  lambda{
150
168
  @subscriber.create!
151
169
  }.should change(@subscriber, :token).to("6af9994a57e420345897b1abb4c27a9db27fa4d0")
152
- end
153
-
170
+ end
171
+
154
172
  it "should raise a Forbidden error if there is already a subscriber with that ID" do
155
- stub_http_with_fixture("existing_subscriber.xml", 403)
173
+ stub_request(:post, spreedly_url("/subscribers.xml")).
174
+ to_return(:body => fixture("existing_subscriber.xml"), :status => 403)
175
+
156
176
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :screen_name => "bob")
157
177
  lambda{
158
178
  @subscriber.create!
159
- }.should raise_error(RSpreedly::Error::Forbidden)
179
+ }.should raise_error(RSpreedly::Error::Forbidden)
160
180
  end
161
181
  end
162
182
 
163
183
  describe "#create" do
164
184
  it "should return true if successful" do
165
- stub_http_with_fixture("create_subscriber.xml", 201)
185
+ stub_request(:post, spreedly_url("/subscribers.xml")).
186
+ to_return(:body => fixture("create_subscriber.xml"), :status => 201)
187
+
166
188
  @subscriber = RSpreedly::Subscriber.new
167
189
  @subscriber.customer_id = 42
168
190
  @subscriber.screen_name = "bob"
169
191
  @subscriber.create.should be_true
170
192
  end
171
-
193
+
172
194
  it "should return nil if the data is invalid" do
173
- stub_http_with_fixture("invalid_subscriber.xml", 422)
195
+ stub_request(:post, spreedly_url("/subscribers.xml")).
196
+ to_return(:body => fixture("invalid_subscriber.xml"), :status => 422)
197
+
174
198
  @subscriber = RSpreedly::Subscriber.new
175
199
  @subscriber.create.should be_nil
176
200
  end
177
-
201
+
178
202
  it "should return nil if there is already a subscriber with that ID" do
179
- stub_http_with_fixture("existing_subscriber.xml", 403)
203
+ stub_request(:post, spreedly_url("/subscribers.xml")).
204
+ to_return(:body => fixture("existing_subscriber.xml"), :status => 403)
205
+
180
206
  @subscriber = RSpreedly::Subscriber.new
181
207
  @subscriber.customer_id = 42
182
208
  @subscriber.screen_name = "bob"
@@ -186,234 +212,339 @@ describe RSpreedly::Subscriber do
186
212
 
187
213
  describe "#update!" do
188
214
  before(:each) do
189
- @subscriber = RSpreedly::Subscriber.new
215
+ @subscriber = RSpreedly::Subscriber.new
190
216
  end
191
-
217
+
192
218
  describe "with basic information" do
193
219
  it "should return true if successful" do
194
- stub_http_with_code(200)
220
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
221
+ to_return(:status => 200)
222
+
195
223
  @subscriber.customer_id = 42
196
224
  @subscriber.email = "new@email.com"
197
225
  @subscriber.update!.should be_true
198
226
  end
199
-
227
+
200
228
  it "should raise NotFound if the subscriber doesn't exist" do
201
- stub_http_with_fixture("subscriber_not_found.xml", 404)
229
+ stub_request(:put, spreedly_url("/subscribers/400.xml")).
230
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
231
+
202
232
  @subscriber.customer_id = 400
203
233
  lambda{
204
234
  @subscriber.update!
205
- }.should raise_error(RSpreedly::Error::NotFound)
206
- end
207
-
235
+ }.should raise_error(RSpreedly::Error::NotFound)
236
+ end
237
+
208
238
  it "should raise BadRequest if the data is invalid" do
209
- stub_http_with_fixture("invalid_update.xml", 422)
210
- @subscriber.customer_id = 42
239
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
240
+ to_return(:body => fixture("invalid_update.xml"), :status => 422)
241
+
242
+ @subscriber.customer_id = 42
211
243
  lambda{
212
244
  @subscriber.update!
213
- }.should raise_error(RSpreedly::Error::BadRequest)
245
+ }.should raise_error(RSpreedly::Error::BadRequest)
214
246
  end
215
247
  end
216
-
248
+
217
249
  describe "with new_customer_id" do
218
250
  it "should return true if successful" do
219
- stub_http_with_code(200)
251
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
252
+ to_return(:status => 200)
253
+
220
254
  @subscriber.customer_id = 42
221
255
  @subscriber.new_customer_id = 43
222
256
  @subscriber.email = "new@email.com"
223
257
  @subscriber.update!.should be_true
224
258
  end
225
-
259
+
226
260
  it "should raise NotFound if the subscriber doesn't exist" do
227
- stub_http_with_fixture("subscriber_not_found.xml", 404)
261
+ stub_request(:put, spreedly_url("/subscribers/400.xml")).
262
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
263
+
228
264
  @subscriber.customer_id = 400
229
265
  @subscriber.new_customer_id = 401
230
266
  lambda{
231
267
  @subscriber.update!
232
- }.should raise_error(RSpreedly::Error::NotFound)
233
- end
234
-
268
+ }.should raise_error(RSpreedly::Error::NotFound)
269
+ end
270
+
235
271
  it "should raise BadRequest if the data is invalid" do
236
- stub_http_with_fixture("invalid_update.xml", 422)
237
- @subscriber.new_customer_id = 43
272
+ stub_request(:put, spreedly_url("/subscribers/.xml")).
273
+ to_return(:body => fixture("invalid_update.xml"), :status => 422)
274
+
275
+ @subscriber.new_customer_id = 43
238
276
  lambda{
239
277
  @subscriber.update!
240
- }.should raise_error(RSpreedly::Error::BadRequest)
241
- end
278
+ }.should raise_error(RSpreedly::Error::BadRequest)
279
+ end
242
280
  end
243
-
281
+
244
282
  describe "with payment information" do
245
283
  before(:each) do
246
- @subscriber.customer_id = 42
284
+ @subscriber.customer_id = 42
247
285
  @subscriber.payment_method = RSpreedly::PaymentMethod::CreditCard.new(:number => "1233445", :verification_value => "234")
248
286
  end
249
-
287
+
250
288
  it "should return true if successful" do
251
- stub_http_with_code(200)
289
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
290
+ to_return(:status => 200)
291
+
252
292
  @subscriber.update!.should be_true
253
293
  end
254
-
294
+
255
295
  it "should raise NotFound if the subscriber doesn't exist" do
256
- stub_http_with_fixture("subscriber_not_found.xml", 404)
296
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
297
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
298
+
257
299
  lambda{
258
300
  @subscriber.update!
259
- }.should raise_error(RSpreedly::Error::NotFound)
260
- end
261
-
301
+ }.should raise_error(RSpreedly::Error::NotFound)
302
+ end
303
+
262
304
  it "should raise BadRequest if the data is invalid" do
263
- stub_http_with_fixture("invalid_update.xml", 422)
305
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
306
+ to_return(:body => fixture("invalid_update.xml"), :status => 422)
307
+
264
308
  lambda{
265
309
  @subscriber.update!
266
- }.should raise_error(RSpreedly::Error::BadRequest)
267
- end
310
+ }.should raise_error(RSpreedly::Error::BadRequest)
311
+ end
268
312
 
269
313
  it "should raise Forbidden if the data is invalid" do
270
- stub_http_with_fixture("invalid_update.xml", 403)
314
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
315
+ to_return(:body => fixture("invalid_update.xml"), :status => 403)
316
+
271
317
  lambda{
272
318
  @subscriber.update!
273
- }.should raise_error(RSpreedly::Error::Forbidden)
274
- end
319
+ }.should raise_error(RSpreedly::Error::Forbidden)
320
+ end
275
321
  end
276
322
  end
277
-
323
+
278
324
  describe "#update" do
279
325
  before(:each) do
280
- @subscriber = RSpreedly::Subscriber.new
281
- end
282
-
326
+ @subscriber = RSpreedly::Subscriber.new
327
+ end
328
+
283
329
  describe "with basic information" do
284
330
  it "should return true if successful" do
285
- stub_http_with_code(200)
331
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
332
+ to_return(:status => 200)
333
+
286
334
  @subscriber.customer_id = 42
287
335
  @subscriber.email = "new@email.com"
288
336
  @subscriber.update!.should be_true
289
337
  end
290
-
338
+
291
339
  it "should return nil if the subscriber doesn't exist" do
292
- stub_http_with_fixture("subscriber_not_found.xml", 404)
340
+ stub_request(:put, spreedly_url("/subscribers/400.xml")).
341
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
342
+
293
343
  @subscriber.customer_id = 400
294
344
  @subscriber.update.should be_nil
295
- end
296
-
345
+ end
346
+
297
347
  it "should return nil if the data is invalid" do
298
- stub_http_with_fixture("invalid_update.xml", 422)
299
- @subscriber.customer_id = 42
348
+ stub_request(:put, spreedly_url("/subscribers/42.xml")).
349
+ to_return(:body => fixture("invalid_update.xml"), :status => 422)
350
+
351
+ @subscriber.customer_id = 42
300
352
  @subscriber.update.should be_nil
301
353
  end
302
- end
354
+ end
303
355
  end
304
-
356
+
305
357
  describe "#destroy" do
306
358
  before(:each) do
307
359
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
308
- end
309
-
360
+ end
361
+
310
362
  it "should return nil if the subscriber doesn't exist" do
311
- stub_http_with_fixture("subscriber_not_found.xml", 404)
363
+ stub_request(:delete, spreedly_url("/subscribers/42.xml")).
364
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
365
+
312
366
  @subscriber.destroy.should be_nil
313
- end
314
-
367
+ end
368
+
315
369
  it "should return true if successful" do
316
- stub_http_with_code(200)
370
+ stub_request(:delete, spreedly_url("/subscribers/42.xml")).
371
+ to_return(:status => 200)
372
+
317
373
  @subscriber.destroy.should be_true
318
- end
374
+ end
319
375
  end
320
376
 
321
377
  describe "#comp_subscription" do
322
378
  before(:each) do
323
379
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :feature_level => "Lowly")
324
- @subscription = RSpreedly::ComplimentarySubscription.new(:duration_quantity => 42, :duration_units => "months", :feature_level => "Pro")
380
+ @subscription = RSpreedly::ComplimentarySubscription.new(:duration_quantity => 42, :duration_units => "months", :feature_level => "Pro")
325
381
  end
326
-
382
+
327
383
  it "should return true if successful" do
328
- stub_http_with_fixture("complimentary_success.xml", 201)
384
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_subscriptions.xml")).
385
+ to_return(:body => fixture("complimentary_success.xml"), :status => 201)
386
+
329
387
  @subscriber.comp_subscription(@subscription).should be_true
330
- end
331
-
388
+ end
389
+
332
390
  it "should update the subscriber if successful" do
333
- stub_http_with_fixture("complimentary_success.xml", 201)
391
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_subscriptions.xml")).
392
+ to_return(:body => fixture("complimentary_success.xml"), :status => 201)
393
+
334
394
  lambda{
335
395
  @subscriber.comp_subscription(@subscription)
336
396
  }.should change(@subscriber, :feature_level).to("Pro")
337
397
  end
338
-
398
+
339
399
  it "should raise NotFound if the subscriber doesn't exist" do
340
- stub_http_with_fixture("subscriber_not_found.xml", 404)
400
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_subscriptions.xml")).
401
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
402
+
341
403
  lambda{
342
404
  @subscriber.comp_subscription(@subscription)
343
- }.should raise_error(RSpreedly::Error::NotFound)
405
+ }.should raise_error(RSpreedly::Error::NotFound)
344
406
  end
345
-
407
+
346
408
  it "should raise BadRequest if validation fails on the subscription" do
347
- stub_http_with_fixture("complimentary_not_valid.xml", 422)
409
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_subscriptions.xml")).
410
+ to_return(:body => fixture("complimentary_not_valid.xml"), :status => 422)
411
+
348
412
  lambda{
349
413
  @subscriber.comp_subscription(@subscription)
350
414
  }.should raise_error(RSpreedly::Error::BadRequest)
351
415
  end
352
-
416
+
353
417
  it "should raise Forbidden if the subscriber is active" do
354
- stub_http_with_fixture("complimentary_failed_active.xml", 403)
418
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_subscriptions.xml")).
419
+ to_return(:body => fixture("complimentary_failed_active.xml"), :status => 403)
420
+
355
421
  lambda{
356
422
  @subscriber.comp_subscription(@subscription)
357
- }.should raise_error(RSpreedly::Error::Forbidden)
423
+ }.should raise_error(RSpreedly::Error::Forbidden)
358
424
  end
359
425
  end
360
426
 
361
427
  describe "#comp_time_extension" do
362
428
  before(:each) do
363
429
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :feature_level => "Lowly")
364
- @subscription = RSpreedly::ComplimentaryTimeExtension.new(:duration_quantity => 42, :duration_units => "months")
430
+ @subscription = RSpreedly::ComplimentaryTimeExtension.new(:duration_quantity => 42, :duration_units => "months")
365
431
  end
366
-
432
+
367
433
  it "should return true if successful" do
368
- stub_http_with_fixture("complimentary_success.xml", 201)
434
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_time_extensions.xml")).
435
+ to_return(:body => fixture("complimentary_success.xml"), :status => 201)
436
+
369
437
  @subscriber.comp_time_extension(@subscription).should be_true
370
- end
371
-
438
+ end
439
+
372
440
  it "should update the subscriber if successful" do
373
- stub_http_with_fixture("complimentary_success.xml", 201)
441
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_time_extensions.xml")).
442
+ to_return(:body => fixture("complimentary_success.xml"), :status => 201)
443
+
374
444
  lambda{
375
445
  @subscriber.comp_time_extension(@subscription)
376
446
  }.should change(@subscriber, :active_until).to(Time.parse("Sun Feb 21 19:04:28 UTC 2010"))
377
447
  end
378
-
448
+
379
449
  it "should raise NotFound if the subscriber doesn't exist" do
380
- stub_http_with_fixture("subscriber_not_found.xml", 404)
450
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_time_extensions.xml")).
451
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
452
+
381
453
  lambda{
382
454
  @subscriber.comp_time_extension(@subscription)
383
- }.should raise_error(RSpreedly::Error::NotFound)
455
+ }.should raise_error(RSpreedly::Error::NotFound)
384
456
  end
385
-
457
+
386
458
  it "should raise BadRequest if validation fails on the subscription" do
387
- stub_http_with_fixture("complimentary_not_valid.xml", 422)
459
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_time_extensions.xml")).
460
+ to_return(:body => fixture("complimentary_not_valid.xml"), :status => 422)
461
+
388
462
  lambda{
389
463
  @subscriber.comp_time_extension(@subscription)
390
464
  }.should raise_error(RSpreedly::Error::BadRequest)
391
465
  end
392
-
466
+
393
467
  it "should raise Forbidden if the subscriber is inactive" do
394
- stub_http_with_fixture("complimentary_failed_inactive.xml", 403)
468
+ stub_request(:post, spreedly_url("/subscribers/42/complimentary_time_extensions.xml")).
469
+ to_return(:body => fixture("complimentary_failed_inactive.xml"), :status => 403)
470
+
395
471
  lambda{
396
472
  @subscriber.comp_time_extension(@subscription)
397
- }.should raise_error(RSpreedly::Error::Forbidden)
473
+ }.should raise_error(RSpreedly::Error::Forbidden)
474
+ end
475
+ end
476
+
477
+ describe "#credit" do
478
+ before(:each) do
479
+ @subscriber = RSpreedly::Subscriber.new(:customer_id => 42, :feature_level => "Lowly")
480
+ @credit_amount = 5
481
+ end
482
+
483
+ it "should return true if successful" do
484
+ stub_request(:post, spreedly_url("/subscribers/42/credit.xml")).
485
+ to_return(:body => fixture("credit_success.xml"), :status => 201)
486
+
487
+ @subscriber.credit(@credit_amount).should be_true
488
+ @subscriber.store_credit.should == @credit_amount
489
+ end
490
+
491
+ it "should raise NotFound if the subscriber doesn't exist" do
492
+ stub_request(:post, spreedly_url("/subscribers/42/credit.xml")).
493
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
494
+
495
+ lambda{
496
+ @subscriber.credit(@credit_amount)
497
+ }.should raise_error(RSpreedly::Error::NotFound)
498
+ end
499
+
500
+ it "should raise BadRequest if validation fails on the subscription" do
501
+ stub_request(:post, spreedly_url("/subscribers/42/credit.xml")).
502
+ to_return(:body => fixture("credit_not_valid.xml"), :status => 422)
503
+
504
+ lambda{
505
+ @subscriber.credit(@credit_amount)
506
+ }.should raise_error(RSpreedly::Error::BadRequest)
507
+ end
508
+
509
+ it "should increment store_credit" do
510
+ stub_request(:post, spreedly_url("/subscribers/42/credit.xml")).
511
+ to_return(:body => fixture("credit_success.xml"), :status => 201)
512
+
513
+ @subscriber.store_credit = 5
514
+ @subscriber.credit(5).should be_true
515
+ @subscriber.store_credit.should == 10
516
+ end
517
+
518
+ it "should decrement store_credit" do
519
+ stub_request(:post, spreedly_url("/subscribers/42/credit.xml")).
520
+ to_return(:body => fixture("credit_success.xml"), :status => 201)
521
+
522
+ @subscriber.store_credit = 5
523
+ @subscriber.credit(-5).should be_true
524
+ @subscriber.store_credit.should == 0
398
525
  end
399
526
  end
400
-
527
+
401
528
  describe "#stop_auto_renew" do
402
529
  before(:each) do
403
530
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
404
- end
405
-
531
+ end
532
+
406
533
  it "should return true if successful" do
407
- stub_http_with_code(200)
534
+ stub_request(:post, spreedly_url("/subscribers/42/stop_auto_renew.xml")).
535
+ to_return(:status => 200)
536
+
408
537
  @subscriber.stop_auto_renew.should be_true
409
- end
410
-
538
+ end
539
+
411
540
  it "should raise NotFound if the subscriber doesn't exist" do
412
- stub_http_with_fixture("subscriber_not_found.xml", 404)
541
+ stub_request(:post, spreedly_url("/subscribers/42/stop_auto_renew.xml")).
542
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
543
+
413
544
  lambda{
414
545
  @subscriber.stop_auto_renew
415
- }.should raise_error(RSpreedly::Error::NotFound)
416
- end
546
+ }.should raise_error(RSpreedly::Error::NotFound)
547
+ end
417
548
  end
418
549
 
419
550
  describe "#subscribe_to_free_trial" do
@@ -421,118 +552,148 @@ describe RSpreedly::Subscriber do
421
552
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
422
553
  @plan = RSpreedly::SubscriptionPlan.new(:id => 2533)
423
554
  end
424
-
555
+
425
556
  it "should return true if successful" do
426
- stub_http_with_fixture("free_plan_success.xml", 200)
557
+ stub_request(:post, spreedly_url("/subscribers/42/subscribe_to_free_trial.xml")).
558
+ to_return(:body => fixture("free_plan_success.xml"), :status => 200)
559
+
427
560
  @subscriber.subscribe_to_free_trial(@plan).should be_true
428
- end
429
-
561
+ end
562
+
430
563
  it "should update the subscriber if successful" do
431
- stub_http_with_fixture("free_plan_success.xml", 200)
564
+ stub_request(:post, spreedly_url("/subscribers/42/subscribe_to_free_trial.xml")).
565
+ to_return(:body => fixture("free_plan_success.xml"), :status => 200)
566
+
432
567
  lambda{
433
568
  @subscriber.subscribe_to_free_trial(@plan)
434
569
  }.should change(@subscriber, :grace_until).to(Time.parse("2013-05-02T00:07:37Z"))
435
570
  end
436
-
571
+
437
572
  it "should raise NotFound if the subscriber doesn't exist" do
438
- stub_http_with_fixture("subscriber_not_found.xml", 404)
573
+ stub_request(:post, spreedly_url("/subscribers/42/subscribe_to_free_trial.xml")).
574
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
575
+
439
576
  lambda{
440
577
  @subscriber.subscribe_to_free_trial(@plan)
441
- }.should raise_error(RSpreedly::Error::NotFound)
578
+ }.should raise_error(RSpreedly::Error::NotFound)
442
579
  end
443
-
580
+
444
581
  it "should raise NotFound if the plan doesn't exist" do
445
- stub_http_with_fixture("plan_not_found.xml", 404)
582
+ stub_request(:post, spreedly_url("/subscribers/42/subscribe_to_free_trial.xml")).
583
+ to_return(:body => fixture("plan_not_found.xml"), :status => 404)
584
+
446
585
  lambda{
447
586
  @subscriber.subscribe_to_free_trial(@plan)
448
- }.should raise_error(RSpreedly::Error::NotFound)
449
- end
450
-
587
+ }.should raise_error(RSpreedly::Error::NotFound)
588
+ end
589
+
451
590
  it "should raise BadRequest if no plan is specified" do
452
- stub_http_with_fixture("free_plan_not_set.xml", 422)
591
+ stub_request(:post, spreedly_url("/subscribers/42/subscribe_to_free_trial.xml")).
592
+ to_return(:body => fixture("free_plan_not_set.xml"), :status => 422)
593
+
453
594
  @plan.id = nil
454
595
  lambda{
455
596
  @subscriber.subscribe_to_free_trial(@plan)
456
597
  }.should raise_error(RSpreedly::Error::BadRequest)
457
598
  end
458
-
599
+
459
600
  it "should raise Forbidden if the subscriber isn't elligable" do
460
- stub_http_with_fixture("free_plan_not_elligable.xml", 403)
601
+ stub_request(:post, spreedly_url("/subscribers/42/subscribe_to_free_trial.xml")).
602
+ to_return(:body => fixture("free_plan_not_elligable.xml"), :status => 403)
603
+
461
604
  lambda{
462
605
  @subscriber.subscribe_to_free_trial(@plan)
463
- }.should raise_error(RSpreedly::Error::Forbidden)
464
- end
606
+ }.should raise_error(RSpreedly::Error::Forbidden)
607
+ end
465
608
 
466
609
  it "should raise Forbidden if the plan isn't a free trial" do
467
- stub_http_with_fixture("free_plan_not_free.xml", 403)
610
+ stub_request(:post, spreedly_url("/subscribers/42/subscribe_to_free_trial.xml")).
611
+ to_return(:body => fixture("free_plan_not_free.xml"), :status => 403)
612
+
468
613
  lambda{
469
614
  @subscriber.subscribe_to_free_trial(@plan)
470
- }.should raise_error(RSpreedly::Error::Forbidden)
471
- end
615
+ }.should raise_error(RSpreedly::Error::Forbidden)
616
+ end
472
617
  end
473
618
 
474
619
  describe "#allow_free_trial" do
475
620
  before(:each) do
476
621
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
477
622
  end
478
-
623
+
479
624
  it "should return true if successful" do
480
- stub_http_with_fixture("free_plan_success.xml", 200)
625
+ stub_request(:post, spreedly_url("/subscribers/42/allow_free_trial.xml")).
626
+ to_return(:body => fixture("free_plan_success.xml"), :status => 200)
627
+
481
628
  @subscriber.allow_free_trial.should be_true
482
- end
483
-
629
+ end
630
+
484
631
  it "should update the subscriber if successful" do
485
- stub_http_with_fixture("free_plan_success.xml", 200)
632
+ stub_request(:post, spreedly_url("/subscribers/42/allow_free_trial.xml")).
633
+ to_return(:body => fixture("free_plan_success.xml"), :status => 200)
634
+
486
635
  lambda{
487
636
  @subscriber.allow_free_trial
488
637
  }.should change(@subscriber, :grace_until).to(Time.parse("2013-05-02T00:07:37Z"))
489
638
  end
490
-
639
+
491
640
  it "should raise NotFound if the subscriber doesn't exist" do
492
- stub_http_with_fixture("subscriber_not_found.xml", 404)
641
+ stub_request(:post, spreedly_url("/subscribers/42/allow_free_trial.xml")).
642
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
643
+
493
644
  lambda{
494
645
  @subscriber.allow_free_trial
495
- }.should raise_error(RSpreedly::Error::NotFound)
646
+ }.should raise_error(RSpreedly::Error::NotFound)
496
647
  end
497
648
  end
498
-
649
+
499
650
  describe "#grant_lifetime_subscription" do
500
-
651
+
501
652
  before(:each) do
502
653
  @subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
503
- end
504
-
654
+ end
655
+
505
656
  it "should return true if successful" do
506
- stub_http_with_fixture("lifetime_subscription_success.xml", 200)
657
+ stub_request(:post, spreedly_url("/subscribers/42/lifetime_complimentary_subscriptions.xml")).
658
+ to_return(:body => fixture("lifetime_subscription_success.xml"), :status => 200)
659
+
507
660
  @subscriber.grant_lifetime_subscription("Something").should be_true
508
- end
509
-
661
+ end
662
+
510
663
  it "should update the subscriber's lifetime_subscription if successful" do
511
- stub_http_with_fixture("lifetime_subscription_success.xml", 200)
664
+ stub_request(:post, spreedly_url("/subscribers/42/lifetime_complimentary_subscriptions.xml")).
665
+ to_return(:body => fixture("lifetime_subscription_success.xml"), :status => 200)
666
+
512
667
  lambda{
513
668
  @subscriber.grant_lifetime_subscription("Something")
514
669
  }.should change(@subscriber, :lifetime_subscription).to(true)
515
670
  end
516
-
671
+
517
672
  it "should update the subscriber's feature_level if successful" do
518
- stub_http_with_fixture("lifetime_subscription_success.xml", 200)
673
+ stub_request(:post, spreedly_url("/subscribers/42/lifetime_complimentary_subscriptions.xml")).
674
+ to_return(:body => fixture("lifetime_subscription_success.xml"), :status => 200)
675
+
519
676
  lambda{
520
677
  @subscriber.grant_lifetime_subscription("Something")
521
678
  }.should change(@subscriber, :feature_level).to("Something")
522
- end
523
-
679
+ end
680
+
524
681
  it "should raise NotFound if the subscriber doesn't exist" do
525
- stub_http_with_fixture("subscriber_not_found.xml", 404)
682
+ stub_request(:post, spreedly_url("/subscribers/42/lifetime_complimentary_subscriptions.xml")).
683
+ to_return(:body => fixture("subscriber_not_found.xml"), :status => 404)
684
+
526
685
  lambda{
527
686
  @subscriber.grant_lifetime_subscription("Something")
528
- }.should raise_error(RSpreedly::Error::NotFound)
687
+ }.should raise_error(RSpreedly::Error::NotFound)
529
688
  end
530
-
689
+
531
690
  it "should raise BadRequest if the feature level is blank" do
532
- stub_http_with_fixture("feature_level_blank.xml", 422)
691
+ stub_request(:post, spreedly_url("/subscribers/42/lifetime_complimentary_subscriptions.xml")).
692
+ to_return(:body => fixture("feature_level_blank.xml"), :status => 422)
693
+
533
694
  lambda{
534
695
  @subscriber.grant_lifetime_subscription("Something")
535
- }.should raise_error(RSpreedly::Error::BadRequest)
696
+ }.should raise_error(RSpreedly::Error::BadRequest)
536
697
  end
537
698
  end
538
699
  end