2019-10-13 18:52:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
module DiscoursePatrons
|
|
|
|
RSpec.describe SubscriptionsController do
|
2019-10-28 20:43:32 -04:00
|
|
|
context "not authenticated" do
|
|
|
|
it "does not create a subscription" do
|
|
|
|
::Stripe::Plan.expects(:retrieve).never
|
|
|
|
::Stripe::Subscription.expects(:create).never
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-16 21:07:06 -04:00
|
|
|
context "authenticated" do
|
2019-10-31 22:43:09 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
2019-10-13 18:52:43 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2019-10-16 21:07:06 -04:00
|
|
|
describe "create" do
|
2019-10-23 20:37:20 -04:00
|
|
|
it "creates a subscription" do
|
|
|
|
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'awesome' })
|
|
|
|
::Stripe::Subscription.expects(:create).with(
|
|
|
|
customer: 'cus_1234',
|
2019-11-12 18:19:49 -05:00
|
|
|
items: [ plan: 'plan_1234' ],
|
2019-11-12 22:21:21 -05:00
|
|
|
metadata: { user_id: user.id, username: user.username_lower },
|
2019-10-23 20:37:20 -04:00
|
|
|
)
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
2019-10-16 21:07:06 -04:00
|
|
|
end
|
2019-10-27 23:48:59 -04:00
|
|
|
|
2019-11-04 00:37:21 -05:00
|
|
|
it "creates a customer model" do
|
2019-10-27 23:48:59 -04:00
|
|
|
::Stripe::Plan.expects(:retrieve).returns(metadata: {})
|
|
|
|
::Stripe::Subscription.expects(:create).returns(status: 'active')
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
|
|
|
}.to change { DiscoursePatrons::Customer.count }
|
|
|
|
end
|
2019-10-13 18:52:43 -04:00
|
|
|
end
|
2019-10-13 20:47:49 -04:00
|
|
|
|
2019-10-16 21:07:06 -04:00
|
|
|
describe "user groups" do
|
2019-10-23 20:37:20 -04:00
|
|
|
let(:group_name) { 'group-123' }
|
|
|
|
let(:group) { Fabricate(:group, name: group_name) }
|
2019-10-16 21:07:06 -04:00
|
|
|
|
2019-10-23 20:52:31 -04:00
|
|
|
context "unauthorized group" do
|
2019-10-24 00:48:03 -04:00
|
|
|
before do
|
|
|
|
::Stripe::Subscription.expects(:create).returns(status: 'active')
|
|
|
|
end
|
|
|
|
|
2019-10-23 20:52:31 -04:00
|
|
|
it "does not add the user to the admins group" do
|
|
|
|
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'admins' })
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
|
|
|
expect(user.admin).to eq false
|
|
|
|
end
|
2019-10-24 00:48:03 -04:00
|
|
|
|
|
|
|
it "does not add the user to other group" do
|
|
|
|
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'other' })
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
|
|
|
expect(user.groups).to be_empty
|
|
|
|
end
|
2019-10-23 20:52:31 -04:00
|
|
|
end
|
|
|
|
|
2019-10-23 20:37:20 -04:00
|
|
|
context "plan has group in metadata" do
|
|
|
|
before do
|
|
|
|
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: group_name })
|
|
|
|
end
|
2019-10-16 21:07:06 -04:00
|
|
|
|
2019-10-23 20:52:31 -04:00
|
|
|
it "does not add the user to the group when subscription fails" do
|
2019-10-23 20:37:20 -04:00
|
|
|
::Stripe::Subscription.expects(:create).returns(status: 'failed')
|
2019-10-16 21:07:06 -04:00
|
|
|
|
2019-10-23 20:37:20 -04:00
|
|
|
expect {
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
|
|
|
}.not_to change { group.users.count }
|
2019-10-24 00:48:03 -04:00
|
|
|
|
|
|
|
expect(user.groups).to be_empty
|
2019-10-23 20:37:20 -04:00
|
|
|
end
|
2019-10-16 21:07:06 -04:00
|
|
|
|
2019-10-23 20:37:20 -04:00
|
|
|
it "adds the user to the group when the subscription is active" do
|
|
|
|
::Stripe::Subscription.expects(:create).returns(status: 'active')
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
|
|
|
}.to change { group.users.count }
|
2019-10-24 00:48:03 -04:00
|
|
|
|
|
|
|
expect(user.groups).not_to be_empty
|
2019-10-23 20:37:20 -04:00
|
|
|
end
|
2019-10-16 21:07:06 -04:00
|
|
|
|
2019-10-23 20:37:20 -04:00
|
|
|
it "adds the user to the group when the subscription is trialing" do
|
|
|
|
::Stripe::Subscription.expects(:create).returns(status: 'trialing')
|
2019-10-16 21:07:06 -04:00
|
|
|
|
2019-10-23 20:37:20 -04:00
|
|
|
expect {
|
|
|
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
|
|
|
}.to change { group.users.count }
|
2019-10-24 00:48:03 -04:00
|
|
|
|
|
|
|
expect(user.groups).not_to be_empty
|
2019-10-23 20:37:20 -04:00
|
|
|
end
|
2019-10-16 21:07:06 -04:00
|
|
|
end
|
2019-10-13 20:47:49 -04:00
|
|
|
end
|
2019-10-13 18:52:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|