obscenity2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,69 @@
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 *h*t out", Obscenity.replacement(:nonconsonants).sanitize('Yo, check that 5hit out')
63
+ assert_equal "Yo, check that *** out", Obscenity.replacement(:stars).sanitize('Yo, check that ass out')
64
+ assert_equal "Yo, check that [censored] out", Obscenity.replacement("[censored]").sanitize('Yo, check that ass out')
65
+ assert_equal "Hello world", Obscenity.sanitize('Hello world')
66
+ end
67
+ end
68
+
69
+ end
data/test/test_rack.rb ADDED
@@ -0,0 +1,207 @@
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 the :nonconsonants replacement" do
190
+ app = middleware(sanitize: { replacement: :nonconsonants })
191
+ status, headers, body = app.call(mock_env(get(foo: '5hit')))
192
+ assert_success_response status, headers, body
193
+ request_params = get_response_params
194
+ assert_equal '*h*t', request_params['foo']
195
+ end
196
+
197
+ should "sanitize the title using a custom replacement" do
198
+ app = middleware(sanitize: { replacement: "[censored]" })
199
+ status, headers, body = app.call(mock_env(get(foo: 'text with ass')))
200
+ assert_success_response status, headers, body
201
+ request_params = get_response_params
202
+ assert_equal 'text with [censored]', request_params['foo']
203
+ end
204
+ end
205
+ end
206
+
207
+ end
@@ -0,0 +1,9 @@
1
+ require 'helper'
2
+
3
+ class TestVersion < Test::Unit::TestCase
4
+
5
+ should "return the correct product version" do
6
+ assert_equal '1.0.2', Obscenity::VERSION
7
+ end
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: obscenity2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thiago Jackiw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " Obscenity is a profanity filter gem for Ruby/Rubinius, Rails (through
14
+ ActiveModel), and Rack middleware "
15
+ email: tjackiw@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".document"
21
+ - ".gitignore"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - config/blacklist.yml
28
+ - config/international.yml
29
+ - lib/obscenity.rb
30
+ - lib/obscenity/active_model.rb
31
+ - lib/obscenity/base.rb
32
+ - lib/obscenity/config.rb
33
+ - lib/obscenity/error.rb
34
+ - lib/obscenity/rack.rb
35
+ - lib/obscenity/rspec_matcher.rb
36
+ - lib/obscenity/version.rb
37
+ - obscenity.gemspec
38
+ - test/helper.rb
39
+ - test/static/422.html
40
+ - test/test_active_model.rb
41
+ - test/test_base.rb
42
+ - test/test_config.rb
43
+ - test/test_obscenity.rb
44
+ - test/test_rack.rb
45
+ - test/test_version.rb
46
+ homepage: http://github.com/tjackiw/obscenity
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.5.1
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Obscenity is a profanity filter gem for Ruby/Rubinius, Rails (through ActiveModel),
70
+ and Rack middleware
71
+ test_files:
72
+ - test/helper.rb
73
+ - test/static/422.html
74
+ - test/test_active_model.rb
75
+ - test/test_base.rb
76
+ - test/test_config.rb
77
+ - test/test_obscenity.rb
78
+ - test/test_rack.rb
79
+ - test/test_version.rb