synchronize_followers_service_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::SynchronizeFollowersService do
  4. subject { described_class.new }
  5. let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/account', inbox_url: 'http://example.com/inbox') }
  6. let(:alice) { Fabricate(:account, username: 'alice') }
  7. let(:bob) { Fabricate(:account, username: 'bob') }
  8. let(:eve) { Fabricate(:account, username: 'eve') }
  9. let(:mallory) { Fabricate(:account, username: 'mallory') }
  10. let(:collection_uri) { 'http://example.com/partial-followers' }
  11. let(:items) do
  12. [alice, eve, mallory].map do |account|
  13. ActivityPub::TagManager.instance.uri_for(account)
  14. end
  15. end
  16. let(:payload) do
  17. {
  18. '@context': 'https://www.w3.org/ns/activitystreams',
  19. type: 'Collection',
  20. id: collection_uri,
  21. items: items,
  22. }.with_indifferent_access
  23. end
  24. shared_examples 'synchronizes followers' do
  25. before do
  26. alice.follow!(actor)
  27. bob.follow!(actor)
  28. mallory.request_follow!(actor)
  29. allow(ActivityPub::DeliveryWorker).to receive(:perform_async)
  30. subject.call(actor, collection_uri)
  31. end
  32. it 'maintains following records and sends Undo Follow to actor' do
  33. expect(alice)
  34. .to be_following(actor) # Keep expected followers
  35. expect(bob)
  36. .to_not be_following(actor) # Remove local followers not in remote list
  37. expect(mallory)
  38. .to be_following(actor) # Convert follow request to follow when accepted
  39. expect(ActivityPub::DeliveryWorker)
  40. .to have_received(:perform_async).with(anything, eve.id, actor.inbox_url) # Send Undo Follow to actor
  41. end
  42. end
  43. describe '#call' do
  44. context 'when the endpoint is a Collection of actor URIs' 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_behaves_like 'synchronizes followers'
  49. end
  50. context 'when the endpoint is an OrderedCollection of actor URIs' do
  51. let(:payload) do
  52. {
  53. '@context': 'https://www.w3.org/ns/activitystreams',
  54. type: 'OrderedCollection',
  55. id: collection_uri,
  56. orderedItems: items,
  57. }.with_indifferent_access
  58. end
  59. before do
  60. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  61. end
  62. it_behaves_like 'synchronizes followers'
  63. end
  64. context 'when the endpoint is a paginated Collection of actor URIs' do
  65. let(:payload) do
  66. {
  67. '@context': 'https://www.w3.org/ns/activitystreams',
  68. type: 'Collection',
  69. id: collection_uri,
  70. first: {
  71. type: 'CollectionPage',
  72. partOf: collection_uri,
  73. items: items,
  74. },
  75. }.with_indifferent_access
  76. end
  77. before do
  78. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  79. end
  80. it_behaves_like 'synchronizes followers'
  81. end
  82. end
  83. end