From 06d7db7ff8ab31c32e529443d995902c2b13a50f Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 5 Feb 2023 18:04:30 +0100 Subject: [PATCH] Remove ShareWorker --- app/workers/share_worker.rb | 43 --------- .../ajax/answer_controller_spec.rb | 12 --- spec/workers/share_worker_spec.rb | 88 ------------------- 3 files changed, 143 deletions(-) delete mode 100644 app/workers/share_worker.rb delete mode 100644 spec/workers/share_worker_spec.rb diff --git a/app/workers/share_worker.rb b/app/workers/share_worker.rb deleted file mode 100644 index 7edcf141..00000000 --- a/app/workers/share_worker.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -class ShareWorker - include Sidekiq::Worker - - sidekiq_options queue: :share, retry: 5 - - # @param user_id [Integer] the user id - # @param answer_id [Integer] the user id - # @param service [String] the service to post to - def perform(user_id, answer_id, service) # rubocop:disable Metrics/AbcSize - @user_service = User.find(user_id).services.find_by!(type: "Services::#{service.camelize}") - - @user_service.post(Answer.find(answer_id)) - rescue ActiveRecord::RecordNotFound - logger.info "Tried to post answer ##{answer_id} for user ##{user_id} to #{service.titleize} but the user/answer/service did not exist (likely deleted), will not retry." - # The question to be posted was deleted - rescue Twitter::Error::DuplicateStatus - logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the status was already posted." - rescue Twitter::Error::Forbidden - # User's Twitter account is suspended - logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the account is suspended." - rescue Twitter::Error::Unauthorized - # User's Twitter token has expired or been revoked - logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the token has expired or been revoked." - revoke_and_notify(user_id, service) - rescue => e - logger.info "failed to post answer #{answer_id} to #{service} for user #{user_id}: #{e.message}" - Sentry.capture_exception(e) - raise - end - - def revoke_and_notify(user_id, service) - @user_service.destroy - - Notification::ServiceTokenExpired.create( - target_id: user_id, - target_type: "User::Expired#{service.camelize}ServiceConnection", - recipient_id: user_id, - new: true - ) - end -end diff --git a/spec/controllers/ajax/answer_controller_spec.rb b/spec/controllers/ajax/answer_controller_spec.rb index 79c911dc..93374bb5 100644 --- a/spec/controllers/ajax/answer_controller_spec.rb +++ b/spec/controllers/ajax/answer_controller_spec.rb @@ -25,13 +25,6 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do expect { subject }.to(change { Answer.count }.by(1)) end - it "enqueues a job for sharing the answer to social networks" do - subject - shared_services.each do |service| - expect(ShareWorker).to have_enqueued_sidekiq_job(user.id, Answer.last.id, service) - end - end - include_examples "returns the expected response" end @@ -40,11 +33,6 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do expect { subject }.not_to(change { Answer.count }) end - it "does not enqueue a job for sharing the answer to social networks" do - subject - expect(ShareWorker).not_to have_enqueued_sidekiq_job(anything, anything, anything) - end - include_examples "returns the expected response" end diff --git a/spec/workers/share_worker_spec.rb b/spec/workers/share_worker_spec.rb deleted file mode 100644 index c8003258..00000000 --- a/spec/workers/share_worker_spec.rb +++ /dev/null @@ -1,88 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -describe ShareWorker do - let(:user) { FactoryBot.create(:user) } - let(:answer) { FactoryBot.create(:answer, user: user) } - let!(:service) do - Services::Twitter.create!(type: "Services::Twitter", - user: user) - end - - before do - stub_const("APP_CONFIG", { - "hostname" => "example.com", - "anonymous_name" => "Anonymous", - "https" => true, - "items_per_page" => 5, - "sharing" => { - "twitter" => { - "consumer_key" => "" - } - } - }) - end - - describe "#perform" do - before do - allow(Sidekiq.logger).to receive(:info) - end - - subject do - Sidekiq::Testing.fake! do - ShareWorker.perform_async(user.id, answer.id, "twitter") - end - end - - context "when answer doesn't exist" do - it "prevents the job from retrying and logs a message" do - answer.destroy! - expect { subject }.to change(ShareWorker.jobs, :size).by(1) - expect { ShareWorker.drain }.to change(ShareWorker.jobs, :size).by(-1) - expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} for user ##{user.id} to Twitter but the user/answer/service did not exist (likely deleted), will not retry.") - end - end - - context "when answer exists" do - it "handles Twitter::Error::DuplicateStatus" do - allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::DuplicateStatus) - subject - ShareWorker.drain - expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the status was already posted.") - end - - it "handles Twitter::Error::Unauthorized" do - allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Unauthorized) - subject - ShareWorker.drain - expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the token has expired or been revoked.") - end - - it "revokes the service connection when Twitter::Error::Unauthorized is raised" do - allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Unauthorized) - subject - expect { ShareWorker.drain }.to change { Services::Twitter.count }.by(-1) - end - - it "sends the user a notification when Twitter::Error::Unauthorized is raised" do - allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Unauthorized) - subject - expect { ShareWorker.drain }.to change { Notification::ServiceTokenExpired.count }.by(1) - end - - it "handles Twitter::Error::Forbidden" do - allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Forbidden) - subject - ShareWorker.drain - expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the account is suspended.") - end - - it "retries on unhandled exceptions" do - expect { subject }.to change(ShareWorker.jobs, :size).by(1) - expect { ShareWorker.drain }.to raise_error(Twitter::Error::BadRequest) - expect(Sidekiq.logger).to have_received(:info) - end - end - end -end