256
  7   ˆl–ßi~” T¾R( 	Å�Šã!S�ÿ‘äÇò 8à_Ÿm¡:VŸÅƒh¹ÄÂÁ      1
  6   	ˆl–ßi~” T¾R( 	Å�†ãN)‹Fÿ�þB gá�¿+O¹üÿÆƒh¹ÅÃÂ      	6
  5   ˆl–ßi~” T¾R( 	Å�ŠàžS�ÿ�þB gáâh€ŸÏÇƒh¹ÆÄÃ      1
  !   �236Âa–ßi~” T¾R( 	Å�Šã"S�ÿÄ  ì    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>
  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' 