@@ -299,106 +299,107 @@ func TestCategoryHandler_UpdateCategory(t *testing.T) {
299299 categoryToUpdateID := uuid .New ()
300300 testJwtSecret := "test-secret-for-jwt-please-change"
301301 testToken := generateTestToken (testUserID , testJwtSecret )
302+ userForToken := & models.User {ID : testUserID } // Define user for token once
302303
303304 tests := []struct {
304- name string
305- categoryID string
306- body string
307- mockUserReturn * models.User
308- mockUserErr error
309- mockUpdateErr error
310- expectedStatus int
311- expectedBody string
305+ name string
306+ categoryID string
307+ body string
308+ mockUserReturnMid * models.User // Renamed for clarity
309+ mockUserErrMid error // Renamed for clarity
310+ mockUpdateErr error
311+ expectedStatus int
312+ expectedBody string
312313 }{
313314 {
314- name : "Success" ,
315- categoryID : categoryToUpdateID .String (),
316- body : `{"name":"Updated Category Name"}` ,
317- mockUserReturn : & models. User { ID : testUserID } ,
318- mockUserErr : nil ,
319- mockUpdateErr : nil ,
320- expectedStatus : http .StatusOK ,
321- expectedBody : "Updated Category Name" ,
315+ name : "Success" ,
316+ categoryID : categoryToUpdateID .String (),
317+ body : `{"name":"Updated Category Name"}` ,
318+ mockUserReturnMid : userForToken ,
319+ mockUserErrMid : nil ,
320+ mockUpdateErr : nil ,
321+ expectedStatus : http .StatusOK ,
322+ expectedBody : "Updated Category Name" ,
322323 },
323324 {
324- name : "Failure - Invalid UUID" ,
325- categoryID : "not-a-uuid" ,
326- body : `{"name":"Update Attempt"}` ,
327- mockUserReturn : & models. User { ID : testUserID } ,
328- mockUserErr : nil ,
329- mockUpdateErr : nil ,
330- expectedStatus : http .StatusNotFound , // <<< CORRECTION: Expect 404
331- expectedBody : "404 page not found" , // <<< CORRECTION: Expect router's 404 message
325+ name : "Failure - Invalid UUID" ,
326+ categoryID : "not-a-uuid" ,
327+ body : `{"name":"Update Attempt"}` ,
328+ mockUserReturnMid : userForToken ,
329+ mockUserErrMid : nil ,
330+ mockUpdateErr : nil ,
331+ expectedStatus : http .StatusNotFound ,
332+ expectedBody : "404 page not found" ,
332333 },
333334 {
334- name : "Failure - Invalid JSON" ,
335- categoryID : categoryToUpdateID .String (),
336- body : `{"name":}` , // Invalid JSON
337- mockUserReturn : & models. User { ID : testUserID } ,
338- mockUserErr : nil ,
339- mockUpdateErr : nil ,
340- expectedStatus : http .StatusBadRequest ,
341- expectedBody : `{"error":"invalid request body"}` ,
335+ name : "Failure - Invalid JSON" ,
336+ categoryID : categoryToUpdateID .String (),
337+ body : `{"name":}` ,
338+ mockUserReturnMid : userForToken ,
339+ mockUserErrMid : nil ,
340+ mockUpdateErr : nil ,
341+ expectedStatus : http .StatusBadRequest ,
342+ expectedBody : `{"error":"invalid request body"}` ,
342343 },
343344 {
344- name : "Failure - Missing Name" ,
345- categoryID : categoryToUpdateID .String (),
346- body : `{}` , // Empty body
347- mockUserReturn : & models. User { ID : testUserID } ,
348- mockUserErr : nil ,
349- mockUpdateErr : nil ,
350- expectedStatus : http .StatusBadRequest ,
351- expectedBody : `{"error":"category name is required"}` ,
345+ name : "Failure - Missing Name" ,
346+ categoryID : categoryToUpdateID .String (),
347+ body : `{}` ,
348+ mockUserReturnMid : userForToken ,
349+ mockUserErrMid : nil ,
350+ mockUpdateErr : nil ,
351+ expectedStatus : http .StatusBadRequest ,
352+ expectedBody : `{"error":"category name is required"}` ,
352353 },
353354 {
354- name : "Failure - Category Not Found" ,
355- categoryID : categoryToUpdateID .String (),
356- body : `{"name":"Update Attempt"}` ,
357- mockUserReturn : & models. User { ID : testUserID } ,
358- mockUserErr : nil ,
359- mockUpdateErr : categories .ErrCategoryNotFound ,
360- expectedStatus : http .StatusNotFound ,
361- expectedBody : `{"error":"category not found"}` ,
355+ name : "Failure - Category Not Found" ,
356+ categoryID : categoryToUpdateID .String (),
357+ body : `{"name":"Update Attempt"}` ,
358+ mockUserReturnMid : userForToken ,
359+ mockUserErrMid : nil ,
360+ mockUpdateErr : categories .ErrCategoryNotFound ,
361+ expectedStatus : http .StatusNotFound ,
362+ expectedBody : `{"error":"category not found"}` ,
362363 },
363364 {
364- name : "Failure - Name Already Exists" ,
365- categoryID : categoryToUpdateID .String (),
366- body : `{"name":"Existing Name"}` ,
367- mockUserReturn : & models. User { ID : testUserID } ,
368- mockUserErr : nil ,
369- mockUpdateErr : categories .ErrCategoryNameExists ,
370- expectedStatus : http .StatusConflict ,
371- expectedBody : `{"error":"category name already exists"}` ,
365+ name : "Failure - Name Already Exists" ,
366+ categoryID : categoryToUpdateID .String (),
367+ body : `{"name":"Existing Name"}` ,
368+ mockUserReturnMid : userForToken ,
369+ mockUserErrMid : nil ,
370+ mockUpdateErr : categories .ErrCategoryNameExists ,
371+ expectedStatus : http .StatusConflict ,
372+ expectedBody : `{"error":"category name already exists"}` ,
372373 },
373374 {
374- name : "Failure - Repo Update Error" ,
375- categoryID : categoryToUpdateID .String (),
376- body : `{"name":"Update Attempt"}` ,
377- mockUserReturn : & models. User { ID : testUserID } ,
378- mockUserErr : nil ,
379- mockUpdateErr : errors .New ("db update failed" ),
380- expectedStatus : http .StatusInternalServerError ,
381- expectedBody : `{"error":"failed to update category"}` ,
375+ name : "Failure - Repo Update Error" ,
376+ categoryID : categoryToUpdateID .String (),
377+ body : `{"name":"Update Attempt"}` ,
378+ mockUserReturnMid : userForToken ,
379+ mockUserErrMid : nil ,
380+ mockUpdateErr : errors .New ("db update failed" ),
381+ expectedStatus : http .StatusInternalServerError ,
382+ expectedBody : `{"error":"failed to update category"}` ,
382383 },
383384 {
384- name : "Failure - Middleware User Check Fails" ,
385- categoryID : categoryToUpdateID .String (),
386- body : `{"name":"Update Attempt"}` ,
387- mockUserReturn : nil ,
388- mockUserErr : users .ErrUserNotFound ,
389- mockUpdateErr : nil ,
390- expectedStatus : http .StatusUnauthorized ,
391- expectedBody : `{"error":"user associated with token not found"}` ,
385+ name : "Failure - Middleware User Check Fails" ,
386+ categoryID : categoryToUpdateID .String (),
387+ body : `{"name":"Update Attempt"}` ,
388+ mockUserReturnMid : nil ,
389+ mockUserErrMid : users .ErrUserNotFound ,
390+ mockUpdateErr : nil ,
391+ expectedStatus : http .StatusUnauthorized ,
392+ expectedBody : `{"error":"user associated with token not found"}` ,
392393 },
393394 {
394- name : "Failure - No Auth Token" ,
395- categoryID : categoryToUpdateID .String (),
396- body : `{"name":"Update Attempt"}` ,
397- mockUserReturn : nil ,
398- mockUserErr : nil ,
399- mockUpdateErr : nil ,
400- expectedStatus : http .StatusUnauthorized ,
401- expectedBody : `{"error":"authorization header required"}` , // <<< CORRECTION: Actual middleware message
395+ name : "Failure - No Auth Token" ,
396+ categoryID : categoryToUpdateID .String (),
397+ body : `{"name":"Update Attempt"}` ,
398+ mockUserReturnMid : nil ,
399+ mockUserErrMid : nil ,
400+ mockUpdateErr : nil ,
401+ expectedStatus : http .StatusUnauthorized ,
402+ expectedBody : `{"error":"authorization header required"}` , // <<< CORRECTION: Actual middleware message
402403 },
403404 }
404405
@@ -410,11 +411,11 @@ func TestCategoryHandler_UpdateCategory(t *testing.T) {
410411
411412 // Mock middleware user check only if the UUID is valid AND we are not testing the "No Auth Token" case directly
412413 if tc .categoryID != "not-a-uuid" && tc .expectedBody != `{"error":"authorization header required"}` {
413- mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturn , tc .mockUserErr ).Once ()
414+ mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturnMid , tc .mockUserErrMid ).Once ()
414415 }
415416
416417 // Mock category repo update (only if middleware/validation/parsing passes)
417- if tc .categoryID != "not-a-uuid" && tc .mockUserErr == nil && tc .expectedStatus != http .StatusBadRequest && tc .expectedStatus != http .StatusUnauthorized {
418+ if tc .categoryID != "not-a-uuid" && tc .mockUserErrMid == nil && tc .expectedStatus != http .StatusBadRequest && tc .expectedStatus != http .StatusUnauthorized {
418419 parsedID , _ := uuid .Parse (tc .categoryID )
419420 mockCategoryRepo .On ("Update" , mock .Anything , parsedID , mock .AnythingOfType ("*models.Category" )).
420421 Return (func (ctx context.Context , id uuid.UUID , c * models.Category ) * models.Category {
@@ -449,69 +450,70 @@ func TestCategoryHandler_DeleteCategory(t *testing.T) {
449450 categoryToDeleteID := uuid .New ()
450451 testJwtSecret := "test-secret-for-jwt-please-change"
451452 testToken := generateTestToken (testUserID , testJwtSecret )
453+ userForToken := & models.User {ID : testUserID } // Define user for token once
452454
453455 tests := []struct {
454- name string
455- categoryID string
456- mockUserReturn * models.User
457- mockUserErr error
458- mockDeleteErr error
459- expectedStatus int
460- expectedBody string // Usually empty for No Content, but check error messages
456+ name string
457+ categoryID string
458+ mockUserReturnMid * models.User // Renamed
459+ mockUserErrMid error // Renamed
460+ mockDeleteErr error
461+ expectedStatus int
462+ expectedBody string
461463 }{
462464 {
463- name : "Success" ,
464- categoryID : categoryToDeleteID .String (),
465- mockUserReturn : & models. User { ID : testUserID },
466- mockUserErr : nil ,
467- mockDeleteErr : nil ,
468- expectedStatus : http .StatusNoContent ,
469- expectedBody : "" , // No body on success
465+ name : "Success" ,
466+ categoryID : categoryToDeleteID .String (),
467+ mockUserReturnMid : userForToken , // Corrected
468+ mockUserErrMid : nil , // Corrected
469+ mockDeleteErr : nil ,
470+ expectedStatus : http .StatusNoContent ,
471+ expectedBody : "" ,
470472 },
471473 {
472- name : "Failure - Invalid UUID" ,
473- categoryID : "not-a-uuid" ,
474- mockUserReturn : & models. User { ID : testUserID },
475- mockUserErr : nil ,
476- mockDeleteErr : nil , // Delete won't be called
477- expectedStatus : http .StatusNotFound , // <<< CORRECTION: Expect 404
478- expectedBody : "404 page not found" , // <<< CORRECTION: Expect router's 404 message
474+ name : "Failure - Invalid UUID" ,
475+ categoryID : "not-a-uuid" ,
476+ mockUserReturnMid : userForToken , // Corrected
477+ mockUserErrMid : nil , // Corrected
478+ mockDeleteErr : nil ,
479+ expectedStatus : http .StatusNotFound ,
480+ expectedBody : "404 page not found" ,
479481 },
480482 {
481- name : "Failure - Category Not Found" ,
482- categoryID : categoryToDeleteID .String (),
483- mockUserReturn : & models. User { ID : testUserID },
484- mockUserErr : nil ,
485- mockDeleteErr : categories .ErrCategoryNotFound ,
486- expectedStatus : http .StatusNotFound ,
487- expectedBody : `{"error":"category not found"}` ,
483+ name : "Failure - Category Not Found" ,
484+ categoryID : categoryToDeleteID .String (),
485+ mockUserReturnMid : userForToken , // Corrected
486+ mockUserErrMid : nil , // Corrected
487+ mockDeleteErr : categories .ErrCategoryNotFound ,
488+ expectedStatus : http .StatusNotFound ,
489+ expectedBody : `{"error":"category not found"}` ,
488490 },
489491 {
490- name : "Failure - Repo Delete Error" ,
491- categoryID : categoryToDeleteID .String (),
492- mockUserReturn : & models. User { ID : testUserID },
493- mockUserErr : nil ,
494- mockDeleteErr : errors .New ("db delete failed" ),
495- expectedStatus : http .StatusInternalServerError ,
496- expectedBody : `{"error":"failed to delete category"}` ,
492+ name : "Failure - Repo Delete Error" ,
493+ categoryID : categoryToDeleteID .String (),
494+ mockUserReturnMid : userForToken , // Corrected
495+ mockUserErrMid : nil , // Corrected
496+ mockDeleteErr : errors .New ("db delete failed" ),
497+ expectedStatus : http .StatusInternalServerError ,
498+ expectedBody : `{"error":"failed to delete category"}` ,
497499 },
498500 {
499- name : "Failure - Middleware User Check Fails" ,
500- categoryID : categoryToDeleteID .String (),
501- mockUserReturn : nil ,
502- mockUserErr : users .ErrUserNotFound ,
503- mockDeleteErr : nil ,
504- expectedStatus : http .StatusUnauthorized ,
505- expectedBody : `{"error":"user associated with token not found"}` ,
501+ name : "Failure - Middleware User Check Fails" ,
502+ categoryID : categoryToDeleteID .String (),
503+ mockUserReturnMid : nil , // Corrected
504+ mockUserErrMid : users .ErrUserNotFound , // Corrected
505+ mockDeleteErr : nil ,
506+ expectedStatus : http .StatusUnauthorized ,
507+ expectedBody : `{"error":"user associated with token not found"}` ,
506508 },
507509 {
508- name : "Failure - No Auth Token" ,
509- categoryID : categoryToDeleteID .String (),
510- mockUserReturn : nil ,
511- mockUserErr : nil ,
512- mockDeleteErr : nil ,
513- expectedStatus : http .StatusUnauthorized ,
514- expectedBody : `{"error":"authorization header required"}` , // <<< CORRECTION: Actual middleware message
510+ name : "Failure - No Auth Token" ,
511+ categoryID : categoryToDeleteID .String (),
512+ mockUserReturnMid : nil , // Corrected
513+ mockUserErrMid : nil , // Corrected
514+ mockDeleteErr : nil ,
515+ expectedStatus : http .StatusUnauthorized ,
516+ expectedBody : `{"error":"authorization header required"}` ,
515517 },
516518 }
517519
@@ -523,11 +525,11 @@ func TestCategoryHandler_DeleteCategory(t *testing.T) {
523525
524526 // Mock middleware user check only if the UUID is valid AND we are not testing the "No Auth Token" case directly
525527 if tc .categoryID != "not-a-uuid" && tc .expectedBody != `{"error":"authorization header required"}` {
526- mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturn , tc .mockUserErr ).Once ()
528+ mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturnMid , tc .mockUserErrMid ).Once () // Corrected
527529 }
528530
529531 // Mock category repo delete (only if middleware/parsing passes)
530- if tc .categoryID != "not-a-uuid" && tc .mockUserErr == nil && tc .expectedStatus != http .StatusBadRequest && tc .expectedStatus != http .StatusUnauthorized {
532+ if tc .categoryID != "not-a-uuid" && tc .mockUserErrMid == nil && tc .expectedStatus != http .StatusBadRequest && tc .expectedStatus != http .StatusUnauthorized { // Corrected
531533 parsedID , _ := uuid .Parse (tc .categoryID )
532534 mockCategoryRepo .On ("Delete" , mock .Anything , parsedID ).Return (tc .mockDeleteErr ).Once ()
533535 }
0 commit comments