4:39
  D   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þDF³Ž˜ÆB¶ÑJ?çÂƒy§ÃÁ_�uÐb&=LyZËcõÁÀ !+    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe WebhooksController, type: :request do
  let(:agent) { create(:agent) }
  let(:admin) { create(:admin) }

  describe 'request handling', authenticated_as: :admin do
    context 'when listing webhooks' do
      let!(:webhooks) { create_list(:webhook, 10, signature_token: 'some_token', basic_auth_password: 'some_password', bearer_token: 'some_token') }
      let(:url) { '/api/v1/webhooks.json' }

      before do
        get url
      end

      context 'without parameters' do

        it 'returns all' do
          expect(json_response.length).to eq(webhooks.length)
        end

        it 'masks sensitive fields' do
          expect(json_response.first).to include(
            'signature_token'     => SensitiveParamsHelper::SENSITIVE_MASK,
            'basic_auth_password' => SensitiveParamsHelper::SENSITIVE_MASK,
            'bearer_token'        => SensitiveParamsHelper::SENSITIVE_MASK
          )
        end

        context 'with agent permissions', authenticated_as: :agent do
          it 'request is forbidden' do
            expect(response).to have_http_status(:forbidden)
          end
        end

        context 'without configured authentication' do
          let!(:webhooks) { create_list(:webhook, 1) }

          it 'does not mask unset sensitive fields' do
            expect(json_response.first).to include(
              'signature_token'     => nil,
              'basic_auth_password' => nil,
              'bearer_token'        => nil
            )
          end
        end
      end

      context 'with expand=1' do
        let(:url) { '/api/v1/webhooks.json?expand=1' }

        it 'returns all' do
          expect(json_response.length).to eq(webhooks.length)
        end

        it 'masks sensitive fields' do
          expect(json_response.first).to include(
            'signature_token'     => SensitiveParamsHelper::SENSITIVE_MASK,
            'basic_auth_password' => SensitiveParamsHelper::SENSITIVE_MASK,
          )

        end
      end

      context 'with full=1' do
        let(:url) { '/api/v1/webhooks.json?full=1' }

        it 'returns all' do
          expect(json_response['record_ids'].length).to eq(webhooks.length)
        end

        it 'masks sensitive fields' do
          expect(json_response['assets']['Webhook'][webhooks.first.id.to_s]).to include(
            'signature_token'     => SensitiveParamsHelper::SENSITIVE_MASK,
            'basic_auth_password' => SensitiveParamsHelper::SENSITIVE_MASK,
          )
        end
      end
    end

    context 'when showing webhook' do
      let!(:webhook) { create(:webhook, signature_token: 'some_token', basic_auth_password: 'some_password') }

      before do
        get "/api/v1/webhooks/#{webhook.id}.json"
      end

      it 'returns ok' do
        expect(response).to have_http_status(:ok)
      end

      context 'with inactive template' do
        let!(:webhook) { create(:webhook, active: false) }

        it 'returns ok' do
          expect(response).to have_http_status(:ok)
        end
      end

      it 'masks sensitive fields' do
        expect(json_response).to include(
          'signature_token'     => SensitiveParamsHelper::SENSITIVE_MASK,
          'basic_auth_password' => SensitiveParamsHelper::SENSITIVE_MASK,
        )
      end

      context 'with agent permissions', authenticated_as: :agent do
        it 'request is forbidden' do
          expect(response).to have_http_status(:forbidden)
        end
      end
    end

    context 'when creating webhook' do
      before do
        post '/api/v1/webhooks.json', params: { name: 'Foo', endpoint: 'http://example.com/endpoint', ssl_verify: true, active: true }
      end

      it 'creates the webhook', :aggregate_failures do
        expect(response).to have_http_status(:created)
        expect(Webhook.find_by(name: 'Foo')).to have_attributes(endpoint: 'http://example.com/endpoint')