setting_spec.rb 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 interpolated value was set and cache is still valid' do
  13. it 'stores interpolated value' do
  14. create(:setting, name: 'broadcast_test', state: 'test')
  15. described_class.send(:load) # prewarm cache
  16. described_class.set('broadcast_test', 'test #{config.fqdn}') # rubocop:disable Lint/InterpolationCheck
  17. expect(described_class.get('broadcast_test'))
  18. .to eq("test #{described_class.get('fqdn')}")
  19. end
  20. end
  21. end
  22. describe '.set' do
  23. context 'when given a valid Setting#name' do
  24. it 'sets #state_current = { value: <arg> }' do
  25. expect { described_class.set(setting.name, 'foo') }
  26. .to change { setting.reload.state_current }.to({ 'value' => 'foo' })
  27. end
  28. it 'logs the value' do
  29. allow(described_class.logger).to receive(:info)
  30. described_class.set(setting.name, 'foo')
  31. expect(described_class.logger).to have_received(:info).with("Setting.set('#{setting.name}', \"foo\")")
  32. end
  33. end
  34. context 'when given a sensitive Setting#name' do
  35. subject(:setting) { create(:setting, name: 'my_token') }
  36. it 'masks the value' do
  37. allow(described_class.logger).to receive(:info)
  38. described_class.set(setting.name, 'foo')
  39. expect(described_class.logger).to have_received(:info).with("Setting.set('#{setting.name}', \"[FILTERED]\")")
  40. end
  41. end
  42. context 'when #preferences hash includes a :cache key' do
  43. subject(:setting) { create(:setting, preferences: { cache: ['foo'] }) }
  44. before { Rails.cache.write('foo', 'bar') }
  45. it 'resets the cache key' do
  46. expect { described_class.set(setting.name, 'baz') }
  47. .to change { Rails.cache.read('foo') }.to(nil)
  48. end
  49. end
  50. end
  51. describe '.reset' do
  52. context 'when given a valid Setting#name' do
  53. it 'sets #state_current = { value: <orig> } (via #state_initial[:value])' do
  54. setting.update(state_initial: { value: 'foo' })
  55. described_class.set(setting.name, 'bar')
  56. expect { described_class.reset(setting.name) }
  57. .to change { setting.reload.state_current }.to({ value: 'foo' })
  58. end
  59. it 'logs the value' do
  60. setting.update(state_initial: { value: 'foo' })
  61. allow(described_class.logger).to receive(:info)
  62. described_class.reset(setting.name)
  63. expect(described_class.logger).to have_received(:info).with("Setting.reset('#{setting.name}', {\"value\"=>\"foo\"})")
  64. end
  65. end
  66. context 'when given a sensitive Setting#name' do
  67. subject(:setting) { create(:setting, name: 'my_token') }
  68. it 'masks the value' do
  69. setting.update(state_initial: { value: 'foo' })
  70. allow(described_class.logger).to receive(:info)
  71. described_class.reset(setting.name)
  72. expect(described_class.logger).to have_received(:info).with("Setting.reset('#{setting.name}', \"[FILTERED]\")")
  73. end
  74. end
  75. end
  76. describe '.cache_valid?' do
  77. context 'when loading first time' do
  78. before do
  79. # ensure no cache checks are set
  80. described_class.class_variable_set(:@@lookup_at, nil) # rubocop:disable Style/ClassVars
  81. described_class.class_variable_set(:@@query_cache_key, nil) # rubocop:disable Style/ClassVars
  82. end
  83. it 'cache is not valid' do
  84. expect(described_class).not_to be_cache_valid
  85. end
  86. end
  87. context 'when cache is valid' do
  88. before do
  89. # ensure cache is warm
  90. described_class.send(:load)
  91. # ensure cache is not touched by broadcasting the new value
  92. allow_any_instance_of(described_class).to receive(:broadcast_frontend)
  93. end
  94. it 'cache is valid' do
  95. expect(described_class).to be_cache_valid
  96. end
  97. it 'cache is still valid after some time' do
  98. travel 1.minute
  99. expect(described_class).to be_cache_valid
  100. end
  101. context 'when Setting is updated in the same process' do
  102. before { described_class.set('maintenance_login', 'sample message') }
  103. it 'cache is not valid' do
  104. expect(described_class).not_to be_cache_valid
  105. end
  106. end
  107. context 'when Setting updated outside of the process and class variables were not touched' do
  108. before { described_class.find_by(name: 'maintenance_login').touch }
  109. it 'cache is seen as valid' do
  110. expect(described_class).to be_cache_valid
  111. end
  112. it 'cache is seen as invalid after some time' do
  113. travel 1.minute
  114. expect(described_class).not_to be_cache_valid
  115. end
  116. end
  117. end
  118. end
  119. describe 'attributes' do
  120. describe '#state_initial' do
  121. subject(:setting) { build(:setting, state: 'foo') }
  122. it 'is set on creation, based on #state' do
  123. expect { setting.save }
  124. .to change(setting, :state_initial).from({}).to({ value: 'foo' })
  125. end
  126. end
  127. end
  128. describe 'broadcast_frontend' do
  129. subject(:setting) do
  130. build(:setting, name: 'broadcast_test', state: value, frontend: frontend)
  131. .tap { |setting| setting.preferences = { authentication: true } if authentication_required }
  132. end
  133. let(:value) { 'foo' }
  134. let(:frontend) { true }
  135. let(:authentication_required) { false }
  136. context 'when setting is non-frontend' do
  137. let(:frontend) { false }
  138. it 'does not broadcast' do
  139. allow(Sessions).to receive(:broadcast)
  140. setting.save
  141. expect(Sessions).not_to have_received(:broadcast)
  142. end
  143. it 'does not trigger subscription' do
  144. allow(Gql::Subscriptions::ConfigUpdates).to receive(:trigger)
  145. setting.save
  146. expect(Gql::Subscriptions::ConfigUpdates).not_to have_received(:trigger).with(setting)
  147. end
  148. end
  149. context 'when setting is public' do
  150. it 'broadcasts to public' do
  151. allow(Sessions).to receive(:broadcast)
  152. setting.save
  153. expect(Sessions).to have_received(:broadcast)
  154. .with({ data: { name: 'broadcast_test', value: 'foo' }, event: 'config_update' }, 'public')
  155. end
  156. it 'triggers subscription' do
  157. allow(Gql::Subscriptions::ConfigUpdates).to receive(:trigger)
  158. setting.save
  159. expect(Gql::Subscriptions::ConfigUpdates).to have_received(:trigger).with(setting)
  160. end
  161. end
  162. context 'when setting requires authentication' do
  163. let(:authentication_required) { true }
  164. it 'broadcasts to authenticated only' do
  165. allow(Sessions).to receive(:broadcast)
  166. setting.save
  167. expect(Sessions).to have_received(:broadcast)
  168. .with({ data: { name: 'broadcast_test', value: 'foo' }, event: 'config_update' }, 'authenticated')
  169. end
  170. it 'triggers subscription' do
  171. allow(Gql::Subscriptions::ConfigUpdates).to receive(:trigger)
  172. setting.save
  173. expect(Gql::Subscriptions::ConfigUpdates).to have_received(:trigger).with(setting)
  174. end
  175. end
  176. context 'when setting uses interpolation' do
  177. let(:value) { 'test #{config.fqdn}' } # rubocop:disable Lint/InterpolationCheck
  178. it 'broadcasts to authenticated only' do
  179. allow(Sessions).to receive(:broadcast)
  180. setting.save
  181. expect(Sessions)
  182. .to have_received(:broadcast)
  183. .with(
  184. { data: { name: 'broadcast_test', value: "test #{described_class.get('fqdn')}" }, event: 'config_update' },
  185. 'public'
  186. )
  187. end
  188. it 'triggers subscription' do
  189. allow(Gql::Subscriptions::ConfigUpdates).to receive(:trigger)
  190. setting.save
  191. expect(Gql::Subscriptions::ConfigUpdates).to have_received(:trigger).with(setting)
  192. end
  193. end
  194. end
  195. end