1280
 ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require 'models/concerns/has_audit_logs_examples'

RSpec.describe Setting, type: :model do
  subject(:setting) { create(:setting) }

  it_behaves_like 'HasAuditLogs', update_attribute: 'title', update_value: 'Some updated title'

  describe 'audit log masking of sensitive settings' do
    let(:audit_log) { AuditLog.find_by(auditable_type: 'Setting', auditable_id: setting.id, action_type: 'update') }

    before do
      described_class.set('system_init_done', true)
    end

    context 'when the setting name is sensitive' do
      subject(:setting) { create(:setting, name: 'some_secret_setting', state_current: { value: 'initial value' }) }

      it 'masks the values in the snapshots' do
        setting.update!(state_current: { value: 'updated value' })

        expect(audit_log).to have_attributes(
          value_from: include('state_current' => include('value' => SensitiveParamsHelper::SENSITIVE_MASK)),
          value_to:   include('state_current' => include('value' => SensitiveParamsHelper::SENSITIVE_MASK)),
        )
      end
    end

    context 'when the setting name is not sensitive' do
      subject(:setting) { create(:setting, name: 'some_regular_setting', state_current: { value: 'initial value' }) }

      it 'keeps the values in the snapshots' do
        setting.update!(state_current: { value: 'updated value' })

        expect(audit_log).to have_attributes(
          value_from: include('state_current' => include('value' => 'initial value')),
          value_to:   include('state_current' => include('value' => 'updated value')),
        )
      end
    end
  end

  describe '.get' do
    context 'when given a valid Setting#name' do
      it 'returns #state_current[:value]' do
        expect { setting.update(state_current: { value: 'foo' }) }
          .to change { described_class.get(setting.name) }.to('foo')
      end
    end

    context 'when interpolated value was set and cache is still valid' do
      it 'stores interpolated value' do
        create(:setting, name: 'broadcast_test', state: 'test')
        described_class.send(:load) # prewarm cache

        described_class.set('broadcast_test', 'test #{config.fqdn}') # rubocop:disable Lint/InterpolationCheck

        expect(described_class.get('broadcast_test'))
          .to eq("test #{described_class.get('fqdn')}")
      end
    end

    context 'when the value refers to a setting that is not sent to the frontend' do
      it 'drops the reference' do
        create(:setting, name: 'broadcast_test', state: 'test')
        described_class.set('broadcast_test', 'test #{config.application_secret}') # rubocop:disable Lint/InterpolationCheck

        expect(described_class.get('broadcast_test')).to eq('test ')
      end

      it 'drops the reference in the notification sender' do
        described_class.set('notification_sender', '#{config.application_secret} <noreply@example.com>') # rubocop:disable Lint/InterpolationCheck

        expect(described_class.get('notification_sender')).to eq(' <noreply@example.com>')
      end
    end

    context 'when the value refers to a setting templates may read although it is not sent to the frontend' do
      it 'takes it in' do
        described_class.set('ticket_hook_divider', '-')
        create(:setting, name: 'broadcast_test', state: 'test')
        described_class.set('broadcast_test', 'a#{config.ticket_hook_divider}b') # rubocop:disable Lint/InterpolationCheck

        expect(described_class.get('broadcast_test')).to eq('a-b')
      end
    end

    context 'when a setting sent to anonymous clients refers to a setting sent to logged-in users only' do
      it 'drops that reference and keeps a public one' do
        create(:setting, name: 'broadcast_test', frontend: true, state: 'test')
        described_class.set('broadcast_test', 'ID #{config.system_id} at #{config.fqdn}') # rubocop:disable Lint/InterpolationCheck

        expect(described_class.get('broadcast_test')).to eq("ID  at #{described_cla