Sha256: e34a9749e4c03b2fc5faf40e3352564c89c738f93dd5ef31230315c1a0fb55db

Contents?: true

Size: 1.47 KB

Versions: 22

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require 'test_plugin_helper'

module ForemanWebhooks
  class WebhookServiceTest < ActiveSupport::TestCase
    let(:event_name) { 'subnet_created' }
    let(:payload) { { id: 2 } }
    let(:payload_json) { payload.to_json }
    let(:webhook) do
      FactoryBot.build(:webhook, :with_template, template: '<%= payload({id: @payload[:id]}, with_defaults: false) %>')
    end
    let(:webhook_service) do
      WebhookService.new(
        webhook: webhook,
        event_name: event_name,
        payload: payload_json,
        headers: {},
        url: webhook.target_url
      )
    end

    it 'executes a request to the configured webhook' do
      expected = { status: :success, message: '', http_status: 200 }

      stub_request(:post, 'https://hook.example.com/api/callback')
        .with(
          body: payload_json,
          headers: {
            'Content-Type' => 'application/json'
          }
        )
        .to_return(status: 200, body: '', headers: {})

      assert_equal expected, webhook_service.execute
    end

    it 'handles a failed request' do
      expected = { status: :error, message: '', http_status: 404 }

      stub_request(:post, 'https://hook.example.com/api/callback')
        .with(
          body: payload_json,
          headers: {
            'Content-Type' => 'application/json'
          }
        )
        .to_return(status: 404, body: '', headers: {})

      assert_equal expected, webhook_service.execute
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
foreman_webhooks-4.0.0 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.2.3 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.2.2 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.1.1 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.2.1 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.2.0 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.1.0 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.0.5 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-2.0.3 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.0.4 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-2.0.2 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.0.3 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.0.2 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.0.1 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-2.0.1 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-3.0.0 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-2.0.0 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-1.1.0 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-1.0.0 test/unit/foreman_webhooks/webhook_service_test.rb
foreman_webhooks-0.0.3 test/unit/foreman_webhooks/webhook_service_test.rb