import pytest from fastapi import HTTPException from app.modules.sag.backend import file_browser as files def test_txt_search_content(): assert files.inspect_file('test.TXT','Router opsætning'.encode())=={'pages':1,'text':'Router opsætning'} def test_other_types_are_not_parsed(): assert files.inspect_file('setup.exe',b'abc')=={'pages':1,'text':''} def test_mail_attachment_must_belong_to_case(monkeypatch): monkeypatch.setattr(files,'catalog',lambda _: [{'id':4,'source':'case'}]) with pytest.raises(HTTPException) as e:files.read_file(186,'mail',4) assert e.value.status_code==404 def test_mail_database_bytes_and_download(monkeypatch): row={'id':4,'source':'mail','filename':'ark.txt','content_data':memoryview(b'test'),'file_path':None} monkeypatch.setattr(files,'catalog',lambda _:[row]) assert files.text(186,'mail',4)=={'pages':1,'text':'test'} response=files.download(186,'mail',4) assert response.body==b'test' and response.headers['x-content-type-options']=='nosniff' def test_pdf_text_and_page_count(): import pypdfium2 as pdfium from io import BytesIO with pdfium.PdfDocument.new() as pdf: pdf.new_page(200,200);out=BytesIO();pdf.save(out) assert files.inspect_file('blank.pdf',out.getvalue())=={'pages':1,'text':''} def test_oversize_content_rejected(monkeypatch): monkeypatch.setattr(files,'MAX_BYTES',2) monkeypatch.setattr(files,'catalog',lambda _:[{'id':1,'source':'mail','filename':'x','content_data':b'abc','file_path':None}]) with pytest.raises(HTTPException) as e:files.read_file(186,'mail',1) assert e.value.status_code==413 def test_jpeg_preview_and_thumbnail(monkeypatch): from PIL import Image from io import BytesIO source=BytesIO();Image.new('RGB',(800,600),'red').save(source,format='JPEG') row={'id':9,'source':'mail','filename':'photo.jpeg','content_data':source.getvalue(),'file_path':None} monkeypatch.setattr(files,'catalog',lambda _:[row]) preview=files.image(186,'mail',9) assert preview.media_type=='image/png' assert Image.open(BytesIO(preview.body)).size==(800,600) small=files.thumbnail(186,'mail',9) width,height=Image.open(BytesIO(small.body)).size assert width<=220 and height<=160 @pytest.mark.parametrize('filename',['notes.txt','archive.zip','report.docx','broken.jpg']) def test_all_files_have_thumbnail_fallback(monkeypatch,filename): row={'id':9,'source':'mail','filename':filename,'content_data':b'not an image','file_path':None} monkeypatch.setattr(files,'catalog',lambda _:[row]) response=files.thumbnail(186,'mail',9) assert response.media_type=='image/svg+xml' and b'