logo

mastofe

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

confirmation_worker_spec.rb (3040B)


  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Pubsubhubbub::ConfirmationWorker do
  4. include RoutingHelper
  5. subject { described_class.new }
  6. let!(:alice) { Fabricate(:account, username: 'alice') }
  7. let!(:subscription) { Fabricate(:subscription, account: alice, callback_url: 'http://example.com/api', confirmed: false, expires_at: 3.days.from_now, secret: nil) }
  8. describe 'perform' do
  9. describe 'with subscribe mode' do
  10. it 'confirms and updates subscription when challenge matches' do
  11. stub_random_value
  12. stub_request(:get, url_for_mode('subscribe'))
  13. .with(headers: http_headers)
  14. .to_return(status: 200, body: challenge_value, headers: {})
  15. seconds = 10.days.seconds.to_i
  16. subject.perform(subscription.id, 'subscribe', 'asdf', seconds)
  17. subscription.reload
  18. expect(subscription.secret).to eq 'asdf'
  19. expect(subscription.confirmed).to eq true
  20. expect(subscription.expires_at).to be_within(5).of(10.days.from_now)
  21. end
  22. it 'does not update subscription when challenge does not match' do
  23. stub_random_value
  24. stub_request(:get, url_for_mode('subscribe'))
  25. .with(headers: http_headers)
  26. .to_return(status: 200, body: 'wrong value', headers: {})
  27. seconds = 10.days.seconds.to_i
  28. subject.perform(subscription.id, 'subscribe', 'asdf', seconds)
  29. subscription.reload
  30. expect(subscription.secret).to be_blank
  31. expect(subscription.confirmed).to eq false
  32. expect(subscription.expires_at).to be_within(5).of(3.days.from_now)
  33. end
  34. end
  35. describe 'with unsubscribe mode' do
  36. it 'confirms and destroys subscription when challenge matches' do
  37. stub_random_value
  38. stub_request(:get, url_for_mode('unsubscribe'))
  39. .with(headers: http_headers)
  40. .to_return(status: 200, body: challenge_value, headers: {})
  41. seconds = 10.days.seconds.to_i
  42. subject.perform(subscription.id, 'unsubscribe', 'asdf', seconds)
  43. expect { subscription.reload }.to raise_error(ActiveRecord::RecordNotFound)
  44. end
  45. it 'does not destroy subscription when challenge does not match' do
  46. stub_random_value
  47. stub_request(:get, url_for_mode('unsubscribe'))
  48. .with(headers: http_headers)
  49. .to_return(status: 200, body: 'wrong value', headers: {})
  50. seconds = 10.days.seconds.to_i
  51. subject.perform(subscription.id, 'unsubscribe', 'asdf', seconds)
  52. expect { subscription.reload }.not_to raise_error
  53. end
  54. end
  55. end
  56. def url_for_mode(mode)
  57. "http://example.com/api?hub.challenge=#{challenge_value}&hub.lease_seconds=863999&hub.mode=#{mode}&hub.topic=https://#{Rails.configuration.x.local_domain}/users/alice.atom"
  58. end
  59. def stub_random_value
  60. allow(SecureRandom).to receive(:hex).and_return(challenge_value)
  61. end
  62. def challenge_value
  63. '1a2s3d4f'
  64. end
  65. def http_headers
  66. { 'Connection' => 'close', 'Host' => 'example.com' }
  67. end
  68. end