2016-12-28 14:46:42 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import mock
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from zerver.lib.attachments import user_attachments
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.models import Attachment
|
|
|
|
|
|
|
|
|
|
|
|
class AttachmentsTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
2016-12-28 14:46:42 +01:00
|
|
|
self.attachment = Attachment.objects.create(
|
2017-05-24 05:08:49 +02:00
|
|
|
file_name='test.txt', path_id='foo/bar/test.txt', owner=user_profile)
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_by_user(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
|
|
|
self.login(user_profile.email)
|
2016-12-28 14:46:42 +01:00
|
|
|
result = self.client_get('/json/attachments')
|
|
|
|
self.assert_json_success(result)
|
2017-05-24 05:08:49 +02:00
|
|
|
attachments = user_attachments(user_profile)
|
2017-08-16 09:49:43 +02:00
|
|
|
self.assertEqual(result.json()['attachments'], attachments)
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2017-12-08 17:03:58 +01:00
|
|
|
def test_remove_attachment_exception(self) -> None:
|
2017-09-19 23:13:52 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
|
|
|
self.login(user_profile.email)
|
|
|
|
with mock.patch('zerver.lib.attachments.delete_message_image', side_effect=Exception()):
|
2017-10-06 13:15:59 +02:00
|
|
|
result = self.client_delete('/json/attachments/{id}'.format(id=self.attachment.id))
|
2017-11-09 16:26:38 +01:00
|
|
|
self.assert_json_error(result, "An error occurred while deleting the attachment. Please try again later.")
|
2017-09-19 23:13:52 +02:00
|
|
|
|
2016-12-28 14:46:42 +01:00
|
|
|
@mock.patch('zerver.lib.attachments.delete_message_image')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_remove_attachment(self, ignored: Any) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
|
|
|
self.login(user_profile.email)
|
2017-10-06 13:15:59 +02:00
|
|
|
result = self.client_delete('/json/attachments/{id}'.format(id=self.attachment.id))
|
2016-12-28 14:46:42 +01:00
|
|
|
self.assert_json_success(result)
|
2017-05-24 05:08:49 +02:00
|
|
|
attachments = user_attachments(user_profile)
|
2016-12-28 14:46:42 +01:00
|
|
|
self.assertEqual(attachments, [])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_another_user(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('iago')
|
|
|
|
self.login(user_profile.email)
|
2016-12-28 14:46:42 +01:00
|
|
|
result = self.client_get('/json/attachments')
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:49:43 +02:00
|
|
|
self.assertEqual(result.json()['attachments'], [])
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_remove_another_user(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('iago')
|
|
|
|
self.login(user_profile.email)
|
2017-10-06 13:15:59 +02:00
|
|
|
result = self.client_delete('/json/attachments/{id}'.format(id=self.attachment.id))
|
2016-12-28 14:46:42 +01:00
|
|
|
self.assert_json_error(result, 'Invalid attachment')
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile_to_remove = self.example_user('cordelia')
|
|
|
|
attachments = user_attachments(user_profile_to_remove)
|
2016-12-28 14:46:42 +01:00
|
|
|
self.assertEqual(attachments, [self.attachment.to_dict()])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_unauthenticated(self) -> None:
|
2016-12-28 14:46:42 +01:00
|
|
|
result = self.client_get('/json/attachments')
|
|
|
|
self.assert_json_error(result, 'Not logged in: API authentication or user session required', status_code=401)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_unauthenticated(self) -> None:
|
2017-10-06 13:15:59 +02:00
|
|
|
result = self.client_delete('/json/attachments/{id}'.format(id=self.attachment.id))
|
2016-12-28 14:46:42 +01:00
|
|
|
self.assert_json_error(result, 'Not logged in: API authentication or user session required', status_code=401)
|