Skip to content

Commit

Permalink
Dedupe test settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyg committed Jan 28, 2019
1 parent a8ba242 commit f851549
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 101 deletions.
64 changes: 51 additions & 13 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
import django
from django.conf import settings


def is_authenticated(user):
if callable(user.is_authenticated): # Django =< 1.9
return user.is_authenticated()
return user.is_authenticated


settings.configure(
DEBUG=True,
DATABASES={
Expand Down Expand Up @@ -40,40 +47,71 @@
}
},
],
AWS_ACCESS_KEY_ID=environ.get('AWS_ACCESS_KEY_ID', ''),
AWS_SECRET_ACCESS_KEY=environ.get('AWS_SECRET_ACCESS_KEY', ''),
AWS_ACCESS_KEY_ID=environ.get('AWS_ACCESS_KEY_ID', '123'),
AWS_SECRET_ACCESS_KEY=environ.get('AWS_SECRET_ACCESS_KEY', '123'),
AWS_STORAGE_BUCKET_NAME=environ.get(
'AWS_STORAGE_BUCKET_NAME', 'test-bucket'),
S3DIRECT_REGION='us-east-1',
S3DIRECT_DESTINATIONS={
'misc': {
'key': lambda original_filename: 'images/unique.jpg',
'key': lambda original_filename: 'images/unique.jpg'
},
'files': {
'key': 'uploads/files',
'auth': lambda u: u.is_staff,
'key': '/',
'auth': lambda u: u.is_staff
},
'protected': {
'key': '/',
'auth': lambda u: u.is_staff
},
'not_protected': {
'key': '/',
},
'imgs': {
'key': 'uploads/imgs',
'auth': lambda u: True,
'allowed': ['image/jpeg', 'image/png'],
'content_length_range': (5000, 20000000), # 5kb - 20mb
'allowed': ['image/jpeg', 'image/png']
},
'thumbs': {
'key': 'uploads/thumbs',
'allowed': ['image/jpeg'],
'content_length_range': (1000, 50000)
},
'vids': {
'key': 'uploads/vids',
'auth': lambda u: u.is_authenticated(),
'allowed': ['video/mp4'],
'auth': is_authenticated,
'allowed': ['video/mp4']
},
'cached': {
'key': 'uploads/vids',
'auth': lambda u: True,
'auth': is_authenticated,
'allowed': '*',
'acl': 'authenticated-read',
'bucket': 'astoragebucketname',
'cache_control': 'max-age=2592000',
'content_disposition': 'attachment',
'server_side_encryption': 'AES256',
}
'server_side_encryption': 'AES256'
},
'accidental-leading-slash': {
'key': '/directory/leading'
},
'accidental-trailing-slash': {
'key': 'directory/trailing/'
},
'region-cn': {
'key': 'uploads/vids',
'region': 'cn-north-1'
},
'region-eu': {
'key': 'uploads/vids',
'region': 'eu-west-1'
},
'region-default': {
'key': 'uploads/vids'
},
'key_args': {
'key': lambda original_filename, args: args + '/' + 'background.jpg',
'key_args': 'assets/backgrounds'
},
}
)

Expand Down
110 changes: 22 additions & 88 deletions s3direct/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,7 @@
from s3direct import widgets
from s3direct.utils import get_aws_credentials

HTML_OUTPUT = (
'<div class="s3direct" data-policy-url="/get_upload_params/" '
'data-signing-url="/get_aws_v4_signature/">\n'
' <a class="file-link" target="_blank" href=""></a>\n'
' <a class="file-remove" href="#remove">Remove</a>\n'
' <input class="csrf-cookie-name" type="hidden" value="csrftoken">\n'
' <input class="file-url" type="hidden" value="" id="" name="filename" />'
'\n'
' <input class="file-dest" type="hidden" value="foo">\n'
' <input class="file-input" type="file" style=""/>\n'
' <div class="progress progress-striped active">\n'
' <div class="bar"></div>\n'
' </div>\n'
'</div>\n')


def is_authenticated(user):
if callable(user.is_authenticated): # Django =< 1.9
return user.is_authenticated()
return user.is_authenticated


TEST_DESTINATIONS = {
'misc': {
'key': lambda original_filename: 'images/unique.jpg'
},
'files': {
'key': '/',
'auth': lambda u: u.is_staff
},
'imgs': {
'key': 'uploads/imgs',
'allowed': ['image/jpeg', 'image/png']
},
'thumbs': {
'key': 'uploads/thumbs',
'allowed': ['image/jpeg'],
'content_length_range': (1000, 50000)
},
'vids': {
'key': 'uploads/vids',
'auth': is_authenticated,
'allowed': ['video/mp4']
},
'cached': {
'key': 'uploads/vids',
'auth': is_authenticated,
'allowed': '*',
'acl': 'authenticated-read',
'bucket': 'astoragebucketname',
'cache_control': 'max-age=2592000',
'content_disposition': 'attachment',
'server_side_encryption': 'AES256'
},
'accidental-leading-slash': {
'key': '/directory/leading'
},
'accidental-trailing-slash': {
'key': 'directory/trailing/'
},
'region-cn': {
'key': 'uploads/vids',
'region': 'cn-north-1'
},
'region-eu': {
'key': 'uploads/vids',
'region': 'eu-west-1'
},
'region-default': {
'key': 'uploads/vids'
},
'key_args': {
'key': lambda original_filename, args: args + '/' + 'background.jpg',
'key_args': 'assets/backgrounds'
},
}


@override_settings(
S3DIRECT_DESTINATIONS=TEST_DESTINATIONS,
AWS_ACCESS_KEY_ID='123',
AWS_SECRET_ACCESS_KEY='123')

class WidgetTestCase(TestCase):
def setUp(self):
admin = User.objects.create_superuser('admin', '[email protected]', 'admin')
Expand All @@ -112,8 +31,23 @@ def test_urls(self):
self.assertEqual(resolved_url.view_name, 's3direct')

def test_widget_html(self):
expected = (
'<div class="s3direct" data-policy-url="/get_upload_params/" '
'data-signing-url="/get_aws_v4_signature/">\n'
' <a class="file-link" target="_blank" href=""></a>\n'
' <a class="file-remove" href="#remove">Remove</a>\n'
' <input class="csrf-cookie-name" type="hidden" value="csrftoken">\n'
' <input class="file-url" type="hidden" value="" id="" name="filename" />'
'\n'
' <input class="file-dest" type="hidden" value="foo">\n'
' <input class="file-input" type="file" style=""/>\n'
' <div class="progress progress-striped active">\n'
' <div class="bar"></div>\n'
' </div>\n'
'</div>\n')

widget = widgets.S3DirectWidget(dest='foo')
self.assertEqual(widget.render('filename', None), HTML_OUTPUT)
self.assertEqual(widget.render('filename', None), expected)

def test_get_upload_parameters_logged_in(self):
self.client.login(username='admin', password='admin')
Expand Down Expand Up @@ -280,7 +214,8 @@ def test_function_object_key_with_args(self):
policy_dict = json.loads(response.content.decode())
self.assertEqual(
policy_dict['object_key'],
TEST_DESTINATIONS['key_args']['key_args'] + '/' + data['name'])
settings.S3DIRECT_DESTINATIONS['key_args']['key_args'] + '/' +
data['name'])

def test_function_region_cn_north_1(self):
data = {
Expand Down Expand Up @@ -346,7 +281,6 @@ def test_policy_conditions(self):
self.assertEqual(policy_dict['server_side_encryption'], u'AES256')


@override_settings(AWS_ACCESS_KEY_ID='abc', AWS_SECRET_ACCESS_KEY='123')
class SignatureViewTestCase(TestCase):
EXAMPLE_SIGNING_DATE = datetime(2017, 4, 6, 8, 30)
EXPECTED_SIGNATURE = '76ea6730e10ddc9d392f40bf64872ddb1728cab58301dccb9efb67cb560a9272'
Expand Down Expand Up @@ -393,7 +327,7 @@ def test_signing(self):
data={
'to_sign': string_to_sign,
'datetime': datetime.strftime(signing_date, '%Y%m%dT%H%M%SZ'),
'dest': 'misc' # auth: not protected
'dest': 'not_protected' # auth: not protected
},
enforce_csrf_checks=True,
)
Expand All @@ -410,7 +344,7 @@ def test_signing_with_protected(self):
data={
'to_sign': string_to_sign,
'datetime': datetime.strftime(signing_date, '%Y%m%dT%H%M%SZ'),
'dest': 'files' # auth: is_staff
'dest': 'protected' # auth: is_staff
},
enforce_csrf_checks=True,
)
Expand All @@ -426,7 +360,7 @@ def test_signing_with_protected_without_valid_auth(self):
data={
'to_sign': string_to_sign,
'datetime': datetime.strftime(signing_date, '%Y%m%dT%H%M%SZ'),
'dest': 'files' # auth: is_staff
'dest': 'protected' # auth: is_staff
},
enforce_csrf_checks=True,
)
Expand Down

0 comments on commit f851549

Please sign in to comment.