@@ -564,6 +564,138 @@ describe('User Model', () => {
564564 const url = user . gravatar ( 200 ) ;
565565 expect ( url ) . to . include ( '00000000000000000000000000000000' ) ;
566566 } ) ;
567+
568+ it ( 'Scenario 1: Gravatar generation when email is present - after save' , async ( ) => {
569+ const user = new User ( {
570+ email : 'test@gmail.com' ,
571+ password : 'password123' ,
572+ } ) ;
573+
574+ await user . save ( ) ;
575+
576+ const sha256 = '87924606b4131a8aceeeae8868531fbb9712aaa07a5d3a756b26ce0f5d6ca674' ;
577+
578+ expect ( user . profile . pictures ) . to . be . instanceOf ( Map ) ;
579+ expect ( user . profile . pictures . get ( 'gravatar' ) ) . to . include ( sha256 ) ;
580+ expect ( user . profile . pictureSource ) . to . equal ( 'gravatar' ) ;
581+ expect ( user . profile . picture ) . to . include ( sha256 ) ;
582+ } ) ;
583+
584+ it ( 'Scenario 2: Gravatar update on email change' , async ( ) => {
585+ const user = new User ( {
586+ email : 'user1@example.com' ,
587+ password : 'password123' ,
588+ } ) ;
589+
590+ await user . save ( ) ;
591+
592+ const originalGravatar = user . profile . pictures . get ( 'gravatar' ) ;
593+ expect ( user . profile . picture ) . to . equal ( originalGravatar ) ;
594+
595+ // Change email
596+ user . email = 'user2@example.com' ;
597+ await user . save ( ) ;
598+
599+ const newGravatar = user . profile . pictures . get ( 'gravatar' ) ;
600+ expect ( newGravatar ) . to . not . equal ( originalGravatar ) ;
601+ expect ( user . profile . picture ) . to . equal ( newGravatar ) ;
602+ } ) ;
603+
604+ it ( 'Scenario 3: noMultiPictureUpgrade behavior' , ( ) => {
605+ const user = new User ( {
606+ email : 'test@example.com' ,
607+ password : 'password123' ,
608+ } ) ;
609+
610+ user . noMultiPictureUpgrade ( ) ;
611+
612+ expect ( user . profile . pictures ) . to . be . instanceOf ( Map ) ;
613+ expect ( user . profile . pictureSource ) . to . equal ( 'gravatar' ) ;
614+ expect ( user . profile . pictures . get ( 'gravatar' ) ) . to . include ( user . gravatar ( ) ) ;
615+ expect ( user . profile . picture ) . to . equal ( user . gravatar ( ) ) ;
616+ } ) ;
617+
618+ it ( 'Scenario 4: Preserve non-gravatar pictureSource' , async ( ) => {
619+ const user = new User ( {
620+ email : 'test@example.com' ,
621+ password : 'password123' ,
622+ profile : {
623+ pictureSource : 'facebook' ,
624+ picture : 'https://facebook/pic.jpg' ,
625+ } ,
626+ } ) ;
627+
628+ await user . save ( ) ;
629+
630+ expect ( user . profile . pictures . get ( 'gravatar' ) ) . to . include ( user . gravatar ( ) ) ;
631+ expect ( user . profile . picture ) . to . equal ( 'https://facebook/pic.jpg' ) ;
632+ expect ( user . profile . pictureSource ) . to . equal ( 'facebook' ) ;
633+ } ) ;
634+
635+ it ( 'Scenario 5: Preserve non-gravatar pictureSource - noMultiPictureUpgrade' , ( ) => {
636+ const user = new User ( {
637+ email : 'test@example.com' ,
638+ password : 'password123' ,
639+ profile : {
640+ pictureSource : 'github' ,
641+ picture : 'https://github/pic.jpg' ,
642+ } ,
643+ } ) ;
644+
645+ user . noMultiPictureUpgrade ( ) ;
646+
647+ expect ( user . profile . pictures . get ( 'gravatar' ) ) . to . include ( user . gravatar ( ) ) ;
648+ expect ( user . profile . picture ) . to . equal ( 'https://github/pic.jpg' ) ;
649+ expect ( user . profile . pictureSource ) . to . equal ( 'github' ) ;
650+ } ) ;
651+
652+ it ( 'Scenario 6: Legacy account upgrade path' , ( ) => {
653+ const user = new User ( {
654+ email : 'legacy@example.com' ,
655+ password : 'password123' ,
656+ profile : {
657+ picture : 'old-picture.jpg' ,
658+ // pictureSource and pictures are undefined
659+ } ,
660+ } ) ;
661+
662+ user . noMultiPictureUpgrade ( ) ;
663+
664+ expect ( user . profile . pictures ) . to . be . instanceOf ( Map ) ;
665+ expect ( user . profile . pictures . get ( 'gravatar' ) ) . to . include ( user . gravatar ( ) ) ;
666+ expect ( user . profile . pictureSource ) . to . equal ( 'gravatar' ) ;
667+ expect ( user . profile . picture ) . to . equal ( user . gravatar ( ) ) ;
668+ } ) ;
669+
670+ it ( 'Scenario 7: Map persistence' , async ( ) => {
671+ const user = new User ( {
672+ email : 'maptest@example.com' ,
673+ password : 'password123' ,
674+ } ) ;
675+
676+ await user . save ( ) ;
677+
678+ const reloaded = await User . findById ( user . _id ) ;
679+
680+ expect ( reloaded . profile . pictures ) . to . be . instanceOf ( Map ) ;
681+ expect ( reloaded . profile . pictures . get ( 'gravatar' ) ) . to . include ( user . gravatar ( ) ) ;
682+ } ) ;
683+
684+ it ( 'Scenario 8: No duplicate gravatar entries' , async ( ) => {
685+ const user = new User ( {
686+ email : 'noduplicate@example.com' ,
687+ password : 'password123' ,
688+ } ) ;
689+
690+ await user . save ( ) ;
691+ const initialSize = user . profile . pictures . size ;
692+
693+ // Save again without changing email
694+ await user . save ( ) ;
695+
696+ expect ( user . profile . pictures . size ) . to . equal ( initialSize ) ;
697+ expect ( user . profile . pictures . get ( 'gravatar' ) ) . to . include ( user . gravatar ( ) ) ;
698+ } ) ;
567699 } ) ;
568700
569701 describe ( 'Token Cleanup on Save' , ( ) => {
0 commit comments