setting_spec.rb 3.2 KB

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