2020-01-09 18:24:09 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:35:06 -05:00
|
|
|
require "rails_helper"
|
2020-01-09 18:24:09 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
RSpec.describe DiscourseSubscriptions::HooksController do
|
|
|
|
before { SiteSetting.discourse_subscriptions_webhook_secret = "zascharoo" }
|
2020-01-13 23:37:53 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
it "constructs a webhook event" do
|
|
|
|
payload = "we-want-a-shrubbery"
|
|
|
|
headers = { HTTP_STRIPE_SIGNATURE: "stripe-webhook-signature" }
|
2020-01-13 23:37:53 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
::Stripe::Webhook
|
|
|
|
.expects(:construct_event)
|
|
|
|
.with("we-want-a-shrubbery", "stripe-webhook-signature", "zascharoo")
|
|
|
|
.returns(type: "something")
|
2020-01-12 19:10:06 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
post "/s/hooks.json", params: payload, headers: headers
|
2020-01-12 19:10:06 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
2020-01-14 07:38:26 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
describe "event types" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:customer) do
|
|
|
|
Fabricate(:customer, customer_id: "c_575768", product_id: "p_8654", user_id: user.id)
|
|
|
|
end
|
|
|
|
let(:group) { Fabricate(:group, name: "subscribers-group") }
|
|
|
|
|
|
|
|
let(:event_data) do
|
|
|
|
{
|
|
|
|
object: {
|
|
|
|
customer: customer.customer_id,
|
|
|
|
plan: {
|
|
|
|
product: customer.product_id,
|
|
|
|
metadata: {
|
|
|
|
group_name: group.name,
|
2022-12-29 07:35:06 -05:00
|
|
|
},
|
|
|
|
},
|
2024-01-16 11:51:44 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2020-01-14 18:20:21 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
describe "customer.subscription.updated" do
|
|
|
|
before do
|
|
|
|
event = { type: "customer.subscription.updated", data: event_data }
|
2022-12-29 07:35:06 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
::Stripe::Webhook.stubs(:construct_event).returns(event)
|
|
|
|
end
|
2020-01-14 18:20:21 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
it "is successfull" do
|
|
|
|
post "/s/hooks.json"
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
describe "completing the subscription" do
|
|
|
|
it "does not add the user to the group" do
|
|
|
|
event_data[:object][:status] = "incomplete"
|
|
|
|
event_data[:previous_attributes] = { status: "incomplete" }
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect { post "/s/hooks.json" }.not_to change { user.groups.count }
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
it "does not add the user to the group" do
|
|
|
|
event_data[:object][:status] = "incomplete"
|
|
|
|
event_data[:previous_attributes] = { status: "something-else" }
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect { post "/s/hooks.json" }.not_to change { user.groups.count }
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
it "adds the user to the group when completing the transaction" do
|
|
|
|
event_data[:object][:status] = "complete"
|
|
|
|
event_data[:previous_attributes] = { status: "incomplete" }
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect { post "/s/hooks.json" }.to change { user.groups.count }.by(1)
|
2020-01-25 18:49:51 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect(response.status).to eq 200
|
2020-01-25 18:49:51 -05:00
|
|
|
end
|
2020-01-14 18:20:21 -05:00
|
|
|
end
|
2024-01-16 11:51:44 -05:00
|
|
|
end
|
2020-01-14 18:20:21 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
describe "customer.subscription.deleted" do
|
|
|
|
before do
|
|
|
|
event = { type: "customer.subscription.deleted", data: event_data }
|
2020-01-14 18:20:21 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
::Stripe::Webhook.stubs(:construct_event).returns(event)
|
2020-01-24 23:06:05 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
group.add(user)
|
|
|
|
end
|
2020-01-24 23:06:05 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
it "deletes the customer" do
|
|
|
|
expect { post "/s/hooks.json" }.to change { DiscourseSubscriptions::Customer.count }.by(-1)
|
2020-01-24 23:06:05 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
2020-01-24 23:06:05 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
it "removes the user from the group" do
|
|
|
|
expect { post "/s/hooks.json" }.to change { user.groups.count }.by(-1)
|
2020-01-14 18:20:21 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
expect(response.status).to eq 200
|
2020-01-14 18:20:21 -05:00
|
|
|
end
|
2020-01-14 07:38:26 -05:00
|
|
|
end
|
2020-01-09 18:24:09 -05:00
|
|
|
end
|
|
|
|
end
|