add_spec.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Activity::Add do
  4. let(:sender) { Fabricate(:account, featured_collection_url: 'https://example.com/featured', domain: 'example.com') }
  5. let(:status) { Fabricate(:status, account: sender, visibility: :private) }
  6. let(:json) do
  7. {
  8. '@context': 'https://www.w3.org/ns/activitystreams',
  9. id: 'foo',
  10. type: 'Add',
  11. actor: ActivityPub::TagManager.instance.uri_for(sender),
  12. object: ActivityPub::TagManager.instance.uri_for(status),
  13. target: sender.featured_collection_url,
  14. }.with_indifferent_access
  15. end
  16. describe '#perform' do
  17. subject { described_class.new(json, sender) }
  18. it 'creates a pin' do
  19. subject.perform
  20. expect(sender.pinned?(status)).to be true
  21. end
  22. context 'when status was not known before' do
  23. let(:service_stub) { instance_double(ActivityPub::FetchRemoteStatusService) }
  24. let(:json) do
  25. {
  26. '@context': 'https://www.w3.org/ns/activitystreams',
  27. id: 'foo',
  28. type: 'Add',
  29. actor: ActivityPub::TagManager.instance.uri_for(sender),
  30. object: 'https://example.com/unknown',
  31. target: sender.featured_collection_url,
  32. }.with_indifferent_access
  33. end
  34. before do
  35. allow(ActivityPub::FetchRemoteStatusService).to receive(:new).and_return(service_stub)
  36. end
  37. context 'when there is a local follower' do
  38. before do
  39. account = Fabricate(:account)
  40. account.follow!(sender)
  41. end
  42. it 'fetches the status and pins it' do
  43. allow(service_stub).to receive(:call) do |uri, id: true, on_behalf_of: nil, **|
  44. expect(uri).to eq 'https://example.com/unknown'
  45. expect(id).to be true
  46. expect(on_behalf_of&.following?(sender)).to be true
  47. status
  48. end
  49. subject.perform
  50. expect(service_stub).to have_received(:call)
  51. expect(sender.pinned?(status)).to be true
  52. end
  53. end
  54. context 'when there is no local follower' do
  55. it 'tries to fetch the status' do
  56. allow(service_stub).to receive(:call) do |uri, id: true, on_behalf_of: nil, **|
  57. expect(uri).to eq 'https://example.com/unknown'
  58. expect(id).to be true
  59. expect(on_behalf_of).to be_nil
  60. nil
  61. end
  62. subject.perform
  63. expect(service_stub).to have_received(:call)
  64. expect(sender.pinned?(status)).to be false
  65. end
  66. end
  67. end
  68. end
  69. end