mirror of https://github.com/tootsuite/mastodon
Add coverage for secret generation
parent
da7997943c
commit
09a85b5c54
|
@ -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
|
||||
|
|
|
@ -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) }
|
||||
|
||||
|
|
Loading…
Reference in New Issue