Skip to content

Commit

Permalink
chore: adding test to verify default-acl behaviour. (#33074)
Browse files Browse the repository at this point in the history
* chore: adding test to verify default-acl behaviour.
  • Loading branch information
awais786 authored Aug 22, 2023
1 parent 5960840 commit a4b11c0
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion cms/djangoapps/contentstore/tests/test_video_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

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
Expand Down Expand Up @@ -369,8 +370,14 @@ def test_no_video_thumbnail_downloaded(
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,
Expand All @@ -390,3 +397,84 @@ def test_boto3_backend_with_params(self):
)(**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',
}
)

0 comments on commit a4b11c0

Please sign in to comment.