summaryrefslogtreecommitdiff
path: root/spec/services/integrations/slack_event_service_spec.rb
blob: 17433aee3298fafe861e52d57b922548d3cc20e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::SlackEventService, feature_category: :integrations do
  describe '#execute' do
    subject(:execute) { described_class.new(params).execute }

    let(:params) do
      {
        type: 'event_callback',
        event: {
          type: 'app_home_opened',
          foo: 'bar'
        }
      }
    end

    it 'queues a worker and returns success response' do
      expect(Integrations::SlackEventWorker).to receive(:perform_async)
        .with(
          {
            slack_event: 'app_home_opened',
            params: {
              event: {
                foo: 'bar'
              }
            }
          }
        )
      expect(execute.payload).to eq({})
      is_expected.to be_success
    end

    context 'when event a url verification request' do
      let(:params) { { type: 'url_verification', foo: 'bar' } }

      it 'executes the service instead of queueing a worker and returns success response' do
        expect(Integrations::SlackEventWorker).not_to receive(:perform_async)
        expect_next_instance_of(Integrations::SlackEvents::UrlVerificationService, { foo: 'bar' }) do |service|
          expect(service).to receive(:execute).and_return({ baz: 'qux' })
        end
        expect(execute.payload).to eq({ baz: 'qux' })
        is_expected.to be_success
      end
    end

    context 'when event is unknown' do
      let(:params) { super().merge(event: { type: 'foo' }) }

      it 'raises an error' do
        expect { execute }.to raise_error(described_class::UnknownEventError)
      end
    end
  end
end