setting_spec.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Setting, type: :model do
  4. subject(:setting) { create(:setting) }
  5. describe '.get' do
  6. context 'when given a valid Setting#name' do
  7. it 'returns #state_current[:value]' do
  8. expect { setting.update(state_current: { value: 'foo' }) }
  9. .to change { described_class.get(setting.name) }.to('foo')
  10. end
  11. end
  12. end
  13. describe '.set' do
  14. context 'when given a valid Setting#name' do
  15. it 'sets #state_current = { value: <arg> }' do
  16. expect { described_class.set(setting.name, 'foo') }
  17. .to change { setting.reload.state_current }.to({ 'value' => 'foo' })
  18. end
  19. end
  20. context 'when #preferences hash includes a :cache key' do
  21. subject(:setting) { create(:setting, preferences: { cache: ['foo'] }) }
  22. before { Cache.write('foo', 'bar') }
  23. it 'resets the cache key' do
  24. expect { described_class.set(setting.name, 'baz') }
  25. .to change { Cache.read('foo') }.to(nil)
  26. end
  27. end
  28. end
  29. describe '.reset' do
  30. context 'when given a valid Setting#name' do
  31. it 'sets #state_current = { value: <orig> } (via #state_initial[:value])' do
  32. setting.update(state_initial: { value: 'foo' })
  33. described_class.set(setting.name, 'bar')
  34. expect { described_class.reset(setting.name) }
  35. .to change { setting.reload.state_current }.to({ value: 'foo' })
  36. end
  37. end
  38. end
  39. describe 'attributes' do
  40. describe '#state_initial' do
  41. subject(:setting) { build(:setting, state: 'foo') }
  42. it 'is set on creation, based on #state' do
  43. expect { setting.save }
  44. .to change(setting, :state_initial).from({}).to({ value: 'foo' })
  45. end
  46. end
  47. end
  48. describe 'check_broadcast' do
  49. context 'when setting is non-frontend' do
  50. subject(:setting) { build(:setting, name: 'broadcast_test', state: 'foo', frontend: false) }
  51. it 'does not broadcast' do
  52. allow(Sessions).to receive(:broadcast)
  53. setting.save
  54. expect(Sessions).not_to have_received(:broadcast)
  55. end
  56. end
  57. context 'when setting is public' do
  58. subject(:setting) { build(:setting, name: 'broadcast_test', state: 'foo', frontend: true) }
  59. it 'broadcasts to public' do
  60. allow(Sessions).to receive(:broadcast)
  61. setting.save
  62. expect(Sessions).to have_received(:broadcast).with({ data: { name: 'broadcast_test', value: 'foo' }, event: 'config_update' }, 'public')
  63. end
  64. end
  65. context 'when setting requires authentication' do
  66. subject(:setting) { build(:setting, name: 'broadcast_test', state: 'foo', frontend: true, preferences: { authentication: true }) }
  67. it 'broadcasts to authenticated only' do
  68. allow(Sessions).to receive(:broadcast)
  69. setting.save
  70. expect(Sessions).to have_received(:broadcast).with({ data: { name: 'broadcast_test', value: 'foo' }, event: 'config_update' }, 'authenticated')
  71. end
  72. end
  73. end
  74. end