From 418d9cce27fb225110bf3458d0439d8cc5969505 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Mon, 10 Nov 2025 20:44:10 +0530 Subject: [PATCH] update stripe controller rspec --- .../webooks/stripe_controller_spec.rb | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/spec/enterprise/controllers/enterprise/webooks/stripe_controller_spec.rb b/spec/enterprise/controllers/enterprise/webooks/stripe_controller_spec.rb index bbcddeb29..96d868610 100644 --- a/spec/enterprise/controllers/enterprise/webooks/stripe_controller_spec.rb +++ b/spec/enterprise/controllers/enterprise/webooks/stripe_controller_spec.rb @@ -2,24 +2,34 @@ require 'rails_helper' RSpec.describe 'Enterprise::Webhooks::StripeController', type: :request do describe 'POST /enterprise/webhooks/stripe' do - let(:params) { { content: 'hello' } } + let(:payload) { { type: 'customer.subscription.updated', content: 'hello' }.to_json } + let(:event_object) { instance_double(Stripe::Event, type: 'customer.subscription.updated') } + + around do |example| + ENV['STRIPE_WEBHOOK_SECRET'] = 'test_secret' + ENV['STRIPE_WEBHOOK_SECRET_V2'] = 'test_secret_v2' + example.run + ENV.delete('STRIPE_WEBHOOK_SECRET') + ENV.delete('STRIPE_WEBHOOK_SECRET_V2') + end it 'call the Enterprise::Billing::HandleStripeEventService with the params' do handle_stripe = double - allow(Stripe::Webhook).to receive(:construct_event).and_return(params) + allow(Stripe::Webhook).to receive(:construct_event).and_return(event_object) allow(Enterprise::Billing::HandleStripeEventService).to receive(:new).and_return(handle_stripe) allow(handle_stripe).to receive(:perform) - post '/enterprise/webhooks/stripe', headers: { 'Stripe-Signature': 'test' }, params: params - expect(handle_stripe).to have_received(:perform).with(event: params) + post '/enterprise/webhooks/stripe', params: payload, headers: { 'Content-Type' => 'application/json', 'Stripe-Signature' => 'test' } + expect(handle_stripe).to have_received(:perform).with(event: event_object) end it 'returns a bad request if the headers are missing' do - post '/enterprise/webhooks/stripe', params: params + post '/enterprise/webhooks/stripe', params: payload, headers: { 'Content-Type' => 'application/json' } expect(response).to have_http_status(:bad_request) end it 'returns a bad request if the headers are invalid' do - post '/enterprise/webhooks/stripe', headers: { 'Stripe-Signature': 'test' }, params: params + allow(Stripe::Webhook).to receive(:construct_event).and_raise(Stripe::SignatureVerificationError.new('Invalid signature', 'sig')) + post '/enterprise/webhooks/stripe', params: payload, headers: { 'Content-Type' => 'application/json', 'Stripe-Signature' => 'test' } expect(response).to have_http_status(:bad_request) end end