2017-02-15 04:35:10 +01:00
|
|
|
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-02-15 04:35:10 +01:00
|
|
|
|
|
|
|
from zerver.lib.actions import do_create_user, do_deactivate_user, \
|
2017-03-14 06:06:56 +01:00
|
|
|
do_activate_user, do_reactivate_user, do_change_password, \
|
2017-04-06 12:27:58 +02:00
|
|
|
do_change_user_email, do_change_avatar_fields, do_change_bot_owner, \
|
2017-07-12 17:28:27 +02:00
|
|
|
do_regenerate_api_key, do_change_full_name, do_change_tos_version, \
|
|
|
|
bulk_add_subscriptions, bulk_remove_subscriptions
|
2017-02-15 04:35:10 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-05-08 17:54:11 +02:00
|
|
|
from zerver.models import RealmAuditLog, get_realm
|
2017-02-15 04:35:10 +01:00
|
|
|
|
|
|
|
from datetime import timedelta
|
2017-03-14 06:07:14 +01:00
|
|
|
from django.contrib.auth.password_validation import validate_password
|
2017-02-15 04:35:10 +01:00
|
|
|
|
2017-04-07 07:28:28 +02:00
|
|
|
import ujson
|
|
|
|
|
2017-04-05 21:53:14 +02:00
|
|
|
class TestRealmAuditLog(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_activation(self) -> None:
|
2017-02-15 04:35:10 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2017-02-15 04:35:10 +01:00
|
|
|
user = do_create_user('email', 'password', realm, 'full_name', 'short_name')
|
|
|
|
do_deactivate_user(user)
|
|
|
|
do_activate_user(user)
|
|
|
|
do_deactivate_user(user)
|
|
|
|
do_reactivate_user(user)
|
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_time__gte=now).count(), 5)
|
|
|
|
event_types = list(RealmAuditLog.objects.filter(
|
|
|
|
realm=realm, acting_user=None, modified_user=user, modified_stream=None,
|
|
|
|
event_time__gte=now, event_time__lte=now+timedelta(minutes=60))
|
|
|
|
.order_by('event_time').values_list('event_type', flat=True))
|
|
|
|
self.assertEqual(event_types, ['user_created', 'user_deactivated', 'user_activated',
|
|
|
|
'user_deactivated', 'user_reactivated'])
|
2017-03-14 06:07:14 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_password(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-03-14 06:07:14 +01:00
|
|
|
password = 'test1'
|
|
|
|
do_change_password(user, password)
|
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_type='user_change_password',
|
|
|
|
event_time__gte=now).count(), 1)
|
|
|
|
self.assertIsNone(validate_password(password, user))
|
2017-03-14 06:06:56 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_email(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-03-14 06:06:56 +01:00
|
|
|
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)
|
2017-03-23 22:02:35 +01:00
|
|
|
|
2017-09-22 16:09:37 +02:00
|
|
|
# Test the RealmAuditLog stringification
|
|
|
|
audit_entry = RealmAuditLog.objects.get(event_type='user_email_changed', event_time__gte=now)
|
|
|
|
self.assertTrue(str(audit_entry).startswith("<RealmAuditLog: <UserProfile: test@example.com <Realm: zulip 1>> user_email_changed "))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_avatar_source(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-03-23 22:02:35 +01:00
|
|
|
avatar_source = u'G'
|
|
|
|
do_change_avatar_fields(user, avatar_source)
|
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_type='user_change_avatar_source',
|
|
|
|
event_time__gte=now).count(), 1)
|
|
|
|
self.assertEqual(avatar_source, user.avatar_source)
|
2017-03-31 17:27:08 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_full_name(self) -> None:
|
2017-04-07 07:28:28 +02:00
|
|
|
start = timezone_now()
|
|
|
|
new_name = 'George Hamletovich'
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
req = dict(full_name=ujson.dumps(new_name))
|
2018-05-17 19:36:33 +02:00
|
|
|
result = self.client_patch('/json/users/{}'.format(self.example_user("hamlet").id), req)
|
2017-04-07 07:28:28 +02:00
|
|
|
self.assertTrue(result.status_code == 200)
|
|
|
|
query = RealmAuditLog.objects.filter(event_type='user_full_name_changed',
|
|
|
|
event_time__gte=start)
|
|
|
|
self.assertEqual(query.count(), 1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_tos_version(self) -> None:
|
2017-04-07 07:29:29 +02:00
|
|
|
now = timezone_now()
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
tos_version = 'android'
|
|
|
|
do_change_tos_version(user, tos_version)
|
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_type='user_tos_version_changed',
|
|
|
|
event_time__gte=now).count(), 1)
|
|
|
|
self.assertEqual(tos_version, user.tos_version)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_bot_owner(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2017-05-07 17:21:26 +02:00
|
|
|
admin = self.example_user('iago')
|
2017-05-08 17:42:50 +02:00
|
|
|
bot = self.notification_bot()
|
2017-05-07 17:21:26 +02:00
|
|
|
bot_owner = self.example_user('hamlet')
|
2017-03-31 17:27:08 +02:00
|
|
|
do_change_bot_owner(bot, bot_owner, admin)
|
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_type='bot_owner_changed',
|
|
|
|
event_time__gte=now).count(), 1)
|
|
|
|
self.assertEqual(bot_owner, bot.bot_owner)
|
2017-04-06 12:27:58 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_regenerate_api_key(self) -> None:
|
2017-04-06 12:27:58 +02:00
|
|
|
now = timezone_now()
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-04-06 12:27:58 +02:00
|
|
|
do_regenerate_api_key(user, user)
|
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_type='user_api_key_changed',
|
|
|
|
event_time__gte=now).count(), 1)
|
|
|
|
self.assertTrue(user.api_key)
|
2017-07-12 17:28:27 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_subscriptions(self) -> None:
|
2017-07-12 17:28:27 +02:00
|
|
|
now = timezone_now()
|
|
|
|
user = [self.example_user('hamlet')]
|
|
|
|
stream = [self.make_stream('test_stream')]
|
|
|
|
|
|
|
|
bulk_add_subscriptions(stream, user)
|
|
|
|
subscription_creation_logs = RealmAuditLog.objects.filter(event_type='subscription_created',
|
|
|
|
event_time__gte=now)
|
|
|
|
self.assertEqual(subscription_creation_logs.count(), 1)
|
2017-09-17 19:53:38 +02:00
|
|
|
self.assertEqual(subscription_creation_logs[0].modified_stream.id, stream[0].id)
|
2017-07-12 17:28:27 +02:00
|
|
|
self.assertEqual(subscription_creation_logs[0].modified_user, user[0])
|
|
|
|
|
|
|
|
bulk_remove_subscriptions(user, stream)
|
|
|
|
subscription_deactivation_logs = RealmAuditLog.objects.filter(event_type='subscription_deactivated',
|
|
|
|
event_time__gte=now)
|
|
|
|
self.assertEqual(subscription_deactivation_logs.count(), 1)
|
2017-09-17 19:53:38 +02:00
|
|
|
self.assertEqual(subscription_deactivation_logs[0].modified_stream.id, stream[0].id)
|
2017-07-12 17:28:27 +02:00
|
|
|
self.assertEqual(subscription_deactivation_logs[0].modified_user, user[0])
|