about summary refs log tree commit diff
path: root/spec/services/pubsubhubbub/subscribe_service_spec.rb
blob: 82094117b4e826fa9cbf70144f7f4bd5f81873b0 (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

require 'rails_helper'

describe Pubsubhubbub::SubscribeService do
  describe '#call' do
    subject { described_class.new }
    let(:user_account) { Fabricate(:account) }

    context 'with a nil account' do
      it 'returns the invalid topic status results' do
        result = service_call(account: nil)

        expect(result).to eq invalid_topic_status
      end
    end

    context 'with an invalid callback url' do
      it 'returns invalid callback status when callback is blank' do
        result = service_call(callback: '')

        expect(result).to eq invalid_callback_status
      end
      it 'returns invalid callback status when callback is not a URI' do
        result = service_call(callback: 'invalid-hostname')

        expect(result).to eq invalid_callback_status
      end
    end

    context 'with a blocked domain in the callback' do
      it 'returns callback not allowed' do
        Fabricate(:domain_block, domain: 'test.host', severity: :suspend)
        result = service_call(callback: 'https://test.host/api')

        expect(result).to eq not_allowed_callback_status
      end
    end

    context 'with a valid account and callback' do
      it 'returns success status and confirms subscription' do
        allow(Pubsubhubbub::ConfirmationWorker).to receive(:perform_async).and_return(nil)
        subscription = Fabricate(:subscription, account: user_account)

        result = service_call(callback: subscription.callback_url)
        expect(result).to eq success_status
        expect(Pubsubhubbub::ConfirmationWorker).to have_received(:perform_async).with(subscription.id, 'subscribe', 'asdf', 3600)
      end
    end
  end

  def service_call(account: user_account, callback: 'https://callback.host', secret: 'asdf', lease_seconds: 3600)
    subject.call(account, callback, secret, lease_seconds)
  end

  def invalid_topic_status
    ['Invalid topic URL', 422]
  end

  def invalid_callback_status
    ['Invalid callback URL', 422]
  end

  def not_allowed_callback_status
    ['Callback URL not allowed', 403]
  end

  def success_status
    ['', 202]
  end
end