2016-12-28 14:46:42 +01:00
|
|
|
from typing import Any
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest import mock
|
2016-12-28 14:46:42 +01:00
|
|
|
|
|
|
|
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:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("cordelia")
|
2016-12-28 14:46:42 +01:00
|
|
|
self.attachment = Attachment.objects.create(
|
2021-02-12 08:20:45 +01:00
|
|
|
file_name="test.txt", path_id="foo/bar/test.txt", owner=user_profile, size=512
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_by_user(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("cordelia")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/json/attachments")
|
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)
|
2021-02-12 08:20:45 +01: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:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("cordelia")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("zerver.lib.attachments.delete_message_image", side_effect=Exception()):
|
|
|
|
result = self.client_delete(f"/json/attachments/{self.attachment.id}")
|
2021-02-12 08:19:30 +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
|
|
|
|
2021-02-12 08:20:45 +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:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("cordelia")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/attachments/{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:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("iago")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/json/attachments")
|
2016-12-28 14:46:42 +01:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01: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:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("iago")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/attachments/{self.attachment.id}")
|
|
|
|
self.assert_json_error(result, "Invalid attachment")
|
|
|
|
user_profile_to_remove = self.example_user("cordelia")
|
2017-05-24 05:08:49 +02:00
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/json/attachments")
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
2021-02-12 08:20:45 +01:00
|
|
|
result, "Not logged in: API authentication or user session required", status_code=401
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_unauthenticated(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/attachments/{self.attachment.id}")
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
2021-02-12 08:20:45 +01:00
|
|
|
result, "Not logged in: API authentication or user session required", status_code=401
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|