discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb
Justin DiRose a868e6b838
FEATURE: Cancel payments at end of subscription vs immediately
Previously, when a user canceled a subscription, the access would revoke
immediately on Discourse vs. at the end of the billing period. This
commit changes the behavior to remove membership at the end of the
billing period using Stripe's `cancel_at_period_end` attribute on the
Subscription object.

This commit now requires the setup of webhooks for subscription
processing to occur correctly.
2020-08-19 14:37:47 -05:00

88 lines
2.5 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
module DiscourseSubscriptions
RSpec.describe User::SubscriptionsController do
it 'is a subclass of ApplicationController' do
expect(DiscourseSubscriptions::User::SubscriptionsController < ::ApplicationController).to eq(true)
end
context "not authenticated" do
it "does not get the subscriptions" do
::Stripe::Customer.expects(:list).never
get "/s/user/subscriptions.json"
end
it "does not destroy a subscription" do
::Stripe::Subscription.expects(:delete).never
patch "/s/user/subscriptions/sub_12345.json"
end
end
context "authenticated" do
let(:user) { Fabricate(:user, email: 'beanie@example.com') }
let(:customer) { Fabricate(:customer, user_id: user.id, customer_id: "cus_23456", product_id: "prod_123") }
before do
sign_in(user)
Fabricate(:subscription, customer_id: customer.id, external_id: "sub_1234")
end
describe "index" do
let(:plans) do
{
data: [
{
id: "plan_1",
product: { name: 'ACME Subscriptions' },
},
{
id: "plan_2",
product: { name: 'ACME Other Subscriptions' },
}
]
}
end
let(:customers) do
{
data: [{
id: "cus_23456",
subscriptions: {
data: [
{ id: "sub_1234", items: { data: [price: { id: "plan_1" }] } },
{ id: "sub_4567", items: { data: [price: { id: "plan_2" }] } }
]
},
}]
}
end
it "gets subscriptions" do
::Stripe::Price.expects(:list).with(
expand: ['data.product'],
limit: 100
).returns(plans)
::Stripe::Customer.expects(:list).with(
email: user.email,
expand: ['data.subscriptions']
).returns(customers)
get "/s/user/subscriptions.json"
subscription = response.parsed_body.first
expect(subscription).to eq(
"id" => "sub_1234",
"items" => { "data" => [{ "price" => { "id" => "plan_1" } }] },
"plan" => { "id" => "plan_1", "product" => { "name" => "ACME Subscriptions" } },
"product" => { "name" => "ACME Subscriptions" }
)
end
end
end
end
end