report_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. 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. before do
  83. Fabricate(:action_log, target_type: 'Report', account_id: target_account.id, target_id: report.id, created_at: 2.days.ago)
  84. Fabricate(:action_log, target_type: 'Account', account_id: target_account.id, target_id: report.target_account_id, created_at: 2.days.ago)
  85. Fabricate(:action_log, target_type: 'Status', account_id: target_account.id, target_id: status.id, created_at: 2.days.ago)
  86. end
  87. it 'returns right logs' do
  88. expect(action_logs.count).to eq 3
  89. end
  90. end
  91. describe 'validations' do
  92. let(:remote_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
  93. it 'is invalid if comment is longer than 1000 characters only if reporter is local' do
  94. report = Fabricate.build(:report, comment: Faker::Lorem.characters(number: 1001))
  95. expect(report.valid?).to be false
  96. expect(report).to model_have_error_on_field(:comment)
  97. end
  98. it 'is valid if comment is longer than 1000 characters and reporter is not local' do
  99. report = Fabricate.build(:report, account: remote_account, comment: Faker::Lorem.characters(number: 1001))
  100. expect(report.valid?).to be true
  101. end
  102. it 'is invalid if it references invalid rules' do
  103. report = Fabricate.build(:report, category: :violation, rule_ids: [-1])
  104. expect(report.valid?).to be false
  105. expect(report).to model_have_error_on_field(:rule_ids)
  106. end
  107. it 'is invalid if it references rules but category is not "violation"' do
  108. rule = Fabricate(:rule)
  109. report = Fabricate.build(:report, category: :spam, rule_ids: rule.id)
  110. expect(report.valid?).to be false
  111. expect(report).to model_have_error_on_field(:rule_ids)
  112. end
  113. end
  114. end