2020-01-09 18:24:09 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
module DiscourseSubscriptions
|
|
|
|
RSpec.describe HooksController do
|
2020-01-13 23:37:53 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.discourse_subscriptions_webhook_secret = 'zascharoo'
|
|
|
|
end
|
|
|
|
|
2020-01-12 19:10:06 -05:00
|
|
|
it "contructs a webhook event" do
|
2020-01-13 23:37:53 -05:00
|
|
|
payload = 'we-want-a-shrubbery'
|
2020-01-14 07:38:26 -05:00
|
|
|
headers = { HTTP_STRIPE_SIGNATURE: 'stripe-webhook-signature' }
|
2020-01-13 23:37:53 -05:00
|
|
|
|
2020-01-12 19:10:06 -05:00
|
|
|
::Stripe::Webhook
|
|
|
|
.expects(:construct_event)
|
2020-01-13 23:37:53 -05:00
|
|
|
.with('we-want-a-shrubbery', 'stripe-webhook-signature', 'zascharoo')
|
2020-01-14 07:38:26 -05:00
|
|
|
.returns(type: 'something')
|
2020-01-12 19:10:06 -05:00
|
|
|
|
2020-01-13 23:37:53 -05:00
|
|
|
post "/s/hooks.json", params: payload, headers: headers
|
2020-01-12 19:10:06 -05:00
|
|
|
|
2020-01-09 18:24:09 -05:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
2020-01-14 07:38:26 -05:00
|
|
|
|
2020-01-14 18:20:21 -05:00
|
|
|
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) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
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)
|
|
|
|
|
|
|
|
group.add(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the customer" do
|
|
|
|
expect {
|
|
|
|
post "/s/hooks.json"
|
|
|
|
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
|
|
|
|
|
|
|
|
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
|
2020-01-14 07:38:26 -05:00
|
|
|
end
|
2020-01-09 18:24:09 -05:00
|
|
|
end
|
|
|
|
end
|