obscenity-plus 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/obscenity/config.rb +30 -32
- data/lib/obscenity.rb +1 -2
- data/spec/active_model_spec.rb +98 -0
- data/spec/base_spec.rb +41 -0
- data/spec/config_spec.rb +72 -0
- data/spec/factories/factory_bot.rb +7 -0
- data/spec/obscenity_spec.rb +70 -0
- data/spec/rack_spec.rb +99 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/support/dummy_base_model.rb +8 -0
- data/spec/validators/obscenity_validator_spec.rb +53 -0
- data/spec/version_spec.rb +7 -0
- metadata +11 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 964e39e6cc5f091523fa547126151189c1c4e031629a60cdb89a1caf0294935a
|
4
|
+
data.tar.gz: d35c0646dadbd02e1e8d6ad76268f69d8356744a84a5cb239611a825020389dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 076fc08ca6d19e0026199daccbff840c9750836d4a63b6b4d17b836f7debefd10a33151a6be41f82b8a726a6993ebc0cd60fce5a8fee7593be7e2cb28d241700
|
7
|
+
data.tar.gz: e6d4d7f9ed48663414e7a1e1ff5377230895234454171a5c6d9791f8a2488c26da21ccab2684916f4864bfd181eeb9b961f07514381741de9d7ef94b8807c03f
|
data/lib/obscenity/config.rb
CHANGED
@@ -1,51 +1,49 @@
|
|
1
1
|
module Obscenity
|
2
|
+
DEFAULT_BLACKLIST = ["badword"] # Ensure this is not empty
|
3
|
+
DEFAULT_WHITELIST = []
|
4
|
+
|
2
5
|
class Config
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
DEFAULT_WHITELIST = []
|
7
|
-
DEFAULT_BLACKLIST = File.expand_path("../../config/blacklist.yml", __dir__)
|
8
|
-
|
6
|
+
attr_accessor :replacement, :blacklist, :whitelist
|
7
|
+
|
9
8
|
def initialize
|
10
|
-
|
9
|
+
puts "DEBUG: Initializing Config with DEFAULT_BLACKLIST: #{DEFAULT_BLACKLIST.inspect}"
|
10
|
+
@blacklist ||= DEFAULT_BLACKLIST.dup
|
11
|
+
@whitelist ||= DEFAULT_WHITELIST.dup
|
12
|
+
@replacement ||= :stars
|
11
13
|
validate_config_options
|
12
14
|
end
|
13
15
|
|
14
|
-
def replacement
|
15
|
-
@replacement ||= :garbled
|
16
|
-
end
|
17
|
-
|
18
|
-
def blacklist
|
19
|
-
@blacklist ||= DEFAULT_BLACKLIST
|
20
|
-
end
|
21
|
-
|
22
16
|
def blacklist=(value)
|
23
|
-
|
17
|
+
raise ArgumentError, "Blacklist cannot be empty" if value.nil? || value.empty?
|
18
|
+
puts "DEBUG: Setting blacklist to: #{value.inspect}"
|
19
|
+
@blacklist = value
|
24
20
|
end
|
25
|
-
|
26
|
-
def whitelist
|
27
|
-
@whitelist ||= DEFAULT_WHITELIST
|
28
|
-
end
|
29
|
-
|
30
|
-
def whitelist=(value)
|
31
|
-
@whitelist = value == :default ? DEFAULT_WHITELIST : value
|
32
|
-
end
|
33
|
-
|
21
|
+
|
34
22
|
private
|
23
|
+
|
35
24
|
def validate_config_options
|
36
|
-
[@blacklist, @whitelist].each
|
25
|
+
[@blacklist, @whitelist].each do |content|
|
26
|
+
next if content.nil? || content.empty? # Skip validation for empty whitelist
|
27
|
+
validate_list_content(content)
|
28
|
+
end
|
37
29
|
end
|
38
30
|
|
31
|
+
|
39
32
|
def validate_list_content(content)
|
33
|
+
puts "DEBUG: Validating content: #{content.inspect}"
|
40
34
|
case content
|
41
|
-
when Array
|
42
|
-
|
43
|
-
when
|
44
|
-
|
35
|
+
when Array
|
36
|
+
raise Obscenity::EmptyContentList.new('Content array is empty.') if content.empty?
|
37
|
+
when String
|
38
|
+
raise Obscenity::UnkownContentFile.new("Content file can't be found.") unless File.exist?(content)
|
39
|
+
when Pathname
|
40
|
+
raise Obscenity::UnkownContentFile.new("Content file can't be found.") unless content.exist?
|
41
|
+
when Symbol
|
42
|
+
raise Obscenity::UnkownContent.new("The only accepted symbol is :default.") unless content == :default
|
45
43
|
else
|
46
|
-
raise Obscenity::UnkownContent.new("The content can be
|
44
|
+
raise Obscenity::UnkownContent.new("The content can be an Array, Pathname, or String path to a .yml file.")
|
47
45
|
end
|
48
46
|
end
|
49
47
|
|
50
48
|
end
|
51
|
-
end
|
49
|
+
end
|
data/lib/obscenity.rb
CHANGED
@@ -0,0 +1,98 @@
|
|
1
|
+
#active_model_spec.rb
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'active_model'
|
4
|
+
require 'obscenity'
|
5
|
+
require 'obscenity/active_model'
|
6
|
+
|
7
|
+
RSpec.describe "ActiveModel Obscenity Validation" do
|
8
|
+
before do
|
9
|
+
Obscenity.configure do |config|
|
10
|
+
config.blacklist = ["shits"]
|
11
|
+
config.replacement = :stars
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:generate_new_class) do
|
16
|
+
->(name, options = {}) do
|
17
|
+
klass_name = "Dummy::#{name}"
|
18
|
+
Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name)
|
19
|
+
Class.new do
|
20
|
+
include ActiveModel::Validations
|
21
|
+
attr_accessor :title
|
22
|
+
|
23
|
+
validates :title, options
|
24
|
+
|
25
|
+
def initialize(attributes = {})
|
26
|
+
attributes.each { |key, value| send("#{key}=", value) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when title is profane" do
|
33
|
+
it "is invalid and adds an error message" do
|
34
|
+
klass = generate_new_class.call("Post", obscenity: true)
|
35
|
+
post = klass.new(title: "He who poops, shits itself")
|
36
|
+
|
37
|
+
expect(post.valid?).to be false
|
38
|
+
expect(post.errors).to have_key(:title)
|
39
|
+
expect(post.errors[:title]).to include("cannot be profane")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "includes a custom error message for profanity" do
|
43
|
+
klass = generate_new_class.call("Post", obscenity: { message: "can't be profane!" })
|
44
|
+
post = klass.new(title: "He who poops, shits itself")
|
45
|
+
|
46
|
+
expect(post.valid?).to be false
|
47
|
+
expect(post.errors).to have_key(:title)
|
48
|
+
expect(post.errors[:title]).to include("can't be profane!")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when sanitizing the title" do
|
53
|
+
it "uses the default replacement method" do
|
54
|
+
klass = generate_new_class.call("Post", obscenity: { sanitize: true })
|
55
|
+
post = klass.new(title: "He who poops, shits itself")
|
56
|
+
|
57
|
+
expect(post.valid?).to be true
|
58
|
+
expect(post.errors).not_to have_key(:title)
|
59
|
+
expect(post.title).to eq("He who poops, ***** itself")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "uses the :garbled replacement method" do
|
63
|
+
klass = generate_new_class.call("Post", obscenity: { sanitize: true, replacement: :garbled })
|
64
|
+
post = klass.new(title: "He who poops, shits itself")
|
65
|
+
|
66
|
+
expect(post.valid?).to be true
|
67
|
+
expect(post.errors).not_to have_key(:title)
|
68
|
+
expect(post.title).to eq("He who poops, $@!#% itself")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "uses the :stars replacement method" do
|
72
|
+
klass = generate_new_class.call("Post", obscenity: { sanitize: true, replacement: :stars })
|
73
|
+
post = klass.new(title: "He who poops, shits itself")
|
74
|
+
|
75
|
+
expect(post.valid?).to be true
|
76
|
+
expect(post.errors).not_to have_key(:title)
|
77
|
+
expect(post.title).to eq("He who poops, ***** itself")
|
78
|
+
end
|
79
|
+
|
80
|
+
it "uses the :vowels replacement method" do
|
81
|
+
klass = generate_new_class.call("Post", obscenity: { sanitize: true, replacement: :vowels })
|
82
|
+
post = klass.new(title: "He who poops, shits itself")
|
83
|
+
|
84
|
+
expect(post.valid?).to be true
|
85
|
+
expect(post.errors).not_to have_key(:title)
|
86
|
+
expect(post.title).to eq("He who poops, sh*ts itself")
|
87
|
+
end
|
88
|
+
|
89
|
+
it "uses a custom replacement string" do
|
90
|
+
klass = generate_new_class.call("Post", obscenity: { sanitize: true, replacement: "[censored]" })
|
91
|
+
post = klass.new(title: "He who poops, shits itself")
|
92
|
+
|
93
|
+
expect(post.valid?).to be true
|
94
|
+
expect(post.errors).not_to have_key(:title)
|
95
|
+
expect(post.title).to eq("He who poops, [censored] itself")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#base_spec.rb
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'obscenity'
|
4
|
+
|
5
|
+
RSpec.describe Obscenity::Base do
|
6
|
+
before do
|
7
|
+
Obscenity.configure do |config|
|
8
|
+
config.blacklist = ["badword", "foulword"]
|
9
|
+
config.whitelist = ["safe"]
|
10
|
+
config.replacement = :stars
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "responding to methods" do
|
15
|
+
it "responds to expected methods" do
|
16
|
+
[:blacklist, :whitelist, :profane?, :sanitize, :replacement, :offensive, :replace].each do |method|
|
17
|
+
expect(Obscenity::Base).to respond_to(method)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#profane?" do
|
23
|
+
it "detects profane words" do
|
24
|
+
expect(Obscenity::Base.profane?("badword")).to be true
|
25
|
+
end
|
26
|
+
|
27
|
+
it "does not detect clean words as profane" do
|
28
|
+
expect(Obscenity::Base.profane?("cleanword")).to be false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#sanitize" do
|
33
|
+
it "replaces profane words with stars" do
|
34
|
+
expect(Obscenity::Base.sanitize("badword")).to eq("*******")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns clean words unchanged" do
|
38
|
+
expect(Obscenity::Base.sanitize("cleanword")).to eq("cleanword")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
#config_spec.rb
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'obscenity/config'
|
4
|
+
|
5
|
+
RSpec.describe Obscenity::Config do
|
6
|
+
describe '#respond_to?' do
|
7
|
+
it 'responds to expected methods and attributes' do
|
8
|
+
config = Obscenity::Config.new
|
9
|
+
[:blacklist, :whitelist, :replacement].each do |field|
|
10
|
+
expect(config).to respond_to(field)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'configuration parameters' do
|
16
|
+
it 'properly sets config parameters' do
|
17
|
+
blacklist = ['ass', 'shit', 'penis']
|
18
|
+
whitelist = ['penis']
|
19
|
+
replacement = :stars
|
20
|
+
|
21
|
+
config = Obscenity::Config.new do |c|
|
22
|
+
c.blacklist = blacklist
|
23
|
+
c.whitelist = whitelist
|
24
|
+
c.replacement = replacement
|
25
|
+
end
|
26
|
+
|
27
|
+
expect(config.blacklist).to eq(blacklist)
|
28
|
+
expect(config.whitelist).to eq(whitelist)
|
29
|
+
expect(config.replacement).to eq(replacement)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns default values if none are set' do
|
33
|
+
config = Obscenity::Config.new
|
34
|
+
expect(config.whitelist).to eq([])
|
35
|
+
expect(config.replacement).to eq(:garbled)
|
36
|
+
expect(config.blacklist.to_s).to match(/config\/blacklist.yml/)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns default values when explicitly set to default' do
|
40
|
+
config = Obscenity::Config.new do |c|
|
41
|
+
c.blacklist = :default
|
42
|
+
c.replacement = :default
|
43
|
+
end
|
44
|
+
|
45
|
+
expect(config.whitelist).to eq([])
|
46
|
+
expect(config.replacement).to eq(:default)
|
47
|
+
expect(config.blacklist.to_s).to match(/config\/blacklist.yml/)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'validating configuration options' do
|
52
|
+
it 'raises exceptions for invalid config options' do
|
53
|
+
invalid_values = [
|
54
|
+
[Obscenity::UnkownContent, {}],
|
55
|
+
[Obscenity::UnkownContent, ":unkown"],
|
56
|
+
[Obscenity::EmptyContentList, []],
|
57
|
+
[Obscenity::UnkownContentFile, "'path/to/file'"],
|
58
|
+
[Obscenity::UnkownContentFile, Pathname.new("'path/to/file'")]
|
59
|
+
]
|
60
|
+
|
61
|
+
[:blacklist, :whitelist].each do |field|
|
62
|
+
invalid_values.each do |klass, value|
|
63
|
+
expect {
|
64
|
+
Obscenity::Config.new do |config|
|
65
|
+
config.instance_eval("config.#{field} = #{value}")
|
66
|
+
end
|
67
|
+
}.to raise_error(klass)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#obscenity_spec.rb
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'obscenity'
|
4
|
+
|
5
|
+
RSpec.describe Obscenity do
|
6
|
+
before do
|
7
|
+
Obscenity.configure do |config|
|
8
|
+
config.blacklist = :default
|
9
|
+
config.replacement = :garbled
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "respond_to?" do
|
14
|
+
it "responds to the expected methods and attributes" do
|
15
|
+
[:configure, :config, :profane?, :sanitize, :offensive, :replacement].each do |field|
|
16
|
+
expect(Obscenity).to respond_to(field)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".configure" do
|
22
|
+
it "accepts a configuration block without raising errors" do
|
23
|
+
expect {
|
24
|
+
Obscenity.configure do |config|
|
25
|
+
config.blacklist = :default
|
26
|
+
config.replacement = :garbled
|
27
|
+
end
|
28
|
+
}.not_to raise_error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ".config" do
|
33
|
+
it "returns the current configuration object" do
|
34
|
+
expect(Obscenity.config).not_to be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe ".profane?" do
|
39
|
+
it "validates the profanity of the given content" do
|
40
|
+
expect(Obscenity.profane?('Yo, check that ass out')).to be true
|
41
|
+
expect(Obscenity.profane?('Hello world')).to be false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe ".sanitize" do
|
46
|
+
it "sanitizes the given content" do
|
47
|
+
expect(Obscenity.sanitize('Yo, check that ass out')).to eq("Yo, check that $@!#% out")
|
48
|
+
expect(Obscenity.sanitize('Hello world')).to eq("Hello world")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe ".offensive" do
|
53
|
+
it "returns the offensive words for the given content" do
|
54
|
+
expect(Obscenity.offensive('Yo, check that ass biatch')).to eq(['ass', 'biatch'])
|
55
|
+
expect(Obscenity.offensive('Hello world')).to eq([])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe ".replacement" do
|
60
|
+
it "sanitizes the given content based on the replacement method" do
|
61
|
+
expect(Obscenity.replacement(:garbled).sanitize('Yo, check that ass out')).to eq("Yo, check that $@!#% out")
|
62
|
+
expect(Obscenity.replacement(:default).sanitize('Yo, check that ass out')).to eq("Yo, check that $@!#% out")
|
63
|
+
expect(Obscenity.replacement(:vowels).sanitize('Yo, check that ass out')).to eq("Yo, check that *ss out")
|
64
|
+
expect(Obscenity.replacement(:nonconsonants).sanitize('Yo, check that 5hit out')).to eq("Yo, check that *h*t out")
|
65
|
+
expect(Obscenity.replacement(:stars).sanitize('Yo, check that ass out')).to eq("Yo, check that *** out")
|
66
|
+
expect(Obscenity.replacement("[censored]").sanitize('Yo, check that ass out')).to eq("Yo, check that [censored] out")
|
67
|
+
expect(Obscenity.sanitize('Hello world')).to eq("Hello world")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/spec/rack_spec.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'rack/mock'
|
2
|
+
require 'obscenity/rack'
|
3
|
+
|
4
|
+
RSpec.describe Rack::Obscenity do
|
5
|
+
let(:app) do
|
6
|
+
lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['hello']] }
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:env) { Rack::MockRequest.env_for('/') }
|
10
|
+
|
11
|
+
def middleware(options = {})
|
12
|
+
described_class.new(app, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(params = {})
|
16
|
+
{ 'QUERY_STRING' => Rack::Utils.build_query(params) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(params = {})
|
20
|
+
{ 'rack.input' => StringIO.new(Rack::Utils.build_query(params)) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse_query(query)
|
24
|
+
Rack::Utils.parse_query(query, '&')
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_response_params(env)
|
28
|
+
parse_query(env['QUERY_STRING'])
|
29
|
+
end
|
30
|
+
|
31
|
+
def post_response_params(env)
|
32
|
+
parse_query(env['rack.input'].read)
|
33
|
+
end
|
34
|
+
|
35
|
+
def call_middleware(middleware, options)
|
36
|
+
middleware.call(Rack::MockRequest.env_for('/', options))
|
37
|
+
end
|
38
|
+
|
39
|
+
it "does not evaluate profanity by default" do
|
40
|
+
app = middleware
|
41
|
+
status, headers, body = call_middleware(app, {})
|
42
|
+
expect(status).to eq(200)
|
43
|
+
expect(headers).to eq('Content-Type' => 'text/plain')
|
44
|
+
expect(body).to eq(['hello'])
|
45
|
+
end
|
46
|
+
|
47
|
+
context "rejecting requests" do
|
48
|
+
it "does not reject non-profane parameters" do
|
49
|
+
app = middleware(reject: true)
|
50
|
+
status, headers, body = call_middleware(app, get(foo: 'bar'))
|
51
|
+
expect(status).to eq(200)
|
52
|
+
expect(headers).to eq('Content-Type' => 'text/plain')
|
53
|
+
expect(body).to eq(['hello'])
|
54
|
+
end
|
55
|
+
|
56
|
+
it "rejects requests with GET parameters containing profanity" do
|
57
|
+
app = middleware(reject: true)
|
58
|
+
status, headers, body = call_middleware(app, get(foo: 'bar', baz: 'shit'))
|
59
|
+
expect(status).to eq(422)
|
60
|
+
expect(body).to eq([''])
|
61
|
+
end
|
62
|
+
|
63
|
+
it "rejects requests with POST parameters containing profanity" do
|
64
|
+
app = middleware(reject: true)
|
65
|
+
status, headers, body = call_middleware(app, post(foo: 'bar', baz: 'ass'))
|
66
|
+
expect(status).to eq(422)
|
67
|
+
expect(body).to eq([''])
|
68
|
+
end
|
69
|
+
|
70
|
+
it "rejects requests with specific parameter values containing profanity" do
|
71
|
+
app = middleware(reject: { params: [:foo] })
|
72
|
+
status, headers, body = call_middleware(app, get(foo: 'ass', baz: 'clean'))
|
73
|
+
expect(status).to eq(422)
|
74
|
+
expect(body).to eq([''])
|
75
|
+
end
|
76
|
+
|
77
|
+
it "does not reject requests if other parameter values contain profanity" do
|
78
|
+
app = middleware(reject: { params: [:foo] })
|
79
|
+
status, headers, body = call_middleware(app, get(foo: 'clean', baz: 'shit'))
|
80
|
+
expect(status).to eq(200)
|
81
|
+
expect(headers).to eq('Content-Type' => 'text/plain')
|
82
|
+
expect(body).to eq(['hello'])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "sanitizing requests" do
|
87
|
+
it "does not sanitize non-profane parameters" do
|
88
|
+
app = middleware(sanitize: true)
|
89
|
+
status, headers, body = call_middleware(app, get(foo: 'bar'))
|
90
|
+
expect(status).to eq(200)
|
91
|
+
expect(headers).to eq('Content-Type' => 'text/plain')
|
92
|
+
expect(body).to eq(['hello'])
|
93
|
+
|
94
|
+
request_params = get_response_params(env)
|
95
|
+
expect(request_params['foo']).to eq('bar')
|
96
|
+
end
|
97
|
+
|
98
|
+
it "sanitizes GET parameters containing profanity" do
|
99
|
+
app = middleware(sanitize: tru
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rspec'
|
4
|
+
require 'factory_bot'
|
5
|
+
|
6
|
+
# Load ActiveModel and Obscenity
|
7
|
+
require 'active_model'
|
8
|
+
require 'obscenity'
|
9
|
+
require 'obscenity/active_model'
|
10
|
+
|
11
|
+
# Load the Dummy module and BaseModel class
|
12
|
+
require_relative 'support/dummy_base_model'
|
13
|
+
|
14
|
+
# Dummy Model for Testing
|
15
|
+
FactoryBot.define do
|
16
|
+
factory :base_model, class: Dummy::BaseModel do
|
17
|
+
title { "Sample title" }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Configure RSpec
|
22
|
+
RSpec.configure do |config|
|
23
|
+
Obscenity.configure do |c|
|
24
|
+
puts "DEBUG: Configuring blacklist in tests"
|
25
|
+
c.blacklist = ["badword"]
|
26
|
+
c.whitelist = []
|
27
|
+
c.replacement = :stars
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Load support files and factory definitions
|
32
|
+
Dir[File.join(__dir__, 'support/**/*.rb')].each { |file| require file }
|
33
|
+
FactoryBot.definition_file_paths = [File.expand_path('spec/factories', __dir__)]
|
34
|
+
FactoryBot.find_definitions
|
35
|
+
|
36
|
+
Obscenity::Config::DEFAULT_BLACKLIST = ["badword"]
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#spec/validators/obscenity_validator_spec.rb
|
2
|
+
# test to ensure that the ObscenityValidator is working as expected
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
puts "Blacklist: #{Obscenity.config.blacklist}"
|
6
|
+
|
7
|
+
RSpec.describe ActiveModel::Validations::ObscenityValidator do
|
8
|
+
before do
|
9
|
+
Obscenity.configure do |config|
|
10
|
+
config.blacklist = ["badword"]
|
11
|
+
config.whitelist = []
|
12
|
+
config.replacement = :stars
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:dummy_class) do
|
17
|
+
Class.new do
|
18
|
+
include ActiveModel::Validations
|
19
|
+
attr_accessor :title, :content
|
20
|
+
validates :title, obscenity: { message: "cannot be profane" }
|
21
|
+
validates :content, obscenity: { sanitize: true, replacement: '[censored]' }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:dummy_instance) { dummy_class.new }
|
26
|
+
|
27
|
+
context "when validating profanity" do
|
28
|
+
it "adds an error message when profane" do
|
29
|
+
dummy_instance.title = "This is a badword"
|
30
|
+
expect(dummy_instance.valid?).to be false
|
31
|
+
expect(dummy_instance.errors[:title]).to include("cannot be profane")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "does not add an error for clean words" do
|
35
|
+
dummy_instance.title = "This is clean"
|
36
|
+
expect(dummy_instance.valid?).to be true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when sanitizing profanity" do
|
41
|
+
it "sanitizes profane words" do
|
42
|
+
dummy_instance.content = "This is a badword"
|
43
|
+
dummy_instance.valid? # Trigger the validation
|
44
|
+
expect(dummy_instance.content).to eq("This is a [censored]")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "leaves clean words unchanged" do
|
48
|
+
dummy_instance.content = "This is clean"
|
49
|
+
expect(dummy_instance.valid?).to be true
|
50
|
+
expect(dummy_instance.content).to eq("This is clean")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: obscenity-plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ropetow
|
@@ -113,6 +113,16 @@ files:
|
|
113
113
|
- lib/obscenity/rack.rb
|
114
114
|
- lib/obscenity/rspec_matcher.rb
|
115
115
|
- lib/obscenity/version.rb
|
116
|
+
- spec/active_model_spec.rb
|
117
|
+
- spec/base_spec.rb
|
118
|
+
- spec/config_spec.rb
|
119
|
+
- spec/factories/factory_bot.rb
|
120
|
+
- spec/obscenity_spec.rb
|
121
|
+
- spec/rack_spec.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
- spec/support/dummy_base_model.rb
|
124
|
+
- spec/validators/obscenity_validator_spec.rb
|
125
|
+
- spec/version_spec.rb
|
116
126
|
homepage: http://github.com/ropetow/obscenity-plus
|
117
127
|
licenses:
|
118
128
|
- MIT
|