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

85 lines
2.7 KiB
Ruby
Raw Normal View History

2019-10-31 22:43:09 -04:00
# frozen_string_literal: true
require 'rails_helper'
module DiscoursePatrons
RSpec.describe User::SubscriptionsController do
it 'is a subclass of ApplicationController' do
expect(DiscoursePatrons::User::SubscriptionsController < ::ApplicationController).to eq(true)
end
context "not authenticated" do
it "does not get the subscriptions" do
::Stripe::Customer.expects(:list).never
get "/patrons/user/subscriptions.json"
end
2019-11-04 00:37:21 -05:00
it "does not destroy a subscription" do
::Stripe::Subscription.expects(:delete).never
patch "/patrons/user/subscriptions/sub_12345.json"
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
let(:customers) do
{
data: [{
id: "cus_23456",
subscriptions: {
data: [{ id: "sub_1234" }, { id: "sub_4567" }]
},
}]
}
end
it "gets subscriptions" do
::Stripe::Customer.expects(:list).with(
email: user.email,
expand: ['data.subscriptions']
).returns(customers)
get "/patrons/user/subscriptions.json"
expect(JSON.parse(response.body)).to eq([{ "id" => "sub_1234" }, { "id" => "sub_4567" }])
end
end
2019-11-04 00:37:21 -05:00
describe "delete" do
2019-11-25 19:09:54 -05:00
before do
# Users can have more than one customer id
Customer.create(user_id: user.id, customer_id: 'customer_id_1')
Customer.create(user_id: user.id, customer_id: 'customer_id_2')
2019-11-11 20:43:23 -05:00
end
2019-11-25 19:09:54 -05:00
it "does not delete a subscription" do
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'wrong_id')
::Stripe::Subscription.expects(:delete).never
delete "/patrons/user/subscriptions/sub_12345.json"
expect(response.status).to eq 422
end
2019-11-11 20:43:23 -05:00
2019-11-25 19:09:54 -05:00
it "deletes the first subscription" do
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_1')
::Stripe::Subscription.expects(:delete).with('sub_12345')
delete "/patrons/user/subscriptions/sub_12345.json"
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
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_2')
::Stripe::Subscription.expects(:delete).with('sub_12345')
delete "/patrons/user/subscriptions/sub_12345.json"
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