@@ -47,6 +47,7 @@ describe('Reset Password Route', () => {
4747 // Setup mock reply
4848 mockReply = {
4949 status : vi . fn ( ) . mockReturnThis ( ) ,
50+ type : vi . fn ( ) . mockReturnThis ( ) ,
5051 send : vi . fn ( ) . mockReturnThis ( ) ,
5152 } ;
5253
@@ -76,14 +77,15 @@ describe('Reset Password Route', () => {
7677 await handler ( mockRequest , mockReply ) ;
7778
7879 expect ( mockPasswordResetService . isPasswordResetAvailable ) . toHaveBeenCalled ( ) ;
79- expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' ) ;
80+ expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' , mockFastify . log ) ;
8081 expect ( mockFastify . log ! . info ) . toHaveBeenCalledWith ( 'Password reset attempt with token' ) ;
8182 expect ( mockFastify . log ! . info ) . toHaveBeenCalledWith ( 'Password reset successful for user: user-123' ) ;
8283 expect ( mockReply . status ) . toHaveBeenCalledWith ( 200 ) ;
83- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
84+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
85+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
8486 success : true ,
8587 message : 'Password has been reset successfully. All sessions have been invalidated for security. Please log in with your new password.' ,
86- } ) ;
88+ } ) ) ;
8789 } ) ;
8890
8991 it ( 'should return 503 when password reset is not available' , async ( ) => {
@@ -95,10 +97,11 @@ describe('Reset Password Route', () => {
9597 expect ( mockPasswordResetService . isPasswordResetAvailable ) . toHaveBeenCalled ( ) ;
9698 expect ( mockPasswordResetService . validateAndResetPassword ) . not . toHaveBeenCalled ( ) ;
9799 expect ( mockReply . status ) . toHaveBeenCalledWith ( 503 ) ;
98- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
100+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
101+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
99102 success : false ,
100103 error : 'Password reset is currently disabled. Email functionality is not enabled.' ,
101- } ) ;
104+ } ) ) ;
102105 } ) ;
103106
104107 it ( 'should return 400 for invalid or expired token' , async ( ) => {
@@ -110,12 +113,13 @@ describe('Reset Password Route', () => {
110113 const handler = routeHandlers [ 'POST /email/reset-password' ] ;
111114 await handler ( mockRequest , mockReply ) ;
112115
113- expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' ) ;
116+ expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' , mockFastify . log ) ;
114117 expect ( mockReply . status ) . toHaveBeenCalledWith ( 400 ) ;
115- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
118+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
119+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
116120 success : false ,
117121 error : 'Invalid or expired reset token' ,
118- } ) ;
122+ } ) ) ;
119123 } ) ;
120124
121125 it ( 'should return 403 for user not eligible for password reset' , async ( ) => {
@@ -127,12 +131,13 @@ describe('Reset Password Route', () => {
127131 const handler = routeHandlers [ 'POST /email/reset-password' ] ;
128132 await handler ( mockRequest , mockReply ) ;
129133
130- expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' ) ;
134+ expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' , mockFastify . log ) ;
131135 expect ( mockReply . status ) . toHaveBeenCalledWith ( 403 ) ;
132- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
136+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
137+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
133138 success : false ,
134139 error : 'This user is not eligible for password reset.' ,
135- } ) ;
140+ } ) ) ;
136141 } ) ;
137142
138143 it ( 'should return 500 for other service errors' , async ( ) => {
@@ -144,12 +149,13 @@ describe('Reset Password Route', () => {
144149 const handler = routeHandlers [ 'POST /email/reset-password' ] ;
145150 await handler ( mockRequest , mockReply ) ;
146151
147- expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' ) ;
152+ expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'valid-reset-token-123' , 'newPassword123!' , mockFastify . log ) ;
148153 expect ( mockReply . status ) . toHaveBeenCalledWith ( 500 ) ;
149- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
154+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
155+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
150156 success : false ,
151157 error : 'Database connection failed' ,
152- } ) ;
158+ } ) ) ;
153159 } ) ;
154160
155161 it ( 'should return 500 for service errors without error message' , async ( ) => {
@@ -161,10 +167,11 @@ describe('Reset Password Route', () => {
161167 await handler ( mockRequest , mockReply ) ;
162168
163169 expect ( mockReply . status ) . toHaveBeenCalledWith ( 500 ) ;
164- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
170+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
171+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
165172 success : false ,
166173 error : 'An error occurred during password reset.' ,
167- } ) ;
174+ } ) ) ;
168175 } ) ;
169176
170177 it ( 'should handle unexpected errors during password reset' , async ( ) => {
@@ -175,10 +182,11 @@ describe('Reset Password Route', () => {
175182
176183 expect ( mockFastify . log ! . error ) . toHaveBeenCalledWith ( expect . any ( Error ) , 'Error during password reset:' ) ;
177184 expect ( mockReply . status ) . toHaveBeenCalledWith ( 500 ) ;
178- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
185+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
186+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
179187 success : false ,
180188 error : 'An unexpected error occurred during password reset.' ,
181- } ) ;
189+ } ) ) ;
182190 } ) ;
183191
184192 it ( 'should handle validateAndResetPassword throwing an error' , async ( ) => {
@@ -189,10 +197,11 @@ describe('Reset Password Route', () => {
189197
190198 expect ( mockFastify . log ! . error ) . toHaveBeenCalledWith ( expect . any ( Error ) , 'Error during password reset:' ) ;
191199 expect ( mockReply . status ) . toHaveBeenCalledWith ( 500 ) ;
192- expect ( mockReply . send ) . toHaveBeenCalledWith ( {
200+ expect ( mockReply . type ) . toHaveBeenCalledWith ( 'application/json' ) ;
201+ expect ( mockReply . send ) . toHaveBeenCalledWith ( JSON . stringify ( {
193202 success : false ,
194203 error : 'An unexpected error occurred during password reset.' ,
195- } ) ;
204+ } ) ) ;
196205 } ) ;
197206
198207 it ( 'should handle different token formats' , async ( ) => {
@@ -204,7 +213,7 @@ describe('Reset Password Route', () => {
204213 const handler = routeHandlers [ 'POST /email/reset-password' ] ;
205214 await handler ( mockRequest , mockReply ) ;
206215
207- expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'different-token-format-456' , 'anotherPassword456!' ) ;
216+ expect ( mockPasswordResetService . validateAndResetPassword ) . toHaveBeenCalledWith ( 'different-token-format-456' , 'anotherPassword456!' , mockFastify . log ) ;
208217 expect ( mockReply . status ) . toHaveBeenCalledWith ( 200 ) ;
209218 } ) ;
210219
0 commit comments