2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest.mock import patch
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import botocore.exceptions
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2019-06-22 01:25:53 +02:00
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from analytics.models import RealmCount
|
2019-03-27 00:57:33 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.test_helpers import (
|
|
|
|
create_dummy_file,
|
|
|
|
create_s3_buckets,
|
|
|
|
stdout_suppressed,
|
|
|
|
use_s3_backend,
|
|
|
|
)
|
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-22 01:25:53 +02:00
|
|
|
|
|
|
|
|
2019-03-27 00:57:33 +01:00
|
|
|
class RealmExportTest(ZulipTestCase):
|
2019-08-11 21:56:05 +02:00
|
|
|
"""
|
|
|
|
API endpoint testing covers the full end-to-end flow
|
|
|
|
from both the S3 and local uploads perspective.
|
|
|
|
|
|
|
|
`test_endpoint_s3` and `test_endpoint_local_uploads` follow
|
|
|
|
an identical pattern, which is documented in both test
|
|
|
|
functions.
|
|
|
|
"""
|
|
|
|
|
2019-03-27 00:57:33 +01:00
|
|
|
def test_export_as_not_admin(self) -> None:
|
|
|
|
user = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2019-03-27 00:57:33 +01:00
|
|
|
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')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(admin)
|
2019-06-22 01:25:53 +02:00
|
|
|
bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0]
|
2019-08-04 20:45:24 +02:00
|
|
|
tarball_path = create_dummy_file('test-export.tar.gz')
|
2019-03-27 00:57:33 +01:00
|
|
|
|
2019-08-11 21:56:05 +02:00
|
|
|
# Test the export logic.
|
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:
|
2020-07-26 21:45:51 +02:00
|
|
|
with self.settings(LOCAL_UPLOADS_DIR=None), stdout_suppressed(), \
|
|
|
|
self.assertLogs(level='INFO') as info_logs:
|
2019-06-22 01:25:53 +02:00
|
|
|
result = self.client_post('/json/export/realm')
|
2020-07-26 21:45:51 +02:00
|
|
|
self.assertTrue('INFO:root:Completed data export for zulip in ' in info_logs.output[0])
|
2019-08-11 21:56:05 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertFalse(os.path.exists(tarball_path))
|
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)
|
|
|
|
|
2019-08-11 21:56:05 +02:00
|
|
|
# Get the entry and test that iago initiated it.
|
|
|
|
audit_log_entry = RealmAuditLog.objects.filter(
|
2019-09-26 03:20:56 +02:00
|
|
|
event_type=RealmAuditLog.REALM_EXPORTED).first()
|
2019-08-11 21:56:05 +02:00
|
|
|
self.assertEqual(audit_log_entry.acting_user_id, admin.id)
|
2019-07-13 01:17:21 +02:00
|
|
|
|
2019-08-11 21:56:05 +02:00
|
|
|
# Test that the file is hosted, and the contents are as expected.
|
2020-08-07 01:09:47 +02:00
|
|
|
path_id = orjson.loads(audit_log_entry.extra_data).get('export_path')
|
2019-08-11 21:56:05 +02:00
|
|
|
self.assertIsNotNone(path_id)
|
2018-12-07 17:52:01 +01:00
|
|
|
self.assertEqual(bucket.Object(path_id).get()['Body'].read(), b'zulip!')
|
2019-06-22 01:25:53 +02:00
|
|
|
|
2019-06-23 22:57:14 +02:00
|
|
|
result = self.client_get('/json/export/realm')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2019-08-11 21:56:05 +02:00
|
|
|
# Test that the export we have is the export we created.
|
2019-06-24 02:51:13 +02:00
|
|
|
export_dict = result.json()['exports']
|
2019-08-11 21:56:05 +02:00
|
|
|
self.assertEqual(export_dict[0]['id'], audit_log_entry.id)
|
2019-09-24 22:46:53 +02:00
|
|
|
self.assertEqual(export_dict[0]['export_url'],
|
|
|
|
'https://test-avatar-bucket.s3.amazonaws.com' + path_id)
|
2019-06-23 22:57:14 +02:00
|
|
|
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-08-11 21:56:05 +02:00
|
|
|
# Finally, delete the file.
|
2020-06-09 00:25:09 +02:00
|
|
|
result = self.client_delete(f'/json/export/realm/{audit_log_entry.id}')
|
2019-08-01 19:59:36 +02:00
|
|
|
self.assert_json_success(result)
|
2018-12-07 17:52:01 +01:00
|
|
|
with self.assertRaises(botocore.exceptions.ClientError):
|
|
|
|
bucket.Object(path_id).load()
|
2019-06-27 20:41:47 +02:00
|
|
|
|
2019-08-01 19:59:36 +02:00
|
|
|
# Try to delete an export with a `deleted_timestamp` key.
|
|
|
|
audit_log_entry.refresh_from_db()
|
2020-08-07 01:09:47 +02:00
|
|
|
export_data = orjson.loads(audit_log_entry.extra_data)
|
2019-08-01 19:59:36 +02:00
|
|
|
self.assertIn('deleted_timestamp', export_data)
|
2020-06-09 00:25:09 +02:00
|
|
|
result = self.client_delete(f'/json/export/realm/{audit_log_entry.id}')
|
2019-08-01 19:59:36 +02:00
|
|
|
self.assert_json_error(result, "Export already deleted")
|
|
|
|
|
|
|
|
# Now try to delete a non-existent export.
|
|
|
|
result = self.client_delete('/json/export/realm/0')
|
|
|
|
self.assert_json_error(result, "Invalid data export 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')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(admin)
|
2019-08-04 20:45:24 +02:00
|
|
|
tarball_path = create_dummy_file('test-export.tar.gz')
|
2019-05-16 00:51:12 +02:00
|
|
|
|
2019-08-11 21:56:05 +02:00
|
|
|
# Test the export logic.
|
2019-06-22 01:25:53 +02:00
|
|
|
with patch('zerver.lib.export.do_export_realm',
|
|
|
|
return_value=tarball_path) as mock_export:
|
2020-07-26 21:45:51 +02:00
|
|
|
with stdout_suppressed(), self.assertLogs(level='INFO') as info_logs:
|
2019-09-13 08:23:28 +02:00
|
|
|
result = self.client_post('/json/export/realm')
|
2020-07-26 21:45:51 +02:00
|
|
|
self.assertTrue('INFO:root:Completed data export for zulip in ' in info_logs.output[0])
|
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))
|
|
|
|
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)
|
|
|
|
|
2019-08-11 21:56:05 +02:00
|
|
|
# Get the entry and test that iago initiated it.
|
|
|
|
audit_log_entry = RealmAuditLog.objects.filter(
|
2019-09-26 03:20:56 +02:00
|
|
|
event_type=RealmAuditLog.REALM_EXPORTED).first()
|
2019-08-11 21:56:05 +02:00
|
|
|
self.assertEqual(audit_log_entry.acting_user_id, admin.id)
|
2019-07-12 23:10:10 +02:00
|
|
|
|
2019-08-11 21:56:05 +02:00
|
|
|
# Test that the file is hosted, and the contents are as expected.
|
2020-08-07 01:09:47 +02:00
|
|
|
path_id = orjson.loads(audit_log_entry.extra_data).get('export_path')
|
2019-07-13 01:17:21 +02:00
|
|
|
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-08-11 21:56:05 +02:00
|
|
|
# Test that the export we have is the export we created.
|
2019-06-24 02:51:13 +02:00
|
|
|
export_dict = result.json()['exports']
|
2019-08-11 21:56:05 +02:00
|
|
|
self.assertEqual(export_dict[0]['id'], audit_log_entry.id)
|
2019-09-24 22:46:53 +02:00
|
|
|
self.assertEqual(export_dict[0]['export_url'], admin.realm.uri + path_id)
|
2019-06-23 22:57:14 +02:00
|
|
|
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-08-11 21:56:05 +02:00
|
|
|
# Finally, delete the file.
|
2020-06-09 00:25:09 +02:00
|
|
|
result = self.client_delete(f'/json/export/realm/{audit_log_entry.id}')
|
2019-08-01 19:59:36 +02:00
|
|
|
self.assert_json_success(result)
|
2019-06-27 20:41:47 +02:00
|
|
|
response = self.client_get(path_id)
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
2019-08-01 19:59:36 +02:00
|
|
|
# Try to delete an export with a `deleted_timestamp` key.
|
|
|
|
audit_log_entry.refresh_from_db()
|
2020-08-07 01:09:47 +02:00
|
|
|
export_data = orjson.loads(audit_log_entry.extra_data)
|
2019-08-01 19:59:36 +02:00
|
|
|
self.assertIn('deleted_timestamp', export_data)
|
2020-06-09 00:25:09 +02:00
|
|
|
result = self.client_delete(f'/json/export/realm/{audit_log_entry.id}')
|
2019-08-01 19:59:36 +02:00
|
|
|
self.assert_json_error(result, "Export already deleted")
|
|
|
|
|
|
|
|
# Now try to delete a non-existent export.
|
|
|
|
result = self.client_delete('/json/export/realm/0')
|
|
|
|
self.assert_json_error(result, "Invalid data export ID")
|
|
|
|
|
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')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(admin)
|
2019-05-16 00:51:12 +02:00
|
|
|
|
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.')
|
2019-08-13 04:10:09 +02:00
|
|
|
|
|
|
|
def test_upload_and_message_limit(self) -> None:
|
|
|
|
admin = self.example_user('iago')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(admin)
|
2019-08-13 04:10:09 +02:00
|
|
|
realm_count = RealmCount.objects.create(realm_id=admin.realm.id,
|
|
|
|
end_time=timezone_now(),
|
|
|
|
subgroup=1,
|
|
|
|
value=0,
|
|
|
|
property='messages_sent:client:day')
|
|
|
|
|
|
|
|
# Space limit is set as 10 GiB
|
|
|
|
with patch('zerver.models.Realm.currently_used_upload_space_bytes',
|
|
|
|
return_value=11 * 1024 * 1024 * 1024):
|
|
|
|
result = self.client_post('/json/export/realm')
|
2020-06-14 02:57:50 +02:00
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
|
|
|
f'Please request a manual export from {settings.ZULIP_ADMINISTRATOR}.',
|
|
|
|
)
|
2019-08-13 04:10:09 +02:00
|
|
|
|
|
|
|
# Message limit is set as 250000
|
|
|
|
realm_count.value = 250001
|
|
|
|
realm_count.save(update_fields=['value'])
|
|
|
|
result = self.client_post('/json/export/realm')
|
2020-06-14 02:57:50 +02:00
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
|
|
|
f'Please request a manual export from {settings.ZULIP_ADMINISTRATOR}.',
|
|
|
|
)
|