1
  7   /ˆl–Ãa¾”*i?u8 …p/Üiå1hß�þBûq¬ã€²2ó²JÙ?ŸÞƒq§ÃÝØ¿Ú [    /# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe(FormUpdater::Updater::Ticket::BulkEdit) do
  subject(:resolved_result) do
    described_class.new(
      context:         context,
      relation_fields: relation_fields,
      meta:            meta,
      data:            data,
      id:              nil
    )
  end

  let(:group)   { create(:group) }
  let(:user)    { create(:agent, groups: [group]) }
  let(:tickets) { create_list(:ticket, 5, group: group) }
  let(:context) { { current_user: user } }
  let(:meta)    { { initial: true, form_id: SecureRandom.uuid } }
  let(:data)    { {} }

  let(:relation_fields) do
    [
      {
        name:     'group_id',
        relation: 'Group',
      },
      {
        name:     'owner_id',
        relation: 'User',
      },
    ]
  end

  describe '#authorized?' do
    it 'is authorized for agents' do
      expect(resolved_result.authorized?).to be true
    end

    context 'with customer user' do
      let(:user) { create(:customer) }

      it 'is not authorized for customers' do
        expect(resolved_result.authorized?).to be false
      end
    end

    context 'with admin-only user' do
      let(:user) { create(:user, roles: [Role.find_by(name: 'Admin')]) }

      it 'is not authorized' do
        expect(resolved_result.authorized?).to be false
      end
    end
  end

  context 'when filtering users for owners' do
    let(:new_group)          { create(:group) }
    let(:agents_with_access) { create_list(:agent, 3, groups: [group, new_group]) }
    let(:agents_wo_access)   { create_list(:agent, 3, groups: [group]) }

    let(:meta) do
      {
        additional_data: {
          'entityIds' => tickets.map { |ticket| Gql::ZammadSchema.id_from_object(ticket) },
        },
      }
    end

    before do
      agents_with_access && agents_wo_access
    end

    context 'when no group_id is given in data' do
      it 'returns all users from the current group' do
        expect(resolved_result.resolve[:fields]['owner_id'][:options]).to include(
          { value: agents_with_access[0].id, label: agents_with_access[0].fullname },
          { value: agents_with_access[1].id, label: agents_with_access[1].fullname },
          { value: agents_with_access[2].id, label: agents_with_access[2].fullname },
          { value: agents_wo_access[0].id, label: agents_wo_access[0].fullname },
          { value: agents_wo_access[1].id, label: agents_wo_access[1].fullname },
          { value: agents_wo_access[2].id, label: agents_wo_access[2].fullname },
        )
      end

    end

    context 'when group_id is given in data' do
      let(:data) { { 'group_id' => new_group.id } }

      it 'returns given group users only' do
        expect(resolved_result.resolve[:fields]['owner_id'][:options]).to include(
          { value: agents_with_access[0].id, label: agents_with_access[0].fullname },
          { value: agents_with_access[1].id, label: agents_with_access[1].fullname },
          { value: agents_with_access[2].id, label: agents_with_access[2].fullname },
        ).and not_include(
          { value: agents_wo_access[0].id, label: agents_wo_access[0].fullname },
          { value: agents_wo_access[1].id, label: agents_wo_access[1].fullname },
          { value: agents_wo_access[2].id, label: agents_wo_access[2].fullname },
        )
      end
    end

    context 'with enrichOwnerOptions in additional_data' do
      let(:meta) do
        {
          additional_data: {
            'enrichOwnerOptions' => true,
            'entityIds'          => tickets.map { |ticket| Gql::ZammadSchema.id_from_object(ticket) },
          },
        }
      end

      it 'includes user objects in owner options' do
        expect(resolved_result.resolve[:fields]['owner_id'][:options]).to include(
          include(
            value:  user.id,
            label:  user.fullname,
            object: include(
              '__typename' => 'User',
              'id'       