@@ -2,6 +2,7 @@ import { GroupRole } from "@generated/client";
22import {
33 BadRequestException ,
44 ForbiddenException ,
5+ GoneException ,
56 NotFoundException ,
67} from "@nestjs/common" ;
78import { mockAppLogger } from "@/testUtils/mockAppLogger" ;
@@ -540,6 +541,111 @@ describe("DocumentController", () => {
540541 controller . downloadDocument ( "1" , res , mockReq as any ) ,
541542 ) . rejects . toThrow ( NotFoundException ) ;
542543 } ) ;
544+
545+ it ( "should throw GoneException when the document has been purged" , async ( ) => {
546+ documentService . findDocument . mockResolvedValue ( {
547+ id : "1" ,
548+ file_path : "cuid/ocr/file.pdf" ,
549+ original_filename : "file.pdf" ,
550+ group_id : mockGroupId ,
551+ purged_at : new Date ( ) ,
552+ } as any ) ;
553+ const res : any = { } ;
554+ await expect (
555+ controller . downloadDocument ( "1" , res , mockReq as any ) ,
556+ ) . rejects . toThrow ( GoneException ) ;
557+ expect ( blobStorage . read ) . not . toHaveBeenCalled ( ) ;
558+ } ) ;
559+ } ) ;
560+
561+ describe ( "viewDocument" , ( ) => {
562+ const mockReq = createMockReq ( ) ;
563+
564+ it ( "should stream the normalized PDF when present" , async ( ) => {
565+ documentService . findDocument . mockResolvedValue ( {
566+ id : "1" ,
567+ normalized_file_path : "cuid/ocr/normalized.pdf" ,
568+ group_id : mockGroupId ,
569+ purged_at : null ,
570+ } as any ) ;
571+ blobStorage . read . mockResolvedValue ( Buffer . from ( "pdf" ) ) ;
572+ const res : any = { setHeader : jest . fn ( ) , send : jest . fn ( ) } ;
573+ await controller . viewDocument ( "1" , res , mockReq as any ) ;
574+ expect ( res . setHeader ) . toHaveBeenCalledWith (
575+ "Content-Type" ,
576+ "application/pdf" ,
577+ ) ;
578+ expect ( res . send ) . toHaveBeenCalledWith ( Buffer . from ( "pdf" ) ) ;
579+ } ) ;
580+
581+ it ( "should throw GoneException when the document has been purged" , async ( ) => {
582+ documentService . findDocument . mockResolvedValue ( {
583+ id : "1" ,
584+ normalized_file_path : "cuid/ocr/normalized.pdf" ,
585+ group_id : mockGroupId ,
586+ purged_at : new Date ( ) ,
587+ } as any ) ;
588+ const res : any = { setHeader : jest . fn ( ) , send : jest . fn ( ) } ;
589+ await expect (
590+ controller . viewDocument ( "1" , res , mockReq as any ) ,
591+ ) . rejects . toThrow ( GoneException ) ;
592+ expect ( blobStorage . read ) . not . toHaveBeenCalled ( ) ;
593+ expect ( mockAuditService . recordEvent ) . not . toHaveBeenCalled ( ) ;
594+ } ) ;
595+
596+ it ( "should throw NotFoundException when normalized PDF path is missing" , async ( ) => {
597+ documentService . findDocument . mockResolvedValue ( {
598+ id : "1" ,
599+ normalized_file_path : null ,
600+ group_id : mockGroupId ,
601+ purged_at : null ,
602+ } as any ) ;
603+ const res : any = { setHeader : jest . fn ( ) , send : jest . fn ( ) } ;
604+ await expect (
605+ controller . viewDocument ( "1" , res , mockReq as any ) ,
606+ ) . rejects . toThrow ( NotFoundException ) ;
607+ } ) ;
608+
609+ it ( "should throw NotFoundException (not 500) when the blob read fails" , async ( ) => {
610+ documentService . findDocument . mockResolvedValue ( {
611+ id : "1" ,
612+ normalized_file_path : "cuid/ocr/normalized.pdf" ,
613+ group_id : mockGroupId ,
614+ purged_at : null ,
615+ } as any ) ;
616+ blobStorage . read . mockRejectedValue ( new Error ( "NoSuchKey" ) ) ;
617+ const res : any = { setHeader : jest . fn ( ) , send : jest . fn ( ) } ;
618+ await expect (
619+ controller . viewDocument ( "1" , res , mockReq as any ) ,
620+ ) . rejects . toThrow ( NotFoundException ) ;
621+ } ) ;
622+
623+ it ( "should throw ForbiddenException if user is not a group member" , async ( ) => {
624+ const notMemberReq = {
625+ resolvedIdentity : {
626+ userId : "user-1" ,
627+ isSystemAdmin : false ,
628+ groupRoles : { } ,
629+ } ,
630+ } ;
631+ documentService . findDocument . mockResolvedValue ( {
632+ id : "1" ,
633+ normalized_file_path : "cuid/ocr/normalized.pdf" ,
634+ group_id : mockGroupId ,
635+ } as any ) ;
636+ const res : any = { setHeader : jest . fn ( ) , send : jest . fn ( ) } ;
637+ await expect (
638+ controller . viewDocument ( "1" , res , notMemberReq as any ) ,
639+ ) . rejects . toThrow ( ForbiddenException ) ;
640+ } ) ;
641+
642+ it ( "should throw NotFoundException if document not found" , async ( ) => {
643+ documentService . findDocument . mockResolvedValue ( null ) ;
644+ const res : any = { setHeader : jest . fn ( ) , send : jest . fn ( ) } ;
645+ await expect (
646+ controller . viewDocument ( "1" , res , mockReq as any ) ,
647+ ) . rejects . toThrow ( NotFoundException ) ;
648+ } ) ;
543649 } ) ;
544650
545651 describe ( "getDocument" , ( ) => {
0 commit comments