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

require 'rails_helper'

RSpec.describe 'Link', type: :request do

  describe 'GET /api/v1/links' do

    context 'when requesting links of Ticket', authenticated_as: -> { agent } do

      subject!(:ticket) { create(:ticket) }

      let(:agent) { create(:agent, groups: [ticket.group]) }

      let(:params) do
        {
          link_object:       ticket.class.name,
          link_object_value: ticket.id,
        }
      end
      let(:linked) { create(:ticket, group: ticket.group) }

      before do
        create(:link, from: ticket, to: linked)
        get '/api/v1/links', params: params, as: :json
      end

      it 'is present in response' do
        expect(response).to have_http_status(:ok)
        expect(json_response['links']).to eq([
                                               {
                                                 'link_type'         => 'normal',
                                                 'link_object'       => 'Ticket',
                                                 'link_object_value' => linked.id
                                               }
                                             ])
      end

      context 'without permission to linked Ticket Group' do
        let(:linked) { create(:ticket) }

        it 'is not present in response' do
          expect(response).to have_http_status(:ok)
          expect(json_response['links']).to be_blank
        end
      end
    end

    context 'when a customer requests links of a foreign Ticket', authenticated_as: -> { customer } do
      let(:ticket)   { create(:ticket) }
      let(:customer) { create(:customer) }
      let(:linked)   { create(:ticket, customer: customer) }

      before do
        create(:link, from: ticket, to: linked)

        get '/api/v1/links', params: { link_object: 'Ticket', link_object_value: ticket.id }, as: :json
      end

      it 'is forbidden' do
        expect(response).to have_http_status(:forbidden)
      end
    end

    context 'when requesting links of a non-existing object', authenticated_as: -> { agent } do
      let(:agent) { create(:agent) }

      before do
        get '/api/v1/links', params: { link_object: 'Ticket', link_object_value: 99_999_999 }, as: :json
      end

      it 'is forbidden and does not reveal the missing object' do
        expect(response).to have_http_status(:forbidden)
      end
    end

    context 'when requesting links of an unsupported object', authenticated_as: -> { agent } do
      let(:agent) { create(:agent) }

      before do
        get '/api/v1/links', params: { link_object: 'Announcement', link_object_value: 1 }, as: :json
      end

      it 'is forbidden and does not raise' do
        expect(response).to have_http_status(:forbidden)
      end
    end
  end

  describe 'POST /api/v1/links/add', authenticated_as: -> { agent } do
    let(:ticket) { create(:ticket) }
    let(:source) { create(:ticket, group: ticket.group) }
    let(:agent)  { create(:agent, groups: [ticket.group]) }

    let(:params) do
      {
        link_type:                 'normal',
        link_object_target:        ticket.class.name,
        link_object_target_value:  ticket.id,
        link_object_source:        source.class.name,
        link_object_source_number: source.number,
      }
    end

    let(:existing_links) { [] }

    before do
      existing_links.each { |link| create(:link, **link) }

      post '/api/v1/links/add', params: params, as: :json
    end

    it 'adds the link' do
      expect(response).to have_http_status(:created)
      expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id))
        .to include(include('link_object_value' => source.id))
    end

    context 'when the source is the target' do
      let(:source) { ticket }

      it 'responds with an error' do
        expect(response).to have_http_status(:unprocessable_content)
        expect(json_response['error']).to eq('An object cannot be linked to itself.')
      end

      it 'does not add the link' do
        expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id)).to be_blank
      end
    end

    context 'when the link already exists' do
      let(:existing_links) { [{ from: source, to: ticket }] }

      it 'responds with an error' do
        expect(response).to have_http_status(:unprocessable_content)
        expect(json_response['error']).to eq('Link already exists')
      end
    end

    context 'when the source does not exist' do
      let(:params) { super().merge(link_object_source_number: '99999999') }

      it 'is forbidden and does not reveal the missing object' do
        expect(response).to have_http_status(:forbidden)
      end
    end
  end

  describe 'DELETE /api/v1/links/remove', authenticated_as: -> { agent } do
    let(:ticket) { create(:ticket) }
    let(:source) { create(:ticket, group: ticket.group) }
    let(:agent)  { create(:agent, groups: [ticket.group]) }

    let(:params) do
      {
        link_type:                'normal',
        link_object_target:       ticket.class.name,
        link_object_target_value: ticket.id,
        link_object_source:       source.class.name,
        link_object_source_value: source.id,
      }
    end

    let(:foreign)        { create(:ticket) }
    let(:existing_links) { [{ from: source, to: ticket }] }

    before do
      existing_links.each { |link| create(:link, **link) }

      delete '/api/v1/links/remove', params: params, as: :json
    end

    it 'removes the link' do
      expect(response).to have_http_status(:created)
      expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id)).to be_blank
    end

    context 'when the source is not accessible' do
      let(:source) { create(:ticket) }

      it 'is forbidden and keeps the link' do
        expect(response).to have_http_status(:forbidden)
        expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id))
          .to include(include('link_object_value' => source.id))
      end
    end

    context 'when the source does not exist' do
      let(:params) { super().merge(link_object_source_value: 99_999_999) }

      it 'is forbidden and does not reveal the missing object' do
        expect(response).to have_http_status(:forbidden)
      end
    end

    context 'when an inaccessible source is passed together with an accessible one' do
      let(:existing_links) { [{ from: source, to: ticket }, { from: foreign, to: ticket }] }
      let(:params)         { super().merge(link_object_source_value: [source.id, foreign.id]) }

      it 'is forbidden and keeps the links' do
        expect(response).to have_http_status(:forbidden)
        expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id).pluck('link_object_value'))
          .to contain_exactly(source.id, foreign.id)
      end
    end

    context 'when an inaccessible target is passed together with an accessible one' do
      let(:existing_links) { [{ from: source, to: ticket }, { from: source, to: foreign }] }
      let(:params)         { super().merge(link_object_target_value: [ticket.id, foreign.id]) }

      it 'is forbidden and keeps the links' do
        expect(response).to have_http_status(:forbidden)
        expect(Link.list(link_object: 'Ticket', link_object_value: source.id).pluck('link_object_value'))
          .to contain_exactly(ticket.id, foreign.id)
      end
    end
  end
end
