# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe HtmlSanitizer::Scrubber::InlineImages do
  let(:scrubber) { described_class.new }
  let(:base64)   { 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...' }

  describe('#scrubber') do
    subject(:actual) { fragment.scrub!(scrubber).to_html }

    let(:fragment) { Loofah.html5_fragment(input) }

    context 'when matching image' do
      let(:input)  { '<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...">' }
      let(:target) { %r{<img src="cid:.+?">} }

      it { is_expected.to match target }

      it 'adds attachment to scrubber' do
        actual

        expect(scrubber.attachments_inline).to match_array(include(filename: 'image1.jpeg'))
      end

      context 'when uploaded image is handled' do
        let(:input)         { "<img src='/api/v1/attachments/#{Store.last.id}'>" }
        let(:target)        { %r{<img src="cid:.+?">} }
        let(:created_by_id) { 1 }

        before do
          form_id = SecureRandom.uuid

          file_name    = 'file1.png'
          file_type    = 'image/png'
          file_content = Base64.strict_encode64('file1')

          UploadCache.new(form_id).tap do |cache|
            UserInfo.with_user_id(created_by_id) do
              cache.add(
                data:          file_content,
                filename:      file_name,
                preferences:   { 'Content-Type' => file_type },
                created_by_id: created_by_id
              )
            end
          end
        end

        it { is_expected.to match target }

        context 'when current user is not present' do
          it 'adds attachment to scrubber' do
            actual

            expect(scrubber.attachments_inline).to match_array(include(filename: 'file1.png'))
          end
        end

        context 'when current user is present', current_user_id: -> { user.id } do
          let(:user) { create(:agent) }

          context 'when the user has access to the attachment' do
            let(:created_by_id) { user.id }

            it 'adds attachment to scrubber' do
              actual

              expect(scrubber.attachments_inline).to match_array(include(filename: 'file1.png'))
            end
          end

          context 'when the user does not have access to the attachment' do
            it 'raises an error' do
              expect { actual }.to raise_error(Pundit::NotAuthorizedError)
            end
          end
        end
      end

      context 'when a quoted ticket attachment image is handled' do
        let(:article)    { create(:ticket_article) }
        let(:attachment) { create(:store, :image, object: 'Ticket::Article', o_id: article.id) }
        let(:input)      { "<img src='/api/v1/ticket_attachment/#{article.ticket_id}/#{article.id}/#{attachment.id}?view=inline'>" }
        let(:target)     { %r{<img src="cid:.+?">} }

        it { is_expected.to match target }

        it 'adds a copy of the attachment to scrubber' do
          actual

          expect(scrubber.attachments_inline).to match_array(include(filename: attachment.filename, data: attachment.content))
        end

        context 'when the user does not have access to the attachment', current_user_id: -> { create(:agent).id } do
          it 'keeps the reference' do
            expect(actual).to include('/api/v1/ticket_attachment/')
          end

          it 'adds no attachment to scrubber' do
            actual

            expect(scrubber.attachments_inline).to be_blank
          end
        end

        context 'when the attachment does not exist anymore' do
          let(:input) { "<img src='/api/v1/ticket_attachment/#{article.ticket_id}/#{article.id}/999999?view=inline'>" }

          it 'keeps the reference' do
            expect(actual).to include('/api/v1/ticket_attachment/')
          end
        end
      end

      context 'when uploaded image reference is surrounded by other content' do
        let(:input)  { %(<img src="junk\n/api/v1/attachments/999999">) }
        let(:target) { %r{<img src="junk\n/api/v1/attachments/999999">} }

        it { is_expected.to match target }

        it 'adds no attachment to scrubber' do
          actual

          expect(scrubber.attachments_inline).to be_blank
        end
      end

      context 'when upload failed' do
        let(:input)  { '<img src="blob:/api/v1/attachments/111">' }
        let(:target) { '' }

        it { is_expected.to match target }
      end
    end

    context 'when not matching image' do
      let(:input)  { '<img src="/image1.jpg">' }
      let(:target) { '<img src="/image1.jpg">' }

      it { is_expected.to eq target }

      it 'adds no attachments to scrubber' do
        actual

        expect(scrubber.attachments_inline).to be_blank
      end
    end
  end

  describe '#inline_image_data' do
    %w[image/jpeg image/jpg image/png].each do |type|
      context "with #{type}" do
        it 'returns truthy' do
          input = "data:#{type};base64,/9j/4AAQSkZJRgABAQAAAQABAAD/..."

          expect(scrubber.send(:inline_image_data, input)).to be_truthy
        end
      end
    end

    it 'falsey when non-jpeg/png' do
      input = 'data:image/gif;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...'

      expect(scrubber.send(:inline_image_data, input)).to be_falsey
    end

    it 'falsey when URL' do
      input = '/image.jpeg'

      expect(scrubber.send(:inline_image_data, input)).to be_falsey
    end
  end

  describe '#process_inline_image' do
    it 'adds image to attachments' do
      scrubber.send(:process_inline_image, {}, base64)

      first_attachment = scrubber.send(:attachments_inline).first

      expect(first_attachment).to include(filename: 'image1.jpeg')
    end

    it 'adds multiple numbered images to attachments' do
      2.times { scrubber.send(:process_inline_image, {}, base64) }

      filenames = scrubber.send(:attachments_inline).pluck(:filename)

      expect(filenames).to eq %w[image1.jpeg image2.jpeg]
    end

    it 'sets src to cid' do
      node = {}
      allow(scrubber).to receive(:generate_cid).and_return('identifier')

      scrubber.send(:process_inline_image, node, base64)

      expect(node).to include('src' => 'cid:identifier')
    end
  end

  describe '#generate_cid' do
    it 'generates cid' do
      allow(scrubber).to receive(:prefix).and_return(:prefix)

      expect(scrubber.send(:generate_cid)).to start_with('prefix.').and(end_with('zammad.example.com'))
    end
  end

  describe '#parse_inline_image' do
    let(:expected) do
      include(
        data:        be_present,
        filename:    'image1.jpeg',
        preferences: {
          'Content-Type'        => 'image/jpeg',
          'Mime-Type'           => 'image/jpeg',
          'Content-ID'          => :identifier,
          'Content-Disposition' => 'inline',
        }
      )
    end

    it 'returns hash' do
      expect(scrubber.send(:parse_inline_image, base64, :identifier)).to expected
    end
  end
end
