Ejemplo n.º 1
0
 def setUpClass(cls):
     super(StaticCloudinaryStorageTests, cls).setUpClass()
     cls.storage = StaticCloudinaryStorage(tag=get_random_name())
     name = get_random_name()
     cls.content = b'some content'
     cls.file = ContentFile(cls.content)
     cls.name = cls.storage.save(name, cls.file)
Ejemplo n.º 2
0
 def setUpClass(cls):
     super(StaticCloudinaryStorageTests, cls).setUpClass()
     cls.storage = StaticCloudinaryStorage(tag=get_random_name())
     name = get_random_name()
     cls.content = b'some content'
     cls.file = ContentFile(cls.content)
     cls.name = cls.storage.save(name, cls.file)
 def test_command_doesnt_remove_anything_without_uploaded_files(self):
     DeleteRedundantStaticCommand.TAG = get_random_name()
     output = execute_command('deleteredundantstatic', '--noinput')
     try:
         self.assertIn('There is no file to delete.', output)
     finally:
         DeleteRedundantStaticCommand.TAG = app_settings.STATIC_TAG
Ejemplo n.º 4
0
 def test_invalid_video_raises_valuation_error(self):
     model = TestVideoModel(name='name')
     invalid_file = SimpleUploadedFile(get_random_name(),
                                       b'this is not a video',
                                       content_type='video/mp4')
     model.video = invalid_file
     with self.assertRaises(ValidationError) as e:
         model.full_clean()
     self.assertEqual(e.exception.messages,
                      [app_settings.INVALID_VIDEO_ERROR_MESSAGE])
 def test_command_saves_manifest_file(self):
     name = get_random_name()
     StaticHashedCloudinaryStorage.manifest_name = name
     execute_command('collectstatic', '--noinput')
     try:
         manifest_path = os.path.join(
             app_settings.STATICFILES_MANIFEST_ROOT, name)
         self.assertTrue(os.path.exists(manifest_path))
         os.remove(manifest_path)
     finally:
         StaticHashedCloudinaryStorage.manifest_name = 'staticfiles.json'
Ejemplo n.º 6
0
 def test_file_exists_after_model_instance_with_file_is_saved(self):
     file_name = get_random_name()
     content = ContentFile(b'Content of model file')
     model = TestModel(name='name')
     model.file.save(file_name, content)
     file_name = model.file.name
     storage = RawMediaCloudinaryStorage()
     try:
         self.assertTrue(storage.exists(file_name))
     finally:
         storage.delete(file_name)
 def setUpClass(cls):
     super(BaseOrphanedMediaCommandTestsMixin, cls).setUpClass()
     set_media_tag(get_random_name())
     TestModelWithoutFile.objects.create(name='without file')
     TestModel.objects.create(name='without file')
     TestImageModel.objects.create(name='without image')
     cls.file = cls.add_file_to_model(TestModel(name='with file')).file.name
     cls.file_2 = cls.add_file_to_model(
         TestModel(name='with file')).file.name
     image_model_instance = cls.add_file_to_model(
         TestImageModel(name='with file and image'))
     cls.file_removed = image_model_instance.file.name
     cls.add_file_to_model(image_model_instance)
     cls.file_removed_2 = image_model_instance.file.name
     cls.add_file_to_model(image_model_instance)
     cls.file_3 = image_model_instance.file.name
     image = ImageFile(
         open(os.path.join('tests', 'dummy-files', 'dummy-image.jpg'),
              'rb'))
     image_model_instance.image.save(get_random_name(), image)
     cls.file_4 = image_model_instance.image.name
Ejemplo n.º 8
0
 def test_video_can_be_uploaded(self):
     file_name = get_random_name()
     video = File(
         open(os.path.join('tests', 'dummy-files', 'dummy-video.mp4'),
              'rb'))
     model = TestVideoModel(name='name')
     model.video.save(file_name, video)
     model.full_clean()
     file_name = model.video.name
     storage = VideoMediaCloudinaryStorage()
     try:
         self.assertTrue(storage.exists(file_name))
     finally:
         storage.delete(file_name)
Ejemplo n.º 9
0
 def test_exists_raises_http_error(self, head_mock):
     response = head_mock.return_value
     response.status_code = 500
     response.raise_for_status.side_effect = HTTPError
     with self.assertRaises(HTTPError):
         self.storage.exists(get_random_name())
Ejemplo n.º 10
0
 def test_file_doesnt_exist_without_upload(self):
     file_name = get_random_name()
     self.assertFalse(self.storage.exists(file_name))
Ejemplo n.º 11
0
 def upload_file(cls, prefix='', directory_name=''):
     file_name = prefix + directory_name + get_random_name()
     content = ContentFile(cls.file_content)
     file_name = cls.storage.save(file_name, content)
     return file_name, content
Ejemplo n.º 12
0
 def test_hashed_name_raises_error_when_file_not_found(self, find_mock):
     storage = StaticHashedCloudinaryStorage()
     not_existing_file = get_random_name()
     find_mock.return_value = not_existing_file
     with self.assertRaises(ValueError):
         storage.hashed_name(not_existing_file)
Ejemplo n.º 13
0
 def test_hashed_name_raises_error_when_file_not_found(self, find_mock):
     storage = StaticHashedCloudinaryStorage()
     not_existing_file = get_random_name()
     find_mock.return_value = not_existing_file
     with self.assertRaises(ValueError):
         storage.hashed_name(not_existing_file)
Ejemplo n.º 14
0
import os.path

from requests.exceptions import HTTPError
import cloudinary.uploader
from django.test import SimpleTestCase, override_settings
from django.core.files.base import ContentFile
from django.conf import settings

from cloudinary_storage.storage import (MediaCloudinaryStorage, ManifestCloudinaryStorage, StaticCloudinaryStorage,
                                        StaticHashedCloudinaryStorage, RESOURCE_TYPES)
from cloudinary_storage import app_settings
from tests.tests.test_helpers import get_random_name, import_mock

mock = import_mock()

TAG = get_random_name()


class CloudinaryMediaStorageTests(SimpleTestCase):
    @classmethod
    def setUpClass(cls):
        super(CloudinaryMediaStorageTests, cls).setUpClass()
        cls.file_content = b'Content of file'
        cls.storage = MediaCloudinaryStorage(tag=TAG, resource_type='raw')
        cls.file_name, cls.file = cls.upload_file()

    @classmethod
    def upload_file(cls, prefix='', directory_name=''):
        file_name = prefix + directory_name + get_random_name()
        content = ContentFile(cls.file_content)
        file_name = cls.storage.save(file_name, content)
 def add_file_to_model(cls, model_instance):
     content = ContentFile(b'Content of file')
     model_instance.file.save(get_random_name(), content)
     return model_instance
Ejemplo n.º 16
0
 def upload_file(cls, prefix='', directory_name=''):
     file_name = prefix + directory_name + get_random_name()
     content = ContentFile(cls.file_content)
     file_name = cls.storage.save(file_name, content)
     return file_name, content
Ejemplo n.º 17
0
 def test_file_doesnt_exist_without_upload(self):
     file_name = get_random_name()
     self.assertFalse(self.storage.exists(file_name))
Ejemplo n.º 18
0
 def test_delete_returns_false_when_file_didnt_exist(self):
     file_name = get_random_name()
     self.assertFalse(self.storage.delete(file_name))
Ejemplo n.º 19
0
 def test_size_of_not_existing_file_returns_none(self):
     file_name = get_random_name()
     size = self.storage.size(file_name)
     self.assertEqual(size, None)
Ejemplo n.º 20
0
 def test_delete_returns_false_when_file_didnt_exist(self):
     file_name = get_random_name()
     self.assertFalse(self.storage.delete(file_name))
Ejemplo n.º 21
0
 def test_exists_raises_http_error(self, head_mock):
     response = head_mock.return_value
     response.status_code = 500
     response.raise_for_status.side_effect = HTTPError
     with self.assertRaises(HTTPError):
         self.storage.exists(get_random_name())
Ejemplo n.º 22
0
 def test_size_of_not_existing_file_returns_none(self):
     file_name = get_random_name()
     size = self.storage.size(file_name)
     self.assertEqual(size, None)
Ejemplo n.º 23
0
import cloudinary.uploader
from django.test import SimpleTestCase, override_settings
from django.core.files.base import ContentFile
from django.conf import settings

from cloudinary_storage.storage import (MediaCloudinaryStorage,
                                        ManifestCloudinaryStorage,
                                        StaticCloudinaryStorage,
                                        StaticHashedCloudinaryStorage,
                                        RESOURCE_TYPES)
from cloudinary_storage import app_settings
from tests.tests.test_helpers import get_random_name, import_mock

mock = import_mock()

TAG = get_random_name()


class CloudinaryMediaStorageTests(SimpleTestCase):
    @classmethod
    def setUpClass(cls):
        super(CloudinaryMediaStorageTests, cls).setUpClass()
        cls.file_content = b'Content of file'
        cls.storage = MediaCloudinaryStorage(tag=TAG, resource_type='raw')
        cls.file_name, cls.file = cls.upload_file()

    @classmethod
    def upload_file(cls, prefix='', directory_name=''):
        file_name = prefix + directory_name + get_random_name()
        content = ContentFile(cls.file_content)
        file_name = cls.storage.save(file_name, content)