mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Introduction of TextSentinel to enforce title and body quality.
This commit is contained in:
@@ -11,7 +11,7 @@ describe PostCreator do
|
||||
|
||||
context 'new topic' do
|
||||
let(:category) { Fabricate(:category, user: user) }
|
||||
let(:basic_topic_params) { {title: 'hello world', raw: 'my name is fred', archetype_id: 1} }
|
||||
let(:basic_topic_params) { {title: 'hello world topic', raw: 'my name is fred', archetype_id: 1} }
|
||||
let(:image_sizes) { {'http://an.image.host/image.jpg' => {'width' => 111, 'height' => 222}} }
|
||||
|
||||
let(:creator) { PostCreator.new(user, basic_topic_params) }
|
||||
@@ -83,7 +83,7 @@ describe PostCreator do
|
||||
let(:target_user1) { Fabricate(:coding_horror) }
|
||||
let(:target_user2) { Fabricate(:moderator) }
|
||||
let(:post) do
|
||||
PostCreator.create(user, title: 'hi there',
|
||||
PostCreator.create(user, title: 'hi there welcome to my topic',
|
||||
raw: 'this is my awesome message',
|
||||
archetype: Archetype.private_message,
|
||||
target_usernames: [target_user1.username, target_user2.username].join(','))
|
||||
|
||||
@@ -14,7 +14,7 @@ describe Search do
|
||||
context 'post indexing observer' do
|
||||
before do
|
||||
@category = Fabricate(:category, name: 'america')
|
||||
@topic = Fabricate(:topic, title: 'sam test', category: @category)
|
||||
@topic = Fabricate(:topic, title: 'sam test topic', category: @category)
|
||||
@post = Fabricate(:post, topic: @topic, raw: 'this <b>fun test</b> <img src="bla" title="my image">')
|
||||
@indexed = Topic.exec_sql("select search_data from posts_search where id = #{@post.id}").first["search_data"]
|
||||
end
|
||||
@@ -29,7 +29,7 @@ describe Search do
|
||||
end
|
||||
|
||||
it "should pick up on title updates" do
|
||||
@topic.title = "harpi"
|
||||
@topic.title = "harpi is the new title"
|
||||
@topic.save!
|
||||
@indexed = Topic.exec_sql("select search_data from posts_search where id = #{@post.id}").first["search_data"]
|
||||
|
||||
|
||||
96
spec/components/text_sentinel_spec.rb
Normal file
96
spec/components/text_sentinel_spec.rb
Normal file
@@ -0,0 +1,96 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'spec_helper'
|
||||
require 'text_sentinel'
|
||||
require 'iconv'
|
||||
|
||||
describe TextSentinel do
|
||||
|
||||
|
||||
context "entropy" do
|
||||
|
||||
|
||||
it "returns 0 for an empty string" do
|
||||
TextSentinel.new("").entropy.should == 0
|
||||
end
|
||||
|
||||
it "returns 0 for a nil string" do
|
||||
TextSentinel.new(nil).entropy.should == 0
|
||||
end
|
||||
|
||||
it "returns 1 for a string with many leading spaces" do
|
||||
TextSentinel.new((" " * 10) + "x").entropy.should == 1
|
||||
end
|
||||
|
||||
it "returns 1 for one char, even repeated" do
|
||||
TextSentinel.new("a" * 10).entropy.should == 1
|
||||
end
|
||||
|
||||
it "returns an accurate count of many chars" do
|
||||
TextSentinel.new("evil trout is evil").entropy.should == 10
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "cleaning up" do
|
||||
|
||||
it "strips leading or trailing whitespace" do
|
||||
TextSentinel.new(" \t test \t ").text.should == "test"
|
||||
end
|
||||
|
||||
it "allows utf-8 chars" do
|
||||
TextSentinel.new("йȝîûηыეமிᚉ⠛").text.should == "йȝîûηыეமிᚉ⠛"
|
||||
end
|
||||
|
||||
context "interior spaces" do
|
||||
|
||||
let(:spacey_string) { "hello there's weird spaces here." }
|
||||
|
||||
it "ignores intra spaces by default" do
|
||||
TextSentinel.new(spacey_string).text.should == spacey_string
|
||||
end
|
||||
|
||||
it "fixes intra spaces when enabled" do
|
||||
TextSentinel.new(spacey_string, remove_interior_spaces: true).text.should == "hello there's weird spaces here."
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "validity" do
|
||||
|
||||
let(:valid_string) { "This is a cool topic about Discourse" }
|
||||
|
||||
it "allows a valid string" do
|
||||
TextSentinel.new(valid_string).should be_valid
|
||||
end
|
||||
|
||||
it "doesn't allow all caps topics" do
|
||||
TextSentinel.new(valid_string.upcase).should_not be_valid
|
||||
end
|
||||
|
||||
it "enforces the minimum entropy" do
|
||||
TextSentinel.new(valid_string, min_entropy: 16).should be_valid
|
||||
end
|
||||
|
||||
it "enforces the minimum entropy" do
|
||||
TextSentinel.new(valid_string, min_entropy: 17).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't allow a long alphanumeric string with no spaces" do
|
||||
TextSentinel.new("jfewjfoejwfojeojfoejofjeo38493824jfkjewfjeoifijeoijfoejofjeojfoewjfo834988394032jfiejoijofijeojfeojfojeofjewojfojeofjeowjfojeofjeojfoe3898439849032jfeijfwoijfoiewj",
|
||||
max_word_length: 30).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't except junk symbols as a string" do
|
||||
TextSentinel.new("[[[").should_not be_valid
|
||||
TextSentinel.new("<<<").should_not be_valid
|
||||
TextSentinel.new("{{$!").should_not be_valid
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
@@ -11,11 +11,11 @@ describe TopicQuery do
|
||||
let(:admin) { Fabricate(:moderator) }
|
||||
|
||||
context 'a bunch of topics' do
|
||||
let!(:regular_topic) { Fabricate(:topic, title: 'regular', user: creator, bumped_at: 15.minutes.ago) }
|
||||
let!(:pinned_topic) { Fabricate(:topic, title: 'pinned', user: creator, pinned: true, bumped_at: 10.minutes.ago) }
|
||||
let!(:archived_topic) { Fabricate(:topic, title: 'archived', user: creator, archived: true, bumped_at: 6.minutes.ago) }
|
||||
let!(:invisible_topic) { Fabricate(:topic, title: 'invisible', user: creator, visible: false, bumped_at: 5.minutes.ago) }
|
||||
let!(:closed_topic) { Fabricate(:topic, title: 'closed', user: creator, closed: true, bumped_at: 1.minute.ago) }
|
||||
let!(:regular_topic) { Fabricate(:topic, title: 'this is a regular topic', user: creator, bumped_at: 15.minutes.ago) }
|
||||
let!(:pinned_topic) { Fabricate(:topic, title: 'this is a pinned topic', user: creator, pinned: true, bumped_at: 10.minutes.ago) }
|
||||
let!(:archived_topic) { Fabricate(:topic, title: 'this is an archived topic', user: creator, archived: true, bumped_at: 6.minutes.ago) }
|
||||
let!(:invisible_topic) { Fabricate(:topic, title: 'this is an invisible topic', user: creator, visible: false, bumped_at: 5.minutes.ago) }
|
||||
let!(:closed_topic) { Fabricate(:topic, title: 'this is a closed topic', user: creator, closed: true, bumped_at: 1.minute.ago) }
|
||||
|
||||
context 'list_popular' do
|
||||
let(:topics) { topic_query.list_popular.topics }
|
||||
|
||||
Reference in New Issue
Block a user