Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
}
)
Loading