Skip to content

Commit 3570b7c

Browse files
Merge pull request #49 from CareerPlug/CP-11565-2
CP-11565 - Update webhook secret to load for new accounts
2 parents 4173fec + f8a5666 commit 3570b7c

8 files changed

Lines changed: 215 additions & 54 deletions

File tree

app/models/account.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Account < ApplicationRecord
5757

5858
validates :external_account_id, uniqueness: true, allow_nil: true
5959

60+
after_commit :create_careerplug_webhook, on: :create
61+
6062
scope :active, -> { where(archived_at: nil) }
6163

6264
def self.find_or_create_by_external_id(external_id, name, attributes = {})
@@ -72,4 +74,16 @@ def default_template_folder
7274
super || build_default_template_folder(name: TemplateFolder::DEFAULT_NAME,
7375
author_id: users.minimum(:id)).tap(&:save!)
7476
end
77+
78+
private
79+
80+
def create_careerplug_webhook
81+
return if ENV['CAREERPLUG_WEBHOOK_SECRET'].blank?
82+
83+
webhook_urls.create!(
84+
url: ENV.fetch('CAREERPLUG_WEBHOOK_URL'),
85+
events: %w[form.viewed form.started form.completed form.declined],
86+
secret: { 'X-CareerPlug-Secret' => ENV.fetch('CAREERPLUG_WEBHOOK_SECRET') }
87+
)
88+
end
7589
end

bin/start_console_production

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ fetch_env_variables() {
169169
export NEWRELIC_APP_NAME=$(echo "$SECRET_JSON" | jq -r '.newrelic_app_name')
170170
export NEWRELIC_MONITOR_MODE=$(echo "$SECRET_JSON" | jq -r '.newrelic_monitor_mode')
171171
export ENCRYPTION_SECRET=$(echo "$SECRET_JSON" | jq -r '.ENCRYPTION_SECRET // empty')
172+
export CAREERPLUG_WEBHOOK_SECRET=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_SECRET // empty')
173+
export CAREERPLUG_WEBHOOK_URL=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_URL // empty')
172174

173175

174176
# Validate that we got the values
@@ -187,19 +189,11 @@ fetch_env_variables() {
187189
# Write variables to .env.production file
188190
echo "Writing environment variables to .env.production..."
189191

190-
# Remove existing DB_HOST, REDIS_URL, and S3_ATTACHMENTS_BUCKET lines if they exist
192+
# Remove existing environment variables if they exist
191193
if [ -f "./.env.production" ]; then
192194
echo "Removing existing variables from .env.production"
193-
grep -v "^DB_HOST=" ./.env.production > ./.env.production.tmp || true
194-
grep -v "^REDIS_URL=" ./.env.production.tmp > ./.env.production || true
195-
grep -v "^S3_ATTACHMENTS_BUCKET=" ./.env.production.tmp > ./.env.production || true
196-
grep -v "^AIRBRAKE_ID=" ./.env.production.tmp > ./.env.production || true
197-
grep -v "^AIRBRAKE_KEY=" ./.env.production.tmp > ./.env.production || true
198-
grep -v "^NEWRELIC_LICENSE_KEY=" ./.env.production.tmp > ./.env.production || true
199-
grep -v "^NEWRELIC_APP_NAME=" ./.env.production.tmp > ./.env.production || true
200-
grep -v "^NEWRELIC_MONITOR_MODE=" ./.env.production.tmp > ./.env.production || true
201-
grep -v "^ENCRYPTION_SECRET=" ./.env.production.tmp > ./.env.production || true
202-
rm -f ./.env.production.tmp
195+
grep -Ev "^(DB_HOST|REDIS_URL|S3_ATTACHMENTS_BUCKET|AIRBRAKE_ID|AIRBRAKE_KEY|NEWRELIC_LICENSE_KEY|NEWRELIC_APP_NAME|NEWRELIC_MONITOR_MODE|ENCRYPTION_SECRET|CAREERPLUG_WEBHOOK_SECRET|CAREERPLUG_WEBHOOK_URL)=" ./.env.production > ./.env.production.tmp || true
196+
mv ./.env.production.tmp ./.env.production
203197
fi
204198

205199
# Append the new credentials
@@ -218,6 +212,18 @@ fetch_env_variables() {
218212
echo "✓ ENCRYPTION_SECRET written to .env.production"
219213
fi
220214

215+
# Add CareerPlug webhook secret if it exists
216+
if [ -n "$CAREERPLUG_WEBHOOK_SECRET" ]; then
217+
echo "CAREERPLUG_WEBHOOK_SECRET=$CAREERPLUG_WEBHOOK_SECRET" >> ./.env.production
218+
echo "✓ CAREERPLUG_WEBHOOK_SECRET written to .env.production"
219+
fi
220+
221+
# Add CareerPlug webhook URL if it exists
222+
if [ -n "$CAREERPLUG_WEBHOOK_URL" ]; then
223+
echo "CAREERPLUG_WEBHOOK_URL=$CAREERPLUG_WEBHOOK_URL" >> ./.env.production
224+
echo "✓ CAREERPLUG_WEBHOOK_URL written to .env.production"
225+
fi
226+
221227
echo "✓ Environment variables successfully retrieved and written to .env.production"
222228
}
223229

bin/start_console_staging

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ fetch_env_variables() {
169169
export NEWRELIC_APP_NAME=$(echo "$SECRET_JSON" | jq -r '.newrelic_app_name')
170170
export NEWRELIC_MONITOR_MODE=$(echo "$SECRET_JSON" | jq -r '.newrelic_monitor_mode')
171171
export ENCRYPTION_SECRET=$(echo "$SECRET_JSON" | jq -r '.ENCRYPTION_SECRET // empty')
172+
export CAREERPLUG_WEBHOOK_SECRET=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_SECRET // empty')
173+
export CAREERPLUG_WEBHOOK_URL=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_URL // empty')
172174

173175
# Validate that we got the values
174176
if [ "$DB_HOST" = "null" ] || [ "$REDIS_URL" = "null" ] || [ "$S3_ATTACHMENTS_BUCKET" = "null" ] || [ -z "$DB_HOST" ] || [ -z "$REDIS_URL" ] || [ -z "$S3_ATTACHMENTS_BUCKET" ]; then
@@ -186,19 +188,11 @@ fetch_env_variables() {
186188
# Write variables to .env.staging file
187189
echo "Writing environment variables to .env.staging..."
188190

189-
# Remove existing DB_HOST, REDIS_URL, and S3_ATTACHMENTS_BUCKET lines if they exist
191+
# Remove existing environment variables if they exist
190192
if [ -f "./.env.staging" ]; then
191193
echo "Removing existing variables from .env.staging"
192-
grep -v "^DB_HOST=" ./.env.staging > ./.env.staging.tmp || true
193-
grep -v "^REDIS_URL=" ./.env.staging.tmp > ./.env.staging || true
194-
grep -v "^S3_ATTACHMENTS_BUCKET=" ./.env.staging.tmp > ./.env.staging || true
195-
grep -v "^AIRBRAKE_ID=" ./.env.staging.tmp > ./.env.staging || true
196-
grep -v "^AIRBRAKE_KEY=" ./.env.staging.tmp > ./.env.staging || true
197-
grep -v "^NEWRELIC_LICENSE_KEY=" ./.env.staging.tmp > ./.env.staging || true
198-
grep -v "^NEWRELIC_APP_NAME=" ./.env.staging.tmp > ./.env.staging || true
199-
grep -v "^NEWRELIC_MONITOR_MODE=" ./.env.staging.tmp > ./.env.staging || true
200-
grep -v "^ENCRYPTION_SECRET=" ./.env.staging.tmp > ./.env.staging || true
201-
rm -f ./.env.staging.tmp
194+
grep -Ev "^(DB_HOST|REDIS_URL|S3_ATTACHMENTS_BUCKET|AIRBRAKE_ID|AIRBRAKE_KEY|NEWRELIC_LICENSE_KEY|NEWRELIC_APP_NAME|NEWRELIC_MONITOR_MODE|ENCRYPTION_SECRET|CAREERPLUG_WEBHOOK_SECRET|CAREERPLUG_WEBHOOK_URL)=" ./.env.staging > ./.env.staging.tmp || true
195+
mv ./.env.staging.tmp ./.env.staging
202196
fi
203197

204198
# Append the new credentials
@@ -217,6 +211,18 @@ fetch_env_variables() {
217211
echo "✓ ENCRYPTION_SECRET written to .env.staging"
218212
fi
219213

214+
# Add CareerPlug webhook secret if it exists
215+
if [ -n "$CAREERPLUG_WEBHOOK_SECRET" ]; then
216+
echo "CAREERPLUG_WEBHOOK_SECRET=$CAREERPLUG_WEBHOOK_SECRET" >> ./.env.staging
217+
echo "✓ CAREERPLUG_WEBHOOK_SECRET written to .env.staging"
218+
fi
219+
220+
# Add CareerPlug webhook URL if it exists
221+
if [ -n "$CAREERPLUG_WEBHOOK_URL" ]; then
222+
echo "CAREERPLUG_WEBHOOK_URL=$CAREERPLUG_WEBHOOK_URL" >> ./.env.staging
223+
echo "✓ CAREERPLUG_WEBHOOK_URL written to .env.staging"
224+
fi
225+
220226
echo "✓ Environment variables successfully retrieved and written to .env.staging"
221227
}
222228

bin/start_production

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ fetch_allowed_hosts() {
128128
exit 1
129129
fi
130130

131+
# Remove existing ALLOWED_HOSTS line if it exists
132+
if [ -f "./.env.production" ]; then
133+
grep -v "^ALLOWED_HOSTS=" ./.env.production > ./.env.production.tmp || true
134+
mv ./.env.production.tmp ./.env.production
135+
fi
136+
131137
# Write allowed hosts to .env.production file
132138
echo "Writing allowed hosts to .env.production..."
133139
echo "ALLOWED_HOSTS=$ALLOWED_HOSTS" >> ./.env.production
@@ -170,6 +176,8 @@ fetch_env_variables() {
170176
export SECURED_STORAGE_BUCKET=$(echo "$SECRET_JSON" | jq -r '.secured_storage_bucket')
171177
export SECURED_STORAGE_REGION=$(echo "$SECRET_JSON" | jq -r '.secured_storage_region')
172178
export ENCRYPTION_SECRET=$(echo "$SECRET_JSON" | jq -r '.ENCRYPTION_SECRET // empty')
179+
export CAREERPLUG_WEBHOOK_SECRET=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_SECRET // empty')
180+
export CAREERPLUG_WEBHOOK_URL=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_URL // empty')
173181

174182
# Validate that we got the values
175183
if [ "$DB_HOST" = "null" ] || [ "$REDIS_URL" = "null" ] || [ "$S3_ATTACHMENTS_BUCKET" = "null" ] || [ -z "$DB_HOST" ] || [ -z "$REDIS_URL" ] || [ -z "$S3_ATTACHMENTS_BUCKET" ]; then
@@ -193,24 +201,11 @@ fetch_env_variables() {
193201
# Write variables to .env.production file
194202
echo "Writing environment variables to .env.production..."
195203

196-
# Remove existing DB_HOST, REDIS_URL, and S3_ATTACHMENTS_BUCKET lines if they exist
204+
# Remove existing environment variables if they exist
197205
if [ -f "./.env.production" ]; then
198206
echo "Removing existing variables from .env.production"
199-
grep -v "^DB_HOST=" ./.env.production > ./.env.production.tmp || true
200-
grep -v "^REDIS_URL=" ./.env.production.tmp > ./.env.production || true
201-
grep -v "^S3_ATTACHMENTS_BUCKET=" ./.env.production.tmp > ./.env.production || true
202-
grep -v "^AIRBRAKE_ID=" ./.env.production.tmp > ./.env.production || true
203-
grep -v "^AIRBRAKE_KEY=" ./.env.production.tmp > ./.env.production || true
204-
grep -v "^NEWRELIC_LICENSE_KEY=" ./.env.production.tmp > ./.env.production || true
205-
grep -v "^NEWRELIC_APP_NAME=" ./.env.production.tmp > ./.env.production || true
206-
grep -v "^NEWRELIC_MONITOR_MODE=" ./.env.production.tmp > ./.env.production || true
207-
grep -v "^CF_URL=" ./.env.production.tmp > ./.env.production || true
208-
grep -v "^CF_KEY_PAIR_ID=" ./.env.production.tmp > ./.env.production || true
209-
grep -v "^CF_KEY_SECRET=" ./.env.production.tmp > ./.env.production || true
210-
grep -v "^SECURED_STORAGE_BUCKET=" ./.env.production.tmp > ./.env.production || true
211-
grep -v "^SECURED_STORAGE_REGION=" ./.env.production.tmp > ./.env.production || true
212-
grep -v "^ENCRYPTION_SECRET=" ./.env.production.tmp > ./.env.production || true
213-
rm -f ./.env.production.tmp
207+
grep -Ev "^(DB_HOST|REDIS_URL|S3_ATTACHMENTS_BUCKET|AIRBRAKE_ID|AIRBRAKE_KEY|NEWRELIC_LICENSE_KEY|NEWRELIC_APP_NAME|NEWRELIC_MONITOR_MODE|CF_URL|CF_KEY_PAIR_ID|CF_KEY_SECRET|SECURED_STORAGE_BUCKET|SECURED_STORAGE_REGION|ENCRYPTION_SECRET|CAREERPLUG_WEBHOOK_SECRET|CAREERPLUG_WEBHOOK_URL)=" ./.env.production > ./.env.production.tmp || true
208+
mv ./.env.production.tmp ./.env.production
214209
fi
215210

216211
# Append the new credentials
@@ -234,6 +229,18 @@ fetch_env_variables() {
234229
echo "✓ ENCRYPTION_SECRET written to .env.production"
235230
fi
236231

232+
# Add CareerPlug webhook secret if it exists
233+
if [ -n "$CAREERPLUG_WEBHOOK_SECRET" ]; then
234+
echo "CAREERPLUG_WEBHOOK_SECRET=$CAREERPLUG_WEBHOOK_SECRET" >> ./.env.production
235+
echo "✓ CAREERPLUG_WEBHOOK_SECRET written to .env.production"
236+
fi
237+
238+
# Add CareerPlug webhook URL if it exists
239+
if [ -n "$CAREERPLUG_WEBHOOK_URL" ]; then
240+
echo "CAREERPLUG_WEBHOOK_URL=$CAREERPLUG_WEBHOOK_URL" >> ./.env.production
241+
echo "✓ CAREERPLUG_WEBHOOK_URL written to .env.production"
242+
fi
243+
237244
echo "✓ Environment variables successfully retrieved and written to .env.production"
238245
}
239246

bin/start_staging

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ fetch_env_variables() {
174174
export SECURED_STORAGE_BUCKET=$(echo "$SECRET_JSON" | jq -r '.secured_storage_bucket')
175175
export SECURED_STORAGE_REGION=$(echo "$SECRET_JSON" | jq -r '.secured_storage_region')
176176
export ENCRYPTION_SECRET=$(echo "$SECRET_JSON" | jq -r '.ENCRYPTION_SECRET // empty')
177+
export CAREERPLUG_WEBHOOK_SECRET=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_SECRET // empty')
178+
export CAREERPLUG_WEBHOOK_URL=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_URL // empty')
177179

178180

179181
# Validate that we got the values
@@ -198,24 +200,11 @@ fetch_env_variables() {
198200
# Write variables to .env.staging file
199201
echo "Writing environment variables to .env.staging..."
200202

201-
# Remove existing DB_HOST, REDIS_URL, and S3_ATTACHMENTS_BUCKET lines if they exist
203+
# Remove existing environment variables if they exist
202204
if [ -f "./.env.staging" ]; then
203205
echo "Removing existing variables from .env.staging"
204-
grep -v "^DB_HOST=" ./.env.staging > ./.env.staging.tmp || true
205-
grep -v "^REDIS_URL=" ./.env.staging.tmp > ./.env.staging || true
206-
grep -v "^S3_ATTACHMENTS_BUCKET=" ./.env.staging.tmp > ./.env.staging || true
207-
grep -v "^AIRBRAKE_ID=" ./.env.staging.tmp > ./.env.staging || true
208-
grep -v "^AIRBRAKE_KEY=" ./.env.staging.tmp > ./.env.staging || true
209-
grep -v "^NEWRELIC_LICENSE_KEY=" ./.env.staging.tmp > ./.env.staging || true
210-
grep -v "^NEWRELIC_APP_NAME=" ./.env.staging.tmp > ./.env.staging || true
211-
grep -v "^NEWRELIC_MONITOR_MODE=" ./.env.staging.tmp > ./.env.staging || true
212-
grep -v "^CF_URL=" ./.env.staging.tmp > ./.env.staging || true
213-
grep -v "^CF_KEY_PAIR_ID=" ./.env.staging.tmp > ./.env.staging || true
214-
grep -v "^CF_KEY_SECRET=" ./.env.staging.tmp > ./.env.staging || true
215-
grep -v "^SECURED_STORAGE_BUCKET=" ./.env.staging.tmp > ./.env.staging || true
216-
grep -v "^SECURED_STORAGE_REGION=" ./.env.staging.tmp > ./.env.staging || true
217-
grep -v "^ENCRYPTION_SECRET=" ./.env.staging.tmp > ./.env.staging || true
218-
rm -f ./.env.staging.tmp
206+
grep -Ev "^(DB_HOST|REDIS_URL|S3_ATTACHMENTS_BUCKET|AIRBRAKE_ID|AIRBRAKE_KEY|NEWRELIC_LICENSE_KEY|NEWRELIC_APP_NAME|NEWRELIC_MONITOR_MODE|CF_URL|CF_KEY_PAIR_ID|CF_KEY_SECRET|SECURED_STORAGE_BUCKET|SECURED_STORAGE_REGION|ENCRYPTION_SECRET|CAREERPLUG_WEBHOOK_SECRET|CAREERPLUG_WEBHOOK_URL)=" ./.env.staging > ./.env.staging.tmp || true
207+
mv ./.env.staging.tmp ./.env.staging
219208
fi
220209

221210
# Append the new credentials
@@ -239,6 +228,18 @@ fetch_env_variables() {
239228
echo "✓ ENCRYPTION_SECRET written to .env.staging"
240229
fi
241230

231+
# Add CareerPlug webhook secret if it exists
232+
if [ -n "$CAREERPLUG_WEBHOOK_SECRET" ]; then
233+
echo "CAREERPLUG_WEBHOOK_SECRET=$CAREERPLUG_WEBHOOK_SECRET" >> ./.env.staging
234+
echo "✓ CAREERPLUG_WEBHOOK_SECRET written to .env.staging"
235+
fi
236+
237+
# Add CareerPlug webhook URL if it exists
238+
if [ -n "$CAREERPLUG_WEBHOOK_URL" ]; then
239+
echo "CAREERPLUG_WEBHOOK_URL=$CAREERPLUG_WEBHOOK_URL" >> ./.env.staging
240+
echo "✓ CAREERPLUG_WEBHOOK_URL written to .env.staging"
241+
fi
242+
242243
echo "✓ Environment variables successfully retrieved and written to .env.staging"
243244
}
244245

config/dotenv.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# frozen_string_literal: true
22

3+
if ENV['RAILS_ENV'] == 'development'
4+
require 'dotenv'
5+
Dotenv.load('.env')
6+
end
7+
38
if ENV['RAILS_ENV'] == 'production' || ENV['RAILS_ENV'] == 'staging'
49
if !ENV['AWS_SECRET_MANAGER_ID'].to_s.empty?
510
require 'aws-sdk-secretsmanager'

lib/tasks/webhooks.rake

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
namespace :webhooks do
4+
desc 'Configure CareerPlug webhook secret from CAREERPLUG_WEBHOOK_SECRET env var'
5+
task configure_careerplug: :environment do
6+
secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET') do
7+
if Rails.env.development?
8+
'development_webhook_secret'
9+
else
10+
abort 'CAREERPLUG_WEBHOOK_SECRET environment variable is required'
11+
end
12+
end
13+
14+
webhook_urls = WebhookUrl.where('url LIKE ? OR url LIKE ? OR url LIKE ?',
15+
'%careerplug%', '%cpats%', '%localhost:3000%')
16+
17+
if webhook_urls.any?
18+
webhook_urls.find_each do |webhook_url|
19+
webhook_url.update!(secret: { 'X-CareerPlug-Secret' => secret })
20+
puts "Updated webhook secret for #{webhook_url.url}"
21+
end
22+
puts "Updated #{webhook_urls.count} webhook URL(s)"
23+
else
24+
puts 'No CareerPlug webhook URLs found. Available webhooks:'
25+
WebhookUrl.find_each { |w| puts " - #{w.id}: #{w.url}" }
26+
end
27+
end
28+
29+
desc 'Set up development webhook URLs for all accounts (creates URLs + configures secret)'
30+
task setup_development: :environment do
31+
abort 'This task is only for development' unless Rails.env.development?
32+
33+
url = 'http://localhost:3000/api/docuseal/events'
34+
secret = { 'X-CareerPlug-Secret' => 'development_webhook_secret' }
35+
events = %w[form.viewed form.started form.completed form.declined]
36+
37+
created = 0
38+
updated = 0
39+
40+
Account.find_each do |account|
41+
webhook_url = WebhookUrl.find_or_initialize_by(account: account, sha1: Digest::SHA1.hexdigest(url))
42+
43+
if webhook_url.new_record?
44+
webhook_url.assign_attributes(url: url, events: events, secret: secret)
45+
webhook_url.save!
46+
created += 1
47+
puts "Created webhook URL for account #{account.id}: #{account.name}"
48+
elsif webhook_url.secret != secret
49+
webhook_url.update!(secret: secret)
50+
updated += 1
51+
puts "Updated webhook secret for account #{account.id}: #{account.name}"
52+
end
53+
end
54+
55+
puts "Done: #{created} created, #{updated} updated"
56+
end
57+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe Account, '#create_careerplug_webhook' do
6+
around do |example|
7+
original_secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET', nil)
8+
original_url = ENV.fetch('CAREERPLUG_WEBHOOK_URL', nil)
9+
10+
# Set required env vars for webhook creation
11+
ENV['CAREERPLUG_WEBHOOK_SECRET'] = 'test_secret'
12+
ENV['CAREERPLUG_WEBHOOK_URL'] = 'http://example.com/webhook'
13+
14+
example.run
15+
16+
# Restore original env vars
17+
ENV['CAREERPLUG_WEBHOOK_SECRET'] = original_secret
18+
ENV['CAREERPLUG_WEBHOOK_URL'] = original_url
19+
end
20+
21+
describe 'CareerPlug webhook creation' do
22+
it 'creates webhook after successful account creation' do
23+
account = build(:account)
24+
expect(account.webhook_urls).to be_empty
25+
26+
account.save!
27+
28+
expect(account.webhook_urls.count).to eq(1)
29+
webhook = account.webhook_urls.first
30+
expect(webhook.url).to eq('http://example.com/webhook')
31+
expect(webhook.events).to eq(['form.viewed', 'form.started', 'form.completed', 'form.declined'])
32+
expect(webhook.secret).to eq({ 'X-CareerPlug-Secret' => 'test_secret' })
33+
end
34+
35+
it 'does not create webhook if account creation fails' do
36+
# This test verifies that after_commit behavior works correctly
37+
# by simulating a transaction rollback
38+
39+
expect do
40+
described_class.transaction do
41+
create(:account)
42+
# Simulate some error that would cause rollback
43+
raise ActiveRecord::Rollback
44+
end
45+
end.not_to change(described_class, :count)
46+
47+
expect do
48+
described_class.transaction do
49+
create(:account)
50+
raise ActiveRecord::Rollback
51+
end
52+
end.not_to change(WebhookUrl, :count)
53+
end
54+
55+
it 'does not create webhook when CAREERPLUG_WEBHOOK_SECRET is blank' do
56+
original_secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET', nil)
57+
ENV['CAREERPLUG_WEBHOOK_SECRET'] = ''
58+
59+
account = create(:account)
60+
expect(account.webhook_urls.count).to eq(0)
61+
62+
ENV['CAREERPLUG_WEBHOOK_SECRET'] = original_secret
63+
end
64+
end
65+
end

0 commit comments

Comments
 (0)