2019-03-27 00:57:33 +01:00
|
|
|
from mock import patch
|
|
|
|
|
|
|
|
from django.test import override_settings
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
2019-05-24 20:30:59 +02:00
|
|
|
from zerver.lib.test_helpers import use_s3_backend
|
2019-05-23 13:01:42 +02:00
|
|
|
|
2019-03-27 00:57:33 +01:00
|
|
|
from zerver.models import RealmAuditLog
|
2019-05-23 13:01:42 +02:00
|
|
|
from zerver.views.public_export import public_only_realm_export
|
|
|
|
from zerver.worker.queue_processors import DeferredWorker
|
2019-03-27 00:57:33 +01:00
|
|
|
|
|
|
|
class RealmExportTest(ZulipTestCase):
|
|
|
|
def test_export_as_not_admin(self) -> None:
|
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.login(user.email)
|
|
|
|
with self.assertRaises(JsonableError):
|
|
|
|
public_only_realm_export(self.client_post, user)
|
|
|
|
|
|
|
|
@use_s3_backend
|
|
|
|
def test_endpoint_s3(self) -> None:
|
2019-05-16 00:51:12 +02:00
|
|
|
admin = self.example_user('iago')
|
|
|
|
self.login(admin.email)
|
2019-03-27 00:57:33 +01:00
|
|
|
|
|
|
|
with patch('zerver.views.public_export.queue_json_publish') as mock_publish:
|
|
|
|
result = self.client_post('/json/export/realm')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
mock_publish.assert_called_once()
|
2019-05-26 11:06:38 +02:00
|
|
|
event = mock_publish.call_args_list[0][0][1]
|
|
|
|
self.assertEqual(mock_publish.call_args_list[0][0][0], 'deferred_work')
|
2019-03-27 00:57:33 +01:00
|
|
|
self.assertEqual(event['realm_id'], 1)
|
|
|
|
self.assertEqual(event['user_profile_id'], 5)
|
|
|
|
self.assertEqual(event['type'], 'realm_exported')
|
2019-05-17 00:54:56 +02:00
|
|
|
self.assertTrue(type(event['id']), int)
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2019-05-23 13:01:42 +02:00
|
|
|
with patch('zerver.lib.export.do_export_realm',
|
|
|
|
side_effect=FileNotFoundError) as mock_export:
|
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
DeferredWorker().consume(event)
|
2019-03-27 00:57:33 +01:00
|
|
|
args = mock_export.call_args_list[0][1]
|
2019-05-23 13:01:42 +02:00
|
|
|
self.assertEqual(args['realm'], admin.realm)
|
|
|
|
self.assertEqual(args['public_only'], True)
|
|
|
|
self.assertIn('/tmp/zulip-export-', args['output_dir'])
|
|
|
|
self.assertEqual(args['threads'], 6)
|
2019-03-27 00:57:33 +01:00
|
|
|
|
|
|
|
@override_settings(LOCAL_UPLOADS_DIR='/var/uploads')
|
|
|
|
def test_endpoint_local_uploads(self) -> None:
|
2019-05-16 00:51:12 +02:00
|
|
|
admin = self.example_user('iago')
|
|
|
|
self.login(admin.email)
|
|
|
|
|
2019-05-23 13:01:42 +02:00
|
|
|
with patch('zerver.views.public_export.queue_json_publish') as mock_publish:
|
|
|
|
result = self.client_post('/json/export/realm')
|
2019-03-27 00:57:33 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
mock_publish.assert_called_once()
|
2019-05-26 11:06:38 +02:00
|
|
|
event = mock_publish.call_args_list[0][0][1]
|
|
|
|
self.assertEqual(mock_publish.call_args_list[0][0][0], 'deferred_work')
|
2019-03-27 00:57:33 +01:00
|
|
|
self.assertEqual(event['realm_id'], 1)
|
|
|
|
self.assertEqual(event['user_profile_id'], 5)
|
|
|
|
self.assertEqual(event['type'], 'realm_exported')
|
2019-05-17 00:54:56 +02:00
|
|
|
self.assertEqual(type(event['id']), int)
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2019-05-23 13:01:42 +02:00
|
|
|
with patch('zerver.lib.export.do_export_realm',
|
|
|
|
side_effect=FileNotFoundError) as mock_export:
|
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
DeferredWorker().consume(event)
|
2019-05-11 09:57:33 +02:00
|
|
|
args = mock_export.call_args_list[0][1]
|
2019-05-23 13:01:42 +02:00
|
|
|
self.assertEqual(args['realm'], admin.realm)
|
|
|
|
self.assertEqual(args['public_only'], True)
|
|
|
|
self.assertIn('/tmp/zulip-export-', args['output_dir'])
|
|
|
|
self.assertEqual(args['threads'], 6)
|
2019-03-27 00:57:33 +01:00
|
|
|
|
|
|
|
def test_realm_export_rate_limited(self) -> None:
|
2019-05-16 00:51:12 +02:00
|
|
|
admin = self.example_user('iago')
|
|
|
|
self.login(admin.email)
|
|
|
|
|
2019-03-27 00:57:33 +01:00
|
|
|
current_log = RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.REALM_EXPORTED)
|
|
|
|
self.assertEqual(len(current_log), 0)
|
|
|
|
|
|
|
|
exports = []
|
|
|
|
for i in range(0, 5):
|
2019-05-16 00:51:12 +02:00
|
|
|
exports.append(RealmAuditLog(realm=admin.realm,
|
2019-03-27 00:57:33 +01:00
|
|
|
event_type=RealmAuditLog.REALM_EXPORTED,
|
|
|
|
event_time=timezone_now()))
|
|
|
|
RealmAuditLog.objects.bulk_create(exports)
|
|
|
|
|
2019-05-16 00:51:12 +02:00
|
|
|
result = public_only_realm_export(self.client_post, admin)
|
2019-03-27 00:57:33 +01:00
|
|
|
self.assert_json_error(result, 'Exceeded rate limit.')
|