chore: adding test to verify default-acl behaviour. (#33074)

* chore: adding test to verify default-acl behaviour.
This commit is contained in:
Awais Qureshi
2023-08-22 22:01:28 +05:00
committed by GitHub
parent 5960840f51
commit a4b11c02e0

View File

@@ -5,12 +5,13 @@ Unit tests for video utils.
from datetime import datetime
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import MagicMock, patch
import ddt
import pytz
import requests
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import get_storage_class
from django.core.files.uploadedfile import UploadedFile
from django.test.utils import override_settings
@@ -369,8 +370,14 @@ class ScrapeVideoThumbnailsTestCase(CourseTestCase):
self.assertIsNone(video1_image_url)
@ddt.ddt
class S3Boto3TestCase(TestCase):
""" verify s3boto3 returns valid backend."""
def setUp(self):
self.storage = S3Boto3Storage()
self.storage._connections.connection = MagicMock() # pylint: disable=protected-access
def test_video_backend(self):
self.assertEqual(
S3Boto3Storage,
@@ -390,3 +397,84 @@ class S3Boto3TestCase(TestCase):
)(**settings.VIDEO_IMAGE_SETTINGS.get('STORAGE_KWARGS', {}))
self.assertEqual(S3Boto3Storage, storage.__class__)
def test_storage_without_global_default_acl_setting(self):
"""
In 1.9.1 package provides the default-acl=`public-read`.
AWS_DEFAULT_ACL is not defined but package will send public-read.
In 1.10.1 this test will fail because that version has no default value.
"""
name = 'test_storage_save231.txt'
content = ContentFile('new content')
storage = S3Boto3Storage(**{'bucket_name': 'test'})
storage._connections.connection = MagicMock() # pylint: disable=protected-access
storage.save(name, content)
storage.bucket.Object.assert_called_once_with(name)
obj = storage.bucket.Object.return_value
obj.upload_fileobj.assert_called_with(
content,
ExtraArgs={
'ACL': 'public-read', # it will come from 1.9.1
'ContentType': 'text/plain',
}
)
@override_settings(AWS_DEFAULT_ACL='public-read')
@ddt.data(
('public-read', 'public-read'),
('private', 'private'),
(None, None)
)
@ddt.unpack
def test_storage_without_global_default_acl_setting_and_bucket_acls(self, default_acl, output_acl):
"""
AWS_DEFAULT_ACL set to private and let bucket level acl overrides it behaviour.
"""
name = 'test_storage_save.txt'
content = ContentFile('new content')
storage = S3Boto3Storage(**{'bucket_name': 'test', 'default_acl': default_acl})
storage._connections.connection = MagicMock() # pylint: disable=protected-access
storage.save(name, content)
storage.bucket.Object.assert_called_once_with(name)
obj = storage.bucket.Object.return_value
ExtraArgs = {
'ACL': output_acl,
'ContentType': 'text/plain',
}
if default_acl is None:
del ExtraArgs['ACL']
obj.upload_fileobj.assert_called_with(
content,
ExtraArgs=ExtraArgs
)
@ddt.data('public-read', 'private')
def test_storage_passing_default_acl_as_none(self, input_acl):
"""
check bucket-level None behaviour with different AWS_DEFAULT_ACL
"""
with override_settings(AWS_DEFAULT_ACL=input_acl):
name = 'test_storage_save231.txt'
content = ContentFile('new content')
storage = S3Boto3Storage(**{'bucket_name': 'test', 'default_acl': None})
storage._connections.connection = MagicMock() # pylint: disable=protected-access
storage.save(name, content)
storage.bucket.Object.assert_called_once_with(name)
obj = storage.bucket.Object.return_value
obj.upload_fileobj.assert_called_with(
content,
ExtraArgs={
'ContentType': 'text/plain',
}
)