diff --git a/app/controllers/hooks_controller.rb b/app/controllers/hooks_controller.rb index 442e9eb..babfa31 100644 --- a/app/controllers/hooks_controller.rb +++ b/app/controllers/hooks_controller.rb @@ -31,12 +31,10 @@ module DiscourseSubscriptions if customer customer.delete - # - # binding.pry - # - # user = ::User.find(customer.user_id) - # group = plan_group(event[:plan]) - # group.remove(user) if group + + user = ::User.find(customer.user_id) + group = plan_group(event[:plan]) + group.remove(user) if group end end diff --git a/spec/requests/hooks_controller_spec.rb b/spec/requests/hooks_controller_spec.rb index 345b0d3..50d27a6 100644 --- a/spec/requests/hooks_controller_spec.rb +++ b/spec/requests/hooks_controller_spec.rb @@ -22,32 +22,40 @@ module DiscourseSubscriptions expect(response.status).to eq 200 end - it "cancels a subscription" do - user = Fabricate(:user) - group = Fabricate(:group, name: 'subscribers-group') + describe "canceling a subscription" do + let(:user) { Fabricate(:user) } + let(:group) { Fabricate(:group, name: 'subscribers-group') } + let(:customer) { Fabricate(:customer, customer_id: 'c_575768', product_id: 'p_8654', user_id: user.id) } - customer = Fabricate( - :customer, - customer_id: 'c_575768', - product_id: 'p_8654', - user_id: user.id - ) + before do + event = { + type: 'customer.subscription.deleted', + customer: customer.customer_id, + plan: { product: customer.product_id, metadata: { group_name: group.name } } + } - event = { - type: 'customer.subscription.deleted', - customer: customer.customer_id, - plan: { product: customer.product_id, metadata: { group_name: group.name } } - } + ::Stripe::Webhook + .stubs(:construct_event) + .returns(event) - ::Stripe::Webhook - .expects(:construct_event) - .returns(event) + group.add(user) + end - expect { - post "/s/hooks.json" - }.to change { DiscourseSubscriptions::Customer.count }.by(-1) + it "deletes the customer" do + expect { + post "/s/hooks.json" + }.to change { DiscourseSubscriptions::Customer.count }.by(-1) - expect(response.status).to eq 200 + expect(response.status).to eq 200 + end + + it "removes the user from the group" do + expect { + post "/s/hooks.json" + }.to change { user.groups.count }.by(-1) + + expect(response.status).to eq 200 + end end end end