2019-10-31 22:43:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2019-12-03 19:23:45 -05:00
|
|
|
module DiscourseSubscriptions
|
2019-10-31 22:43:09 -04:00
|
|
|
RSpec.describe User::SubscriptionsController do
|
|
|
|
it 'is a subclass of ApplicationController' do
|
2019-12-03 19:23:45 -05:00
|
|
|
expect(DiscourseSubscriptions::User::SubscriptionsController < ::ApplicationController).to eq(true)
|
2019-10-31 22:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "not authenticated" do
|
|
|
|
it "does not get the subscriptions" do
|
|
|
|
::Stripe::Customer.expects(:list).never
|
2019-12-02 19:48:12 -05:00
|
|
|
get "/s/user/subscriptions.json"
|
2019-10-31 22:43:09 -04:00
|
|
|
end
|
2019-11-04 00:37:21 -05:00
|
|
|
|
|
|
|
it "does not destroy a subscription" do
|
|
|
|
::Stripe::Subscription.expects(:delete).never
|
2019-12-02 19:48:12 -05:00
|
|
|
patch "/s/user/subscriptions/sub_12345.json"
|
2019-11-04 00:37:21 -05:00
|
|
|
end
|
2019-10-31 22:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "authenticated" do
|
|
|
|
let(:user) { Fabricate(:user, email: 'beanie@example.com') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "index" do
|
2019-11-26 18:48:30 -05:00
|
|
|
let(:plans) do
|
|
|
|
{
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: "plan_1",
|
|
|
|
product: { name: 'ACME Subscriptions' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "plan_2",
|
|
|
|
product: { name: 'ACME Other Subscriptions' },
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-10-31 22:43:09 -04:00
|
|
|
let(:customers) do
|
|
|
|
{
|
|
|
|
data: [{
|
|
|
|
id: "cus_23456",
|
|
|
|
subscriptions: {
|
2019-11-26 18:48:30 -05:00
|
|
|
data: [
|
|
|
|
{ id: "sub_1234", plan: { id: "plan_1" } },
|
|
|
|
{ id: "sub_4567", plan: { id: "plan_2" } }
|
|
|
|
]
|
2019-10-31 22:43:09 -04:00
|
|
|
},
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gets subscriptions" do
|
2019-11-26 18:48:30 -05:00
|
|
|
::Stripe::Plan.expects(:list).with(
|
|
|
|
expand: ['data.product']
|
|
|
|
).returns(plans)
|
|
|
|
|
2019-10-31 22:43:09 -04:00
|
|
|
::Stripe::Customer.expects(:list).with(
|
|
|
|
email: user.email,
|
|
|
|
expand: ['data.subscriptions']
|
|
|
|
).returns(customers)
|
|
|
|
|
2019-12-02 19:48:12 -05:00
|
|
|
get "/s/user/subscriptions.json"
|
2019-10-31 22:43:09 -04:00
|
|
|
|
2019-11-26 18:48:30 -05:00
|
|
|
subscription = JSON.parse(response.body).first
|
|
|
|
|
|
|
|
expect(subscription).to eq(
|
|
|
|
"id" => "sub_1234",
|
|
|
|
"plan" => { "id" => "plan_1" },
|
|
|
|
"product" => { "name" => "ACME Subscriptions" }
|
|
|
|
)
|
2019-10-31 22:43:09 -04:00
|
|
|
end
|
|
|
|
end
|
2019-11-04 00:37:21 -05:00
|
|
|
|
|
|
|
describe "delete" do
|
2019-11-28 17:24:56 -05:00
|
|
|
let(:group) { Fabricate(:group, name: 'subscribers') }
|
|
|
|
|
2019-11-25 19:09:54 -05:00
|
|
|
before do
|
|
|
|
# Users can have more than one customer id
|
2019-11-28 16:00:13 -05:00
|
|
|
Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_1')
|
2019-11-28 16:15:46 -05:00
|
|
|
Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_2')
|
2019-11-28 16:00:13 -05:00
|
|
|
Customer.create(user_id: user.id, customer_id: 'customer_id_2', product_id: 'p_2')
|
2019-11-28 17:24:56 -05:00
|
|
|
|
|
|
|
group.add(user)
|
2019-11-11 20:43:23 -05:00
|
|
|
end
|
|
|
|
|
2019-11-28 16:15:46 -05:00
|
|
|
it "does not delete a subscription when the customer is wrong" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:retrieve)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
|
|
|
plan: { product: 'p_1' },
|
|
|
|
customer: 'wrong_id'
|
|
|
|
)
|
|
|
|
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.never
|
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/user/subscriptions/sub_12345.json"
|
2019-12-03 19:23:45 -05:00
|
|
|
}.not_to change { DiscourseSubscriptions::Customer.count }
|
2019-11-28 16:15:46 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq 422
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not deletes the subscription when the product is wrong" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:retrieve)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
|
|
|
plan: { product: 'p_wrong' },
|
|
|
|
customer: 'customer_id_2'
|
|
|
|
)
|
|
|
|
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.never
|
2019-11-28 16:00:13 -05:00
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/user/subscriptions/sub_12345.json"
|
2019-12-03 19:23:45 -05:00
|
|
|
}.not_to change { DiscourseSubscriptions::Customer.count }
|
2019-11-28 16:00:13 -05:00
|
|
|
|
2019-11-25 19:09:54 -05:00
|
|
|
expect(response.status).to eq 422
|
|
|
|
end
|
2019-11-11 20:43:23 -05:00
|
|
|
|
2019-11-28 17:24:56 -05:00
|
|
|
it "removes the user from the group" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:retrieve)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
|
|
|
plan: { product: 'p_1', metadata: { group_name: 'subscribers' } },
|
|
|
|
customer: 'customer_id_1'
|
|
|
|
)
|
|
|
|
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/user/subscriptions/sub_12345.json"
|
2019-11-28 17:24:56 -05:00
|
|
|
}.to change { user.groups.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not remove the user from the group" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:retrieve)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
|
|
|
plan: { product: 'p_1', metadata: { group_name: 'does_not_exist' } },
|
|
|
|
customer: 'customer_id_1'
|
|
|
|
)
|
|
|
|
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/user/subscriptions/sub_12345.json"
|
2019-11-28 17:24:56 -05:00
|
|
|
}.not_to change { user.groups.count }
|
|
|
|
end
|
|
|
|
|
2019-11-28 16:15:46 -05:00
|
|
|
it "deletes the first subscription product 1" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:retrieve)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
2019-11-28 17:24:56 -05:00
|
|
|
plan: { product: 'p_1', metadata: {} },
|
2019-11-28 16:15:46 -05:00
|
|
|
customer: 'customer_id_1'
|
|
|
|
)
|
|
|
|
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.with('sub_12345')
|
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/user/subscriptions/sub_12345.json"
|
2019-12-03 19:23:45 -05:00
|
|
|
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
|
2019-11-28 16:15:46 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the first subscription product 2" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:retrieve)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
2019-11-28 17:24:56 -05:00
|
|
|
plan: { product: 'p_2', metadata: {} },
|
2019-11-28 16:15:46 -05:00
|
|
|
customer: 'customer_id_1'
|
|
|
|
)
|
|
|
|
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.with('sub_12345')
|
2019-11-28 16:00:13 -05:00
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/user/subscriptions/sub_12345.json"
|
2019-12-03 19:23:45 -05:00
|
|
|
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
|
2019-11-28 16:00:13 -05:00
|
|
|
|
2019-11-25 19:09:54 -05:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
2019-11-11 20:43:23 -05:00
|
|
|
|
2019-11-25 19:09:54 -05:00
|
|
|
it "deletes the second subscription" do
|
2019-11-28 16:15:46 -05:00
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:retrieve)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
2019-11-28 17:24:56 -05:00
|
|
|
plan: { product: 'p_2', metadata: {} },
|
2019-11-28 16:15:46 -05:00
|
|
|
customer: 'customer_id_2'
|
|
|
|
)
|
|
|
|
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.with('sub_12345')
|
2019-11-28 16:00:13 -05:00
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/user/subscriptions/sub_12345.json"
|
2019-12-03 19:23:45 -05:00
|
|
|
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
|
2019-11-28 16:00:13 -05:00
|
|
|
|
2019-11-25 19:09:54 -05:00
|
|
|
expect(response.status).to eq 200
|
2019-11-04 00:37:21 -05:00
|
|
|
end
|
|
|
|
end
|
2019-10-31 22:43:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|