mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-22 14:15:22 +00:00
Add coverage for secret generation
This commit is contained in:
parent
da7997943c
commit
09a85b5c54
|
@ -38,7 +38,7 @@ class Webhook < ApplicationRecord
|
||||||
validate :validate_template
|
validate :validate_template
|
||||||
|
|
||||||
normalizes :events, with: ->(events) { events.filter_map { |event| event.strip.presence } }
|
normalizes :events, with: ->(events) { events.filter_map { |event| event.strip.presence } }
|
||||||
before_validation :generate_secret
|
before_validation :generate_secret, unless: :secret?
|
||||||
|
|
||||||
def rotate_secret!
|
def rotate_secret!
|
||||||
update!(secret: SecureRandom.hex(20))
|
update!(secret: SecureRandom.hex(20))
|
||||||
|
@ -99,6 +99,6 @@ class Webhook < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_secret
|
def generate_secret
|
||||||
self.secret = SecureRandom.hex(20) if secret.blank?
|
self.secret = SecureRandom.hex(20)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,6 +35,28 @@ RSpec.describe Webhook do
|
||||||
end
|
end
|
||||||
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
|
describe '.permission_for_event' do
|
||||||
subject { described_class.permission_for_event(event) }
|
subject { described_class.permission_for_event(event) }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue