From 441d6dc734d2590b14ca010076496e652d6ef676 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 9 May 2017 13:58:18 -0400 Subject: [PATCH] Spec and refactor for pubsubhubbub/unsubscribe service (#2946) * Add coverage for pubsub unsubscribe service * Refactor pubsub unsubscribe service --- .../pubsubhubbub/unsubscribe_service.rb | 28 ++++++++--- .../pubsubhubbub/unsubscribe_service_spec.rb | 46 +++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 spec/services/pubsubhubbub/unsubscribe_service_spec.rb diff --git a/app/services/pubsubhubbub/unsubscribe_service.rb b/app/services/pubsubhubbub/unsubscribe_service.rb index 7adadf8ed2..99fec8b146 100644 --- a/app/services/pubsubhubbub/unsubscribe_service.rb +++ b/app/services/pubsubhubbub/unsubscribe_service.rb @@ -1,15 +1,31 @@ # frozen_string_literal: true class Pubsubhubbub::UnsubscribeService < BaseService - def call(account, callback) - return ['Invalid topic URL', 422] if account.nil? + attr_reader :account, :callback_url - subscription = Subscription.find_by(account: account, callback_url: callback) + def call(account, callback_url) + @account = account + @callback_url = callback_url - unless subscription.nil? - Pubsubhubbub::ConfirmationWorker.perform_async(subscription.id, 'unsubscribe') + process_unsubscribe + end + + private + + def process_unsubscribe + if account.nil? + ['Invalid topic URL', 422] + else + confirm_unsubscribe unless subscription.nil? + ['', 202] end + end - ['', 202] + def confirm_unsubscribe + Pubsubhubbub::ConfirmationWorker.perform_async(subscription.id, 'unsubscribe') + end + + def subscription + @_subscription ||= Subscription.find_by(account: account, callback_url: callback_url) end end diff --git a/spec/services/pubsubhubbub/unsubscribe_service_spec.rb b/spec/services/pubsubhubbub/unsubscribe_service_spec.rb new file mode 100644 index 0000000000..59054ed998 --- /dev/null +++ b/spec/services/pubsubhubbub/unsubscribe_service_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Pubsubhubbub::UnsubscribeService do + describe '#call' do + subject { described_class.new } + + context 'with a nil account' do + it 'returns an invalid topic status' do + result = subject.call(nil, 'callback.host') + + expect(result).to eq invalid_topic_status + end + end + + context 'with a valid account' do + let(:account) { Fabricate(:account) } + + it 'returns a valid topic status and does not run confirm when no subscription' do + allow(Pubsubhubbub::ConfirmationWorker).to receive(:perform_async).and_return(nil) + result = subject.call(account, 'callback.host') + + expect(result).to eq valid_topic_status + expect(Pubsubhubbub::ConfirmationWorker).not_to have_received(:perform_async) + end + + it 'returns a valid topic status and does run confirm when there is a subscription' do + subscription = Fabricate(:subscription, account: account, callback_url: 'callback.host') + allow(Pubsubhubbub::ConfirmationWorker).to receive(:perform_async).and_return(nil) + result = subject.call(account, 'callback.host') + + expect(result).to eq valid_topic_status + expect(Pubsubhubbub::ConfirmationWorker).to have_received(:perform_async).with(subscription.id, 'unsubscribe') + end + end + + def invalid_topic_status + ['Invalid topic URL', 422] + end + + def valid_topic_status + ['', 202] + end + end +end