remove the user from the group in user controller

This commit is contained in:
Rimian Perkins 2019-11-29 09:24:56 +11:00
parent 3904310432
commit 36865c5ea1
2 changed files with 44 additions and 3 deletions

View File

@ -49,6 +49,9 @@ module DiscoursePatrons
deleted = ::Stripe::Subscription.delete(params[:id]) deleted = ::Stripe::Subscription.delete(params[:id])
customer.delete customer.delete
group = plan_group(subscription[:plan])
group.remove(current_user) if group
render_json_dump deleted render_json_dump deleted
else else
render_json_error I18n.t('discourse_patrons.customer_not_found') render_json_error I18n.t('discourse_patrons.customer_not_found')

View File

@ -80,11 +80,15 @@ module DiscoursePatrons
end end
describe "delete" do describe "delete" do
let(:group) { Fabricate(:group, name: 'subscribers') }
before do before do
# Users can have more than one customer id # Users can have more than one customer id
Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_1') Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_1')
Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_2') Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_2')
Customer.create(user_id: user.id, customer_id: 'customer_id_2', product_id: 'p_2') Customer.create(user_id: user.id, customer_id: 'customer_id_2', product_id: 'p_2')
group.add(user)
end end
it "does not delete a subscription when the customer is wrong" do it "does not delete a subscription when the customer is wrong" do
@ -127,12 +131,46 @@ module DiscoursePatrons
expect(response.status).to eq 422 expect(response.status).to eq 422
end end
it "removes the user from the group" do
::Stripe::Subscription
.expects(:retrieve)
.with('sub_12345')
.returns(
plan: { product: 'p_1', metadata: { group_name: 'subscribers' } },
customer: 'customer_id_1'
)
::Stripe::Subscription
.expects(:delete)
expect {
delete "/patrons/user/subscriptions/sub_12345.json"
}.to change { user.groups.count }.by(-1)
end
it "does not remove the user from the group" do
::Stripe::Subscription
.expects(:retrieve)
.with('sub_12345')
.returns(
plan: { product: 'p_1', metadata: { group_name: 'does_not_exist' } },
customer: 'customer_id_1'
)
::Stripe::Subscription
.expects(:delete)
expect {
delete "/patrons/user/subscriptions/sub_12345.json"
}.not_to change { user.groups.count }
end
it "deletes the first subscription product 1" do it "deletes the first subscription product 1" do
::Stripe::Subscription ::Stripe::Subscription
.expects(:retrieve) .expects(:retrieve)
.with('sub_12345') .with('sub_12345')
.returns( .returns(
plan: { product: 'p_1' }, plan: { product: 'p_1', metadata: {} },
customer: 'customer_id_1' customer: 'customer_id_1'
) )
@ -152,7 +190,7 @@ module DiscoursePatrons
.expects(:retrieve) .expects(:retrieve)
.with('sub_12345') .with('sub_12345')
.returns( .returns(
plan: { product: 'p_2' }, plan: { product: 'p_2', metadata: {} },
customer: 'customer_id_1' customer: 'customer_id_1'
) )
@ -172,7 +210,7 @@ module DiscoursePatrons
.expects(:retrieve) .expects(:retrieve)
.with('sub_12345') .with('sub_12345')
.returns( .returns(
plan: { product: 'p_2' }, plan: { product: 'p_2', metadata: {} },
customer: 'customer_id_2' customer: 'customer_id_2'
) )