@@ -593,6 +593,7 @@ describe("GET /share-links", () => {
593
593
} ) ;
594
594
595
595
afterEach ( async ( ) => {
596
+ jest . restoreAllMocks ( ) ;
596
597
await clearDatabase ( ) ;
597
598
} ) ;
598
599
@@ -697,3 +698,94 @@ describe("GET /share-links", () => {
697
698
await agent . get ( "/api/v2/share-links?shareLinkIds[]=1000" ) . expect ( 500 ) ;
698
699
} ) ;
699
700
} ) ;
701
+
702
+ describe ( "DELETE /share-links" , ( ) => {
703
+ const agent = request ( app ) ;
704
+
705
+ beforeEach ( async ( ) => {
706
+ ( verifyUserAuthentication as jest . Mock ) . mockImplementation (
707
+ ( req : Request , _ , next : NextFunction ) => {
708
+ (
709
+ req . body as {
710
+ emailFromAuthToken : string ;
711
+ userSubjectFromAuthToken : string ;
712
+ }
713
+ ) . emailFromAuthToken = "[email protected] " ;
714
+ (
715
+ req . body as {
716
+ emailFromAuthToken : string ;
717
+ userSubjectFromAuthToken : string ;
718
+ }
719
+ ) . userSubjectFromAuthToken = "ceca5477-3f9c-4d0a-a7b8-04d5e5adac32" ;
720
+ next ( ) ;
721
+ }
722
+ ) ;
723
+
724
+ await loadFixtures ( ) ;
725
+ } ) ;
726
+
727
+ afterEach ( async ( ) => {
728
+ await clearDatabase ( ) ;
729
+ } ) ;
730
+
731
+ test ( "should return 204 for a valid request" , async ( ) => {
732
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 204 ) ;
733
+ } ) ;
734
+
735
+ test ( "should return 401 if the caller is unauthenticated" , async ( ) => {
736
+ ( verifyUserAuthentication as jest . Mock ) . mockImplementation (
737
+ ( __ : Request , _ , next : NextFunction ) => {
738
+ next ( new createError . Unauthorized ( "Invalid token" ) ) ;
739
+ }
740
+ ) ;
741
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 401 ) ;
742
+ } ) ;
743
+
744
+ test ( "should return 400 if header values are missing" , async ( ) => {
745
+ ( verifyUserAuthentication as jest . Mock ) . mockImplementation (
746
+ ( __ : Request , _ , next : NextFunction ) => {
747
+ next ( ) ;
748
+ }
749
+ ) ;
750
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 400 ) ;
751
+ } ) ;
752
+
753
+ test ( "should delete the share link" , async ( ) => {
754
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 204 ) ;
755
+ const shareLinks = await agent
756
+ . get ( "/api/v2/share-links?shareLinkIds[]=1000" )
757
+ . expect ( 200 ) ;
758
+ expect ( ( shareLinks . body as { items : ShareLink [ ] } ) . items . length ) . toEqual ( 0 ) ;
759
+ } ) ;
760
+
761
+ test ( "should return 404 if the share link doesn't exist" , async ( ) => {
762
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 204 ) ;
763
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 404 ) ;
764
+ } ) ;
765
+
766
+ test ( "should return 404 if the caller doesn't have access to the share link" , async ( ) => {
767
+ ( verifyUserAuthentication as jest . Mock ) . mockImplementation (
768
+ ( req : Request , _ , next : NextFunction ) => {
769
+ (
770
+ req . body as {
771
+ emailFromAuthToken : string ;
772
+ userSubjectFromAuthToken : string ;
773
+ }
774
+ ) . emailFromAuthToken = "[email protected] " ;
775
+ (
776
+ req . body as {
777
+ emailFromAuthToken : string ;
778
+ userSubjectFromAuthToken : string ;
779
+ }
780
+ ) . userSubjectFromAuthToken = "ceca5477-3f9c-4d0a-a7b8-04d5e5adac32" ;
781
+ next ( ) ;
782
+ }
783
+ ) ;
784
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 404 ) ;
785
+ } ) ;
786
+
787
+ test ( "should return 500 if the database call fails" , async ( ) => {
788
+ jest . spyOn ( db , "sql" ) . mockRejectedValue ( new Error ( "Test error" ) ) ;
789
+ await agent . delete ( "/api/v2/share-links/1000" ) . expect ( 500 ) ;
790
+ } ) ;
791
+ } ) ;
0 commit comments