zerver: Replace log_event with RealmAuditLog in do_change_user_email.

This replaces the ancient file logging approach for the auditable
password change event with the database audit log.
This commit is contained in:
Maxim Averin
2017-03-13 22:06:56 -07:00
committed by Tim Abbott
parent fc35982b87
commit b13b660709
2 changed files with 18 additions and 6 deletions

View File

@@ -652,7 +652,6 @@ def do_deactivate_stream(stream, log=True):
def do_change_user_email(user_profile, new_email):
# type: (UserProfile, Text) -> None
old_email = user_profile.email
user_profile.email = new_email
user_profile.save(update_fields=["email"])
@@ -660,10 +659,10 @@ def do_change_user_email(user_profile, new_email):
new_email=new_email)
send_event(dict(type='realm_user', op='update', person=payload),
active_user_ids(user_profile.realm))
log_event({'type': 'user_email_changed',
'old_email': old_email,
'new_email': new_email})
event_time = timezone.now()
RealmAuditLog.objects.create(realm=user_profile.realm, acting_user=user_profile,
modified_user=user_profile, event_type='user_email_changed',
event_time=event_time)
def do_start_email_change_process(user_profile, new_email):
# type: (UserProfile, Text) -> None

View File

@@ -2,7 +2,8 @@
from django.utils import timezone
from zerver.lib.actions import do_create_user, do_deactivate_user, \
do_activate_user, do_reactivate_user, do_change_password
do_activate_user, do_reactivate_user, do_change_password, \
do_change_user_email
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmAuditLog, get_realm
@@ -38,3 +39,15 @@ class TestChangePassword(ZulipTestCase):
self.assertEqual(RealmAuditLog.objects.filter(event_type='user_change_password',
event_time__gte=now).count(), 1)
self.assertIsNone(validate_password(password, user))
class TestChangeEmail(ZulipTestCase):
def test_change_email(self):
# type: () -> None
realm = get_realm('zulip')
now = timezone.now()
user = do_create_user('email', 'password', realm, 'full_name', 'short_name')
email = 'test@example.com'
do_change_user_email(user, email)
self.assertEqual(RealmAuditLog.objects.filter(event_type='user_email_changed',
event_time__gte=now).count(), 1)
self.assertEqual(email, user.email)