2013-02-06 20:09:31 -05:00
|
|
|
|
# encoding: utf-8
|
2019-04-29 20:27:42 -04:00
|
|
|
|
# frozen_string_literal: true
|
2013-02-06 20:09:31 -05:00
|
|
|
|
|
|
|
|
|
require "text_sentinel"
|
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
|
RSpec.describe TextSentinel do
|
2013-04-10 05:00:50 -04:00
|
|
|
|
it "allows utf-8 chars" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new("йȝîûηыეமிᚉ⠛").text).to eq("йȝîûηыეமிᚉ⠛")
|
2013-04-10 05:00:50 -04:00
|
|
|
|
end
|
2013-02-06 20:09:31 -05:00
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
describe "entropy" do
|
2013-02-06 20:09:31 -05:00
|
|
|
|
it "returns 0 for an empty string" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new("").entropy).to eq(0)
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns 0 for a nil string" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new(nil).entropy).to eq(0)
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns 1 for a string with many leading spaces" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new((" " * 10) + "x").entropy).to eq(1)
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns 1 for one char, even repeated" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new("a" * 10).entropy).to eq(1)
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns an accurate count of many chars" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new("evil trout is evil").entropy).to eq(10)
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2013-02-14 16:09:57 -05:00
|
|
|
|
it "Works on foreign characters" do
|
2024-07-22 11:35:52 -04:00
|
|
|
|
expect(TextSentinel.new("去年十社會警告").entropy).to eq(18)
|
2013-06-07 14:47:07 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "generates enough entropy for short foreign strings" do
|
2024-07-22 11:35:52 -04:00
|
|
|
|
expect(TextSentinel.new("又一个测").entropy).to eq(10)
|
2013-06-07 14:47:07 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "handles repeated foreign characters" do
|
2024-07-22 11:35:52 -04:00
|
|
|
|
expect(TextSentinel.new("又一个测试话题" * 3).entropy).to eq(17)
|
2013-02-14 16:09:57 -05:00
|
|
|
|
end
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
describe "body_sentinel" do
|
2013-06-17 12:22:23 -04:00
|
|
|
|
[
|
|
|
|
|
"evil trout is evil",
|
|
|
|
|
"去年十社會警告",
|
|
|
|
|
"P.S. Пробирочка очень толковая и весьма умная, так что не обнимайтесь.",
|
2016-07-12 11:08:55 -04:00
|
|
|
|
"Look: 去年十社會警告",
|
2013-06-17 12:22:23 -04:00
|
|
|
|
].each do |valid_body|
|
|
|
|
|
it "handles a valid body in a private message" do
|
|
|
|
|
expect(TextSentinel.body_sentinel(valid_body, private_message: true)).to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "handles a valid body in a public post" do
|
|
|
|
|
expect(TextSentinel.body_sentinel(valid_body, private_message: false)).to be_valid
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-05-03 03:21:35 -04:00
|
|
|
|
it "uses a sensible min entropy value when min body length is less than min entropy" do
|
|
|
|
|
SiteSetting.min_post_length = 3
|
|
|
|
|
SiteSetting.body_min_entropy = 7
|
|
|
|
|
expect(TextSentinel.body_sentinel("Yup")).to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "uses a sensible min entropy value when min pm body length is less than min entropy" do
|
|
|
|
|
SiteSetting.min_post_length = 5
|
|
|
|
|
SiteSetting.min_personal_message_post_length = 3
|
|
|
|
|
SiteSetting.body_min_entropy = 7
|
|
|
|
|
expect(TextSentinel.body_sentinel("Lol", private_message: true)).to be_valid
|
|
|
|
|
end
|
2013-06-17 12:22:23 -04:00
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
describe "validity" do
|
2013-02-06 20:09:31 -05:00
|
|
|
|
let(:valid_string) { "This is a cool topic about Discourse" }
|
|
|
|
|
|
|
|
|
|
it "allows a valid string" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new(valid_string)).to be_valid
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "doesn't allow all caps topics" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new(valid_string.upcase)).not_to be_valid
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2016-07-12 11:08:55 -04:00
|
|
|
|
it "doesn't allow all caps foreign topics" do
|
|
|
|
|
expect(
|
|
|
|
|
TextSentinel.new(
|
|
|
|
|
"É COM VOCÊ LOMBARDIAM. MA VEJAM SÓ, VEJAM SÓ. VALENDO UM MILHÃO DE REAISAMMM. MA VALE DÉRREAISAM?",
|
2023-01-09 06:18:21 -05:00
|
|
|
|
),
|
2016-07-12 11:08:55 -04:00
|
|
|
|
).not_to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-17 15:27:44 -05:00
|
|
|
|
it "allows all caps topics when loud posts are allowed" do
|
2017-07-07 02:09:14 -04:00
|
|
|
|
SiteSetting.allow_uppercase_posts = true
|
2015-11-17 15:27:44 -05:00
|
|
|
|
expect(TextSentinel.new(valid_string.upcase)).to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
2013-02-06 20:09:31 -05:00
|
|
|
|
it "enforces the minimum entropy" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new(valid_string, min_entropy: 16)).to be_valid
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
|
it "enforces the minimum entropy" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new(valid_string, min_entropy: 17)).not_to be_valid
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2013-02-14 16:09:57 -05:00
|
|
|
|
it "allows all foreign characters" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new("去年十二月,北韓不顧國際社會警告")).to be_valid
|
2013-02-14 16:09:57 -05:00
|
|
|
|
end
|
|
|
|
|
|
2024-07-22 11:35:52 -04:00
|
|
|
|
it "allows all caps for CJK characters" do
|
2019-04-05 09:07:49 -04:00
|
|
|
|
expect(TextSentinel.new("去年SHIER月,北韓不顧國際社會警告")).to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "skips long words check (`seems_unpretentious`) for CJK locale" do
|
|
|
|
|
SiteSetting.default_locale = "zh_CN"
|
|
|
|
|
expect(
|
|
|
|
|
TextSentinel.title_sentinel("非常长的文字没有空格分割肯定会触发警告但这不应该是一个错误这个要超过五十个个字符" * 2),
|
|
|
|
|
).to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
2013-02-06 20:09:31 -05:00
|
|
|
|
it "doesn't allow a long alphanumeric string with no spaces" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(
|
|
|
|
|
TextSentinel.new("jfewjfoejwfojeojfoejofjeo3" * 5, max_word_length: 30),
|
|
|
|
|
).not_to be_valid
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2013-08-28 11:04:28 -04:00
|
|
|
|
it "doesn't accept junk symbols as a string" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.new("[[[")).not_to be_valid
|
|
|
|
|
expect(TextSentinel.new("<<<")).not_to be_valid
|
|
|
|
|
expect(TextSentinel.new("{{$!")).not_to be_valid
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2013-09-16 09:45:57 -04:00
|
|
|
|
it "does allow a long alphanumeric string joined with slashes" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(
|
|
|
|
|
TextSentinel.new("gdfgdfgdfg/fgdfgdfgdg/dfgdfgdfgd/dfgdfgdfgf", max_word_length: 30),
|
|
|
|
|
).to be_valid
|
2013-09-16 09:45:57 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "does allow a long alphanumeric string joined with dashes" do
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(
|
|
|
|
|
TextSentinel.new("gdfgdfgdfg-fgdfgdfgdg-dfgdfgdfgd-dfgdfgdfgf", max_word_length: 30),
|
|
|
|
|
).to be_valid
|
2015-05-19 13:10:25 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "allows a long string with periods" do
|
|
|
|
|
expect(
|
|
|
|
|
TextSentinel.new(
|
|
|
|
|
"error in org.gradle.internal.graph.CachingDirectedGraphWalker",
|
|
|
|
|
max_word_length: 30,
|
2023-01-09 06:18:21 -05:00
|
|
|
|
),
|
2015-05-19 13:10:25 -04:00
|
|
|
|
).to be_valid
|
2016-09-12 23:45:05 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "allows a long string with colons" do
|
|
|
|
|
expect(
|
|
|
|
|
TextSentinel.new(
|
|
|
|
|
"error in org.gradle.internal.graph.CachingDirectedGraphWalker:colon",
|
|
|
|
|
max_word_length: 30,
|
2023-01-09 06:18:21 -05:00
|
|
|
|
),
|
2016-09-12 23:45:05 -04:00
|
|
|
|
).to be_valid
|
2013-09-16 09:45:57 -04:00
|
|
|
|
end
|
2013-02-06 20:09:31 -05:00
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
describe "title_sentinel" do
|
2013-08-28 11:04:28 -04:00
|
|
|
|
it "uses a sensible min entropy value when min title length is less than title_min_entropy" do
|
2017-07-07 02:09:14 -04:00
|
|
|
|
SiteSetting.min_topic_title_length = 3
|
|
|
|
|
SiteSetting.title_min_entropy = 10
|
2015-01-09 11:34:37 -05:00
|
|
|
|
expect(TextSentinel.title_sentinel("Hey")).to be_valid
|
2013-08-28 11:04:28 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
describe "#seems_unpretentious?" do
|
2021-05-03 03:21:35 -04:00
|
|
|
|
it "works with nil title" do
|
|
|
|
|
expect(TextSentinel.title_sentinel(nil).seems_unpretentious?).to eq(true)
|
2013-08-28 11:04:28 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2013-02-15 20:58:33 -05:00
|
|
|
|
end
|