1
  V   gˆl–ßi~”Šj"Tâ‚Æ[¸ØÅ£�þBIYÆäd2†2ºÙþR„�ÒJ�ƒu—�x™¤~VÅ€-¶Ø€ }¨j‰E²u±ß=#5HÄÃø Ô    g# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

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

RSpec.describe Channel, type: :model do
  it_behaves_like 'HasAuditLogs', update_attribute: 'active', update_value: false, name_attribute: 'area'

  describe 'sensitive values masking' do
    subject(:channel) do
      create(:channel, area: 'Facebook::Account', options: {
               adapter: 'facebook',
               auth:    { access_token: 'user-token' },
               pages:   [
                 { id: '1', name: 'Page 1', access_token: 'page-1-token' },
                 { id: '2', name: 'Page 2', access_token: 'page-2-token' },
               ],
             })
    end

    let(:masked_pages) do
      [
        { 'id' => '1', 'name' => 'Page 1', 'access_token' => SensitiveParamsHelper::SENSITIVE_MASK },
        { 'id' => '2', 'name' => 'Page 2', 'access_token' => SensitiveParamsHelper::SENSITIVE_MASK },
      ]
    end

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

    it 'masks the Facebook page access tokens in audit log snapshots' do
      expect(AuditLog.find_by(auditable: channel, action_type: 'create').value_to.dig('options', 'pages'))
        .to eq(masked_pages)
    end

    it 'masks the Facebook page access tokens in both snapshots of an update' do
      channel.update!(options: channel.options.merge('sync' => { 'pages' => {} }))

      expect(AuditLog.find_by(auditable: channel, action_type: 'update')).to have_attributes(
        value_from: include('options' => include('pages' => masked_pages)),
        value_to:   include('options' => include('pages' => masked_pages)),
      )
    end

    it 'masks the Facebook page access tokens in assets' do
      expect(channel.assets({}).dig(:Channel, channel.id, 'options', 'pages')).to eq(masked_pages)
    end

    it 'does not modify the channel options' do
      expect { channel.assets({}) }.not_to change { channel.options.to_json }
    end
  end

  describe '.fetch' do

    describe '#refresh_xoauth2! fails' do

      let(:channel) { create(:channel, area: 'SomeXOAUTH2::Account', options: { adapter: 'DummyXOAUTH2', auth: { type: 'XOAUTH2' } }) }

      before do
        allow(ExternalCredential).to receive(:refresh_token).and_raise(RuntimeError)
      end

      it 'changes Channel status to error' do
        expect { described_class.fetch }.to change { channel.reload.status_in }.to('error')
      end
    end

    context 'when one adapter fetch fails' do

      let(:failing_adapter_class) do
        Class.new(Channel::Driver::Null) do
          def fetchable?(...)
            true
          end

          def fetch(...)
            raise 'some error'
          end
        end
      end

      let(:dummy_adapter_class) do
        Class.new(Channel::Driver::Null) do
          def fetchable?(...)
            true
          end
        end
      end

      let(:failing_channel) do
        create(:email_channel, inbound: {
                 adapter: 'failing',
                 options: {}
               })
      end

      let(:other_channel) do
        create(:email_channel, inbound: {
                 adapter: 'dummy',
                 options: {}
               })
      end

      before do
        allow(described_class).to receive(:driver_class).with('dummy').and_return(dummy_adapter_class)
        allow(described_class).to receive(:driver_class).with('failing').and_return(failing_adapter_class)

        failing_channel
        other_channel
      end

      it 'adds error flag to the failing Channel' do
        expect { described_class.fetch }.to change { failing_channel.reload.preferences[:last_fetch] }.and change { failing_channel.reload.status_in }.to('error')
      end

      it 'fetches others anyway' do
        expect { described_class.fetch }.to change { other_channel.reload.preferences[:last_fetch] }.and change { other_channel.reload.status_in }.to('ok')
      end
    end
  e