闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.1 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Pubsubhubbub::SubscribeService, type: :service do
  4. describe '#call' do
  5. subject { described_class.new }
  6. let(:user_account) { Fabricate(:account) }
  7. context 'with a nil account' do
  8. it 'returns the invalid topic status results' do
  9. result = service_call(account: nil)
  10. expect(result).to eq invalid_topic_status
  11. end
  12. end
  13. context 'with an invalid callback url' do
  14. it 'returns invalid callback status when callback is blank' do
  15. result = service_call(callback: '')
  16. expect(result).to eq invalid_callback_status
  17. end
  18. it 'returns invalid callback status when callback is not a URI' do
  19. result = service_call(callback: 'invalid-hostname')
  20. expect(result).to eq invalid_callback_status
  21. end
  22. end
  23. context 'with a blocked domain in the callback' do
  24. it 'returns callback not allowed' do
  25. Fabricate(:domain_block, domain: 'test.host', severity: :suspend)
  26. result = service_call(callback: 'https://test.host/api')
  27. expect(result).to eq not_allowed_callback_status
  28. end
  29. end
  30. context 'with a valid account and callback' do
  31. it 'returns success status and confirms subscription' do
  32. allow(Pubsubhubbub::ConfirmationWorker).to receive(:perform_async).and_return(nil)
  33. subscription = Fabricate(:subscription, account: user_account)
  34. result = service_call(callback: subscription.callback_url)
  35. expect(result).to eq success_status
  36. expect(Pubsubhubbub::ConfirmationWorker).to have_received(:perform_async).with(subscription.id, 'subscribe', 'asdf', 3600)
  37. end
  38. end
  39. end
  40. def service_call(account: user_account, callback: 'https://callback.host', secret: 'asdf', lease_seconds: 3600)
  41. subject.call(account, callback, secret, lease_seconds)
  42. end
  43. def invalid_topic_status
  44. ['Invalid topic URL', 422]
  45. end
  46. def invalid_callback_status
  47. ['Invalid callback URL', 422]
  48. end
  49. def not_allowed_callback_status
  50. ['Callback URL not allowed', 403]
  51. end
  52. def success_status
  53. ['', 202]
  54. end
  55. end