1212import ddt
1313import pytz
1414from django .conf import settings
15+ from django .core .files .storage import FileSystemStorage
1516from django .test .testcases import TransactionTestCase
1617from django .test .utils import override_settings
1718from django .urls import reverse
1819from rest_framework import status
1920from rest_framework .test import APIClient , APITestCase
21+ from storages .backends .s3boto3 import S3Boto3Storage
2022
2123from common .djangoapps .student .models import PendingEmailChange , UserProfile
2224from common .djangoapps .student .models_api import do_name_change_request , get_pending_name_change
3335 RetirementStateFactory ,
3436 UserRetirementStatusFactory
3537)
38+ from openedx .core .djangoapps .user_api .accounts .image_helpers import get_profile_image_storage
3639from openedx .core .djangoapps .user_api .models import UserPreference , UserRetirementStatus
3740from openedx .core .djangoapps .user_api .preferences .api import set_user_preference
3841from openedx .core .djangoapps .waffle_utils .testutils import WAFFLE_TABLES
@@ -1156,6 +1159,37 @@ def test_patch_serializer_save_fails(self, serializer_save):
11561159 assert "Error thrown when saving account updates: 'bummer'" == error_response .data ['developer_message' ]
11571160 assert error_response .data ['user_message' ] is None
11581161
1162+ def test_profile_image_backend (self ):
1163+ # settings file contains the `VIDEO_IMAGE_SETTINGS` but dont'have STORAGE_CLASS
1164+ # so it returns the default storage.
1165+ storage = get_profile_image_storage ()
1166+ storage_class = storage .__class__
1167+ self .assertEqual (
1168+ settings .PROFILE_IMAGE_BACKEND ['class' ],
1169+ f"{ storage_class .__module__ } .{ storage_class .__name__ } " ,
1170+ )
1171+ self .assertEqual (storage .base_url , settings .PROFILE_IMAGE_BACKEND ['options' ]['base_url' ])
1172+
1173+ @override_settings (PROFILE_IMAGE_BACKEND = {
1174+ 'class' : 'storages.backends.s3boto3.S3Boto3Storage' ,
1175+ 'options' : {
1176+ 'bucket_name' : 'test' ,
1177+ 'default_acl' : 'public' ,
1178+ 'location' : 'abc/def'
1179+ }
1180+ })
1181+ def test_profile_backend_with_params (self ):
1182+ storage = get_profile_image_storage ()
1183+ self .assertIsInstance (storage , S3Boto3Storage )
1184+ self .assertEqual (storage .bucket_name , "test" )
1185+ self .assertEqual (storage .default_acl , 'public' )
1186+ self .assertEqual (storage .location , "abc/def" )
1187+
1188+ @override_settings (PROFILE_IMAGE_BACKEND = {'class' : None , 'options' : {}})
1189+ def test_profile_backend_without_backend (self ):
1190+ storage = get_profile_image_storage ()
1191+ self .assertIsInstance (storage , FileSystemStorage )
1192+
11591193 @override_settings (PROFILE_IMAGE_BACKEND = TEST_PROFILE_IMAGE_BACKEND )
11601194 def test_convert_relative_profile_url (self ):
11611195 """
@@ -1170,6 +1204,37 @@ def test_convert_relative_profile_url(self):
11701204 'image_url_full' : 'http://testserver/static/default_50.png' ,
11711205 'image_url_small' : 'http://testserver/static/default_10.png' }
11721206
1207+ @override_settings (
1208+ PROFILE_IMAGE_BACKEND = {},
1209+ STORAGES = {
1210+ 'profile_image' : {
1211+ 'BACKEND' : 'storages.backends.s3boto3.S3Boto3Storage' ,
1212+ 'OPTIONS' : {
1213+ 'bucket_name' : 'profiles' ,
1214+ 'default_acl' : 'public' ,
1215+ 'location' : 'profile/images' ,
1216+ }
1217+ }
1218+ }
1219+ )
1220+ def test_profile_backend_with_profile_image_settings (self ):
1221+ """ It will use the storages dict with profile_images backend"""
1222+ storage = get_profile_image_storage ()
1223+ self .assertIsInstance (storage , S3Boto3Storage )
1224+ self .assertEqual (storage .bucket_name , "profiles" )
1225+ self .assertEqual (storage .default_acl , 'public' )
1226+ self .assertEqual (storage .location , "profile/images" )
1227+
1228+ @override_settings (
1229+ PROFILE_IMAGE_BACKEND = {},
1230+ )
1231+ def test_profile_backend_with_default_hardcoded_backend (self ):
1232+ """ In case of empty storages scenario uses the hardcoded backend."""
1233+ del settings .DEFAULT_FILE_STORAGE
1234+ del settings .STORAGES
1235+ storage = get_profile_image_storage ()
1236+ self .assertIsInstance (storage , FileSystemStorage )
1237+
11731238 @ddt .data (
11741239 ("client" , "user" , True ),
11751240 ("different_client" , "different_user" , False ),
0 commit comments