1
1
require 'spec_helper'
2
2
3
+ if ActiveRecord . version >= Gem ::Version . new ( '7.1' ) && defined? ( Mysql2 ::Error )
4
+ ActiveRecord ::ConnectionAdapters ::AbstractAdapter . class_eval do
5
+ prepend Blouson ::SensitiveQueryFilter ::AbstractAdapterFilter
6
+ end
7
+ end
8
+
9
+ ActiveRecord ::StatementInvalid . class_eval do
10
+ prepend Blouson ::SensitiveQueryFilter ::StatementInvalidErrorFilter
11
+ end
12
+
3
13
RSpec . describe Blouson ::SensitiveQueryFilter do
4
14
describe 'StatementInvalidErrorFilter' do
5
- def error
6
- model_class . where ( condition ) . first
7
- rescue => e
8
- return e
9
- end
10
-
11
- before do
12
- dummy_error = Class . new ( ActiveRecord ::StatementInvalid ) do
13
- prepend Blouson ::SensitiveQueryFilter ::StatementInvalidErrorFilter
14
- end
15
- stub_const ( 'ActiveRecord::StatementInvalid' , dummy_error )
16
- end
15
+ def error
16
+ model_class . where ( condition ) . first
17
+ rescue => e
18
+ return e
19
+ end
17
20
18
21
context 'with query to sensitive table' do
19
22
let ( :model_class ) { SecureUser }
20
23
let ( :email ) { 'alice@example.com' }
21
24
let ( :condition ) { { invalid_column : email } }
22
25
23
26
it 'filters SQL statement' do
24
- if Rails ::VERSION ::MAJOR >= 6
27
+ if ActiveRecord ::VERSION ::MAJOR >= 6
25
28
expect { model_class . where ( condition ) . first } . to raise_error ( /\[ FILTERED\] / )
26
29
else
27
30
expect { model_class . where ( condition ) . first } . to raise_error ( /SELECT.*\[ FILTERED\] / )
28
31
end
29
32
end
30
33
31
34
it 'filters to_s message' do
32
- if Rails ::VERSION ::MAJOR >= 6
35
+ if ActiveRecord ::VERSION ::MAJOR >= 6
33
36
expect ( error . to_s ) . not_to include ( email )
34
37
expect ( error . to_s ) . to include ( '[FILTERED]' )
35
38
else
@@ -40,7 +43,7 @@ def error
40
43
end
41
44
42
45
it 'filters inspect message' do
43
- if Rails ::VERSION ::MAJOR >= 6
46
+ if ActiveRecord ::VERSION ::MAJOR >= 6
44
47
expect ( error . inspect ) . to include ( '[FILTERED]' )
45
48
else
46
49
expect ( error . to_s ) . to include ( 'SELECT' )
@@ -49,7 +52,7 @@ def error
49
52
end
50
53
end
51
54
52
- if Rails ::VERSION ::MAJOR >= 6
55
+ if ActiveRecord ::VERSION ::MAJOR >= 6
53
56
it 'filters sql message' do
54
57
expect ( error . sql ) . to include ( 'SELECT' )
55
58
expect ( error . sql ) . not_to include ( email )
@@ -64,7 +67,7 @@ def error
64
67
rescue => e
65
68
error = e
66
69
end
67
- if Rails ::VERSION ::MAJOR >= 6
70
+ if ActiveRecord ::VERSION ::MAJOR >= 6
68
71
expect ( error . to_s ) . not_to include ( email )
69
72
expect ( error . to_s ) . to include ( '[FILTERED]' )
70
73
@@ -82,7 +85,7 @@ def error
82
85
let ( :email ) { "'alice'@example'.com''" }
83
86
84
87
it 'filters sensitive data' do
85
- if Rails ::VERSION ::MAJOR >= 6
88
+ if ActiveRecord ::VERSION ::MAJOR >= 6
86
89
expect ( error . to_s ) . not_to include ( 'alice' )
87
90
88
91
expect ( error . sql ) . to include ( 'SELECT' )
@@ -101,7 +104,7 @@ def error
101
104
let ( :condition ) { { invalid_column : email , email2 : email } }
102
105
103
106
it 'filters sensitive data' do
104
- if Rails ::VERSION ::MAJOR >= 6
107
+ if ActiveRecord ::VERSION ::MAJOR >= 6
105
108
expect ( error . to_s ) . not_to include ( 'alice' )
106
109
107
110
expect ( error . sql ) . to include ( 'SELECT' )
@@ -119,11 +122,6 @@ def error
119
122
120
123
context 'with sensitive value in Mysql2::Error' do
121
124
before do
122
- dummy_error = Class . new ( ActiveRecord ::RecordNotUnique ) do
123
- prepend Blouson ::SensitiveQueryFilter ::StatementInvalidErrorFilter
124
- end
125
- stub_const ( 'ActiveRecord::RecordNotUnique' , dummy_error )
126
-
127
125
model_class . create! ( email : email , email2 : email )
128
126
end
129
127
@@ -134,7 +132,7 @@ def error
134
132
it 'filters sensitive data' do
135
133
expect { model_class . create! ( email : email , email2 : email ) } . to raise_error { |e |
136
134
expect ( e ) . to be_a ( ActiveRecord ::RecordNotUnique )
137
- if Rails ::VERSION ::MAJOR >= 6
135
+ if ActiveRecord ::VERSION ::MAJOR >= 6
138
136
expect ( e . message ) . to_not include ( 'alice' )
139
137
140
138
expect ( e . sql ) . to include ( 'INSERT INTO `secure_users` ' )
@@ -162,7 +160,7 @@ def error
162
160
let ( :condition ) { { invalid_column : name } }
163
161
164
162
it 'does not filter SQL statement' do
165
- if Rails ::VERSION ::MAJOR >= 6
163
+ if ActiveRecord ::VERSION ::MAJOR >= 6
166
164
expect { model_class . where ( condition ) . first } . to raise_error ( /Unknown column 'users.invalid_column'/ )
167
165
else
168
166
expect { model_class . where ( condition ) . first } . to raise_error ( /Unknown column 'users.invalid_column'/ )
@@ -171,7 +169,7 @@ def error
171
169
end
172
170
173
171
it 'does not filter to_s' do
174
- if Rails ::VERSION ::MAJOR >= 6
172
+ if ActiveRecord ::VERSION ::MAJOR >= 6
175
173
expect ( error . to_s ) . not_to include ( '[FILTERED]' )
176
174
else
177
175
expect ( error . to_s ) . to include ( 'SELECT' )
@@ -181,7 +179,7 @@ def error
181
179
end
182
180
183
181
it 'does not filter inspect message' do
184
- if Rails ::VERSION ::MAJOR >= 6
182
+ if ActiveRecord ::VERSION ::MAJOR >= 6
185
183
expect ( error . inspect ) . not_to include ( '[FILTERED]' )
186
184
else
187
185
expect ( error . to_s ) . to include ( 'SELECT' )
@@ -190,7 +188,7 @@ def error
190
188
end
191
189
end
192
190
193
- if Rails ::VERSION ::MAJOR >= 6
191
+ if ActiveRecord ::VERSION ::MAJOR >= 6
194
192
it 'does not filter sql message' do
195
193
expect ( error . sql ) . to include ( 'SELECT' )
196
194
expect ( error . sql ) . to include ( name )
@@ -200,11 +198,6 @@ def error
200
198
201
199
context 'with non-sensitive value in Mysql2::Error' do
202
200
before do
203
- dummy_error = Class . new ( ActiveRecord ::RecordNotUnique ) do
204
- prepend Blouson ::SensitiveQueryFilter ::StatementInvalidErrorFilter
205
- end
206
- stub_const ( 'ActiveRecord::RecordNotUnique' , dummy_error )
207
-
208
201
model_class . create! ( name : name )
209
202
end
210
203
@@ -216,7 +209,7 @@ def error
216
209
expect { model_class . create! ( name : name ) } . to raise_error { |e |
217
210
expect ( e ) . to be_a ( ActiveRecord ::RecordNotUnique )
218
211
219
- if Rails ::VERSION ::MAJOR >= 6
212
+ if ActiveRecord ::VERSION ::MAJOR >= 6
220
213
expect ( e . message ) . to include ( name )
221
214
expect ( e . message ) . to_not include ( '[FILTERED]' )
222
215
0 commit comments