You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.6 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe SpamCheck do
  4. let!(:sender) { Fabricate(:account) }
  5. let!(:alice) { Fabricate(:account, username: 'alice') }
  6. let!(:bob) { Fabricate(:account, username: 'bob') }
  7. def status_with_html(text, options = {})
  8. status = PostStatusService.new.call(sender, { text: text }.merge(options))
  9. status.update_columns(text: Formatter.instance.format(status), local: false)
  10. status
  11. end
  12. describe '#hashable_text' do
  13. it 'removes mentions from HTML for remote statuses' do
  14. status = status_with_html('@alice Hello')
  15. expect(described_class.new(status).hashable_text).to eq 'hello'
  16. end
  17. it 'removes mentions from text for local statuses' do
  18. status = PostStatusService.new.call(alice, text: "Hey @#{sender.username}, how are you?")
  19. expect(described_class.new(status).hashable_text).to eq 'hey , how are you?'
  20. end
  21. end
  22. describe '#insufficient_data?' do
  23. it 'returns true when there is no text' do
  24. status = status_with_html('@alice')
  25. expect(described_class.new(status).insufficient_data?).to be true
  26. end
  27. it 'returns false when there is text' do
  28. status = status_with_html('@alice h')
  29. expect(described_class.new(status).insufficient_data?).to be false
  30. end
  31. end
  32. describe '#digest' do
  33. it 'returns a string' do
  34. status = status_with_html('@alice Hello world')
  35. expect(described_class.new(status).digest).to be_a String
  36. end
  37. end
  38. describe '#spam?' do
  39. it 'returns false for a unique status' do
  40. status = status_with_html('@alice Hello')
  41. expect(described_class.new(status).spam?).to be false
  42. end
  43. it 'returns false for different statuses to the same recipient' do
  44. status1 = status_with_html('@alice Hello')
  45. described_class.new(status1).remember!
  46. status2 = status_with_html('@alice Are you available to talk?')
  47. expect(described_class.new(status2).spam?).to be false
  48. end
  49. it 'returns false for statuses with different content warnings' do
  50. status1 = status_with_html('@alice Are you available to talk?')
  51. described_class.new(status1).remember!
  52. status2 = status_with_html('@alice Are you available to talk?', spoiler_text: 'This is a completely different matter than what I was talking about previously, I swear!')
  53. expect(described_class.new(status2).spam?).to be false
  54. end
  55. it 'returns false for different statuses to different recipients' do
  56. status1 = status_with_html('@alice How is it going?')
  57. described_class.new(status1).remember!
  58. status2 = status_with_html('@bob Are you okay?')
  59. expect(described_class.new(status2).spam?).to be false
  60. end
  61. it 'returns false for very short different statuses to different recipients' do
  62. status1 = status_with_html('@alice 🙄')
  63. described_class.new(status1).remember!
  64. status2 = status_with_html('@bob Huh?')
  65. expect(described_class.new(status2).spam?).to be false
  66. end
  67. it 'returns false for statuses with no text' do
  68. status1 = status_with_html('@alice')
  69. described_class.new(status1).remember!
  70. status2 = status_with_html('@bob')
  71. expect(described_class.new(status2).spam?).to be false
  72. end
  73. it 'returns true for duplicate statuses to the same recipient' do
  74. status1 = status_with_html('@alice Hello')
  75. described_class.new(status1).remember!
  76. status2 = status_with_html('@alice Hello')
  77. expect(described_class.new(status2).spam?).to be true
  78. end
  79. it 'returns true for duplicate statuses to different recipients' do
  80. status1 = status_with_html('@alice Hello')
  81. described_class.new(status1).remember!
  82. status2 = status_with_html('@bob Hello')
  83. expect(described_class.new(status2).spam?).to be true
  84. end
  85. it 'returns true for nearly identical statuses with random numbers' do
  86. source_text = 'Sodium, atomic number 11, was first isolated by Humphry Davy in 1807. A chemical component of salt, he named it Na in honor of the saltiest region on earth, North America.'
  87. status1 = status_with_html('@alice ' + source_text + ' 1234')
  88. described_class.new(status1).remember!
  89. status2 = status_with_html('@bob ' + source_text + ' 9568')
  90. expect(described_class.new(status2).spam?).to be true
  91. end
  92. end
  93. describe '#skip?' do
  94. it 'returns true when the sender is already silenced' do
  95. status = status_with_html('@alice Hello')
  96. sender.silence!
  97. expect(described_class.new(status).skip?).to be true
  98. end
  99. it 'returns true when the mentioned person follows the sender' do
  100. status = status_with_html('@alice Hello')
  101. alice.follow!(sender)
  102. expect(described_class.new(status).skip?).to be true
  103. end
  104. it 'returns false when even one mentioned person doesn\'t follow the sender' do
  105. status = status_with_html('@alice @bob Hello')
  106. alice.follow!(sender)
  107. expect(described_class.new(status).skip?).to be false
  108. end
  109. it 'returns true when the sender is replying to a status that mentions the sender' do
  110. parent = PostStatusService.new.call(alice, text: "Hey @#{sender.username}, how are you?")
  111. status = status_with_html('@alice @bob Hello', thread: parent)
  112. expect(described_class.new(status).skip?).to be true
  113. end
  114. end
  115. describe '#remember!' do
  116. let(:status) { status_with_html('@alice') }
  117. let(:spam_check) { described_class.new(status) }
  118. let(:redis_key) { spam_check.send(:redis_key) }
  119. it 'remembers' do
  120. expect do
  121. spam_check.remember!
  122. end.to change { Redis.current.exists(redis_key) }.from(false).to(true)
  123. end
  124. end
  125. describe '#reset!' do
  126. let(:status) { status_with_html('@alice') }
  127. let(:spam_check) { described_class.new(status) }
  128. let(:redis_key) { spam_check.send(:redis_key) }
  129. before do
  130. spam_check.remember!
  131. end
  132. it 'resets' do
  133. expect do
  134. spam_check.reset!
  135. end.to change { Redis.current.exists(redis_key) }.from(true).to(false)
  136. end
  137. end
  138. describe '#flag!' do
  139. let!(:status1) { status_with_html('@alice General Kenobi you are a bold one') }
  140. let!(:status2) { status_with_html('@alice @bob General Kenobi, you are a bold one') }
  141. before do
  142. described_class.new(status1).remember!
  143. described_class.new(status2).flag!
  144. end
  145. it 'silences the account' do
  146. expect(sender.silenced?).to be true
  147. end
  148. it 'creates a report about the account' do
  149. expect(sender.targeted_reports.unresolved.count).to eq 1
  150. end
  151. it 'attaches both matching statuses to the report' do
  152. expect(sender.targeted_reports.first.status_ids).to include(status1.id, status2.id)
  153. end
  154. end
  155. end