obscenity 1.0.0
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.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/.travis.yml +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +40 -0
- data/LICENSE.txt +20 -0
- data/README.md +280 -0
- data/Rakefile +52 -0
- data/config/blacklist.yml +566 -0
- data/config/international.yml +820 -0
- data/lib/obscenity.rb +36 -0
- data/lib/obscenity/active_model.rb +18 -0
- data/lib/obscenity/base.rb +73 -0
- data/lib/obscenity/config.rb +51 -0
- data/lib/obscenity/error.rb +7 -0
- data/lib/obscenity/rack.rb +89 -0
- data/lib/obscenity/version.rb +5 -0
- data/obscenity.gemspec +84 -0
- data/test/helper.rb +33 -0
- data/test/static/422.html +1 -0
- data/test/test_active_model.rb +69 -0
- data/test/test_base.rb +253 -0
- data/test/test_config.rb +66 -0
- data/test/test_obscenity.rb +68 -0
- data/test/test_rack.rb +199 -0
- data/test/test_version.rb +9 -0
- metadata +198 -0
data/test/test_base.rb
ADDED
@@ -0,0 +1,253 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestBase < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "#respond_to?" do
|
6
|
+
should "respond to methods and attributes" do
|
7
|
+
[:blacklist, :whitelist, :profane?, :sanitize, :replacement, :offensive, :replace].each do |field|
|
8
|
+
assert Obscenity::Base.respond_to?(field)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "#blacklist" do
|
14
|
+
context "without custom config" do
|
15
|
+
setup { Obscenity::Base.blacklist = :default }
|
16
|
+
should "use the default content file when no config is found" do
|
17
|
+
assert Obscenity::Base.blacklist.is_a?(Array)
|
18
|
+
assert_equal 565, Obscenity::Base.blacklist.size
|
19
|
+
end
|
20
|
+
end
|
21
|
+
context "with custom config" do
|
22
|
+
setup { Obscenity::Base.blacklist = ['bad', 'word'] }
|
23
|
+
should "respect the config options" do
|
24
|
+
assert_equal ['bad', 'word'], Obscenity::Base.blacklist
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "#whitelist" do
|
30
|
+
context "without custom config" do
|
31
|
+
setup { Obscenity::Base.whitelist = :default }
|
32
|
+
should "use the default content file when no config is found" do
|
33
|
+
assert Obscenity::Base.whitelist.is_a?(Array)
|
34
|
+
assert Obscenity::Base.whitelist.empty?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
context "with custom config" do
|
38
|
+
setup { Obscenity::Base.whitelist = ['safe', 'word'] }
|
39
|
+
should "respect the config options" do
|
40
|
+
assert_equal ['safe', 'word'], Obscenity::Base.whitelist
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "#profane?" do
|
46
|
+
context "without whitelist" do
|
47
|
+
context "without custom config" do
|
48
|
+
setup {
|
49
|
+
Obscenity::Base.blacklist = :default
|
50
|
+
Obscenity::Base.whitelist = :default
|
51
|
+
}
|
52
|
+
should "validate the profanity of a word based on the default list" do
|
53
|
+
assert Obscenity::Base.profane?('ass')
|
54
|
+
assert Obscenity::Base.profane?('biatch')
|
55
|
+
assert !Obscenity::Base.profane?('hello')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
context "with custom blacklist config" do
|
59
|
+
setup { Obscenity::Base.blacklist = ['ass', 'word'] }
|
60
|
+
should "validate the profanity of a word based on the custom list" do
|
61
|
+
assert Obscenity::Base.profane?('ass')
|
62
|
+
assert Obscenity::Base.profane?('word')
|
63
|
+
assert !Obscenity::Base.profane?('biatch')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
context "with whitelist" do
|
68
|
+
context "without custom blacklist config" do
|
69
|
+
setup {
|
70
|
+
Obscenity::Base.blacklist = :default
|
71
|
+
Obscenity::Base.whitelist = ['biatch']
|
72
|
+
}
|
73
|
+
should "validate the profanity of a word based on the default list" do
|
74
|
+
assert Obscenity::Base.profane?('ass')
|
75
|
+
assert !Obscenity::Base.profane?('biatch')
|
76
|
+
assert !Obscenity::Base.profane?('hello')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
context "with custom blacklist/whitelist config" do
|
80
|
+
setup {
|
81
|
+
Obscenity::Base.blacklist = ['ass', 'word']
|
82
|
+
Obscenity::Base.whitelist = ['word']
|
83
|
+
}
|
84
|
+
should "validate the profanity of a word based on the custom list" do
|
85
|
+
assert Obscenity::Base.profane?('ass')
|
86
|
+
assert !Obscenity::Base.profane?('word')
|
87
|
+
assert !Obscenity::Base.profane?('biatch')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "#sanitize" do
|
94
|
+
context "without whitelist" do
|
95
|
+
context "without custom config" do
|
96
|
+
setup {
|
97
|
+
Obscenity::Base.blacklist = :default
|
98
|
+
Obscenity::Base.whitelist = :default
|
99
|
+
}
|
100
|
+
should "sanitize and return a clean text based on the default list" do
|
101
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.sanitize('Yo assclown, sup')
|
102
|
+
assert_equal "Hello world", Obscenity::Base.sanitize('Hello world')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
context "with custom blacklist config" do
|
106
|
+
setup { Obscenity::Base.blacklist = ['ass', 'word'] }
|
107
|
+
should "sanitize and return a clean text based on a custom list" do
|
108
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.sanitize('Yo word, sup')
|
109
|
+
assert_equal "Hello world", Obscenity::Base.sanitize('Hello world')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
context "with whitelist" do
|
114
|
+
context "without custom blacklist config" do
|
115
|
+
setup {
|
116
|
+
Obscenity::Base.blacklist = :default
|
117
|
+
Obscenity::Base.whitelist = ['biatch']
|
118
|
+
}
|
119
|
+
should "sanitize and return a clean text based on the default blacklist and custom whitelist" do
|
120
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.sanitize('Yo assclown, sup')
|
121
|
+
assert_equal "Yo biatch, sup", Obscenity::Base.sanitize('Yo biatch, sup')
|
122
|
+
end
|
123
|
+
end
|
124
|
+
context "with custom blacklist/whitelist config" do
|
125
|
+
setup {
|
126
|
+
Obscenity::Base.blacklist = ['clown', 'biatch']
|
127
|
+
Obscenity::Base.whitelist = ['biatch']
|
128
|
+
}
|
129
|
+
should "validate the profanity of a word based on the custom list" do
|
130
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.sanitize('Yo clown, sup')
|
131
|
+
assert_equal "Yo biatch, sup", Obscenity::Base.sanitize('Yo biatch, sup')
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context "#replacement" do
|
138
|
+
context "without whitelist" do
|
139
|
+
context "without custom config" do
|
140
|
+
setup {
|
141
|
+
Obscenity::Base.blacklist = :default
|
142
|
+
Obscenity::Base.whitelist = :default
|
143
|
+
}
|
144
|
+
should "sanitize and return a clean text based on the default list" do
|
145
|
+
assert_equal "Yo ********, sup", Obscenity::Base.replacement(:stars).sanitize('Yo assclown, sup')
|
146
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo assclown, sup')
|
147
|
+
assert_equal "Yo *sscl*wn, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo assclown, sup')
|
148
|
+
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo assclown, sup')
|
149
|
+
assert_equal "Hello World", Obscenity::Base.replacement(:default).sanitize('Hello World')
|
150
|
+
end
|
151
|
+
end
|
152
|
+
context "with custom blacklist config" do
|
153
|
+
setup { Obscenity::Base.blacklist = ['ass', 'word'] }
|
154
|
+
should "sanitize and return a clean text based on a custom list" do
|
155
|
+
assert_equal "Yo ****, sup", Obscenity::Base.replacement(:stars).sanitize('Yo word, sup')
|
156
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo word, sup')
|
157
|
+
assert_equal "Yo w*rd, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo word, sup')
|
158
|
+
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo word, sup')
|
159
|
+
assert_equal "Hello World", Obscenity::Base.replacement(:default).sanitize('Hello World')
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
context "with whitelist" do
|
164
|
+
context "without custom blacklist config" do
|
165
|
+
setup {
|
166
|
+
Obscenity::Base.blacklist = :default
|
167
|
+
Obscenity::Base.whitelist = ['biatch']
|
168
|
+
}
|
169
|
+
should "sanitize and return a clean text based on the default blacklist and custom whitelist" do
|
170
|
+
assert_equal "Yo ********, sup", Obscenity::Base.replacement(:stars).sanitize('Yo assclown, sup')
|
171
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo assclown, sup')
|
172
|
+
assert_equal "Yo *sscl*wn, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo assclown, sup')
|
173
|
+
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo assclown, sup')
|
174
|
+
assert_equal "Yo biatch, sup", Obscenity::Base.replacement(:default).sanitize('Yo biatch, sup')
|
175
|
+
end
|
176
|
+
end
|
177
|
+
context "with custom blacklist/whitelist config" do
|
178
|
+
setup {
|
179
|
+
Obscenity::Base.blacklist = ['clown', 'biatch']
|
180
|
+
Obscenity::Base.whitelist = ['biatch']
|
181
|
+
}
|
182
|
+
should "validate the profanity of a word based on the custom list" do
|
183
|
+
assert_equal "Yo *****, sup", Obscenity::Base.replacement(:stars).sanitize('Yo clown, sup')
|
184
|
+
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo clown, sup')
|
185
|
+
assert_equal "Yo cl*wn, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo clown, sup')
|
186
|
+
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo clown, sup')
|
187
|
+
assert_equal "Yo biatch, sup", Obscenity::Base.replacement(:default).sanitize('Yo biatch, sup')
|
188
|
+
assert_equal "Yo assclown, sup", Obscenity::Base.replacement(:default).sanitize('Yo assclown, sup')
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context "#offensive" do
|
195
|
+
context "without whitelist" do
|
196
|
+
context "without custom config" do
|
197
|
+
setup {
|
198
|
+
Obscenity::Base.blacklist = :default
|
199
|
+
Obscenity::Base.whitelist = :default
|
200
|
+
}
|
201
|
+
should "return an array with the offensive words based on the default list" do
|
202
|
+
assert_equal ['assclown'], Obscenity::Base.offensive('Yo assclown, sup')
|
203
|
+
assert_equal [], Obscenity::Base.offensive('Hello world')
|
204
|
+
end
|
205
|
+
end
|
206
|
+
context "with custom blacklist config" do
|
207
|
+
setup { Obscenity::Base.blacklist = ['yo', 'word'] }
|
208
|
+
should "return an array with the offensive words based on a custom list" do
|
209
|
+
assert_equal ['yo', 'word'], Obscenity::Base.offensive('Yo word, sup')
|
210
|
+
assert_equal [], Obscenity::Base.offensive('Hello world')
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
context "with whitelist" do
|
215
|
+
context "without custom blacklist config" do
|
216
|
+
setup {
|
217
|
+
Obscenity::Base.blacklist = :default
|
218
|
+
Obscenity::Base.whitelist = ['biatch']
|
219
|
+
}
|
220
|
+
should "return an array with the offensive words based on the default blacklist and custom whitelist" do
|
221
|
+
assert_equal ['assclown'], Obscenity::Base.offensive('Yo assclown, sup')
|
222
|
+
assert_equal [], Obscenity::Base.offensive('Yo biatch, sup')
|
223
|
+
end
|
224
|
+
end
|
225
|
+
context "with custom blacklist/whitelist config" do
|
226
|
+
setup {
|
227
|
+
Obscenity::Base.blacklist = ['clown', 'biatch']
|
228
|
+
Obscenity::Base.whitelist = ['biatch']
|
229
|
+
}
|
230
|
+
should "return an array with the offensive words based on the custom list" do
|
231
|
+
assert_equal ['clown'], Obscenity::Base.offensive('Yo clown, sup')
|
232
|
+
assert_equal [], Obscenity::Base.offensive('Yo biatch, sup')
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
context "#replace" do
|
239
|
+
should "replace the given word by the given replacement method" do
|
240
|
+
[
|
241
|
+
[:vowels, {original: "Yo biatch", clean: "Yo b**tch"}],
|
242
|
+
[:stars, {original: "Yo biatch", clean: "Yo ******"}],
|
243
|
+
[:garbled, {original: "Yo biatch", clean: "Yo $@!#%"}],
|
244
|
+
[:default, {original: "Yo biatch", clean: "Yo $@!#%"}],
|
245
|
+
["[censored]", {original: "Yo biatch", clean: "Yo [censored]"}],
|
246
|
+
[nil, {original: "Yo biatch", clean: "Yo $@!#%"}]
|
247
|
+
].each do |replacement_method, content|
|
248
|
+
assert_equal content[:clean], Obscenity::Base.replacement(replacement_method).sanitize(content[:original])
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
end
|
data/test/test_config.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestConfig < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "#respond_to?" do
|
6
|
+
should "respond to methods and attributes" do
|
7
|
+
Obscenity::Config.new do |config|
|
8
|
+
[:blacklist, :whitelist, :replacement].each do |field|
|
9
|
+
assert config.respond_to?(field)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
should "properly set the config parameters" do
|
16
|
+
blacklist = ['ass', 'shit', 'penis']
|
17
|
+
whitelist = ['penis']
|
18
|
+
replacement = :stars
|
19
|
+
|
20
|
+
config = Obscenity::Config.new do |config|
|
21
|
+
config.blacklist = blacklist
|
22
|
+
config.whitelist = whitelist
|
23
|
+
config.replacement = replacement
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_equal blacklist, config.blacklist
|
27
|
+
assert_equal whitelist, config.whitelist
|
28
|
+
assert_equal replacement, config.replacement
|
29
|
+
end
|
30
|
+
|
31
|
+
should "return default values if none is set" do
|
32
|
+
config = Obscenity::Config.new
|
33
|
+
assert_equal [], config.whitelist
|
34
|
+
assert_equal :garbled, config.replacement
|
35
|
+
assert_match /config\/blacklist.yml/, config.blacklist
|
36
|
+
end
|
37
|
+
|
38
|
+
should "return default values when default values are set" do
|
39
|
+
config = Obscenity::Config.new do |config|
|
40
|
+
config.blacklist = :default
|
41
|
+
config.replacement = :default
|
42
|
+
end
|
43
|
+
assert_equal [], config.whitelist
|
44
|
+
assert_equal :default, config.replacement
|
45
|
+
assert_match /config\/blacklist.yml/, config.blacklist
|
46
|
+
end
|
47
|
+
|
48
|
+
should "properly validate the config options" do
|
49
|
+
[:blacklist, :whitelist].each do |field|
|
50
|
+
exceptions = [
|
51
|
+
[Obscenity::UnkownContent, {}],
|
52
|
+
[Obscenity::UnkownContent, ":unkown"],
|
53
|
+
[Obscenity::EmptyContentList, []],
|
54
|
+
[Obscenity::UnkownContentFile, "'path/to/file'"],
|
55
|
+
[Obscenity::UnkownContentFile, Pathname.new("'path/to/file'")]
|
56
|
+
].each do |klass, value|
|
57
|
+
assert_raise(klass){
|
58
|
+
Obscenity::Config.new do |config|
|
59
|
+
config.instance_eval "config.#{field} = #{value}"
|
60
|
+
end
|
61
|
+
}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestObscenity < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "#respond_to?" do
|
6
|
+
should "respond to methods and attributes" do
|
7
|
+
[:configure, :config, :profane?, :sanitize, :offensive, :replacement].each do |field|
|
8
|
+
assert Obscenity.respond_to?(field)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# More comprehensive test in test_config.rb
|
14
|
+
context "#configure" do
|
15
|
+
should "accept a configuration block " do
|
16
|
+
assert_nothing_raised{
|
17
|
+
Obscenity.configure do |config|
|
18
|
+
config.blacklist = :default
|
19
|
+
config.replacement = :garbled
|
20
|
+
end
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# More comprehensive test in test_config.rb
|
26
|
+
context "#config" do
|
27
|
+
should "return the current config object" do
|
28
|
+
assert_not_nil Obscenity.config
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# More comprehensive test in test_base.rb
|
33
|
+
context "#profane?" do
|
34
|
+
should "validate the profanity of the given content" do
|
35
|
+
assert Obscenity.profane?('Yo, check that ass out')
|
36
|
+
assert !Obscenity.profane?('Hello world')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# More comprehensive test in test_base.rb
|
41
|
+
context "#sanitize" do
|
42
|
+
should "sanitize the given content" do
|
43
|
+
assert_equal "Yo, check that $@!#% out", Obscenity.sanitize('Yo, check that ass out')
|
44
|
+
assert_equal "Hello world", Obscenity.sanitize('Hello world')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# More comprehensive test in test_base.rb
|
49
|
+
context "#offensive" do
|
50
|
+
should "return the offensive words for the given content" do
|
51
|
+
assert_equal ['ass', 'biatch'], Obscenity.offensive('Yo, check that ass biatch')
|
52
|
+
assert_equal [], Obscenity.offensive('Hello world')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# More comprehensive test in test_base.rb
|
57
|
+
context "#replacement" do
|
58
|
+
should "sanitize the given content based on the given replacement" do
|
59
|
+
assert_equal "Yo, check that $@!#% out", Obscenity.replacement(:garbled).sanitize('Yo, check that ass out')
|
60
|
+
assert_equal "Yo, check that $@!#% out", Obscenity.replacement(:default).sanitize('Yo, check that ass out')
|
61
|
+
assert_equal "Yo, check that *ss out", Obscenity.replacement(:vowels).sanitize('Yo, check that ass out')
|
62
|
+
assert_equal "Yo, check that *** out", Obscenity.replacement(:stars).sanitize('Yo, check that ass out')
|
63
|
+
assert_equal "Yo, check that [censored] out", Obscenity.replacement("[censored]").sanitize('Yo, check that ass out')
|
64
|
+
assert_equal "Hello world", Obscenity.sanitize('Hello world')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/test/test_rack.rb
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'rack/mock'
|
3
|
+
require 'obscenity/rack'
|
4
|
+
|
5
|
+
class TestRack < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "Rack::Obscenity" do
|
8
|
+
setup do
|
9
|
+
@env = {}
|
10
|
+
@body = 'hello'
|
11
|
+
@status = 200
|
12
|
+
@headers = { 'Content-Type' => 'text/plain' }
|
13
|
+
@app = lambda { |env| [@status, @headers, [@body]] }
|
14
|
+
end
|
15
|
+
|
16
|
+
def mock_env(options = {})
|
17
|
+
@env = Rack::MockRequest.env_for('/', options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def middleware(options = {})
|
21
|
+
Rack::Obscenity.new(@app, options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(params = {})
|
25
|
+
{ 'QUERY_STRING' => Rack::Utils.build_query(params) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_response_params
|
29
|
+
Rack::Utils.parse_query(@env['QUERY_STRING'], "&")
|
30
|
+
end
|
31
|
+
|
32
|
+
def post(params = {})
|
33
|
+
{ 'rack.input' => StringIO.new(Rack::Utils.build_query(params)) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def post_response_params
|
37
|
+
Rack::Utils.parse_query(@env['rack.input'].read, "&")
|
38
|
+
end
|
39
|
+
|
40
|
+
def assert_success_response(status, headers, body)
|
41
|
+
assert_equal @status, status
|
42
|
+
assert_equal @headers, headers
|
43
|
+
assert_equal [@body], body
|
44
|
+
end
|
45
|
+
|
46
|
+
context "default configuration" do
|
47
|
+
should "not evaluate the profanity of parameters" do
|
48
|
+
app = middleware
|
49
|
+
status, headers, body = app.call(mock_env)
|
50
|
+
assert_success_response status, headers, body
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "rejecting requests" do
|
55
|
+
should "not reject if parameter values don't contain profanity" do
|
56
|
+
app = middleware(reject: true)
|
57
|
+
status, headers, body = app.call(mock_env(get(foo: 'bar')))
|
58
|
+
assert_success_response status, headers, body
|
59
|
+
end
|
60
|
+
|
61
|
+
should "reject if GET parameter values contain profanity" do
|
62
|
+
app = middleware(reject: true)
|
63
|
+
status, headers, body = app.call(mock_env(get(foo: 'bar', baz: 'shit')))
|
64
|
+
assert_equal 422, status
|
65
|
+
assert_equal [''], body
|
66
|
+
end
|
67
|
+
|
68
|
+
should "reject if POST parameter values contain profanity" do
|
69
|
+
app = middleware(reject: true)
|
70
|
+
status, headers, body = app.call(mock_env(post(foo: 'bar', baz: 'ass')))
|
71
|
+
assert_equal 422, status
|
72
|
+
assert_equal [''], body
|
73
|
+
end
|
74
|
+
|
75
|
+
should "reject if given parameter values contain profanity" do
|
76
|
+
app = middleware(reject: { params: [:foo] })
|
77
|
+
[ get(foo: 'ass', baz: 'shit'),
|
78
|
+
post(foo: 'ass').merge(get(foo: 'nice', baz: 'shit'))
|
79
|
+
].each do |options|
|
80
|
+
status, headers, body = app.call(mock_env(options))
|
81
|
+
assert_equal 422, status
|
82
|
+
assert_equal [''], body
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
should "not reject if other parameter values contain profanity" do
|
87
|
+
app = middleware(reject: { params: [:foo] })
|
88
|
+
status, headers, body = app.call(mock_env(get(foo: 'nice', baz: 'shit')))
|
89
|
+
assert_success_response status, headers, body
|
90
|
+
end
|
91
|
+
|
92
|
+
should "reject if parameter values contain profanity" do
|
93
|
+
app = middleware(reject: { params: :all })
|
94
|
+
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
95
|
+
assert_equal 422, status
|
96
|
+
assert_equal [''], body
|
97
|
+
end
|
98
|
+
|
99
|
+
should "reject if parameter values contain profanity and display a custom message" do
|
100
|
+
app = middleware(reject: { message: "We don't accept profanity" })
|
101
|
+
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
102
|
+
assert_equal 422, status
|
103
|
+
assert_equal ["We don't accept profanity"], body
|
104
|
+
end
|
105
|
+
|
106
|
+
should "reject if parameter values contain profanity and render a custom file" do
|
107
|
+
app = middleware(reject: { path: "test/static/422.html" })
|
108
|
+
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
109
|
+
assert_equal 422, status
|
110
|
+
assert_equal ["We don't accept profanity"], body
|
111
|
+
end
|
112
|
+
|
113
|
+
should "reject parameter values when they're a hash and contain profanity" do
|
114
|
+
app = middleware(reject: true)
|
115
|
+
status, headers, body = app.call(mock_env(get(foo: 'clean', bar: {one: 'ass'})))
|
116
|
+
assert_equal 422, status
|
117
|
+
assert_equal [''], body
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context "sanitizing requests" do
|
122
|
+
should "not sanitize if parameter values don't contain profanity" do
|
123
|
+
app = middleware(sanitize: true)
|
124
|
+
status, headers, body = app.call(mock_env(get(foo: 'bar')))
|
125
|
+
assert_success_response status, headers, body
|
126
|
+
request_params = get_response_params
|
127
|
+
assert_equal 'bar', request_params['foo']
|
128
|
+
end
|
129
|
+
|
130
|
+
should "sanitize if GET parameter values contain profanity" do
|
131
|
+
app = middleware(sanitize: true)
|
132
|
+
status, headers, body = app.call(mock_env(get(foo: 'bar', baz: 'shit')))
|
133
|
+
assert_success_response status, headers, body
|
134
|
+
request_params = get_response_params
|
135
|
+
assert_equal 'bar', request_params['foo']
|
136
|
+
assert_equal '$@!#%', request_params['baz']
|
137
|
+
end
|
138
|
+
|
139
|
+
should "sanitize if POST parameter values contain profanity" do
|
140
|
+
app = middleware(sanitize: true)
|
141
|
+
status, headers, body = app.call(mock_env(post(foo: 'bar', baz: 'ass')))
|
142
|
+
assert_success_response status, headers, body
|
143
|
+
request_params = post_response_params
|
144
|
+
assert_equal 'bar', request_params['foo']
|
145
|
+
assert_equal '$@!#%', request_params['baz']
|
146
|
+
end
|
147
|
+
|
148
|
+
should "not sanitize if other parameter values contain profanity" do
|
149
|
+
app = middleware(sanitize: { params: [:foo] })
|
150
|
+
status, headers, body = app.call(mock_env(get(foo: 'nice', baz: 'shit')))
|
151
|
+
assert_success_response status, headers, body
|
152
|
+
request_params = get_response_params
|
153
|
+
assert_equal 'nice', request_params['foo']
|
154
|
+
assert_equal 'shit', request_params['baz']
|
155
|
+
end
|
156
|
+
|
157
|
+
should "sanitize if parameter values contain profanity" do
|
158
|
+
app = middleware(sanitize: { params: :all })
|
159
|
+
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
160
|
+
assert_success_response status, headers, body
|
161
|
+
request_params = get_response_params
|
162
|
+
assert_equal '$@!#%', request_params['foo']
|
163
|
+
end
|
164
|
+
|
165
|
+
should "sanitize the title using the :garbled replacement" do
|
166
|
+
app = middleware(sanitize: { replacement: :garbled })
|
167
|
+
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
168
|
+
assert_success_response status, headers, body
|
169
|
+
request_params = get_response_params
|
170
|
+
assert_equal '$@!#%', request_params['foo']
|
171
|
+
end
|
172
|
+
|
173
|
+
should "sanitize the title using the :stars replacement" do
|
174
|
+
app = middleware(sanitize: { replacement: :stars })
|
175
|
+
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
176
|
+
assert_success_response status, headers, body
|
177
|
+
request_params = get_response_params
|
178
|
+
assert_equal '***', request_params['foo']
|
179
|
+
end
|
180
|
+
|
181
|
+
should "sanitize the title using the :vowels replacement" do
|
182
|
+
app = middleware(sanitize: { replacement: :vowels })
|
183
|
+
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
184
|
+
assert_success_response status, headers, body
|
185
|
+
request_params = get_response_params
|
186
|
+
assert_equal '*ss', request_params['foo']
|
187
|
+
end
|
188
|
+
|
189
|
+
should "sanitize the title using a custom replacement" do
|
190
|
+
app = middleware(sanitize: { replacement: "[censored]" })
|
191
|
+
status, headers, body = app.call(mock_env(get(foo: 'text with ass')))
|
192
|
+
assert_success_response status, headers, body
|
193
|
+
request_params = get_response_params
|
194
|
+
assert_equal 'text with [censored]', request_params['foo']
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|