2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-01-23 11:53:11 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Setting, type: :model do
|
|
|
|
subject(:setting) { create(:setting) }
|
|
|
|
|
|
|
|
describe '.get' do
|
|
|
|
context 'when given a valid Setting#name' do
|
|
|
|
it 'returns #state_current[:value]' do
|
|
|
|
expect { setting.update(state_current: { value: 'foo' }) }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to change { described_class.get(setting.name) }.to('foo')
|
2019-01-23 11:53:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.set' do
|
|
|
|
context 'when given a valid Setting#name' do
|
|
|
|
it 'sets #state_current = { value: <arg> }' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.set(setting.name, 'foo') }
|
2019-01-23 11:53:11 +00:00
|
|
|
.to change { setting.reload.state_current }.to({ 'value' => 'foo' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when #preferences hash includes a :cache key' do
|
|
|
|
subject(:setting) { create(:setting, preferences: { cache: ['foo'] }) }
|
2019-04-15 01:41:17 +00:00
|
|
|
|
2019-01-23 11:53:11 +00:00
|
|
|
before { Cache.write('foo', 'bar') }
|
|
|
|
|
|
|
|
it 'resets the cache key' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.set(setting.name, 'baz') }
|
2021-05-31 13:05:54 +00:00
|
|
|
.to change { Cache.read('foo') }.to(nil)
|
2019-01-23 11:53:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.reset' do
|
|
|
|
context 'when given a valid Setting#name' do
|
|
|
|
it 'sets #state_current = { value: <orig> } (via #state_initial[:value])' do
|
|
|
|
setting.update(state_initial: { value: 'foo' })
|
2019-09-16 15:04:17 +00:00
|
|
|
described_class.set(setting.name, 'bar')
|
2019-01-23 11:53:11 +00:00
|
|
|
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.reset(setting.name) }
|
2019-01-23 11:53:11 +00:00
|
|
|
.to change { setting.reload.state_current }.to({ value: 'foo' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'attributes' do
|
|
|
|
describe '#state_initial' do
|
|
|
|
subject(:setting) { build(:setting, state: 'foo') }
|
|
|
|
|
|
|
|
it 'is set on creation, based on #state' do
|
|
|
|
expect { setting.save }
|
2019-04-15 01:41:17 +00:00
|
|
|
.to change(setting, :state_initial).from({}).to({ value: 'foo' })
|
2019-01-23 11:53:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-13 12:31:49 +00:00
|
|
|
|
|
|
|
describe 'check_broadcast' do
|
|
|
|
context 'when setting is non-frontend' do
|
|
|
|
subject(:setting) { build(:setting, name: 'broadcast_test', state: 'foo', frontend: false) }
|
|
|
|
|
|
|
|
it 'does not broadcast' do
|
|
|
|
allow(Sessions).to receive(:broadcast)
|
|
|
|
setting.save
|
|
|
|
expect(Sessions).not_to have_received(:broadcast)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when setting is public' do
|
|
|
|
subject(:setting) { build(:setting, name: 'broadcast_test', state: 'foo', frontend: true) }
|
|
|
|
|
|
|
|
it 'broadcasts to public' do
|
|
|
|
allow(Sessions).to receive(:broadcast)
|
|
|
|
setting.save
|
|
|
|
expect(Sessions).to have_received(:broadcast).with({ data: { name: 'broadcast_test', value: 'foo' }, event: 'config_update' }, 'public')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when setting requires authentication' do
|
|
|
|
subject(:setting) { build(:setting, name: 'broadcast_test', state: 'foo', frontend: true, preferences: { authentication: true }) }
|
|
|
|
|
|
|
|
it 'broadcasts to authenticated only' do
|
|
|
|
allow(Sessions).to receive(:broadcast)
|
|
|
|
setting.save
|
|
|
|
expect(Sessions).to have_received(:broadcast).with({ data: { name: 'broadcast_test', value: 'foo' }, event: 'config_update' }, 'authenticated')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-23 11:53:11 +00:00
|
|
|
end
|