fetch_replies_service_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'rails_helper'
  2. RSpec.describe ActivityPub::FetchRepliesService, type: :service do
  3. let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/account') }
  4. let(:status) { Fabricate(:status, account: actor) }
  5. let(:collection_uri) { 'http://example.com/replies/1' }
  6. let(:items) do
  7. [
  8. 'http://example.com/self-reply-1',
  9. 'http://example.com/self-reply-2',
  10. 'http://example.com/self-reply-3',
  11. 'http://other.com/other-reply-1',
  12. 'http://other.com/other-reply-2',
  13. 'http://other.com/other-reply-3',
  14. 'http://example.com/self-reply-4',
  15. 'http://example.com/self-reply-5',
  16. 'http://example.com/self-reply-6',
  17. ]
  18. end
  19. let(:payload) do
  20. {
  21. '@context': 'https://www.w3.org/ns/activitystreams',
  22. type: 'Collection',
  23. id: collection_uri,
  24. items: items,
  25. }.with_indifferent_access
  26. end
  27. subject { described_class.new }
  28. describe '#call' do
  29. context 'when the payload is a Collection with inlined replies' do
  30. context 'when there is a single reply, with the array compacted away' do
  31. let(:items) { 'http://example.com/self-reply-1' }
  32. it 'queues the expected worker' do
  33. allow(FetchReplyWorker).to receive(:push_bulk)
  34. subject.call(status, payload)
  35. expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1'])
  36. end
  37. end
  38. context 'when passing the collection itself' do
  39. it 'spawns workers for up to 5 replies on the same server' do
  40. expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
  41. subject.call(status, payload)
  42. end
  43. end
  44. context 'when passing the URL to the collection' do
  45. before do
  46. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  47. end
  48. it 'spawns workers for up to 5 replies on the same server' do
  49. expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
  50. subject.call(status, collection_uri)
  51. end
  52. end
  53. end
  54. context 'when the payload is an OrderedCollection with inlined replies' do
  55. let(:payload) do
  56. {
  57. '@context': 'https://www.w3.org/ns/activitystreams',
  58. type: 'OrderedCollection',
  59. id: collection_uri,
  60. orderedItems: items,
  61. }.with_indifferent_access
  62. end
  63. context 'when passing the collection itself' do
  64. it 'spawns workers for up to 5 replies on the same server' do
  65. expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
  66. subject.call(status, payload)
  67. end
  68. end
  69. context 'when passing the URL to the collection' do
  70. before do
  71. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  72. end
  73. it 'spawns workers for up to 5 replies on the same server' do
  74. expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
  75. subject.call(status, collection_uri)
  76. end
  77. end
  78. end
  79. context 'when the payload is a paginated Collection with inlined replies' do
  80. let(:payload) do
  81. {
  82. '@context': 'https://www.w3.org/ns/activitystreams',
  83. type: 'Collection',
  84. id: collection_uri,
  85. first: {
  86. type: 'CollectionPage',
  87. partOf: collection_uri,
  88. items: items,
  89. }
  90. }.with_indifferent_access
  91. end
  92. context 'when passing the collection itself' do
  93. it 'spawns workers for up to 5 replies on the same server' do
  94. expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
  95. subject.call(status, payload)
  96. end
  97. end
  98. context 'when passing the URL to the collection' do
  99. before do
  100. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  101. end
  102. it 'spawns workers for up to 5 replies on the same server' do
  103. expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
  104. subject.call(status, collection_uri)
  105. end
  106. end
  107. end
  108. end
  109. end