discourse-subscriptions/spec/requests/admin/subscriptions_controller_sp...

96 lines
2.6 KiB
Ruby
Raw Normal View History

2019-10-09 22:09:24 -04:00
# frozen_string_literal: true
require 'rails_helper'
module DiscoursePatrons
2019-10-09 22:52:55 -04:00
RSpec.describe Admin::SubscriptionsController do
it 'is a subclass of AdminController' do
expect(DiscoursePatrons::Admin::SubscriptionsController < ::Admin::AdminController).to eq(true)
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
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
DiscoursePatrons::Customer.create(
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-11-28 04:44:38 -05:00
}.to change { DiscoursePatrons::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