announce_spec.rb 5.2 KB

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