-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
foi_attachment_spec.rb
536 lines (443 loc) · 16.7 KB
/
foi_attachment_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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# == Schema Information
# Schema version: 20230717201410
#
# Table name: foi_attachments
#
# id :integer not null, primary key
# content_type :text
# filename :text
# charset :text
# display_size :text
# url_part_number :integer
# within_rfc822_subject :text
# incoming_message_id :integer
# hexdigest :string(32)
# created_at :datetime
# updated_at :datetime
# prominence :string default("normal")
# prominence_reason :text
# masked_at :datetime
#
require 'spec_helper'
require 'models/concerns/message_prominence'
RSpec.describe FoiAttachment do
it_behaves_like 'concerns/message_prominence', :body_text
describe '.binary' do
subject { described_class.binary }
before do
FactoryBot.create(:body_text)
FactoryBot.create(:html_attachment)
end
let(:binary_attachments) do
[FactoryBot.create(:pdf_attachment),
FactoryBot.create(:rtf_attachment),
FactoryBot.create(:jpeg_attachment),
FactoryBot.create(:unknown_attachment)]
end
it { is_expected.to match_array(binary_attachments) }
end
describe '.cached_urls' do
it 'includes the correct paths' do
att = FactoryBot.create(:jpeg_attachment)
im = FactoryBot.create(:plain_incoming_message)
att.incoming_message = im
request_path = "/request/" + att.info_request.url_title
expect(att.cached_urls).to eq([request_path])
end
end
describe '#body=' do
it "sets the body" do
attachment = FoiAttachment.new
attachment.body = "baz"
expect(attachment.body).to eq("baz")
end
it "sets the size" do
attachment = FoiAttachment.new
attachment.body = "baz"
expect(attachment.body).to eq("baz")
expect(attachment.display_size).to eq("0K")
end
it "reparses the body if it disappears" do
load_raw_emails_data
im = incoming_messages(:useless_incoming_message)
im.extract_attachments!
main = im.get_main_body_text_part
orig_body = main.body
main.delete_cached_file!
expect {
im.get_main_body_text_part.body
}.not_to raise_error
main.delete_cached_file!
main = im.get_main_body_text_part
expect(main.body).to eq(orig_body)
end
it 'can parse raw email and read attachment inside DB transaction' do
im = FactoryBot.create(:plain_incoming_message)
FoiAttachment.transaction do
expect { im.get_text_for_indexing_full }.to_not raise_error
main_part = im.get_main_body_text_part
expect(main_part.body).to match(/That's so totally a rubbish question/)
end
end
it 'does not update hexdigest if already present' do
attachment = FoiAttachment.new(hexdigest: 'ABC')
expect { attachment.body = 'foo' }.to_not change { attachment.hexdigest }
end
it 'allow calls to #body to be made before save' do
attachment = FactoryBot.build(:foi_attachment, :unmasked)
blob = attachment.file.blob
expect {
attachment.body
attachment.save!
}.to change {
ActiveStorage::Blob.services.fetch(blob.service_name).exist?(blob.key)
}.from(false).to(true)
end
it 'does not reset existing blob key' do
attachment = FactoryBot.create(
:foi_attachment, :unmasked, body: 'unmasked'
)
expect { attachment.update(body: 'masked', masked_at: Time.now) }.
to_not change { attachment.file_blob.key }
end
it 'does not reset existing blob metadata' do
attachment = FactoryBot.create(
:foi_attachment, :unmasked, body: 'unmasked'
)
expect { attachment.update(body: 'masked', masked_at: Time.now) }.
to_not change { attachment.file_blob.metadata }
end
it 'persists changes to existing blob checksum' do
attachment = FactoryBot.create(
:foi_attachment, :unmasked, body: 'unmasked'
)
expect { attachment.update(body: 'masked', masked_at: Time.now) }.
to change { attachment.file_blob.checksum }
expect(attachment.file_blob.changed?).to eq false
end
end
describe '#body' do
context 'when masked' do
let(:foi_attachment) { FactoryBot.create(:body_text) }
it 'returns a binary encoded string when newly created' do
expect(foi_attachment.body.encoding.to_s).to eq('ASCII-8BIT')
end
it 'returns a binary encoded string when saved' do
foi_attachment_2 = FoiAttachment.find(foi_attachment.id)
expect(foi_attachment_2.body.encoding.to_s).to eq('ASCII-8BIT')
end
end
context 'when masked but stored attachment is missing' do
let(:foi_attachment) { FactoryBot.create(:body_text) }
before do
allow(foi_attachment.file).
to receive(:download).and_raise(ActiveStorage::FileNotFoundError)
end
it 'calls the FoiAttachmentMaskJob now and return the masked body' do
expect(FoiAttachmentMaskJob).to receive(:perform_now).
with(foi_attachment).
and_invoke(-> (_) {
# mock the job
foi_attachment.update(body: 'maskedbody', masked_at: Time.zone.now)
})
expect(foi_attachment.body).to eq('maskedbody')
end
end
context 'when unmasked and original attachment can be found' do
let(:incoming_message) do
FactoryBot.create(:incoming_message, foi_attachments_factories: [
[:body_text, :unmasked]
])
end
let(:foi_attachment) { incoming_message.foi_attachments.last }
it 'calls the FoiAttachmentMaskJob now and return the masked body' do
expect(FoiAttachmentMaskJob).to receive(:perform_now).
with(foi_attachment).
and_invoke(-> (_) {
# mock the job
foi_attachment.update(body: 'maskedbody', masked_at: Time.zone.now)
})
expect(foi_attachment.body).to eq('maskedbody')
end
end
context 'when unmasked and original attachment can not be found' do
let(:incoming_message) do
FactoryBot.create(:incoming_message, foi_attachments_factories: [
[:body_text, :unmasked]
])
end
let(:foi_attachment) { incoming_message.foi_attachments.last }
before do
foi_attachment.update(hexdigest: '123')
expect(FoiAttachmentMaskJob).to receive(:perform_now).
with(foi_attachment).
and_invoke(-> (_) {
# mock the job
incoming_message.parse_raw_email!(true)
})
end
it 'returns load_attachment_from_incoming_message.body' do
allow(foi_attachment).to(
receive(:load_attachment_from_incoming_message).and_return(
double(body: 'thisisthenewtext')
)
)
expect(foi_attachment.body).to eq('thisisthenewtext')
end
it 'raises MissingAttachment exception if attachment still can not be found' do
allow(foi_attachment).to(
receive(:load_attachment_from_incoming_message).and_return(nil)
)
expect { foi_attachment.body }.to raise_error(
FoiAttachment::MissingAttachment
)
end
end
context 'when attachment has been destroy' do
let(:foi_attachment) { FactoryBot.create(:foi_attachment) }
before { foi_attachment.destroy }
it 'returns load_attachment_from_incoming_message.body' do
allow(foi_attachment).to(
receive(:load_attachment_from_incoming_message).and_return(
double(body: 'thisisthenewtext')
)
)
expect(foi_attachment.body).to eq('thisisthenewtext')
end
end
end
describe '#body_as_text' do
it 'has a valid UTF-8 string when newly created' do
foi_attachment = FactoryBot.create(:body_text)
expect(foi_attachment.body_as_text.string.encoding.to_s).to eq('UTF-8')
expect(foi_attachment.body_as_text.string.valid_encoding?).to be true
end
it 'has a valid UTF-8 string when saved' do
foi_attachment = FactoryBot.create(:body_text)
foi_attachment = FoiAttachment.find(foi_attachment.id)
expect(foi_attachment.body_as_text.string.encoding.to_s).to eq('UTF-8')
expect(foi_attachment.body_as_text.string.valid_encoding?).to be true
end
it 'has a true scrubbed? value if the body has been coerced to valid UTF-8' do
foi_attachment = FactoryBot.create(:body_text)
foi_attachment.body = "\x0FX\x1C\x8F\xA4\xCF\xF6\x8C\x9D\xA7\x06\xD9\xF7\x90lo"
expect(foi_attachment.body_as_text.scrubbed?).to be true
end
it 'has a false scrubbed? value if the body has not been coerced to valid UTF-8' do
foi_attachment = FactoryBot.create(:body_text)
foi_attachment.body = "κόσμε"
expect(foi_attachment.body_as_text.scrubbed?).to be false
end
end
describe '#default_body' do
it 'returns valid UTF-8 for a text attachment' do
foi_attachment = FactoryBot.create(:body_text)
expect(foi_attachment.default_body.encoding.to_s).to eq('UTF-8')
expect(foi_attachment.default_body.valid_encoding?).to be true
end
it 'returns binary for a PDF attachment' do
foi_attachment = FactoryBot.create(:pdf_attachment)
expect(foi_attachment.default_body.encoding.to_s).to eq('ASCII-8BIT')
end
end
describe '#unmasked_body' do
let(:foi_attachment) { FactoryBot.create(:body_text) }
subject(:unmasked_body) { foi_attachment.unmasked_body }
before do
allow(foi_attachment).to receive(:raw_email).
and_return(double(mail: Mail.new))
end
context 'when mail handler finds original attachment by hexdigest' do
before do
allow(MailHandler).to receive(:attachment_body_for_hexdigest).
and_return('hereistheunmaskedtext')
end
it 'returns the attachment body from the raw email' do
is_expected.to eq('hereistheunmaskedtext')
end
end
context 'when able to find original attachment through other means' do
before do
allow(MailHandler).to receive(:attachment_body_for_hexdigest).
and_raise(MailHandler::MismatchedAttachmentHexdigest)
allow(MailHandler).to receive(
:attempt_to_find_original_attachment_attributes
).and_return(hexdigest: 'ABC', body: 'hereistheunmaskedtext')
end
it 'updates the hexdigest' do
expect { unmasked_body }.to change { foi_attachment.hexdigest }.
to('ABC')
end
it 'returns the attachment body from the raw email' do
is_expected.to eq('hereistheunmaskedtext')
end
end
context 'when unable to find original attachment in storage' do
before do
allow(foi_attachment.file).
to receive(:download).and_raise(ActiveStorage::FileNotFoundError)
end
it 'raises missing attachment exception' do
expect { unmasked_body }.to raise_error(
FoiAttachment::MissingAttachment,
"attachment missing from storage (ID=#{foi_attachment.id})"
)
end
end
context 'when unable to find original attachment through other means' do
before do
allow(MailHandler).to receive(:attachment_body_for_hexdigest).
and_raise(MailHandler::MismatchedAttachmentHexdigest)
allow(MailHandler).to receive(
:attempt_to_find_original_attachment_attributes
).and_return(nil)
end
it 'raises missing attachment exception' do
expect { unmasked_body }.to raise_error(
FoiAttachment::MissingAttachment,
"attachment missing in raw email (ID=#{foi_attachment.id})"
)
end
end
end
describe 'masked?' do
let(:foi_attachment) do
FoiAttachment.new(body: 'foo', masked_at: Time.zone.now)
end
subject { foi_attachment.masked? }
it { is_expected.to eq(true) }
context 'without file attached' do
let(:foi_attachment) { FoiAttachment.new(masked_at: Time.zone.now) }
it { is_expected.to eq(false) }
end
context 'without masked_at' do
let(:foi_attachment) { FoiAttachment.new(body: 'foo') }
it { is_expected.to eq(false) }
end
context 'when masked_at is in the future' do
let(:foi_attachment) do
FoiAttachment.new(body: 'foo', masked_at: Time.zone.now + 1.day)
end
it { is_expected.to eq(false) }
end
end
describe '#main_body_part?' do
subject { attachment.main_body_part? }
let(:message) { FactoryBot.build(:incoming_message, :with_pdf_attachment) }
context 'when the attachment is the main body' do
let(:attachment) { message.get_main_body_text_part }
it { is_expected.to eq(true) }
end
context 'when the attachment is not the main body' do
let(:attachment) { message.get_attachments_for_display.first }
it { is_expected.to eq(false) }
end
end
describe '#filename=' do
it 'strips null bytes' do
attachment = FactoryBot.build(:pdf_attachment)
attachment.filename = "Tender Loving Care Trust (Europe).pdf\u0000"
expect(attachment.filename).to eq('Tender Loving Care Trust (Europe).pdf')
end
end
describe '#ensure_filename!' do
it 'should create a filename for an instance with a blank filename' do
attachment = FoiAttachment.new
attachment.filename = ''
attachment.ensure_filename!
expect(attachment.filename).to eq('attachment.bin')
end
end
describe '#has_body_as_html?' do
it 'should be true for a pdf attachment' do
expect(FactoryBot.build(:pdf_attachment).has_body_as_html?).to be true
end
it 'should be false for an html attachment' do
expect(FactoryBot.build(:html_attachment).has_body_as_html?).to be false
end
end
describe '#name_of_content_type' do
subject { foi_attachment.name_of_content_type }
before do
stub = { 'content/named' => 'Named content' }
stub_const("#{described_class}::CONTENT_TYPE_NAMES", stub)
end
let(:foi_attachment) do
FactoryBot.build(:foi_attachment, content_type: content_type)
end
context 'when the content_type has a name' do
let(:content_type) { 'content/named' }
it { is_expected.to eq('Named content') }
end
context 'when the content_type has no name' do
let(:content_type) { 'content/unnamed' }
it { is_expected.to be_nil }
end
end
describe '#extra_note' do
subject { foi_attachment.extra_note }
context 'with a delivery status notification' do
let(:foi_attachment) do
FactoryBot.create(:delivery_status_notification_attachment)
end
let(:note) do
'DSN: 4.4.0 Other or undefined network or routing status'
end
it { is_expected.to eq(note) }
end
context 'with any other content type' do
let(:foi_attachment) { FactoryBot.build(:rtf_attachment) }
it { is_expected.to be_nil }
end
end
describe '#expire' do
let(:incoming_message) { FactoryBot.create(:incoming_message) }
let(:foi_attachment) { incoming_message.foi_attachments.first }
it 'delegates to info_request' do
expect(foi_attachment.info_request).to receive(:expire)
foi_attachment.expire
end
end
describe '#log_event' do
let(:incoming_message) { FactoryBot.create(:incoming_message) }
let(:foi_attachment) { incoming_message.foi_attachments.first }
it 'delegates to info_request' do
expect(foi_attachment.info_request).to receive(:log_event).with('edit')
foi_attachment.log_event('edit')
end
end
describe '#update_and_log_event' do
let(:incoming_message) { FactoryBot.create(:incoming_message) }
let(:info_request) { incoming_message.info_request }
let(:foi_attachment) { incoming_message.foi_attachments.first }
def last_event
info_request.info_request_events.last
end
it 'updates and logs edit_attachment event' do
expect do
foi_attachment.update_and_log_event(prominence: 'hidden')
end.to change { last_event }
expect(last_event.event_type).to eq('edit_attachment')
end
it 'logs prominence and reason changes' do
foi_attachment.update_and_log_event(
prominence: 'hidden', prominence_reason: 'just because'
)
expect(last_event.params[:old_prominence]).to eq('normal')
expect(last_event.params[:prominence]).to eq('hidden')
expect(last_event.params[:old_prominence_reason]).to be_nil
expect(last_event.params[:prominence_reason]).to eq('just because')
end
it 'logs additional event data' do
foi_attachment.update_and_log_event(
prominence: 'hidden', event: { editor: 'me' }
)
expect(last_event.params[:editor]).to eq('me')
end
it 'does not log event if update fails' do
expect do
foi_attachment.update_and_log_event(prominence: nil)
end.to_not change { last_event }
end
end
end