2019-10-09 22:09:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2019-12-03 19:23:45 -05:00
|
|
|
module DiscourseSubscriptions
|
2019-10-09 22:52:55 -04:00
|
|
|
RSpec.describe Admin::SubscriptionsController do
|
|
|
|
it 'is a subclass of AdminController' do
|
2019-12-03 19:23:45 -05:00
|
|
|
expect(DiscourseSubscriptions::Admin::SubscriptionsController < ::Admin::AdminController).to eq(true)
|
2019-10-09 22:52:55 -04:00
|
|
|
end
|
2019-10-09 22:09:24 -04:00
|
|
|
|
2019-10-14 22:18:25 -04:00
|
|
|
context 'unauthenticated' do
|
|
|
|
it "does nothing" do
|
|
|
|
::Stripe::Subscription.expects(:list).never
|
2019-12-02 19:48:12 -05:00
|
|
|
get "/s/admin/subscriptions.json"
|
2019-10-14 22:18:25 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2019-11-13 18:51:04 -05:00
|
|
|
|
|
|
|
it "does not destroy a subscription" do
|
|
|
|
::Stripe::Subscription.expects(:delete).never
|
2019-12-02 19:48:12 -05:00
|
|
|
patch "/s/admin/subscriptions/sub_12345.json"
|
2019-11-13 18:51:04 -05:00
|
|
|
end
|
2019-10-14 22:18:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated' do
|
2019-11-28 04:44:38 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2019-10-14 22:18:25 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
2019-11-13 18:51:04 -05:00
|
|
|
describe "index" do
|
2019-11-25 20:55:49 -05:00
|
|
|
it "gets the subscriptions and products" do
|
|
|
|
::Stripe::Subscription.expects(:list).with(expand: ['data.plan.product'])
|
2019-12-02 19:48:12 -05:00
|
|
|
get "/s/admin/subscriptions.json"
|
2019-11-13 18:51:04 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "destroy" do
|
2019-11-28 18:37:32 -05:00
|
|
|
let(:group) { Fabricate(:group, name: 'subscribers') }
|
|
|
|
|
2019-11-28 04:44:38 -05:00
|
|
|
before do
|
2019-12-03 19:23:45 -05:00
|
|
|
DiscourseSubscriptions::Customer.create(
|
2019-11-28 04:44:38 -05:00
|
|
|
user_id: user.id,
|
|
|
|
customer_id: 'c_123',
|
|
|
|
product_id: 'pr_34578'
|
|
|
|
)
|
2019-11-28 18:37:32 -05:00
|
|
|
|
|
|
|
group.add(user)
|
2019-11-28 04:44:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes a customer" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.with('sub_12345')
|
2019-11-28 18:37:32 -05:00
|
|
|
.returns(
|
|
|
|
plan: { product: 'pr_34578' },
|
|
|
|
customer: 'c_123'
|
|
|
|
)
|
2019-11-28 04:44:38 -05:00
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/admin/subscriptions/sub_12345.json"
|
2019-12-03 19:23:45 -05:00
|
|
|
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
|
2019-11-13 18:51:04 -05:00
|
|
|
end
|
2019-11-28 18:37:32 -05:00
|
|
|
|
|
|
|
it "removes the user from the group" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
|
|
|
plan: { product: 'pr_34578', metadata: { group_name: 'subscribers' } },
|
|
|
|
customer: 'c_123'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/admin/subscriptions/sub_12345.json"
|
2019-11-28 18:37:32 -05:00
|
|
|
}.to change { user.groups.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not remove the user from the group" do
|
|
|
|
::Stripe::Subscription
|
|
|
|
.expects(:delete)
|
|
|
|
.with('sub_12345')
|
|
|
|
.returns(
|
|
|
|
plan: { product: 'pr_34578', metadata: { group_name: 'group_does_not_exist' } },
|
|
|
|
customer: 'c_123'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect {
|
2019-12-02 19:48:12 -05:00
|
|
|
delete "/s/admin/subscriptions/sub_12345.json"
|
2019-11-28 18:37:32 -05:00
|
|
|
}.not_to change { user.groups.count }
|
|
|
|
end
|
2019-10-14 22:18:25 -04:00
|
|
|
end
|
2019-10-09 22:09:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|