@@ -117,6 +117,43 @@ def test_email_user_about_name_change(self):
117117 mail .outbox [0 ].body , "You changed your first name or your last name"
118118 )
119119
120+ def test_does_not_email_user_about_name_change_when_name_excluded_from_update_fields (self ):
121+ account = UserAccount .objects .create (** self .stub_data )
122+ mail .outbox = []
123+ account .first_name = "Homer the Great"
124+ account .password = "New password!"
125+
126+ old_password_updated_at = account .password_updated_at
127+ account .save (update_fields = ["password" ])
128+ self .assertEqual (len (mail .outbox ), 0 ) # `first_name` change was skipped (as a hook).
129+ self .assertNotEqual (account .password_updated_at , old_password_updated_at ) # Ensure the other hook is fired.
130+
131+ def test_emails_user_about_name_change_when_one_field_from_update_fields_intersects_with_condition (self ):
132+ account = UserAccount .objects .create (** self .stub_data )
133+ mail .outbox = []
134+ account .first_name = "Homer the Great"
135+ account .password = "New password!"
136+
137+ old_password_updated_at = account .password_updated_at
138+ account .save (update_fields = ["first_name" , "password" ])
139+ self .assertEqual (
140+ mail .outbox [0 ].body , "You changed your first name or your last name"
141+ )
142+ self .assertNotEqual (account .password_updated_at , old_password_updated_at ) # Both hooks fired.
143+
144+ def test_empty_update_fields_does_not_fire_any_hooks (self ):
145+ # In Django, an empty list supplied to `update_fields` means not updating any field.
146+ account = UserAccount .objects .create (** self .stub_data )
147+ mail .outbox = []
148+ account .first_name = "Flanders"
149+ account .password = "new pass"
150+
151+ old_password_updated_at = account .password_updated_at
152+ account .save (update_fields = [])
153+ # Did not raise, so last name hook didn't fire.
154+ self .assertEqual (len (mail .outbox ), 0 )
155+ self .assertEqual (account .password_updated_at , old_password_updated_at ) # Password hook didn't fire either.
156+
120157 def test_skip_hooks (self ):
121158 """
122159 Hooked method that auto-lowercases email should be skipped.
0 commit comments