Add coverage for secret generation

pull/32516/head
Matt Jankowski 2024-10-12 17:21:46 -04:00
parent da7997943c
commit 09a85b5c54
2 changed files with 24 additions and 2 deletions

View File

@ -38,7 +38,7 @@ class Webhook < ApplicationRecord
validate :validate_template
normalizes :events, with: ->(events) { events.filter_map { |event| event.strip.presence } }
before_validation :generate_secret
before_validation :generate_secret, unless: :secret?
def rotate_secret!
update!(secret: SecureRandom.hex(20))
@ -99,6 +99,6 @@ class Webhook < ApplicationRecord
end
def generate_secret
self.secret = SecureRandom.hex(20) if secret.blank?
self.secret = SecureRandom.hex(20)
end
end

View File

@ -35,6 +35,28 @@ RSpec.describe Webhook do
end
end
describe 'Callbacks' do
describe 'Generating a secret' do
context 'when secret exists already' do
subject { described_class.new(secret: 'secret') }
it 'does not override' do
expect { subject.valid? }
.to_not change(subject, :secret)
end
end
context 'when secret does not exist' do
subject { described_class.new(secret: nil) }
it 'does not override' do
expect { subject.valid? }
.to change(subject, :secret)
end
end
end
end
describe '.permission_for_event' do
subject { described_class.permission_for_event(event) }