2016-12-28 14:46:42 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
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):
|
|
|
|
def setUp(self):
|
|
|
|
# type: () -> 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
|
|
|
|
|
|
|
def test_list_by_user(self):
|
|
|
|
# type: () -> 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
|
|
|
|
|
|
|
@mock.patch('zerver.lib.attachments.delete_message_image')
|
|
|
|
def test_remove_attachment(self, ignored):
|
|
|
|
# type: (Any) -> 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_delete('/json/attachments/{pk}'.format(pk=self.attachment.pk))
|
|
|
|
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, [])
|
|
|
|
|
|
|
|
def test_list_another_user(self):
|
|
|
|
# type: () -> 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
|
|
|
|
|
|
|
def test_remove_another_user(self):
|
|
|
|
# type: () -> 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_delete('/json/attachments/{pk}'.format(pk=self.attachment.pk))
|
|
|
|
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()])
|
|
|
|
|
|
|
|
def test_list_unauthenticated(self):
|
|
|
|
# type: () -> None
|
|
|
|
result = self.client_get('/json/attachments')
|
|
|
|
self.assert_json_error(result, 'Not logged in: API authentication or user session required', status_code=401)
|
|
|
|
|
|
|
|
def test_delete_unauthenticated(self):
|
|
|
|
# type: () -> None
|
|
|
|
result = self.client_delete('/json/attachments/{pk}'.format(pk=self.attachment.pk))
|
|
|
|
self.assert_json_error(result, 'Not logged in: API authentication or user session required', status_code=401)
|