discourse-subscriptions/spec/requests/admin/subscriptions_controller_spec.rb

145 lines
4.5 KiB
Ruby
Raw Normal View History

2019-10-10 13:09:24 +11:00
# frozen_string_literal: true
require 'rails_helper'
2019-12-04 11:23:45 +11:00
module DiscourseSubscriptions
2019-10-10 13:52:55 +11:00
RSpec.describe Admin::SubscriptionsController do
it 'is a subclass of AdminController' do
2019-12-04 11:23:45 +11:00
expect(DiscourseSubscriptions::Admin::SubscriptionsController < ::Admin::AdminController).to eq(true)
2019-10-10 13:52:55 +11:00
end
2019-10-10 13:09:24 +11:00
let(:user) { Fabricate(:user) }
let(:customer) { Fabricate(:customer, user_id: user.id, customer_id: 'c_123', product_id: 'pr_34578') }
before do
Fabricate(:subscription, external_id: "sub_12345", customer_id: customer.id)
Fabricate(:subscription, external_id: "sub_77777", customer_id: customer.id)
end
2022-10-04 09:18:34 +01:00
context 'when unauthenticated' do
2019-10-15 13:18:25 +11:00
it "does nothing" do
::Stripe::Subscription.expects(:list).never
2019-12-03 11:48:12 +11:00
get "/s/admin/subscriptions.json"
expect(response.status).to eq(404)
2019-10-15 13:18:25 +11:00
end
2019-11-14 10:51:04 +11:00
it "does not destroy a subscription" do
::Stripe::Subscription.expects(:delete).never
2019-12-03 11:48:12 +11:00
patch "/s/admin/subscriptions/sub_12345.json"
2019-11-14 10:51:04 +11:00
end
2019-10-15 13:18:25 +11:00
end
2022-10-04 09:18:34 +01:00
context 'when authenticated' do
2019-10-15 13:18:25 +11:00
let(:admin) { Fabricate(:admin) }
before { sign_in(admin) }
2019-11-14 10:51:04 +11:00
describe "index" do
before do
SiteSetting.discourse_subscriptions_public_key = "public-key"
SiteSetting.discourse_subscriptions_secret_key = "secret-key"
end
it "gets the subscriptions and products" do
::Stripe::Subscription.expects(:list)
.with(expand: ['data.plan.product'], limit: 10, starting_after: nil)
.returns(
has_more: false,
data: [
{ id: "sub_12345" },
{ id: "sub_nope" }
]
)
2019-12-03 11:48:12 +11:00
get "/s/admin/subscriptions.json"
subscriptions = response.parsed_body["data"][0]["id"]
2019-11-14 10:51:04 +11:00
expect(response.status).to eq(200)
expect(subscriptions).to eq("sub_12345")
2019-11-14 10:51:04 +11:00
end
it "handles starting at a different point in the set" do
::Stripe::Subscription.expects(:list)
.with(expand: ['data.plan.product'], limit: 10, starting_after: 'sub_nope')
.returns(
has_more: false,
data: [
{ id: "sub_77777" },
{ id: "sub_yepnoep" }
]
)
get "/s/admin/subscriptions.json", params: { last_record: 'sub_nope' }
subscriptions = response.parsed_body["data"][0]["id"]
expect(response.status).to eq(200)
expect(subscriptions).to eq("sub_77777")
end
2019-11-14 10:51:04 +11:00
end
describe "destroy" do
2019-11-29 10:37:32 +11:00
let(:group) { Fabricate(:group, name: 'subscribers') }
2019-11-28 20:44:38 +11:00
before do
2019-11-29 10:37:32 +11:00
group.add(user)
2019-11-28 20:44:38 +11:00
end
it "deletes a customer" do
::Stripe::Subscription
.expects(:delete)
.with('sub_12345')
2019-11-29 10:37:32 +11:00
.returns(
plan: { product: 'pr_34578' },
customer: 'c_123'
)
2019-11-28 20:44:38 +11:00
expect {
2019-12-03 11:48:12 +11:00
delete "/s/admin/subscriptions/sub_12345.json"
2019-12-04 11:23:45 +11:00
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
2019-11-14 10:51:04 +11:00
end
2019-11-29 10:37:32 +11: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-03 11:48:12 +11:00
delete "/s/admin/subscriptions/sub_12345.json"
2019-11-29 10:37:32 +11: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-03 11:48:12 +11:00
delete "/s/admin/subscriptions/sub_12345.json"
2019-11-29 10:37:32 +11:00
}.not_to change { user.groups.count }
end
it "refunds if params[:refund] present" do
::Stripe::Subscription
.expects(:delete)
.with('sub_12345')
.returns(
plan: { product: 'pr_34578' },
customer: 'c_123'
)
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(latest_invoice: 'in_123')
::Stripe::Invoice.expects(:retrieve).with('in_123').returns(payment_intent: 'pi_123')
::Stripe::Refund.expects(:create).with({ payment_intent: 'pi_123' })
delete "/s/admin/subscriptions/sub_12345.json", params: { refund: true }
end
2019-10-15 13:18:25 +11:00
end
2019-10-10 13:09:24 +11:00
end
end
end