4
     EˆÞÝã565ÜÂá 5    E‹      ½”]sš@†ïó+¶{Ñ;8€1éXq3F©Ò¦™N§³À*;Kv×`üõEÑÑv´ifÚ^íayÏÇó2û�;îù^ÝøwCä}z?ôÖ îk= ×w«uÝ0ú#Œp¬TÞ(ŠB/j:sð'«4©ƒT‚…J�T„�{sç\ ;¦$*Od+¦ê²ˆ®Ÿ!àK%YD­}°?Ö$MI*Í!$aL�HI•™.6ÑS9’nÀÂ·¡ª[v‚]+;àÑófót+‹naH°mº	…c«=‘„Í³V<ÇŽÍÒ9’"ì``!Ï$	ÉúœÍ0"‰êà¯ƒÞø[©o²› XÐY¿ëuF×ãŽ‹�I©ä´æ®Ôt±3$R¡”GlÆhtV<­ÄS¶>_Ð­4.•¡`¹b<;H¡D<Fy"sR²6J‚X«*Iô’$üÉ¯;é�|w0©,‰6%Óý•o‡�š)ä2ACÅÅóŽ¯jö6d~½D»é›Ç
;iè¤òÈËl‘ñ";¦.=É»ði-H~UŠîM-Ón¯RãÑŒgÍO8m5f¹;¥­¼öÀõ-4v^�rÿ­eXMÍ45ëÍv£ŽÐ9[jÍÿkK[²û}�fÞGÑü0ZR¯»hFŸƒÖðþ¦«ÁÔo>«ÛþÁ–W¦¼d‹ÑÒ¬²¬vÃ:k‹yiþÆ–?ü�`·`lØ.3ªuú”¹`±±    D   Gˆl–Ãa¾”ªi?u8 ¸Û71hß�þBI³Ž˜ÆB¶  Ïåƒu—žä_�uÐb&=LyZËcõÄã Ü    G# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Store::File, type: :model do
  subject(:file) { described_class.add('foo') }

  describe '.add' do
    context 'with no preconfigured storage provider' do
      before { Setting.set('storage_provider', nil) }

      it 'defaults to the "DB" provider' do
        expect(file.provider).to eq('DB')
      end
    end

    context 'with a preconfigured storage provider' do
      before { Setting.set('storage_provider', 'File') }

      after { Store::Provider::File.delete(described_class.checksum('foo')) }

      it 'defaults to the "DB" provider' do
        expect(file.provider).to eq('File')
      end
    end
  end

  describe '#stream' do
    before do
      file
      allow(Store::Provider::DB).to receive(:get).and_call_original
    end

    it 'yields the content without reading it as a whole' do
      expect { |block| file.stream(&block) }.to yield_with_args('foo')
      expect(Store::Provider::DB).not_to have_received(:get)
    end
  end

  describe '#stream_bytesize' do
    it 'returns the size of the content' do
      expect(file.stream_bytesize).to eq(3)
    end
  end

  describe 'verify' do
    let(:counter) { Set.new }
    let(:stores) do
      [
        create(:store, :txt),
        create(:store, :txt, data: 'foo'),
        create(:store, :txt, data: 'bar')
      ]
    end

    before do
      stores
      allow_any_instance_of(described_class).to receive(:checksum_valid?) { |elem|
        counter << elem.id
        valid?(elem)
      }
    end

    context 'when all files are valid' do
      it 'returns true' do
        expect(described_class.verify).to be_truthy
      end

      it 'runs check for all files' do
        described_class.verify
        expect(counter.count).to eq(3)
      end

      it 'does not update checksum on any files even with fix_it = true' do
        expect_any_instance_of(described_class).not_to receive(:update_checksum!)
        described_class.verify
      end

      def valid?(_elem)
        true
      end
    end

    context 'when one of the files is not valid' do
      it 'returns false' do
        expect(described_class.verify).to be_falsey
      end

      it 'runs check for all files' do
        described_class.verify
        expect(counter.count).to eq(3)
      end

      it 'does not update checksum on any files with fix_it = false' do
        expect_any_instance_of(described_class).not_to receive(:update_checksum!)
        described_class.verify
      end

      it 'updates checksum on the invalid file with fix_it = true' do
        expect_any_instance_of(described_class).to receive(:update_checksum!).once
        described_class.verify(true)
      end

      def valid?(elem)
        elem.id != stores.first.store_file.id
      end
    end
  end

  describe '.move' do
    before { Setting.set('storage_provider', nil) }

    after { Store::Provider::File.delete(described_class.checksum('foo')) }

    let(:storage_path) { Rails.root.join('storage/fs') }

    it 'replaces all Store::Provider::{source} records with Store::Provider::{target} ones' do
      file # create Store::File record

      expect { described_class.move('DB', 'File') }
        .to change { file.reload.provider }.to('File')
        .and change(Store::Provider::DB, :count).by(-1)
        .and change { Dir[storage_path.join('**'