Normal
  5   ˆl–Ðz¾”ªC]Šq@=q¦®41hß�þBÉq°9 €"´á_þÈ316ÍÀÊ <    --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
from: IT example <example@service-now.com>
from_email: example@service-now.com
from_display_name: IT example
to: support@example.com
subject: Incident INC678439 -- zugewiesen an EXT-XXXINIS
body: 'Incident content

  '
content_type: text/plain
attachments: []
  D   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þB&À³Ž˜ÆBµ(DÿóÉƒiÇžÎ_�uÐb&=LyZËcõÂÌ P    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Sessions::Event::Base do

  describe '#initialize' do
    it 'does not accept unknown params' do
      expect { described_class.new(clients: {}, user_id: 1) }.to raise_error(ArgumentError)
    end
  end

  describe 'restoring the session user' do
    # The session of a web socket connection is restored once, at login time, so it has to be
    #   re-checked for every event that is handled for it.
    let(:event_class) do
      Class.new(described_class) do
        database_connection_required

        def run
          @session
        end
      end
    end

    let(:session)   { { 'id' => user.id } }
    let(:event)     { event_class.new(session: session, client_id: 'sess_id', client: {}) }

    context 'with an active user' do
      let(:user) { create(:agent) }

      it 'keeps the user in the session' do
        expect(event.run).to eq({ 'id' => user.id })
      end
    end

    context 'with an inactive user' do
      let(:user) { create(:agent, active: false) }

      it 'removes the user from the session' do
        expect(event.run).to eq({})
      end
    end

    context 'with a deleted user' do
      let(:user) { create(:agent).tap(&:destroy!) }

      it 'removes the user from the session' do
        expect(event.run).to eq({})
      end
    end

    context 'with maintenance mode enabled' do
      let(:user) { create(:agent) }

      before { Setting.set('maintenance_mode', true) }

      it 'removes the user from the session' do
        expect(event.run).to eq({})
      end

      context 'when the user has maintenance permissions' do
        let(:user) { create(:admin) }

        it 'keeps the user in the session' do
          expect(event.run).to eq({ 'id' => user.id })
        end
      end

      context 'when an admin switched to the user' do
        let(:session) { { 'id' => user.id, 'switched_from_user_id' => create(:admin).id } }

        it 'keeps the user in the session' do
          expect(event.run).to eq(session)
        end
      end
    end

    # Such a session belongs to the admin and is served as the user, so the admin's own state
    #   has to be re-checked here as well (see Auth::SwitchedSession).
    context 'with an admin switched to the user' do
      let(:user)    { create(:agent) }
      let(:admin)   { create(:admin) }
      let(:session) { { 'id' => user.id, 'switched_from_user_id' => admin.id } }

      it 'keeps the user in the session' do
        expect(event.run).to eq(session)
      end

      # Bypasses the callback that drops the admin's sessions (see User::TerminatesSessions), so
      #   that this covers the prerequisite itself - the legacy transport serves a copy of the
      #   session anyway, which no destroyed record reaches.
      context 'when the admin is deactivated' do
        before { admin.update_columns(active: false) }

        it 'removes the user from the session' do
          expect(event.run).to eq({ 'switched_from_user_id' => admin.id })
        end
      end
    end
  end

  describe '#remote_ip' do
    let(:instance) { described_class.new(headers:) }

    context 'without X-Forwarded-For' do
      let(:headers) { {} }

      it 'returns no value' do
        expect(instance.remote_ip).to be_nil
      end
    end

    context 'with X-Forwarded-For' do
      before do
        allow(Rails.application.config.action_dispatch).to receive(:trusted_proxies).and_return(trusted_proxies)
      end

      let(:trusted_proxies) { [IPAddr.new('127.0.0.1'), IPAddr.new('::1')] }

 