Tests for changing subscriptions and resulting permissions

This commit is contained in:
Andrew Brown 2017-01-25 19:14:29 +00:00
parent 863116d8e4
commit defba2249b
2 changed files with 104 additions and 38 deletions

View File

@ -40,6 +40,6 @@ Rails.application.configure do
default_url_options[:host] = 'test.host'
# Set test-mode Stripe API key
Stripe.api_key = "We don't want to actually hit Stripe from tests"
Stripe.api_key = "We-don't-want-to-actually-hit-Stripe-from-tests"
config.stripe_publishable_key = 'pk_test_eXI4iyJ2gR9UOGJyJERvDlHF'
end

View File

@ -8,26 +8,26 @@ RSpec.describe SubscriptionsController, type: :controller do
WebMock.disable_net_connect!(allow_localhost: true)
# Need to stub .save on StripeObject, but this doesn't seem to work
#allow_any_instance_of(Stripe::StripeObject).to receive(:save)
#Stripe::StripeObject.any_instance.stub(:save).and_return(true)
# Stub Stripe::Customer.create
stub_request(:post, "https://api.stripe.com/v1/customers")
.with(body: { email: "email1@example.com" })
.to_return(status: 200, body: {id: 'customer-with-card'}.to_json, headers: {})
.to_return(status: 200, body: {id: 'stripe-id'}.to_json, headers: {})
# Stub Stripe::Customer.retrieve
stub_request(:get, "https://api.stripe.com/v1/customers/customer-with-card")
stub_request(:get, "https://api.stripe.com/v1/customers/stripe-id")
.to_return(
status: 200,
body: {
id: 'stripe-id',
sources: {
total_count: 1,
data: [Stripe::StripeObject.new]
total_count: 0,
data: []
},
subscriptions: {
total_count: 1,
data: []
data: [Stripe::StripeObject.new]
}
}.to_json,
headers: {}
@ -35,17 +35,18 @@ RSpec.describe SubscriptionsController, type: :controller do
# Stub downgrading subscription to starter
stub_request(:post, "https://api.stripe.com/v1/subscriptions")
.with(body: { customer: "customer-with-card", plan: 'starter' })
.with(body: { customer: "stripe-id", plan: 'starter' })
.to_return(status: 200, body: {id: 'stripe-id'}.to_json, headers: {})
# Stub updating subscription to premium
stub_request(:post, "https://api.stripe.com/v1/subscriptions")
.with(body: { customer: "customer-with-card", plan: 'premium' })
.with(body: { customer: "stripe-id", plan: 'premium' })
.to_return(status: 200, body: {id: 'stripe-id'}.to_json, headers: {})
@request.env['devise.mapping'] = Devise.mappings[:user]
#@user = create(:user)
#sign_in @user
@user = create(:user)
@user.update(stripe_customer_id: 'stripe-id')
sign_in @user
@premium_plan = BillingPlan.create(
name: 'Premium',
@ -74,45 +75,110 @@ RSpec.describe SubscriptionsController, type: :controller do
)
end
describe "User on Starter / no plan changes their plan" do
describe "User with no plan (fallback to Starter)" do
it "redirects to payment method form if they don't have a payment method saved" do
#post :change, {stripe_plan_id: 'premium'}
#expect redirect
end
it "allows upgrading to Premium when they have a payment method saved" do
# expect(@user.active_subscriptions).to eq([])
# post :change, {stripe_plan_id: 'premium'}
# expect(@user.active_billing_plans).to eq([@premium_plan])
expect(@user.active_subscriptions).to eq([])
post :change, {stripe_plan_id: 'premium'}
expect(subject).to redirect_to action: :information, plan: 'premium'
end
end
describe "Subscription permissions" do
describe "Premium subscriptions" do
it "allows Premium users to create Characters, Locations, and Items" do
end
it "allows Premium users to create Creatures, Races, and Religions" do
end
it "allows Premium users to create Scenes" do
end
it "allows creating more than 5 universes" do
end
describe "User on Starter" do
before do
# Create a Starter subscription for the user
@user.active_subscriptions.create(billing_plan: @free_plan, start_date: Time.now - 5.days, end_date: Time.now + 5.days)
expect(@user.active_subscriptions.map(&:billing_plan_id)).to eq([@free_plan.id])
end
describe "Starter subscriptions" do
it "allows Starter users to create Characters, Locations, and Items" do
it "redirects to payment method form if they don't have a payment method saved" do
post :change, {stripe_plan_id: 'premium'}
expect(subject).to redirect_to action: :information, plan: 'premium'
end
it "allows upgrading to Premium when they have a payment method saved" do
# Re-stub Stripe::Customer.retrieve to include a payment method (source)
stub_request(:get, "https://api.stripe.com/v1/customers/stripe-id")
.to_return(
status: 200,
body: {
id: 'stripe-id',
sources: {
total_count: 1,
data: [Stripe::StripeObject.new]
},
subscriptions: {
total_count: 1,
data: [Stripe::StripeObject.new]
}
}.to_json,
headers: {}
)
#todo
# post :change, {stripe_plan_id: 'premium'}
# expect(@user.active_billing_plans).to eq([@premium_plan])
end
describe "Starter Permissions" do
it "allows Starter users to create core content types" do
expect(@user.can_create?(Character)).to eq(true)
expect(@user.can_create?(Location)).to eq(true)
expect(@user.can_create?(Item)).to eq(true)
end
it "doesn't allow Starter users to create Creatures, Races, and Religions" do
it "doesn't allow Starter users to create extended content types" do
expect(@user.can_create?(Creature)).to eq(false)
expect(@user.can_create?(Race)).to eq(false)
expect(@user.can_create?(Religion)).to eq(false)
expect(@user.can_create?(Group)).to eq(false)
expect(@user.can_create?(Magic)).to eq(false)
expect(@user.can_create?(Language)).to eq(false)
end
it "doesn't allow Starter users to create Scenes" do
it "doesn't allow Starter users to create collective content types" do
expect(@user.can_create?(Scene)).to eq(false)
end
it "allows creating up to five universes" do
#todo allow editing existing non-core content
end
end
describe "User on Premium" do
before do
# Create a premium subscription for the user
@user.active_subscriptions.create(billing_plan: @premium_plan, start_date: Time.now - 5.days, end_date: Time.now + 5.days)
expect(@user.active_subscriptions.map(&:billing_plan_id)).to eq([@premium_plan.id])
end
it "allows downgrading to Starter" do
# Downgrade to Starter
post :change, { stripe_plan_id: 'starter' }
@user.reload
expect(@user.selected_billing_plan_id).to eq(@free_plan.id)
expect(@user.active_billing_plans).to eq([@free_plan])
expect(@user.active_subscriptions.map(&:billing_plan_id)).to eq([@free_plan.id])
end
describe "Premium Permissions" do
it "allows Premium users to create core content types" do
expect(@user.can_create?(Character)).to eq(true)
expect(@user.can_create?(Location)).to eq(true)
expect(@user.can_create?(Item)).to eq(true)
end
it "allows Premium users to create extended content types" do
expect(@user.can_create?(Creature)).to eq(true)
expect(@user.can_create?(Race)).to eq(true)
expect(@user.can_create?(Religion)).to eq(true)
expect(@user.can_create?(Group)).to eq(true)
expect(@user.can_create?(Magic)).to eq(true)
expect(@user.can_create?(Language)).to eq(true)
end
it "allows Premium users to create collective content types" do
expect(@user.can_create?(Scene)).to eq(true)
end
end
end