1
  6   ˆÄ�þ_+8àYŒd+DOÿ?Ëƒ"gÊÃa–äY>” ”¾R( 	Å Œ‚à_S�ÿÈ 	    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Gql::Subscriptions::MacrosUpdate, type: :graphql do
  let(:mock_channel) { build_mock_channel }
  let(:group1)       { create(:group) }
  let(:macro)        { create(:macro, group_ids: [group1.id]) }

  let(:subscription) do
    <<~QUERY
      subscription macrosUpdate {
        macrosUpdate {
          macroId
          groupIds
          removeMacroId
        }
      }
    QUERY
  end

  let(:expected_group_ids) { [gql.id(group1)] }

  let(:expected_msg) do
    {
      'data' => {
        'macrosUpdate' => {
          'macroId'       => gql.id(macro),
          'groupIds'      => expected_group_ids,
          'removeMacroId' => nil
        }
      }
    }
  end

  before do
    gql.execute(subscription, context: { channel: mock_channel })

    macro
  end

  context 'when authenticated', authenticated_as: :agent do
    let(:agent) { create(:agent) }

    it 'creating a macro triggers subscription' do
      expect(mock_channel.mock_broadcasted_messages.last[:result]).to eq(expected_msg)
    end

    context 'when macro is updated' do
      let(:expected_group_ids) { [] }

      it 'triggers subscription' do
        macro.update!(name: 'New Name', group_ids: [])

        expect(mock_channel.mock_broadcasted_messages.last[:result]).to eq(expected_msg)
      end
    end

    context 'when macro is destroyed' do
      let(:expected_msg) do
        {
          'data' => {
            'macrosUpdate' => {
              'macroId'       => nil,
              'groupIds'      => nil,
              'removeMacroId' => gql.id(macro),
            }
          }
        }
      end

      it 'triggers subscription' do
        macro.destroy

        expect(mock_channel.mock_broadcasted_messages.last[:result]).to eq(expected_msg)
      end
    end
  end

  context 'when authenticated as customer', authenticated_as: :customer do
    let(:customer) { create(:customer) }

    it_behaves_like 'graphql responds with forbidden'

    it 'does not deliver macro updates' do
      macro.update!(name: 'New Name', group_ids: [])
      macro.destroy

      expect(mock_channel.mock_broadcasted_messages).to be_empty
    end
  end

  it_behaves_like 'graphql responds with error if unauthenticated'
end
     ˆÅ�þBcYÇÌc!Z šùÌƒi ‡ËÄ¾È ;    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Gql::Subscriptions::LinkUpdates, type: :graphql do
  let(:subscription) do
    <<~SUBSCRIPTION
      subscription linkUpdates($objectId: ID!, $targetType: String!) {
        linkUpdates(objectId: $objectId, targetType: $targetType) {
          links {
            item {
              ... on Ticket {
                id
              }
              ... on KnowledgeBaseAnswerTranslation {
                id
              }
            }
            type
          }
        }
      }
    SUBSCRIPTION
  end

  let(:mock_channel)  { build_mock_channel }
  let(:from_group)    { create(:group) }
  let(:from)          { create(:ticket, group: from_group) }
  let(:to_group)      { create(:group) }
  let(:to)            { create(:ticket, group: to_group) }
  let(:link)          { create(:link, from:, to:) }
  let(:variables)     { { objectId: gql.id(from), targetType: 'Ticket' } }

  before do
    link
    gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  end

  context 'with authenticated user', authenticated_as: :agent do

    context 'when object is not accessible' do
      let(:agent) { create(:agent) }

      it 'raises an error' do
        expect(gql.result.error_type).to eq(Pundit::NotAuthorizedError)
      end
    end

    context 'when object is accessible' do
      let(:agent) { create(:agent, groups: [ from_group, to_group ]) }

      it 'subscribes to the channel' do
        expect(gql.result.data).to eq({ 'links' => nil })
      end

      context 'when link is updated' 