announce_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Activity::Announce do
  4. subject { described_class.new(json, sender) }
  5. let(:sender) { Fabricate(:account, followers_url: 'http://example.com/followers', uri: 'https://example.com/actor', domain: 'example.com') }
  6. let(:recipient) { Fabricate(:account) }
  7. let(:status) { Fabricate(:status, account: recipient) }
  8. let(:json) do
  9. {
  10. '@context': 'https://www.w3.org/ns/activitystreams',
  11. id: 'foo',
  12. type: 'Announce',
  13. actor: 'https://example.com/actor',
  14. object: object_json,
  15. to: 'http://example.com/followers',
  16. }.with_indifferent_access
  17. end
  18. let(:unknown_object_json) do
  19. {
  20. '@context': 'https://www.w3.org/ns/activitystreams',
  21. id: 'https://example.com/actor/hello-world',
  22. type: 'Note',
  23. attributedTo: 'https://example.com/actor',
  24. content: 'Hello world',
  25. to: 'http://example.com/followers',
  26. }
  27. end
  28. describe '#perform' do
  29. context 'when sender is followed by a local account' do
  30. before do
  31. Fabricate(:account).follow!(sender)
  32. stub_request(:get, 'https://example.com/actor/hello-world').to_return(body: Oj.dump(unknown_object_json))
  33. subject.perform
  34. end
  35. context 'with known status' do
  36. let(:object_json) do
  37. ActivityPub::TagManager.instance.uri_for(status)
  38. end
  39. it 'creates a reblog by sender of status' do
  40. expect(sender.reblogged?(status)).to be true
  41. end
  42. end
  43. context 'with unknown status' do
  44. let(:object_json) { 'https://example.com/actor/hello-world' }
  45. it 'creates a reblog by sender of status' do
  46. reblog = sender.statuses.first
  47. expect(reblog).to_not be_nil
  48. expect(reblog.reblog.text).to eq 'Hello world'
  49. end
  50. end
  51. context 'when self-boost of a previously unknown status with correct attributedTo' do
  52. let(:object_json) do
  53. {
  54. id: 'https://example.com/actor#bar',
  55. type: 'Note',
  56. content: 'Lorem ipsum',
  57. attributedTo: 'https://example.com/actor',
  58. to: 'http://example.com/followers',
  59. }
  60. end
  61. it 'creates a reblog by sender of status' do
  62. expect(sender.reblogged?(sender.statuses.first)).to be true
  63. end
  64. end
  65. context 'when self-boost of a previously unknown status with correct attributedTo, inlined Collection in audience' do
  66. let(:object_json) do
  67. {
  68. id: 'https://example.com/actor#bar',
  69. type: 'Note',
  70. content: 'Lorem ipsum',
  71. attributedTo: 'https://example.com/actor',
  72. to: {
  73. type: 'OrderedCollection',
  74. id: 'http://example.com/followers',
  75. first: 'http://example.com/followers?page=true',
  76. },
  77. }
  78. end
  79. it 'creates a reblog by sender of status' do
  80. expect(sender.reblogged?(sender.statuses.first)).to be true
  81. end
  82. end
  83. end
  84. context 'when the status belongs to a local user' do
  85. before do
  86. subject.perform
  87. end
  88. let(:object_json) do
  89. ActivityPub::TagManager.instance.uri_for(status)
  90. end
  91. it 'creates a reblog by sender of status' do
  92. expect(sender.reblogged?(status)).to be true
  93. end
  94. end
  95. context 'when the sender is relayed' do
  96. subject { described_class.new(json, sender, relayed_through_actor: relay_account) }
  97. let!(:relay_account) { Fabricate(:account, inbox_url: 'https://relay.example.com/inbox', domain: 'relay.example.com') }
  98. let!(:relay) { Fabricate(:relay, inbox_url: 'https://relay.example.com/inbox') }
  99. let(:object_json) { 'https://example.com/actor/hello-world' }
  100. before do
  101. stub_request(:get, 'https://example.com/actor/hello-world').to_return(body: Oj.dump(unknown_object_json))
  102. end
  103. context 'when the relay is enabled' do
  104. before do
  105. relay.update(state: :accepted)
  106. subject.perform
  107. end
  108. it 'fetches the remote status' do
  109. expect(a_request(:get, 'https://example.com/actor/hello-world')).to have_been_made
  110. expect(Status.find_by(uri: 'https://example.com/actor/hello-world').text).to eq 'Hello world'
  111. end
  112. end
  113. context 'when the relay is disabled' do
  114. before do
  115. subject.perform
  116. end
  117. it 'does not fetch the remote status' do
  118. expect(a_request(:get, 'https://example.com/actor/hello-world')).to_not have_been_made
  119. expect(Status.find_by(uri: 'https://example.com/actor/hello-world')).to be_nil
  120. end
  121. it 'does not create anything' do
  122. expect(sender.statuses.count).to eq 0
  123. end
  124. end
  125. end
  126. context 'when the sender has no relevance to local activity' do
  127. before do
  128. subject.perform
  129. end
  130. let(:object_json) do
  131. {
  132. id: 'https://example.com/actor#bar',
  133. type: 'Note',
  134. content: 'Lorem ipsum',
  135. to: 'http://example.com/followers',
  136. attributedTo: 'https://example.com/actor',
  137. }
  138. end
  139. it 'does not create anything' do
  140. expect(sender.statuses.count).to eq 0
  141. end
  142. end
  143. end
  144. end