-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
censor_rule_spec.rb
357 lines (305 loc) · 11.5 KB
/
censor_rule_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# == Schema Information
# Schema version: 20240313094449
#
# Table name: censor_rules
#
# id :integer not null, primary key
# info_request_id :integer
# user_id :integer
# public_body_id :integer
# text :text not null
# replacement :text not null
# last_edit_editor :string not null
# last_edit_comment :text not null
# created_at :datetime not null
# updated_at :datetime not null
# regexp :boolean default(FALSE), not null
#
require 'spec_helper'
RSpec.describe CensorRule do
describe '#apply_to_text' do
it 'applies the rule to the text' do
rule = FactoryBot.build(:censor_rule, text: 'secret')
text = 'Some secret text'
expect(rule.apply_to_text(text)).to eq('Some [REDACTED] text')
end
it 'does not mutate the input' do
rule = FactoryBot.build(:censor_rule, text: 'secret')
text = 'Some secret text'
rule.apply_to_text(text)
expect(text).to eq('Some secret text')
end
it 'returns the text if the rule is unmatched' do
rule = FactoryBot.build(:censor_rule, text: 'secret')
text = 'Some text'
expect(rule.apply_to_text(text)).to eq('Some text')
end
it 'replaces the regexp with the replacement text when applied to text' do
attrs = { text: '--PRIVATE.*--PRIVATE',
replacement: "--REMOVED\nHidden private info\n--REMOVED",
regexp: true }
rule = FactoryBot.build(:censor_rule, attrs)
text = <<-EOF.strip_heredoc
Some public information
--PRIVATE
Some private information
--PRIVATE
EOF
expect(rule.apply_to_text(text)).to eq <<-EOF.strip_heredoc
Some public information
--REMOVED
Hidden private info
--REMOVED
EOF
end
end
describe '#apply_to_binary' do
it 'applies the rule to the text' do
rule = FactoryBot.build(:censor_rule, text: 'secret')
text = 'Some secret text'
expect(rule.apply_to_binary(text)).to eq('Some xxxxxx text')
end
it 'does not modify the size of the string' do
rule = FactoryBot.build(:censor_rule, text: 'secret')
text = 'Some secret text'
original_text = text.dup
redacted = rule.apply_to_binary(text)
expect(redacted.bytesize).to eq(original_text.bytesize)
end
it 'does not modify the size of UTF-8 string' do
rule = FactoryBot.build(:censor_rule, text: 'sécret')
text = 'Some sécret text'
original_text = text.dup
redacted = rule.apply_to_binary(text)
expect(redacted.bytesize).to eq(original_text.bytesize)
end
it 'does not mutate the input' do
rule = FactoryBot.build(:censor_rule, text: 'secret')
text = 'Some secret text'
rule.apply_to_binary(text)
expect(text).to eq('Some secret text')
end
it 'returns the text if the rule is unmatched' do
rule = FactoryBot.build(:censor_rule, text: 'secret')
text = 'Some text'
expect(rule.apply_to_binary(text)).to eq('Some text')
end
it 'handles UTF-8 text' do
rule = FactoryBot.build(:censor_rule, text: 'sécret')
text = 'Some sécret text'
text.force_encoding('UTF-8')
expect(rule.apply_to_binary(text)).to eq("Some xxxxxxx text")
end
it 'handles a UTF-8 rule and ASCII-8BIT text' do
rule = FactoryBot.build(:censor_rule, text: 'sécret')
text = 'Some sécret text'
text.force_encoding('ASCII-8BIT')
expect(rule.apply_to_binary(text)).to eq("Some xxxxxxx text")
end
it "replaces the regexp with the same number of 'x' characters as the text
replaced when applied to binary" do
attrs = { text: '--PRIVATE.*--PRIVATE',
replacement: "--REMOVED\nHidden private info\n--REMOVED",
regexp: true }
rule = FactoryBot.build(:censor_rule, attrs)
text = <<-EOF.strip_heredoc
Some public information
--PRIVATE
Some private information
--PRIVATE
EOF
expect(rule.apply_to_binary(text)).to eq <<-EOF.strip_heredoc
Some public information
xxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxx
EOF
end
it 'handles a UTF-8 rule with ASCII-8BIT text' do
attrs = { text: '--PRIVATE.*--P‘RIVATE',
replacement: "--REMOVED\nHidden private info\n--REMOVED",
regexp: true }
rule = FactoryBot.build(:censor_rule, attrs)
text = <<-EOF.strip_heredoc
Some public information
--PRIVATE
Some private information
--P‘RIVATE
EOF
text.force_encoding('ASCII-8BIT')
expect(rule.apply_to_binary(text)).to eq <<-EOF.strip_heredoc
Some public information
xxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx
EOF
end
end
describe '#expire_requests' do
it 'create expire job for the request if it is a request rule' do
request = FactoryBot.create(:info_request)
rule = FactoryBot.create(:info_request_censor_rule,
info_request: request)
expect(InfoRequestExpireJob).to receive(:perform_later).with(request)
expect(NotifyCacheJob).to receive(:perform_later).with(request)
rule.expire_requests
end
it 'create expire job for the user if it is a user rule' do
user = FactoryBot.create(:user)
rule = FactoryBot.create(:user_censor_rule, user: user)
expect(InfoRequestExpireJob).to receive(:perform_later).
with(user, :info_requests)
rule.expire_requests
end
it 'create expire job for the public body if it is a public body rule' do
body = FactoryBot.create(:public_body)
rule = FactoryBot.create(:public_body_censor_rule, public_body: body)
expect(InfoRequestExpireJob).to receive(:perform_later).
with(body, :info_requests)
rule.expire_requests
end
it 'create expire job for all requests if it is a global rule' do
rule = FactoryBot.build(:global_censor_rule)
expect(InfoRequestExpireJob).to receive(:perform_later).
with(InfoRequest, :all)
rule.expire_requests
end
end
describe '#censorable_requests' do
subject { censor_rule.censorable_requests }
context 'with an info_request censor rule' do
let(:censor_rule) { FactoryBot.create(:info_request_censor_rule) }
let(:requests) { censorable.info_requests }
it { is_expected.to match_array([censor_rule.info_request]) }
end
context 'with a public_body censor rule' do
let(:censor_rule) { FactoryBot.create(:public_body_censor_rule) }
let(:censorable) { censor_rule.public_body }
before { FactoryBot.create(:info_request, public_body: censorable) }
it { is_expected.to match_array(censorable.info_requests) }
end
context 'with a user censor rule' do
let(:censor_rule) { FactoryBot.create(:user_censor_rule) }
let(:censorable) { censor_rule.user }
before { FactoryBot.create(:info_request, user: censorable) }
it { is_expected.to match_array(censorable.info_requests) }
end
context 'with a global censor rule' do
let(:censor_rule) { FactoryBot.create(:global_censor_rule) }
it { is_expected.to eq(InfoRequest.unscoped) }
end
end
describe '#censorable' do
subject { censor_rule.censorable }
context 'with an info_request censor rule' do
let(:censor_rule) { FactoryBot.build(:info_request_censor_rule) }
it { is_expected.to eq(censor_rule.info_request) }
end
context 'with a public_body censor rule' do
let(:censor_rule) { FactoryBot.build(:public_body_censor_rule) }
it { is_expected.to eq(censor_rule.public_body) }
end
context 'with a user censor rule' do
let(:censor_rule) { FactoryBot.build(:user_censor_rule) }
it { is_expected.to eq(censor_rule.user) }
end
context 'with a global censor rule' do
let(:censor_rule) { FactoryBot.build(:global_censor_rule) }
it { is_expected.to be_nil }
end
end
end
RSpec.describe 'when validating rules' do
it 'must have the text to redact' do
censor_rule = CensorRule.new
censor_rule.valid?
expect(censor_rule.errors[:text].size).to eq(1)
expect(censor_rule.errors[:text]).to eql(["can't be blank"])
end
it 'must have a replacement' do
censor_rule = CensorRule.new
censor_rule.valid?
expect(censor_rule.errors[:replacement].size).to eq(1)
end
it 'must have a last_edit_editor' do
censor_rule = CensorRule.new
censor_rule.valid?
expect(censor_rule.errors[:last_edit_editor].size).to eq(1)
end
it 'must have a last_edit_comment' do
censor_rule = CensorRule.new
censor_rule.valid?
expect(censor_rule.errors[:last_edit_comment].size).to eq(1)
end
describe 'when validating a regexp rule' do
before do
@censor_rule = CensorRule.new(regexp: true,
text: '*',
replacement: '---',
last_edit_comment: 'test',
last_edit_editor: 'rspec')
end
it 'should try to create a regexp from the text' do
expect(Regexp).to receive(:new).with('*', Regexp::MULTILINE)
@censor_rule.valid?
end
describe 'if a regexp error is produced' do
it 'should add an error message to the text field with the regexp error message' do
allow(Regexp).to receive(:new).and_raise(RegexpError.new("very bad regexp"))
expect(@censor_rule.valid?).to eq(false)
expect(@censor_rule.errors[:text]).to eq(["very bad regexp"])
end
end
describe 'if a regexp contains unescaped characters' do
before { @censor_rule.text = 'foo]' }
it 'does not output a warning' do
expect { @censor_rule.valid? }.not_to output.to_stderr
end
it 'adds an error message to the text field' do
msg = "regular expression has ']' without escape: /foo]/"
@censor_rule.valid?
expect(@censor_rule.errors[:text]).to eq([msg])
end
end
describe 'if no regexp error is produced' do
it 'should not add any error message to the text field' do
allow(Regexp).to receive(:new)
@censor_rule.valid?
expect(@censor_rule.errors[:text]).to eq([])
end
end
end
end
RSpec.describe 'when handling global rules' do
describe 'an instance without user_id, request_id or public_body_id' do
before do
@global_rule = CensorRule.new
end
it 'should return a value of true from is_global?' do
expect(@global_rule.is_global?).to eq(true)
end
end
describe '.global' do
before do
@global_rule = CensorRule.create!(text: 'hide me',
replacement: 'nothing to see here',
last_edit_editor: 1,
last_edit_comment: 'comment')
@user_rule = CensorRule.create!(user_id: 1,
text: 'hide me',
replacement: 'nothing to see here',
last_edit_editor: 1,
last_edit_comment: 'comment')
end
it 'should include an instance without user_id, request_id or public_body_id' do
expect(CensorRule.global.include?(@global_rule)).to eq(true)
end
it 'should not include a request with user_id' do
expect(CensorRule.global.include?(@user_rule)).to eq(false)
end
after do
@global_rule.destroy if @global_rule
@user_rule.destroy if @user_rule
end
end
end