2023-02-22 00:55:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-06-09 19:57:36 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 03:49:53 +00:00
|
|
|
RSpec.describe Webhook do
|
2022-06-09 19:57:36 +00:00
|
|
|
let(:webhook) { Fabricate(:webhook) }
|
|
|
|
|
2023-11-13 22:47:44 +00:00
|
|
|
describe 'Validations' do
|
2024-11-25 08:19:16 +00:00
|
|
|
subject { Fabricate.build :webhook }
|
|
|
|
|
2024-09-05 15:36:05 +00:00
|
|
|
it { is_expected.to validate_presence_of(:events) }
|
2023-11-13 22:47:44 +00:00
|
|
|
|
2024-11-25 08:19:16 +00:00
|
|
|
it { is_expected.to_not allow_values([], %w(account.invalid)).for(:events) }
|
2023-11-13 22:47:44 +00:00
|
|
|
|
2024-11-25 08:19:16 +00:00
|
|
|
it { is_expected.to_not allow_values('{{account }').for(:template) }
|
|
|
|
|
|
|
|
context 'when current_account is assigned' do
|
|
|
|
subject { Fabricate.build :webhook, current_account: account }
|
2023-11-13 22:47:44 +00:00
|
|
|
|
2024-11-25 08:19:16 +00:00
|
|
|
context 'with account that has permissions' do
|
|
|
|
let(:account) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
|
2023-11-13 22:47:44 +00:00
|
|
|
|
2024-11-25 08:19:16 +00:00
|
|
|
it { is_expected.to allow_values(%w(account.created)).for(:events) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with account lacking permissions' do
|
|
|
|
let(:account) { Fabricate :account }
|
|
|
|
|
|
|
|
it { is_expected.to_not allow_values(%w(account.created)).for(:events) }
|
|
|
|
end
|
2023-11-13 22:47:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Normalizations' do
|
2024-09-04 05:12:40 +00:00
|
|
|
describe 'events' do
|
|
|
|
it { is_expected.to normalize(:events).from(['account.approved', 'account.created ', '']).to(%w(account.approved account.created)) }
|
2023-11-13 22:47:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-25 08:19:16 +00:00
|
|
|
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) }
|
|
|
|
|
|
|
|
context 'with a nil value' do
|
|
|
|
let(:event) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an account approved event' do
|
|
|
|
let(:event) { 'account.approved' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(:manage_users) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an account created event' do
|
|
|
|
let(:event) { 'account.created' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(:manage_users) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an account updated event' do
|
|
|
|
let(:event) { 'account.updated' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(:manage_users) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an report created event' do
|
|
|
|
let(:event) { 'report.created' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(:manage_reports) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an report updated event' do
|
|
|
|
let(:event) { 'report.updated' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(:manage_reports) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an status created event' do
|
|
|
|
let(:event) { 'status.created' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(:view_devops) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an status updated event' do
|
|
|
|
let(:event) { 'status.updated' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(:view_devops) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#required_permissions' do
|
|
|
|
subject { described_class.new(events:).required_permissions }
|
|
|
|
|
|
|
|
context 'with empty events' do
|
|
|
|
let(:events) { [] }
|
|
|
|
|
|
|
|
it { is_expected.to eq([]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple event types' do
|
|
|
|
let(:events) { %w(account.created account.updated status.created) }
|
|
|
|
|
|
|
|
it { is_expected.to eq %i(manage_users view_devops) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-09 19:57:36 +00:00
|
|
|
describe '#rotate_secret!' do
|
|
|
|
it 'changes the secret' do
|
2024-09-03 15:30:13 +00:00
|
|
|
expect { webhook.rotate_secret! }
|
|
|
|
.to change(webhook, :secret)
|
|
|
|
expect(webhook.secret)
|
|
|
|
.to_not be_blank
|
2022-06-09 19:57:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#enable!' do
|
2024-09-03 15:30:13 +00:00
|
|
|
let(:webhook) { Fabricate(:webhook, enabled: false) }
|
2022-06-09 19:57:36 +00:00
|
|
|
|
|
|
|
it 'enables the webhook' do
|
2024-09-03 15:30:13 +00:00
|
|
|
expect { webhook.enable! }
|
|
|
|
.to change(webhook, :enabled?).to(true)
|
2022-06-09 19:57:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#disable!' do
|
2024-09-03 15:30:13 +00:00
|
|
|
let(:webhook) { Fabricate(:webhook, enabled: true) }
|
|
|
|
|
2022-06-09 19:57:36 +00:00
|
|
|
it 'disables the webhook' do
|
2024-09-03 15:30:13 +00:00
|
|
|
expect { webhook.disable! }
|
|
|
|
.to change(webhook, :enabled?).to(false)
|
2022-06-09 19:57:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|