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-03-27 00:57:33 +01:00
|
|
|
from zerver.views.public_export import public_only_realm_export
|
|
|
|
from zerver.models import RealmAuditLog
|
|
|
|
|
|
|
|
# TODO: Mock export_realm_wrapper to test for s3 or local
|
|
|
|
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')
|
|
|
|
queue_data = mock_publish.call_args_list[0][0]
|
|
|
|
worker = mock_publish.call_args_list[0][0][0]
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
mock_publish.assert_called_once()
|
|
|
|
event = queue_data[1]
|
|
|
|
self.assertEqual(worker, 'deferred_work')
|
|
|
|
self.assertEqual(event['realm_id'], 1)
|
|
|
|
self.assertEqual(event['user_profile_id'], 5)
|
|
|
|
self.assertEqual(event['type'], 'realm_exported')
|
|
|
|
|
|
|
|
with patch('zerver.lib.export.do_export_realm') as mock_export:
|
|
|
|
result = self.client_post('/json/export/realm')
|
|
|
|
args = mock_export.call_args_list[0][1]
|
|
|
|
# TODO: Clean up the way we do the mocking here; we will
|
|
|
|
# want to mock do_export_realm in a way that captures its
|
|
|
|
# arguments but doesn't lead to (silent) error spam from
|
|
|
|
# do_write_stats_file_for_realm_export.
|
|
|
|
#
|
|
|
|
# Probably setting a `side_effect` makes sense?
|
|
|
|
self.assert_json_success(result)
|
2019-05-16 00:51:12 +02:00
|
|
|
self.assertEqual(args['realm'], admin.realm)
|
2019-03-27 00:57:33 +01:00
|
|
|
self.assertEqual(args['public_only'], True)
|
|
|
|
self.assertEqual(args['output_dir'].startswith('/tmp/zulip-export-'), True)
|
|
|
|
self.assertEqual(args['threads'], 6)
|
|
|
|
|
|
|
|
@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-03-27 00:57:33 +01:00
|
|
|
with patch('zerver.lib.export.do_export_realm'):
|
|
|
|
with patch('zerver.views.public_export.queue_json_publish') as mock_publish:
|
|
|
|
result = self.client_post('/json/export/realm')
|
|
|
|
queue_data = mock_publish.call_args_list[0][0]
|
|
|
|
worker = mock_publish.call_args_list[0][0][0]
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
mock_publish.assert_called_once()
|
|
|
|
event = queue_data[1]
|
|
|
|
self.assertEqual(worker, 'deferred_work')
|
|
|
|
self.assertEqual(event['realm_id'], 1)
|
|
|
|
self.assertEqual(event['user_profile_id'], 5)
|
|
|
|
self.assertEqual(event['type'], 'realm_exported')
|
|
|
|
|
2019-05-11 09:57:33 +02:00
|
|
|
with patch('zerver.lib.export.do_export_realm') as mock_export:
|
|
|
|
result = self.client_post('/json/export/realm')
|
|
|
|
args = mock_export.call_args_list[0][1]
|
|
|
|
# TODO: Clean up the way we do the mocking here; we will
|
|
|
|
# want to mock do_export_realm in a way that captures its
|
|
|
|
# arguments but doesn't lead to (silent) error spam from
|
|
|
|
# do_write_stats_file_for_realm_export.
|
|
|
|
#
|
|
|
|
# Probably setting a `side_effect` makes sense?
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2019-05-16 00:51:12 +02:00
|
|
|
self.assertEqual(args['realm'], admin.realm)
|
2019-05-11 09:57:33 +02:00
|
|
|
self.assertEqual(args['public_only'], True)
|
|
|
|
self.assertEqual(args['output_dir'].startswith('/tmp/zulip-export-'), True)
|
|
|
|
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.')
|