require File.dirname(__FILE__) + '/../test_helper' class SubscriptionTest < ActiveSupport::TestCase include MuckCommerceTestDefinitions context "Subscription Instance" do setup do @subscription = Factory(:subscription) end subject { @subscription } should_belong_to :user should_belong_to :subscription_plan should_have_many :subscription_records should_have_many :order_coupons should_have_many :coupons, :through => :subscription_coupons should_have_many :order_transactions, :as => :transactionable, :dependent => :destroy should_validate_presence_of :amount should_validate_presence_of :user context "named scope" do setup do @user = Factory(:user) @default_subscription = Factory(:subscription, :default => :true) @user.subscriptions << @default_subscription @non_default_subscription = Factory(:subscription, :default => :false) @user.subscriptions << @non_default_subscription @subscription = Factory(:subscription, :aasm_state => 'active', :next_renewal_at => 2.weeks.from_now) @due_today_subscription = Factory(:subscription, :aasm_state => 'active', :next_renewal_at => DateTime.now) @past_due_subscription = Factory(:subscription, :aasm_state => 'active', :next_renewal_at => DateTime.now) @expiring_trial_subscription = Factory(:subscription, :aasm_state => 'trial', :next_renewal_at => DateTime.now) @expired_trial_subscription = Factory(:subscription, :aasm_state => 'trial', :next_renewal_at => 1.week.ago) @suspended_subscription = Factory(:subscription, :aasm_state => 'suspended', :next_renewal_at => 1.week.ago) end should_scope_newest context "expiring_trials" do should "find expiring trials" do assert Subscription.expiring_trials.include?(@expiring_trial_subscription) end should "find expired trial" do assert Subscription.expiring_trials.include?(@expired_trial_subscription) end should "not find active subscription" do assert !Subscription.expiring_trials.include?(@subscription) end should "not find past due active subscription" do assert !Subscription.expiring_trials.include?(@past_due_subscription) end should "not find active subscription due today" do assert !Subscription.expiring_trials.include?(@due_today_subscription) end end context "due_today" do should "find expiring trials" do assert Subscription.due_today.include?(@expiring_trial_subscription) end should "not find expired trial" do assert !Subscription.due_today.include?(@expired_trial_subscription) end should "not find active subscription" do assert !Subscription.due_today.include?(@subscription) end should "not find past due active subscription" do assert !Subscription.due_today.include?(@past_due_subscription) end should "find active subscription due today" do assert Subscription.due_today.include?(@due_today_subscription) end should "not find suspeded subscription" do assert !Subscription.past_due.include?(@suspended_subscription) end end context "past_due" do should "find expiring trials" do assert Subscription.past_due.include?(@expiring_trial_subscription) end should "find expired trial" do assert Subscription.past_due.include?(@expired_trial_subscription) end should "not find active subscription" do assert !Subscription.past_due.include?(@subscription) end should "find past due active subscription" do assert Subscription.past_due.include?(@past_due_subscription) end should "find active subscription due today" do assert Subscription.past_due.include?(@due_today_subscription) end should "find suspeded subscription" do assert Subscription.past_due.include?(@suspended_subscription) end end context "default" do should "get default subscription" do assert @user.subscriptions.default.include?(@default_subscription) end should "not get non default subscription" do assert !@user.subscriptions.default.include?(@non_default_subscription) end end context "states" do setup do @subscription = Factory(:subscription) end should "be in the trial state" do assert @subscription.trial? end should "move to the active state" do assert @subscription.activate! assert @subscription.active? end should "move to the suspended state from the active state" do assert @subscription.activate! assert @subscription.active? assert @subscription.suspend! assert @subscription.suspended? end should "move to the suspended state from the trial state" do assert @subscription.trial? assert @subscription.suspend! assert @subscription.suspended? end should "move to the cancelled state from the trial state" do assert @subscription.trial? assert @subscription.cancel! assert @subscription.cancelled? end should "move to the cancelled state from the active state" do assert @subscription.activate! assert @subscription.active? assert @subscription.cancel! assert @subscription.cancelled? end end context "authorize_subscription" do authorize_subscription(user, options = {}) end context "subscribe" do subscribe(user, subscription_plan_id, discount_code = '') end context "subscribe_custom" do subscribe_custom(user, amount, subscription_values = {}, renewal_period = nil, discount_codes = [], run_authorization = false) end end end end