@@ -597,6 +597,105 @@ def test_get_name_validation_error_too_long(self):
597597 result = get_name_validation_error ("A" * 256 )
598598 assert result == "Full name can't be longer than 255 symbols"
599599
600+ def test_update_extended_profile_with_meta_only (self ):
601+ """
602+ Test updating extended profile using only the meta field (legacy behavior)
603+ """
604+ update_data = {
605+ "extended_profile" : [
606+ {"field_name" : "department" , "field_value" : "Engineering" },
607+ {"field_name" : "title" , "field_value" : "Software Engineer" },
608+ ],
609+ "bio" : "Updated bio" ,
610+ }
611+
612+ update_account_settings (self .user , update_data )
613+
614+ user_profile = UserProfile .objects .get (user = self .user )
615+ meta = user_profile .get_meta ()
616+ self .assertEqual (meta ["department" ], "Engineering" )
617+ self .assertEqual (meta ["title" ], "Software Engineer" )
618+ self .assertEqual (user_profile .bio , "Updated bio" )
619+
620+ def test_update_extended_profile_with_form (self ):
621+ """
622+ Test updating extended profile with a validated form
623+ """
624+ mock_form = Mock ()
625+ mock_instance = Mock ()
626+ mock_instance .user = self .user
627+ mock_form .save .return_value = mock_instance
628+ update_data = {"extended_profile" : [{"field_name" : "department" , "field_value" : "Engineering" }]}
629+
630+ update_account_settings (self .user , update_data , extended_profile_form = mock_form )
631+
632+ mock_form .save .assert_called_once_with (commit = False )
633+ mock_instance .save .assert_called_once ()
634+ user_profile = UserProfile .objects .get (user = self .user )
635+ meta = user_profile .get_meta ()
636+ self .assertEqual (meta ["department" ], "Engineering" )
637+
638+ def test_update_extended_profile_with_form_new_instance (self ):
639+ """
640+ Test updating extended profile with a form for a new instance
641+ """
642+ mock_form = Mock ()
643+ mock_instance = Mock ()
644+ mock_instance .user = None
645+ mock_form .save .return_value = mock_instance
646+ update_data = {"extended_profile" : [{"field_name" : "department" , "field_value" : "Engineering" }]}
647+
648+ update_account_settings (self .user , update_data , extended_profile_form = mock_form )
649+
650+ self .assertEqual (mock_instance .user , self .user )
651+ mock_form .save .assert_called_once_with (commit = False )
652+ mock_instance .save .assert_called_once ()
653+
654+ @patch ("openedx.core.djangoapps.user_api.accounts.api.logger" )
655+ def test_update_extended_profile_form_save_error (self , mock_logger ):
656+ """
657+ Test that errors during form save are logged but don't crash
658+ """
659+ mock_form = Mock ()
660+ mock_form .save .side_effect = Exception ("Database error" )
661+ update_data = {"extended_profile" : [{"field_name" : "department" , "field_value" : "Engineering" }]}
662+
663+ update_account_settings (self .user , update_data , extended_profile_form = mock_form )
664+
665+ mock_logger .error .assert_called_once ()
666+ self .assertIn ("Error saving extended profile model" , str (mock_logger .error .call_args ))
667+ user_profile = UserProfile .objects .get (user = self .user )
668+ meta = user_profile .get_meta ()
669+ self .assertEqual (meta ["department" ], "Engineering" )
670+
671+ def test_update_extended_profile_without_extended_profile_data (self ):
672+ """
673+ Test that update_account_settings works when no extended_profile is provided
674+ """
675+ update_data = {"bio" : "Updated bio" }
676+
677+ update_account_settings (self .user , update_data )
678+
679+ user_profile = UserProfile .objects .get (user = self .user )
680+ self .assertEqual (user_profile .bio , "Updated bio" )
681+
682+ def test_update_extended_profile_form_without_data_in_update (self ):
683+ """
684+ Test that form is saved even if extended_profile is not in update data
685+ """
686+ mock_form = Mock ()
687+ mock_instance = Mock ()
688+ mock_instance .user = self .user
689+ mock_form .save .return_value = mock_instance
690+ update_data = {"bio" : "Updated bio" }
691+
692+ update_account_settings (self .user , update_data , extended_profile_form = mock_form )
693+
694+ mock_form .save .assert_called_once_with (commit = False )
695+ mock_instance .save .assert_called_once ()
696+ user_profile = UserProfile .objects .get (user = self .user )
697+ self .assertEqual (user_profile .bio , "Updated bio" )
698+
600699
601700@patch ('openedx.core.djangoapps.user_api.accounts.image_helpers._PROFILE_IMAGE_SIZES' , [50 , 10 ])
602701@patch .dict (
0 commit comments