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

require 'rails_helper'

RSpec.describe Issue5567RestoreOutboundSharedMailbox, type: :db_migration do
  describe 'with a shared mailbox account that lost its outbound shared mailbox' do
    before do
      create(:microsoft_graph_channel, microsoft_shared_mailbox: 'exshared@domain.tld').tap do |channel|
        channel.options[:outbound][:options].delete(:shared_mailbox) # reproduce the reauthentication issue
        channel.save!
      end
    end

    it 'restores outbound options' do
      expect { migrate }
        .to change { Channel.last.options[:outbound][:options] }
        .from(not_include(:shared_mailbox))
        .to(include(shared_mailbox: 'exshared@domain.tld'))
    end
  end

  describe 'with an intact shared mailbox account' do
    before do
      create(:microsoft_graph_channel, microsoft_shared_mailbox: 'exshared@domain.tld')
    end

    it 'does not update the channel' do
      expect { migrate }.not_to change { Channel.last.updated_at }
    end
  end

  describe 'with a user mailbox account' do
    before do
      create(:microsoft_graph_channel)
    end

    it 'does not migrate outbound options' do
      expect { migrate }.not_to change { Channel.last.options[:outbound][:options] }
    end
  end
end
