flag_spec.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. expect(report.application).to be_nil
  33. end
  34. end
  35. context 'when the report comment is excessively long' do
  36. subject do
  37. described_class.new({
  38. '@context': 'https://www.w3.org/ns/activitystreams',
  39. id: flag_id,
  40. type: 'Flag',
  41. content: long_comment,
  42. actor: ActivityPub::TagManager.instance.uri_for(sender),
  43. object: [
  44. ActivityPub::TagManager.instance.uri_for(flagged),
  45. ActivityPub::TagManager.instance.uri_for(status),
  46. ],
  47. }.with_indifferent_access, sender)
  48. end
  49. let(:long_comment) { 'a' * described_class::COMMENT_SIZE_LIMIT * 2 }
  50. before do
  51. subject.perform
  52. end
  53. it 'creates a report but with a truncated comment' do
  54. report = Report.find_by(account: sender, target_account: flagged)
  55. expect(report)
  56. .to be_present
  57. .and have_attributes(status_ids: [status.id])
  58. expect(report.comment)
  59. .to have_attributes(length: described_class::COMMENT_SIZE_LIMIT)
  60. .and eq(long_comment[0...described_class::COMMENT_SIZE_LIMIT])
  61. end
  62. end
  63. context 'when the reported status is private and should not be visible to the remote server' do
  64. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  65. before do
  66. subject.perform
  67. end
  68. it 'creates a report with no attached status' do
  69. report = Report.find_by(account: sender, target_account: flagged)
  70. expect(report).to_not be_nil
  71. expect(report.comment).to eq 'Boo!!'
  72. expect(report.status_ids).to eq []
  73. end
  74. end
  75. context 'when the reported status is private and the author has a follower on the remote instance' do
  76. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  77. let(:follower) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/users/account') }
  78. before do
  79. follower.follow!(flagged)
  80. subject.perform
  81. end
  82. it 'creates a report with the attached status' do
  83. report = Report.find_by(account: sender, target_account: flagged)
  84. expect(report).to_not be_nil
  85. expect(report.comment).to eq 'Boo!!'
  86. expect(report.status_ids).to eq [status.id]
  87. end
  88. end
  89. context 'when the reported status is private and the author mentions someone else on the remote instance' do
  90. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  91. let(:mentioned) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/users/account') }
  92. before do
  93. status.mentions.create(account: mentioned)
  94. subject.perform
  95. end
  96. it 'creates a report with the attached status' do
  97. report = Report.find_by(account: sender, target_account: flagged)
  98. expect(report).to_not be_nil
  99. expect(report.comment).to eq 'Boo!!'
  100. expect(report.status_ids).to eq [status.id]
  101. end
  102. end
  103. context 'when the reported status is private and the author mentions someone else on the local instance' do
  104. let(:status) { Fabricate(:status, account: flagged, uri: 'foobar', visibility: :private) }
  105. let(:mentioned) { Fabricate(:account) }
  106. before do
  107. status.mentions.create(account: mentioned)
  108. subject.perform
  109. end
  110. it 'creates a report with no attached status' do
  111. report = Report.find_by(account: sender, target_account: flagged)
  112. expect(report).to_not be_nil
  113. expect(report.comment).to eq 'Boo!!'
  114. expect(report.status_ids).to eq []
  115. end
  116. end
  117. context 'when an account is passed but no status' do
  118. let(:mentioned) { Fabricate(:account) }
  119. let(:json) do
  120. {
  121. '@context': 'https://www.w3.org/ns/activitystreams',
  122. id: flag_id,
  123. type: 'Flag',
  124. content: 'Boo!!',
  125. actor: ActivityPub::TagManager.instance.uri_for(sender),
  126. object: [
  127. ActivityPub::TagManager.instance.uri_for(flagged),
  128. ],
  129. }.with_indifferent_access
  130. end
  131. before do
  132. subject.perform
  133. end
  134. it 'creates a report with no attached status' do
  135. report = Report.find_by(account: sender, target_account: flagged)
  136. expect(report).to_not be_nil
  137. expect(report.comment).to eq 'Boo!!'
  138. expect(report.status_ids).to eq []
  139. end
  140. end
  141. end
  142. describe '#perform with a defined uri' do
  143. subject { described_class.new(json, sender) }
  144. let(:flag_id) { 'http://example.com/reports/1' }
  145. before do
  146. subject.perform
  147. end
  148. it 'creates a report' do
  149. report = Report.find_by(account: sender, target_account: flagged)
  150. expect(report).to_not be_nil
  151. expect(report.comment).to eq 'Boo!!'
  152. expect(report.status_ids).to eq [status.id]
  153. expect(report.uri).to eq flag_id
  154. end
  155. end
  156. end