announce_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. }.with_indifferent_access
  14. end
  15. let(:unknown_object_json) do
  16. {
  17. '@context': 'https://www.w3.org/ns/activitystreams',
  18. id: 'https://example.com/actor/hello-world',
  19. type: 'Note',
  20. attributedTo: 'https://example.com/actor',
  21. content: 'Hello world',
  22. to: 'http://example.com/followers',
  23. }
  24. end
  25. subject { described_class.new(json, sender) }
  26. describe '#perform' do
  27. context 'when sender is followed by a local account' do
  28. before do
  29. Fabricate(:account).follow!(sender)
  30. stub_request(:get, 'https://example.com/actor/hello-world').to_return(body: Oj.dump(unknown_object_json))
  31. subject.perform
  32. end
  33. context 'a known status' do
  34. let(:object_json) do
  35. ActivityPub::TagManager.instance.uri_for(status)
  36. end
  37. it 'creates a reblog by sender of status' do
  38. expect(sender.reblogged?(status)).to be true
  39. end
  40. end
  41. context 'an unknown status' do
  42. let(:object_json) { 'https://example.com/actor/hello-world' }
  43. it 'creates a reblog by sender of status' do
  44. reblog = sender.statuses.first
  45. expect(reblog).to_not be_nil
  46. expect(reblog.reblog.text).to eq 'Hello world'
  47. end
  48. end
  49. context 'self-boost of a previously unknown status with missing attributedTo' do
  50. let(:object_json) do
  51. {
  52. id: 'https://example.com/actor#bar',
  53. type: 'Note',
  54. content: 'Lorem ipsum',
  55. to: 'http://example.com/followers',
  56. }
  57. end
  58. it 'creates a reblog by sender of status' do
  59. expect(sender.reblogged?(sender.statuses.first)).to be true
  60. end
  61. end
  62. context 'self-boost of a previously unknown status with correct attributedTo' do
  63. let(:object_json) do
  64. {
  65. id: 'https://example.com/actor#bar',
  66. type: 'Note',
  67. content: 'Lorem ipsum',
  68. attributedTo: 'https://example.com/actor',
  69. to: 'http://example.com/followers',
  70. }
  71. end
  72. it 'creates a reblog by sender of status' do
  73. expect(sender.reblogged?(sender.statuses.first)).to be true
  74. end
  75. end
  76. end
  77. context 'when the status belongs to a local user' do
  78. before do
  79. subject.perform
  80. end
  81. let(:object_json) do
  82. ActivityPub::TagManager.instance.uri_for(status)
  83. end
  84. it 'creates a reblog by sender of status' do
  85. expect(sender.reblogged?(status)).to be true
  86. end
  87. end
  88. context 'when the sender is relayed' do
  89. let!(:relay_account) { Fabricate(:account, inbox_url: 'https://relay.example.com/inbox') }
  90. let!(:relay) { Fabricate(:relay, inbox_url: 'https://relay.example.com/inbox') }
  91. subject { described_class.new(json, sender, relayed_through_account: relay_account) }
  92. context 'and the relay is enabled' do
  93. before do
  94. relay.update(state: :accepted)
  95. subject.perform
  96. end
  97. let(:object_json) do
  98. {
  99. id: 'https://example.com/actor#bar',
  100. type: 'Note',
  101. content: 'Lorem ipsum',
  102. to: 'http://example.com/followers',
  103. }
  104. end
  105. it 'creates a reblog by sender of status' do
  106. expect(sender.statuses.count).to eq 2
  107. end
  108. end
  109. context 'and the relay is disabled' do
  110. before do
  111. subject.perform
  112. end
  113. let(:object_json) do
  114. {
  115. id: 'https://example.com/actor#bar',
  116. type: 'Note',
  117. content: 'Lorem ipsum',
  118. to: 'http://example.com/followers',
  119. }
  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. }
  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