report_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Report do
  4. describe 'statuses' do
  5. it 'returns the statuses for the report' do
  6. status = Fabricate(:status)
  7. _other = Fabricate(:status)
  8. report = Fabricate(:report, status_ids: [status.id])
  9. expect(report.statuses).to eq [status]
  10. end
  11. end
  12. describe 'media_attachments_count' do
  13. it 'returns count of media attachments in statuses' do
  14. status1 = Fabricate(:status, ordered_media_attachment_ids: [1, 2])
  15. status2 = Fabricate(:status, ordered_media_attachment_ids: [5])
  16. report = Fabricate(:report, status_ids: [status1.id, status2.id])
  17. expect(report.media_attachments_count).to eq 3
  18. end
  19. end
  20. describe 'assign_to_self!' do
  21. subject { report.assigned_account_id }
  22. let(:report) { Fabricate(:report, assigned_account_id: original_account) }
  23. let(:original_account) { Fabricate(:account) }
  24. let(:current_account) { Fabricate(:account) }
  25. before do
  26. report.assign_to_self!(current_account)
  27. end
  28. it 'assigns to a given account' do
  29. expect(subject).to eq current_account.id
  30. end
  31. end
  32. describe 'unassign!' do
  33. subject { report.assigned_account_id }
  34. let(:report) { Fabricate(:report, assigned_account_id: account.id) }
  35. let(:account) { Fabricate(:account) }
  36. before do
  37. report.unassign!
  38. end
  39. it 'unassigns' do
  40. expect(subject).to be_nil
  41. end
  42. end
  43. describe 'resolve!' do
  44. subject(:report) { Fabricate(:report, action_taken_at: nil, action_taken_by_account_id: nil) }
  45. let(:acting_account) { Fabricate(:account) }
  46. before do
  47. report.resolve!(acting_account)
  48. end
  49. it 'records action taken' do
  50. expect(report.action_taken?).to be true
  51. expect(report.action_taken_by_account_id).to eq acting_account.id
  52. end
  53. end
  54. describe 'unresolve!' do
  55. subject(:report) { Fabricate(:report, action_taken_at: Time.now.utc, action_taken_by_account_id: acting_account.id) }
  56. let(:acting_account) { Fabricate(:account) }
  57. before do
  58. report.unresolve!
  59. end
  60. it 'unresolves' do
  61. expect(report.action_taken?).to be false
  62. expect(report.action_taken_by_account_id).to be_nil
  63. end
  64. end
  65. describe 'unresolved?' do
  66. subject { report.unresolved? }
  67. let(:report) { Fabricate(:report, action_taken_at: action_taken) }
  68. context 'when action is taken' do
  69. let(:action_taken) { Time.now.utc }
  70. it { is_expected.to be false }
  71. end
  72. context 'when action not is taken' do
  73. let(:action_taken) { nil }
  74. it { is_expected.to be true }
  75. end
  76. end
  77. describe 'history' do
  78. subject(:action_logs) { report.history }
  79. let(:report) { Fabricate(:report, target_account_id: target_account.id, status_ids: [status.id], created_at: 3.days.ago, updated_at: 1.day.ago) }
  80. let(:target_account) { Fabricate(:account) }
  81. let(:status) { Fabricate(:status) }
  82. let(:account_warning) { Fabricate(:account_warning, report_id: report.id) }
  83. before do
  84. Fabricate(:action_log, target_type: 'Report', account_id: target_account.id, target_id: report.id, created_at: 2.days.ago)
  85. Fabricate(:action_log, target_type: 'Account', account_id: target_account.id, target_id: report.target_account_id, created_at: 2.days.ago)
  86. Fabricate(:action_log, target_type: 'Status', account_id: target_account.id, target_id: status.id, created_at: 2.days.ago)
  87. Fabricate(:action_log, target_type: 'AccountWarning', account_id: target_account.id, target_id: account_warning.id, created_at: 2.days.ago)
  88. end
  89. it 'returns expected logs' do
  90. expect(action_logs)
  91. .to have_attributes(count: 4)
  92. .and include(have_attributes(target_type: 'Account'))
  93. .and include(have_attributes(target_type: 'AccountWarning'))
  94. .and include(have_attributes(target_type: 'Report'))
  95. .and include(have_attributes(target_type: 'Status'))
  96. end
  97. end
  98. describe 'validations' do
  99. let(:remote_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
  100. it 'is invalid if comment is longer than character limit and reporter is local' do
  101. report = Fabricate.build(:report, comment: comment_over_limit)
  102. expect(report.valid?).to be false
  103. expect(report).to model_have_error_on_field(:comment)
  104. end
  105. it 'is valid if comment is longer than character limit and reporter is not local' do
  106. report = Fabricate.build(:report, account: remote_account, comment: comment_over_limit)
  107. expect(report.valid?).to be true
  108. end
  109. it 'is invalid if it references invalid rules' do
  110. report = Fabricate.build(:report, category: :violation, rule_ids: [-1])
  111. expect(report.valid?).to be false
  112. expect(report).to model_have_error_on_field(:rule_ids)
  113. end
  114. it 'is invalid if it references rules but category is not "violation"' do
  115. rule = Fabricate(:rule)
  116. report = Fabricate.build(:report, category: :spam, rule_ids: rule.id)
  117. expect(report.valid?).to be false
  118. expect(report).to model_have_error_on_field(:rule_ids)
  119. end
  120. def comment_over_limit
  121. 'a' * described_class::COMMENT_SIZE_LIMIT * 2
  122. end
  123. end
  124. end