require 'spec_helper'


describe "User Subscriptions" do

  let(:user1) {FactoryGirl.create(:user)}
  let(:client) { RecurlyClient.new }
  before(:each) do

  end

  it "empty results" do
    user1.touch

    User.subscription_sync

    user1.reload

    user1.subscription_sync_code.should == 'no_recurly_account'
    user1.subscription_last_checked_at.should_not be_nil
  end

  it "user not in trial" do
    user1.subscription_plan_code = SubscriptionDefinitions::JAM_PLATINUM
    user1.subscription_trial_ends_at = 1.days.ago

    client.sync_subscription(user1)
    user1.reload
    user1.subscription_sync_code.should == "trial_ended"
    user1.subscription_last_checked_at.should_not be_nil
    user1.subscription_plan_code.should be_nil

    user1.subscription_trial_ends_at = 3.days.ago
    user1.subscription_last_checked_at = 2.days.ago
    user1.save!

    User.subscription_sync

    user1.reload

    user1.subscription_sync_code.should == "trial_ended"
    user1.subscription_last_checked_at.should_not be_nil
    user1.subscription_plan_code.should be_nil
  end

  it "revert admin user down" do
    user1.subscription_plan_code = SubscriptionDefinitions::JAM_PLATINUM
    user1.subscription_trial_ends_at = 1.days.ago
    user1.admin_override_plan_code = SubscriptionDefinitions::JAM_PLATINUM
    user1.admin_override_ends_at = 10.days.from_now
    user1.save!

    User.subscription_sync

    user1.reload

    user1.subscription_sync_code.should == "admin_control"
    user1.subscription_last_checked_at.should_not be_nil
    user1.subscription_plan_code.should == SubscriptionDefinitions::JAM_PLATINUM

    user1.subscription_trial_ends_at = 3.days.ago
    user1.admin_override_ends_at = 10.days.ago
    user1.subscription_last_checked_at = 2.days.ago
    user1.save!

    User.subscription_sync
    user1.reload

    user1.subscription_sync_code.should == "trial_ended"
    user1.subscription_last_checked_at.should_not be_nil
    user1.subscription_plan_code.should be_nil


  end

end
