fetch_replies_service_spec.rb 5.6 KB

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