synchronize_followers_service_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::SynchronizeFollowersService, type: :service 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. [
  13. ActivityPub::TagManager.instance.uri_for(alice),
  14. ActivityPub::TagManager.instance.uri_for(eve),
  15. ActivityPub::TagManager.instance.uri_for(mallory),
  16. ]
  17. end
  18. let(:payload) do
  19. {
  20. '@context': 'https://www.w3.org/ns/activitystreams',
  21. type: 'Collection',
  22. id: collection_uri,
  23. items: items,
  24. }.with_indifferent_access
  25. end
  26. shared_examples 'synchronizes followers' do
  27. before do
  28. alice.follow!(actor)
  29. bob.follow!(actor)
  30. mallory.request_follow!(actor)
  31. allow(ActivityPub::DeliveryWorker).to receive(:perform_async)
  32. subject.call(actor, collection_uri)
  33. end
  34. it 'keeps expected followers' do
  35. expect(alice.following?(actor)).to be true
  36. end
  37. it 'removes local followers not in the remote list' do
  38. expect(bob.following?(actor)).to be false
  39. end
  40. it 'converts follow requests to follow relationships when they have been accepted' do
  41. expect(mallory.following?(actor)).to be true
  42. end
  43. it 'sends an Undo Follow to the actor' do
  44. expect(ActivityPub::DeliveryWorker).to have_received(:perform_async).with(anything, eve.id, actor.inbox_url)
  45. end
  46. end
  47. describe '#call' do
  48. context 'when the endpoint is a Collection of actor URIs' do
  49. before do
  50. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload))
  51. end
  52. it_behaves_like 'synchronizes followers'
  53. end
  54. context 'when the endpoint is an OrderedCollection of actor URIs' 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. before do
  64. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload))
  65. end
  66. it_behaves_like 'synchronizes followers'
  67. end
  68. context 'when the endpoint is a paginated Collection of actor URIs' do
  69. let(:payload) do
  70. {
  71. '@context': 'https://www.w3.org/ns/activitystreams',
  72. type: 'Collection',
  73. id: collection_uri,
  74. first: {
  75. type: 'CollectionPage',
  76. partOf: collection_uri,
  77. items: items,
  78. },
  79. }.with_indifferent_access
  80. end
  81. before do
  82. stub_request(:get, collection_uri).to_return(status: 200, body: Oj.dump(payload))
  83. end
  84. it_behaves_like 'synchronizes followers'
  85. end
  86. end
  87. end