@@ -694,4 +694,115 @@ describe('Templates API', () => {
694694 expect ( response . body . error ) . toBe ( 'Internal server error' )
695695 } )
696696 } )
697+
698+ describe ( 'GET /all-templates' , ( ) => {
699+ beforeEach ( async ( ) => {
700+ // Insert test templates for both user and public
701+ await db ( 'templates' ) . insert ( [
702+ {
703+ title : 'User Template 1' ,
704+ url : 'https://example.com/user1' ,
705+ json_data : '{"key": "user1"}' ,
706+ owner_address : testAccount . address ,
707+ moderated : true ,
708+ } ,
709+ {
710+ title : 'User Template 2' ,
711+ url : 'https://example.com/user2' ,
712+ json_data : '{"key": "user2"}' ,
713+ owner_address : testAccount . address ,
714+ moderated : false , // Non-moderated template from user
715+ } ,
716+ {
717+ title : 'Public Template 1' ,
718+ url : 'https://example.com/public1' ,
719+ json_data : '{"key": "public1"}' ,
720+ owner_address : otherAccount . address ,
721+ moderated : true ,
722+ } ,
723+ {
724+ title : 'Public Template 2' ,
725+ url : 'https://example.com/public2' ,
726+ json_data : '{"key": "public2"}' ,
727+ owner_address : otherAccount . address ,
728+ moderated : false , // Non-moderated template from other user
729+ } ,
730+ {
731+ title : 'Deleted Template' ,
732+ url : 'https://example.com/deleted' ,
733+ json_data : '{"key": "deleted"}' ,
734+ owner_address : otherAccount . address ,
735+ moderated : true ,
736+ deleted_at : db . fn . now ( ) , // Deleted template
737+ } ,
738+ ] )
739+
740+ // Recreate expressApp to ensure the router is fresh
741+ expressApp = express ( )
742+ expressApp . use ( express . json ( ) )
743+ expressApp . use ( '/api/templates' , createTemplatesRouter ( testDb . getDb ( ) , new MockNotificationService ( ) ) )
744+ } )
745+
746+ it ( 'should return both user templates and public templates' , async ( ) => {
747+ const response = await request ( expressApp )
748+ . get ( '/api/templates/all-templates' )
749+ . set ( 'x-wallet-address' , testAccount . address )
750+ . query ( { address : testAccount . address } )
751+
752+ expect ( response . status ) . toBe ( 200 )
753+
754+ // Check response structure
755+ expect ( response . body ) . toHaveProperty ( 'userTemplates' )
756+ expect ( response . body ) . toHaveProperty ( 'publicTemplates' )
757+
758+ const { userTemplates, publicTemplates } = response . body
759+
760+ // User templates should include both moderated and non-moderated templates owned by the user
761+ expect ( userTemplates ) . toHaveLength ( 2 )
762+ const userTemplateTitles = userTemplates . map ( ( template : DbTemplate ) => template . title )
763+ expect ( userTemplateTitles ) . toContain ( 'User Template 1' )
764+ expect ( userTemplateTitles ) . toContain ( 'User Template 2' )
765+
766+ // Public templates should only include moderated templates not owned by the user
767+ expect ( publicTemplates ) . toHaveLength ( 1 )
768+ const publicTemplateTitles = publicTemplates . map ( ( template : DbTemplate ) => template . title )
769+ expect ( publicTemplateTitles ) . toContain ( 'Public Template 1' )
770+ expect ( publicTemplateTitles ) . not . toContain ( 'Public Template 2' ) // Non-moderated
771+ expect ( publicTemplateTitles ) . not . toContain ( 'Deleted Template' ) // Deleted
772+ } , 30000 )
773+
774+ it ( 'should fail without address parameter' , async ( ) => {
775+ const response = await request ( expressApp )
776+ . get ( '/api/templates/all-templates' )
777+ . set ( 'x-wallet-address' , testAccount . address )
778+
779+ expect ( response . status ) . toBe ( 400 )
780+ expect ( response . body . error ) . toBe ( 'Address parameter is required' )
781+ } , 30000 )
782+
783+ it ( 'should handle errors gracefully' , async ( ) => {
784+ // Silence console.error during this test
785+ console . error = jest . fn ( )
786+
787+ // Create a special app just for this test
788+ const errorApp = express ( )
789+ errorApp . use ( express . json ( ) )
790+
791+ // Create a simplified router with an error-throwing handler
792+ const errorRouter = Router ( )
793+ errorRouter . get ( '/templates/all-templates' , ( req , res ) => {
794+ res . status ( 500 ) . json ( { error : 'Internal server error' } )
795+ } )
796+
797+ errorApp . use ( '/api' , errorRouter )
798+
799+ const response = await request ( errorApp )
800+ . get ( '/api/templates/all-templates' )
801+ . set ( 'x-wallet-address' , testAccount . address )
802+ . query ( { address : testAccount . address } )
803+
804+ expect ( response . status ) . toBe ( 500 )
805+ expect ( response . body . error ) . toBe ( 'Internal server error' )
806+ } )
807+ } )
697808} )
0 commit comments