flag_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Activity::Flag do
  4. let(:sender) { Fabricate(:account, username: 'example.com', domain: 'example.com', uri: 'http://example.com/actor') }
  5. let(:flagged) { Fabricate(:account) }
  6. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar') }
  7. let(:flag_id) { nil }
  8. let(:json) do
  9. {
  10. '@context': 'https://www.w3.org/ns/activitystreams',
  11. id: flag_id,
  12. type: 'Flag',
  13. content: 'Boo!!',
  14. actor: ActivityPub::TagManager.instance.uri_for(sender),
  15. object: [
  16. ActivityPub::TagManager.instance.uri_for(flagged),
  17. ActivityPub::TagManager.instance.uri_for(status),
  18. ],
  19. }.with_indifferent_access
  20. end
  21. describe '#perform' do
  22. subject { described_class.new(json, sender) }
  23. context 'when the reported status is public' do
  24. before do
  25. subject.perform
  26. end
  27. it 'creates a report' do
  28. report = Report.find_by(account: sender, target_account: flagged)
  29. expect(report).to_not be_nil
  30. expect(report.comment).to eq 'Boo!!'
  31. expect(report.status_ids).to eq [status.id]
  32. end
  33. end
  34. context 'when the report comment is excessively long' do
  35. subject do
  36. described_class.new({
  37. '@context': 'https://www.w3.org/ns/activitystreams',
  38. id: flag_id,
  39. type: 'Flag',
  40. content: long_comment,
  41. actor: ActivityPub::TagManager.instance.uri_for(sender),
  42. object: [
  43. ActivityPub::TagManager.instance.uri_for(flagged),
  44. ActivityPub::TagManager.instance.uri_for(status),
  45. ],
  46. }.with_indifferent_access, sender)
  47. end
  48. let(:long_comment) { Faker::Lorem.characters(number: 6000) }
  49. before do
  50. subject.perform
  51. end
  52. it 'creates a report but with a truncated comment' do
  53. report = Report.find_by(account: sender, target_account: flagged)
  54. expect(report).to_not be_nil
  55. expect(report.comment.length).to eq 5000
  56. expect(report.comment).to eq long_comment[0...5000]
  57. expect(report.status_ids).to eq [status.id]
  58. end
  59. end
  60. context 'when the reported status is private and should not be visible to the remote server' do
  61. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  62. before do
  63. subject.perform
  64. end
  65. it 'creates a report with no attached status' do
  66. report = Report.find_by(account: sender, target_account: flagged)
  67. expect(report).to_not be_nil
  68. expect(report.comment).to eq 'Boo!!'
  69. expect(report.status_ids).to eq []
  70. end
  71. end
  72. context 'when the reported status is private and the author has a follower on the remote instance' do
  73. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  74. let(:follower) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/users/account') }
  75. before do
  76. follower.follow!(flagged)
  77. subject.perform
  78. end
  79. it 'creates a report with the attached status' do
  80. report = Report.find_by(account: sender, target_account: flagged)
  81. expect(report).to_not be_nil
  82. expect(report.comment).to eq 'Boo!!'
  83. expect(report.status_ids).to eq [status.id]
  84. end
  85. end
  86. context 'when the reported status is private and the author mentions someone else on the remote instance' do
  87. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  88. let(:mentioned) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/users/account') }
  89. before do
  90. status.mentions.create(account: mentioned)
  91. subject.perform
  92. end
  93. it 'creates a report with the attached status' do
  94. report = Report.find_by(account: sender, target_account: flagged)
  95. expect(report).to_not be_nil
  96. expect(report.comment).to eq 'Boo!!'
  97. expect(report.status_ids).to eq [status.id]
  98. end
  99. end
  100. context 'when the reported status is private and the author mentions someone else on the local instance' do
  101. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  102. let(:mentioned) { Fabricate(:account) }
  103. before do
  104. status.mentions.create(account: mentioned)
  105. subject.perform
  106. end
  107. it 'creates a report with no attached status' do
  108. report = Report.find_by(account: sender, target_account: flagged)
  109. expect(report).to_not be_nil
  110. expect(report.comment).to eq 'Boo!!'
  111. expect(report.status_ids).to eq []
  112. end
  113. end
  114. context 'when an account is passed but no status' do
  115. let(:mentioned) { Fabricate(:account) }
  116. let(:json) do
  117. {
  118. '@context': 'https://www.w3.org/ns/activitystreams',
  119. id: flag_id,
  120. type: 'Flag',
  121. content: 'Boo!!',
  122. actor: ActivityPub::TagManager.instance.uri_for(sender),
  123. object: [
  124. ActivityPub::TagManager.instance.uri_for(flagged),
  125. ],
  126. }.with_indifferent_access
  127. end
  128. before do
  129. subject.perform
  130. end
  131. it 'creates a report with no attached status' do
  132. report = Report.find_by(account: sender, target_account: flagged)
  133. expect(report).to_not be_nil
  134. expect(report.comment).to eq 'Boo!!'
  135. expect(report.status_ids).to eq []
  136. end
  137. end
  138. end
  139. describe '#perform with a defined uri' do
  140. subject { described_class.new(json, sender) }
  141. let(:flag_id) { 'http://example.com/reports/1' }
  142. before do
  143. subject.perform
  144. end
  145. it 'creates a report' do
  146. report = Report.find_by(account: sender, target_account: flagged)
  147. expect(report).to_not be_nil
  148. expect(report.comment).to eq 'Boo!!'
  149. expect(report.status_ids).to eq [status.id]
  150. expect(report.uri).to eq flag_id
  151. end
  152. end
  153. end