Permalink
Browse files

renamed subscription#exceeds_plan to #plan_check (and keep #exceeds_p…

…lan?)

fixed a bug and added specs for #trial_ends_on
  • Loading branch information...
1 parent 8e5b212 commit f8e0c1a0273e33d6d80b063626c876ccbccc49eb @linojon committed Oct 7, 2009
@@ -54,7 +54,7 @@
Then /^the subscription should have next renewal (.*)$/ do |value|
subscriber = find_subscriber
- subscriber.subscription.next_renewal_on.should == renewal_text_to_date(value)
+ subscriber.subscription.next_renewal_on.should == text_to_date(value)
end
Then /^the profile should be "(no info|authorized|error)"$/ do |state|
@@ -69,6 +69,6 @@
Then /^the next renewal should be set to "(.*)"$/ do |text|
subscriber = find_subscriber
- subscriber.subscription.next_renewal_on.should == renewal_text_to_date(text)
+ subscriber.subscription.next_renewal_on.should == text_to_date(text)
end
View
@@ -146,11 +146,11 @@ def allowed_plans
# test if subscriber can use a plan, returns true or false
def exceeds_plan?( plan = self.plan)
- !exceeds_plan(plan).blank?
+ !(plan_check(plan).blank?)
end
# check if subscriber can use a plan and returns list of attributes exceeded, or blank for ok
- def exceeds_plan( plan = self.plan)
+ def plan_check( plan = self.plan)
subscriber.subscription_plan_check(plan)
end
@@ -220,10 +220,12 @@ def trial_ends_on
case
# in trial, days remaining
when trial? : next_renewal_on
- # start the trial?
+ # new record? would start from today
when plan.nil? : Time.zone.today + SubscriptionConfig.trial_period.days
- # never had a trial? prorate since creation
- when plan.free? : d = created_at.to_date + SubscriptionConfig.trial_period.days
+ # start or continue a trial? prorate since creation
+ #when active? :
+ else
+ d = created_at.to_date + SubscriptionConfig.trial_period.days
d unless d <= Time.zone.today
# else nil not eligable
end
@@ -47,7 +47,7 @@
@subscriber.subscription_plan.should == plan
end
- if described_class.paranoid?
+ if described_class.respond_to?(:paranoid?) && described_class.paranoid?
describe "when paranoid" do
it "does not actually destroy subscription" do
@subscriber.destroy
@@ -147,7 +147,7 @@
# -------------------------
describe "days_remaining" do
before :each do
- @subscription = create_subscription( :plan => @plan )
+ @subscription = create_subscription
end
it "and_return nil when next renewal is nil" do
@@ -171,6 +171,33 @@
end
end
+ # -------------------------
+ describe "trial_ends_on" do
+ before :each do
+ @subscription = create_subscription
+ end
+
+ it "is nil if no trial period in config" do
+ SubscriptionConfig.stub!(:trial_period).and_return(0)
+ @subscription.trial_ends_on.should be_nil
+ end
+ it "is current next renewal if presently in trial" do
+ @subscription.state = "trial"
+ @subscription.next_renewal_on = Time.zone.today + 3.days
+ @subscription.trial_ends_on.should == Time.zone.today + 3.days
+ end
+ it "is from today if no plan defined yet (eg inquiring what it would be for a gui)" do
+ @subscription.plan = nil
+ @subscription.trial_ends_on.should == Time.zone.today + 30.days
+ end
+ it "is calculated from subscription creation date" do
+ @plan = SubscriptionPlan.create( :name => 'basic', :rate_cents => 1000)
+ @subscription.plan = @plan
+ @subscription.created_at = Time.zone.now - 10.days
+ @subscription.trial_ends_on.should == Time.zone.today + 20.days
+ end
+ end
+
# -------------------------
describe "due?" do
before :each do
@@ -211,7 +238,7 @@
@subscription = @subscriber.subscription
@subscription.subscriber.should_receive(:subscription_plan_check).with(@plan).and_return("exceeded limits")
- @subscription.exceeds_plan.should == "exceeded limits"
+ @subscription.plan_check.should == "exceeded limits"
end
it "checks if exceeds another plan" do
@@ -221,7 +248,7 @@
@subscription = @subscriber.subscription
@subscription.subscriber.should_receive(:subscription_plan_check).with(@plan2).and_return("exceeded limits")
- @subscription.exceeds_plan(@plan2).should == "exceeded limits"
+ @subscription.plan_check(@plan2).should == "exceeded limits"
end
it "finds allowed plans" do
@@ -244,12 +271,12 @@
@plan = SubscriptionPlan.create( :name => 'basic', :rate_cents => 1000, :interval => 1)
@new_plan = SubscriptionPlan.create( :name => 'pro', :rate_cents => 2000, :interval => 1)
@today = Time.zone.today
- SubscriptionTransaction.stub!(:charge).and_return(SubscriptionTransaction.new(:success => true))
end
describe "when active" do
before :each do
@subscription = create_subscription( :plan => @plan )
+ @subscription.created_at = @today - 1.year #dont calc we're in trial
@subscription.next_renewal_on = @today + 6.days # remaining value = 1000/(30/6) = 200
@subscription.state = 'active'
end
@@ -295,6 +322,7 @@
describe "when past due" do
before :each do
@subscription = create_subscription( :plan => @plan )
+ @subscription.created_at = @today - 1.year
@subscription.next_renewal_on = @today - 6.days
@subscription.state = 'past_due'
@subscription.balance = @plan.rate # 1000, remaining value = 800

0 comments on commit f8e0c1a

Please sign in to comment.