mirror of https://github.com/docusealco/docuseal
Merge pull request #49 from CareerPlug/CP-11565-2
CP-11565 - Update webhook secret to load for new accountspull/608/head
commit
3570b7c0b5
@ -0,0 +1,57 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
namespace :webhooks do
|
||||
desc 'Configure CareerPlug webhook secret from CAREERPLUG_WEBHOOK_SECRET env var'
|
||||
task configure_careerplug: :environment do
|
||||
secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET') do
|
||||
if Rails.env.development?
|
||||
'development_webhook_secret'
|
||||
else
|
||||
abort 'CAREERPLUG_WEBHOOK_SECRET environment variable is required'
|
||||
end
|
||||
end
|
||||
|
||||
webhook_urls = WebhookUrl.where('url LIKE ? OR url LIKE ? OR url LIKE ?',
|
||||
'%careerplug%', '%cpats%', '%localhost:3000%')
|
||||
|
||||
if webhook_urls.any?
|
||||
webhook_urls.find_each do |webhook_url|
|
||||
webhook_url.update!(secret: { 'X-CareerPlug-Secret' => secret })
|
||||
puts "Updated webhook secret for #{webhook_url.url}"
|
||||
end
|
||||
puts "Updated #{webhook_urls.count} webhook URL(s)"
|
||||
else
|
||||
puts 'No CareerPlug webhook URLs found. Available webhooks:'
|
||||
WebhookUrl.find_each { |w| puts " - #{w.id}: #{w.url}" }
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Set up development webhook URLs for all accounts (creates URLs + configures secret)'
|
||||
task setup_development: :environment do
|
||||
abort 'This task is only for development' unless Rails.env.development?
|
||||
|
||||
url = 'http://localhost:3000/api/docuseal/events'
|
||||
secret = { 'X-CareerPlug-Secret' => 'development_webhook_secret' }
|
||||
events = %w[form.viewed form.started form.completed form.declined]
|
||||
|
||||
created = 0
|
||||
updated = 0
|
||||
|
||||
Account.find_each do |account|
|
||||
webhook_url = WebhookUrl.find_or_initialize_by(account: account, sha1: Digest::SHA1.hexdigest(url))
|
||||
|
||||
if webhook_url.new_record?
|
||||
webhook_url.assign_attributes(url: url, events: events, secret: secret)
|
||||
webhook_url.save!
|
||||
created += 1
|
||||
puts "Created webhook URL for account #{account.id}: #{account.name}"
|
||||
elsif webhook_url.secret != secret
|
||||
webhook_url.update!(secret: secret)
|
||||
updated += 1
|
||||
puts "Updated webhook secret for account #{account.id}: #{account.name}"
|
||||
end
|
||||
end
|
||||
|
||||
puts "Done: #{created} created, #{updated} updated"
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,65 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Account, '#create_careerplug_webhook' do
|
||||
around do |example|
|
||||
original_secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET', nil)
|
||||
original_url = ENV.fetch('CAREERPLUG_WEBHOOK_URL', nil)
|
||||
|
||||
# Set required env vars for webhook creation
|
||||
ENV['CAREERPLUG_WEBHOOK_SECRET'] = 'test_secret'
|
||||
ENV['CAREERPLUG_WEBHOOK_URL'] = 'http://example.com/webhook'
|
||||
|
||||
example.run
|
||||
|
||||
# Restore original env vars
|
||||
ENV['CAREERPLUG_WEBHOOK_SECRET'] = original_secret
|
||||
ENV['CAREERPLUG_WEBHOOK_URL'] = original_url
|
||||
end
|
||||
|
||||
describe 'CareerPlug webhook creation' do
|
||||
it 'creates webhook after successful account creation' do
|
||||
account = build(:account)
|
||||
expect(account.webhook_urls).to be_empty
|
||||
|
||||
account.save!
|
||||
|
||||
expect(account.webhook_urls.count).to eq(1)
|
||||
webhook = account.webhook_urls.first
|
||||
expect(webhook.url).to eq('http://example.com/webhook')
|
||||
expect(webhook.events).to eq(['form.viewed', 'form.started', 'form.completed', 'form.declined'])
|
||||
expect(webhook.secret).to eq({ 'X-CareerPlug-Secret' => 'test_secret' })
|
||||
end
|
||||
|
||||
it 'does not create webhook if account creation fails' do
|
||||
# This test verifies that after_commit behavior works correctly
|
||||
# by simulating a transaction rollback
|
||||
|
||||
expect do
|
||||
described_class.transaction do
|
||||
create(:account)
|
||||
# Simulate some error that would cause rollback
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
end.not_to change(described_class, :count)
|
||||
|
||||
expect do
|
||||
described_class.transaction do
|
||||
create(:account)
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
end.not_to change(WebhookUrl, :count)
|
||||
end
|
||||
|
||||
it 'does not create webhook when CAREERPLUG_WEBHOOK_SECRET is blank' do
|
||||
original_secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET', nil)
|
||||
ENV['CAREERPLUG_WEBHOOK_SECRET'] = ''
|
||||
|
||||
account = create(:account)
|
||||
expect(account.webhook_urls.count).to eq(0)
|
||||
|
||||
ENV['CAREERPLUG_WEBHOOK_SECRET'] = original_secret
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue