2019-03-27 00:57:33 +01:00
|
|
|
from mock import patch
|
|
|
|
|
|
|
|
from django.utils.timezone import now as timezone_now
|
2019-06-22 01:25:53 +02:00
|
|
|
from django.conf import settings
|
2019-03-27 00:57:33 +01:00
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
2019-06-22 01:25:53 +02:00
|
|
|
from zerver.lib.test_helpers import use_s3_backend, create_s3_buckets
|
2019-05-23 13:01:42 +02:00
|
|
|
|
2019-03-27 00:57:33 +01:00
|
|
|
from zerver.models import RealmAuditLog
|
2019-06-24 02:51:13 +02:00
|
|
|
from zerver.views.realm_export import export_realm
|
2019-06-27 20:41:47 +02:00
|
|
|
import zerver.lib.upload
|
2019-06-22 01:25:53 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
def create_tarball_path() -> str:
|
2019-07-06 02:07:56 +02:00
|
|
|
tarball_path = os.path.join(settings.TEST_WORKER_DIR, 'test-export.tar.gz')
|
2019-06-22 01:25:53 +02:00
|
|
|
with open(tarball_path, 'w') as f:
|
|
|
|
f.write('zulip!')
|
|
|
|
return tarball_path
|
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):
|
2019-06-24 02:51:13 +02:00
|
|
|
export_realm(self.client_post, user)
|
2019-03-27 00:57:33 +01:00
|
|
|
|
|
|
|
@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-06-22 01:25:53 +02:00
|
|
|
bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0]
|
|
|
|
tarball_path = create_tarball_path()
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2019-05-23 13:01:42 +02:00
|
|
|
with patch('zerver.lib.export.do_export_realm',
|
2019-06-22 01:25:53 +02:00
|
|
|
return_value=tarball_path) as mock_export:
|
|
|
|
with self.settings(LOCAL_UPLOADS_DIR=None):
|
|
|
|
result = self.client_post('/json/export/realm')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertFalse(os.path.exists(tarball_path))
|
|
|
|
|
|
|
|
args = mock_export.call_args_list[0][1]
|
|
|
|
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)
|
|
|
|
|
|
|
|
export_object = RealmAuditLog.objects.filter(
|
|
|
|
event_type='realm_exported').first()
|
2019-07-13 01:17:21 +02:00
|
|
|
path_id = getattr(export_object, 'extra_data')
|
|
|
|
self.assertIsNotNone(path_id)
|
|
|
|
|
2019-06-22 01:25:53 +02:00
|
|
|
self.assertEqual(bucket.get_key(path_id).get_contents_as_string(),
|
|
|
|
b'zulip!')
|
|
|
|
|
2019-06-23 22:57:14 +02:00
|
|
|
result = self.client_get('/json/export/realm')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2019-06-24 02:51:13 +02:00
|
|
|
export_dict = result.json()['exports']
|
2019-06-23 22:57:14 +02:00
|
|
|
self.assertEqual(export_dict[0]['path'], path_id)
|
|
|
|
self.assertEqual(export_dict[0]['acting_user_id'], admin.id)
|
|
|
|
self.assert_length(export_dict,
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=admin.realm,
|
|
|
|
event_type=RealmAuditLog.REALM_EXPORTED).count())
|
|
|
|
|
2019-06-27 20:41:47 +02:00
|
|
|
result = zerver.lib.upload.upload_backend.delete_export_tarball(path_id)
|
|
|
|
self.assertEqual(result, path_id)
|
|
|
|
self.assertIsNone(bucket.get_key(path_id))
|
|
|
|
|
2019-03-27 00:57:33 +01:00
|
|
|
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-06-22 01:25:53 +02:00
|
|
|
tarball_path = create_tarball_path()
|
2019-05-16 00:51:12 +02:00
|
|
|
|
2019-06-22 01:25:53 +02:00
|
|
|
with patch('zerver.lib.export.do_export_realm',
|
|
|
|
return_value=tarball_path) as mock_export:
|
2019-05-23 13:01:42 +02:00
|
|
|
result = self.client_post('/json/export/realm')
|
2019-03-27 00:57:33 +01:00
|
|
|
self.assert_json_success(result)
|
2019-06-22 01:25:53 +02:00
|
|
|
self.assertFalse(os.path.exists(tarball_path))
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2019-06-22 01:25:53 +02:00
|
|
|
args = mock_export.call_args_list[0][1]
|
|
|
|
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)
|
|
|
|
|
|
|
|
export_object = RealmAuditLog.objects.filter(
|
|
|
|
event_type='realm_exported').first()
|
2019-07-12 23:10:10 +02:00
|
|
|
self.assertEqual(export_object.acting_user_id, admin.id)
|
|
|
|
|
2019-07-13 01:17:21 +02:00
|
|
|
path_id = getattr(export_object, 'extra_data')
|
|
|
|
response = self.client_get(path_id)
|
2019-06-22 01:25:53 +02:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-07-13 01:17:21 +02:00
|
|
|
self.assert_url_serves_contents_of_file(path_id, b'zulip!')
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2019-06-23 22:57:14 +02:00
|
|
|
result = self.client_get('/json/export/realm')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2019-06-24 02:51:13 +02:00
|
|
|
export_dict = result.json()['exports']
|
2019-06-23 22:57:14 +02:00
|
|
|
self.assertEqual(export_dict[0]['path'], path_id)
|
|
|
|
self.assertEqual(export_dict[0]['acting_user_id'], admin.id)
|
|
|
|
self.assert_length(export_dict,
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=admin.realm,
|
|
|
|
event_type=RealmAuditLog.REALM_EXPORTED).count())
|
|
|
|
|
2019-06-27 20:41:47 +02:00
|
|
|
result = zerver.lib.upload.upload_backend.delete_export_tarball(path_id)
|
|
|
|
self.assertEqual(result, path_id)
|
|
|
|
response = self.client_get(path_id)
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
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-06-24 02:51:13 +02:00
|
|
|
result = export_realm(self.client_post, admin)
|
2019-03-27 00:57:33 +01:00
|
|
|
self.assert_json_error(result, 'Exceeded rate limit.')
|