block_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Activity::Block do
  4. subject { described_class.new(json, sender) }
  5. let(:sender) { Fabricate(:account) }
  6. let(:recipient) { Fabricate(:account) }
  7. let(:json) do
  8. {
  9. '@context': 'https://www.w3.org/ns/activitystreams',
  10. id: 'foo',
  11. type: 'Block',
  12. actor: ActivityPub::TagManager.instance.uri_for(sender),
  13. object: ActivityPub::TagManager.instance.uri_for(recipient),
  14. }.with_indifferent_access
  15. end
  16. describe '#perform' do
  17. context 'when the recipient does not follow the sender' do
  18. it 'creates a block from sender to recipient' do
  19. subject.perform
  20. expect(sender)
  21. .to be_blocking(recipient)
  22. end
  23. end
  24. context 'when the recipient is already blocked' do
  25. before { sender.block!(recipient, uri: 'old') }
  26. it 'creates a block from sender to recipient and sets uri to last received block activity' do
  27. subject.perform
  28. expect(sender)
  29. .to be_blocking(recipient)
  30. expect(sender.block_relationships.find_by(target_account: recipient).uri)
  31. .to eq 'foo'
  32. end
  33. end
  34. context 'when the recipient follows the sender' do
  35. before { recipient.follow!(sender) }
  36. it 'creates a block from sender to recipient and ensures recipient not following sender' do
  37. subject.perform
  38. expect(sender)
  39. .to be_blocking(recipient)
  40. expect(recipient)
  41. .to_not be_following(sender)
  42. end
  43. end
  44. context 'when a matching undo has been received first' do
  45. let(:undo_json) do
  46. {
  47. '@context': 'https://www.w3.org/ns/activitystreams',
  48. id: 'bar',
  49. type: 'Undo',
  50. actor: ActivityPub::TagManager.instance.uri_for(sender),
  51. object: json,
  52. }.with_indifferent_access
  53. end
  54. before do
  55. recipient.follow!(sender)
  56. ActivityPub::Activity::Undo.new(undo_json, sender).perform
  57. end
  58. it 'does not create a block from sender to recipient and ensures recipient not following sender' do
  59. subject.perform
  60. expect(sender)
  61. .to_not be_blocking(recipient)
  62. expect(recipient)
  63. .to_not be_following(sender)
  64. end
  65. end
  66. end
  67. end