fetch_replies_service_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 passing the collection itself' do
  31. it 'spawns workers for up to 5 replies on the same server' do
  32. 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'])
  33. subject.call(status, payload)
  34. end
  35. end
  36. context 'when passing the URL to the collection' do
  37. before do
  38. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  39. end
  40. it 'spawns workers for up to 5 replies on the same server' do
  41. 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'])
  42. subject.call(status, collection_uri)
  43. end
  44. end
  45. end
  46. context 'when the payload is an OrderedCollection with inlined replies' do
  47. let(:payload) do
  48. {
  49. '@context': 'https://www.w3.org/ns/activitystreams',
  50. type: 'OrderedCollection',
  51. id: collection_uri,
  52. orderedItems: items,
  53. }.with_indifferent_access
  54. end
  55. context 'when passing the collection itself' do
  56. it 'spawns workers for up to 5 replies on the same server' do
  57. 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'])
  58. subject.call(status, payload)
  59. end
  60. end
  61. context 'when passing the URL to the collection' do
  62. before do
  63. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  64. end
  65. it 'spawns workers for up to 5 replies on the same server' do
  66. 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'])
  67. subject.call(status, collection_uri)
  68. end
  69. end
  70. end
  71. context 'when the payload is a paginated Collection with inlined replies' do
  72. let(:payload) do
  73. {
  74. '@context': 'https://www.w3.org/ns/activitystreams',
  75. type: 'Collection',
  76. id: collection_uri,
  77. first: {
  78. type: 'CollectionPage',
  79. partOf: collection_uri,
  80. items: items,
  81. }
  82. }.with_indifferent_access
  83. end
  84. context 'when passing the collection itself' do
  85. it 'spawns workers for up to 5 replies on the same server' do
  86. 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'])
  87. subject.call(status, payload)
  88. end
  89. end
  90. context 'when passing the URL to the collection' do
  91. before do
  92. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  93. end
  94. it 'spawns workers for up to 5 replies on the same server' do
  95. 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'])
  96. subject.call(status, collection_uri)
  97. end
  98. end
  99. end
  100. end
  101. end