logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

delivery_worker_spec.rb (2557B)


  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Pubsubhubbub::DeliveryWorker do
  4. include RoutingHelper
  5. subject { described_class.new }
  6. let(:payload) { 'test' }
  7. describe 'perform' do
  8. it 'raises when subscription does not exist' do
  9. expect { subject.perform 123, payload }.to raise_error(ActiveRecord::RecordNotFound)
  10. end
  11. it 'does not attempt to deliver when domain blocked' do
  12. _domain_block = Fabricate(:domain_block, domain: 'example.com', severity: :suspend)
  13. subscription = Fabricate(:subscription, callback_url: 'https://example.com/api', last_successful_delivery_at: 2.days.ago)
  14. subject.perform(subscription.id, payload)
  15. expect(subscription.reload.last_successful_delivery_at).to be_within(2).of(2.days.ago)
  16. end
  17. it 'raises when request fails' do
  18. subscription = Fabricate(:subscription)
  19. stub_request_to_respond_with(subscription, 500)
  20. expect { subject.perform(subscription.id, payload) }.to raise_error Mastodon::UnexpectedResponseError
  21. end
  22. it 'updates subscriptions when delivery succeeds' do
  23. subscription = Fabricate(:subscription)
  24. stub_request_to_respond_with(subscription, 200)
  25. subject.perform(subscription.id, payload)
  26. expect(subscription.reload.last_successful_delivery_at).to be_within(2).of(Time.now.utc)
  27. end
  28. it 'updates subscription without a secret when delivery succeeds' do
  29. subscription = Fabricate(:subscription, secret: nil)
  30. stub_request_to_respond_with(subscription, 200)
  31. subject.perform(subscription.id, payload)
  32. expect(subscription.reload.last_successful_delivery_at).to be_within(2).of(Time.now.utc)
  33. end
  34. def stub_request_to_respond_with(subscription, code)
  35. stub_request(:post, 'http://example.com/callback')
  36. .with(body: payload, headers: expected_headers(subscription))
  37. .to_return(status: code, body: '', headers: {})
  38. end
  39. def expected_headers(subscription)
  40. {
  41. 'Connection' => 'close',
  42. 'Content-Type' => 'application/atom+xml',
  43. 'Host' => 'example.com',
  44. 'Link' => "<https://#{Rails.configuration.x.local_domain}/api/push>; rel=\"hub\", <https://#{Rails.configuration.x.local_domain}/users/#{subscription.account.username}.atom>; rel=\"self\"",
  45. }.tap do |basic|
  46. known_digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), subscription.secret.to_s, payload)
  47. basic.merge('X-Hub-Signature' => "sha1=#{known_digest}") if subscription.secret?
  48. end
  49. end
  50. end
  51. end