@@ -50,26 +50,111 @@ smMock.on(GetSecretValueCommand).resolves({
5050  SecretString : secretJson , 
5151} ) ; 
5252
53- // Mock successful DynamoDB operations 
54- ddbMock . on ( PutItemCommand ) . resolves ( { } ) ; 
53+ const  testJwt  =  createJwt ( 
54+   undefined ,  // No specific date 
55+   undefined ,  // No specific group 
56+   "[email protected] " ,  // Test email  57+   [ "AppRoles.LINKS_MANAGER" ,  "AppRoles.LINKS_ADMIN" ] ,  // Add required roles 
58+ ) ; 
5559
56- // Mock ScanCommand to return empty Items array 
57- ddbMock . on ( ScanCommand ) . resolves ( { 
58-   Items : [ ] , 
59- } ) ; 
60+ test ( "Happy path: Fetch all linkry redirects with proper roles" ,  async  ( )  =>  { 
61+   // Create a test JWT with roles 
6062
61- ddbMock . on ( QueryCommand ) . resolves ( { 
62-   Items : [ ] , 
63- } ) ; 
63+   // Mock successful DynamoDB operations 
64+   ddbMock . on ( QueryCommand ) . resolves ( { 
65+     Items : [ ] ,  // Simulate no existing records 
66+   } ) ; 
6467
65- const  testJwt  =  createJwt ( undefined ,  "83c275f8-e533-4987-b537-a94b86c9d28e" ) ; 
68+   // Make the request to the /api/v1/linkry/redir endpoint 
69+   const  response  =  await  app . inject ( { 
70+     method : "GET" , 
71+     url : "/api/v1/linkry/redir" , 
72+     headers : { 
73+       Authorization : `Bearer ${ testJwt }  ` ,  // Include the JWT with roles 
74+     } , 
75+   } ) ; 
6676
67- const  allLinkryResponse  =  await  app . inject ( { 
68-   method : "GET" , 
69-   url : "/api/v1/linkry/redir" , 
70-   headers : { 
71-     Authorization : `Bearer ${ testJwt }  ` , 
72-   } , 
77+   // Assert the response status code 
78+   expect ( response . statusCode ) . toBe ( 200 ) ; 
79+   expect ( response . headers . etag ) . toBe ( "0" ) ; 
7380} ) ; 
7481
75- expect ( allLinkryResponse . statusCode ) . toBe ( 200 ) ; 
82+ //2. Create a new link using supertest 
83+ // const eventResponse = await supertest(app.server) 
84+ //   .post("/api/v1/linkry/redir/") 
85+ //   .set("Authorization", `Bearer ${testJwt}`) 
86+ //   .send({ 
87+ //     description: "Test event for ETag verification", 
88+ //     host: "Social Committee", 
89+ //     location: "Siebel Center", 
90+ //     start: "2024-09-25T18:00:00", 
91+ //     title: "ETag Test Event", 
92+ //     featured: false, 
93+ //   }); 
94+ 
95+ // expect(eventResponse.statusCode).toBe(201); 
96+ // const eventId = eventResponse.body.id; 
97+ 
98+ // test("Happy path: Create or update a linkry redirect", async () => { 
99+ //   // Mock successful DynamoDB operations 
100+ //   ddbMock.on(QueryCommand).resolves({ 
101+ //     Items: [], // Simulate no existing records for the slug 
102+ //   }); 
103+ 
104+ //   // Define the request payload 
105+ //   const payload = { 
106+ //     access: [], 
107+ //     counter: 0, 
108+ //     isEdited: true, 
109+ //     redirect: "https://www.rainbow.com", 
110+ //     slug: "bQjryt", 
111+ //   }; 
112+ 
113+ //   // Make the request to the /api/v1/linkry/redir/ endpoint 
114+ //   const response = await supertest(app.server) 
115+ //     .post("/api/v1/linkry/redir/") 
116+ //     .set("Authorization", `Bearer ${testJwt}`) // Add authorization header 
117+ //     .send(payload); // Send the payload 
118+ 
119+ //   // Assert the response status code 
120+ //   expect(response.statusCode).toBe(201); 
121+ 
122+ //   // Assert the response body (optional, based on your API's response structure) 
123+ //   expect(response.body).toStrictEqual({ 
124+ //     message: "Linkry redirect created or updated successfully", 
125+ //     slug: "bQjryt", 
126+ //   }); 
127+ // }); 
128+ 
129+ test ( "Happy path: Create a new linkry redirect" ,  async  ( )  =>  { 
130+   // Mock successful DynamoDB operations 
131+   ddbMock . on ( QueryCommand ) . resolves ( { 
132+     Items : [ ] ,  // Simulate no existing records for the slug 
133+   } ) ; 
134+ 
135+   ddbMock . on ( PutItemCommand ) . resolves ( { } ) ;  // Simulate successful insertion 
136+ 
137+   // Define the request payload 
138+   const  payload  =  { 
139+     access : [ ] , 
140+     counter : 0 , 
141+     isEdited : true , 
142+     redirect : "https://www.acm.illinois.edu/" , 
143+     slug : "acm-test-slug" , 
144+   } ; 
145+ 
146+   // Make the request to the /api/v1/linkry/redir/ endpoint 
147+   const  response  =  await  supertest ( app . server ) 
148+     . post ( "/api/v1/linkry/redir" ) 
149+     . set ( "Authorization" ,  `Bearer ${ testJwt }  ` )  // Include the JWT with roles 
150+     . send ( payload ) ;  // Send the payload 
151+ 
152+   // Assert the response status code 
153+   expect ( response . statusCode ) . toBe ( 201 ) ; 
154+ 
155+   // Assert the response body 
156+   expect ( response . body ) . toStrictEqual ( { 
157+     message : "New Shortened Link Created" , 
158+     id : "acm-test-slug" , 
159+   } ) ; 
160+ } ) ; 
0 commit comments