mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 22:14:25 +01:00
Fix syncing down an empty S3 bucket (#783)
* Fix syncing down an empty S3 bucket * Add test for S3 uploader * Remove boto.cfg because of https://github.com/travis-ci/travis-ci/issues/7940#issuecomment-310759657 * Specify dummy AWS creds in travis.yml * Fix S3Uploader in Python 3 and fix test
This commit is contained in:
59
tests/utils/test_uploaders.py
Normal file
59
tests/utils/test_uploaders.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import boto3
|
||||
from moto import mock_s3
|
||||
from tests.helpers import *
|
||||
from CTFd.utils.uploads import S3Uploader, FilesystemUploader, rmdir
|
||||
from CTFd.utils import binary_type
|
||||
from six import BytesIO
|
||||
import os
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_s3_uploader():
|
||||
conn = boto3.resource('s3', region_name='us-east-1')
|
||||
conn.create_bucket(Bucket='bucket')
|
||||
|
||||
app = create_ctfd()
|
||||
with app.app_context():
|
||||
app.config['UPLOAD_PROVIDER'] = 's3'
|
||||
app.config['AWS_ACCESS_KEY_ID'] = 'AKIAIOSFODNN7EXAMPLE'
|
||||
app.config['AWS_SECRET_ACCESS_KEY'] = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
|
||||
app.config['AWS_S3_BUCKET'] = 'bucket'
|
||||
|
||||
uploader = S3Uploader()
|
||||
|
||||
assert uploader.s3
|
||||
assert uploader.bucket == 'bucket'
|
||||
|
||||
fake_file = BytesIO('fakedfile'.encode())
|
||||
path = uploader.upload(fake_file, 'fake_file.txt')
|
||||
|
||||
assert 'fake_file.txt' in uploader.download(path).location
|
||||
destroy_ctfd(app)
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_s3_sync():
|
||||
conn = boto3.resource('s3', region_name='us-east-1')
|
||||
conn.create_bucket(Bucket='bucket')
|
||||
|
||||
app = create_ctfd()
|
||||
with app.app_context():
|
||||
app.config['UPLOAD_PROVIDER'] = 's3'
|
||||
app.config['AWS_ACCESS_KEY_ID'] = 'AKIAIOSFODNN7EXAMPLE'
|
||||
app.config['AWS_SECRET_ACCESS_KEY'] = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
|
||||
app.config['AWS_S3_BUCKET'] = 'bucket'
|
||||
|
||||
uploader = S3Uploader()
|
||||
uploader.sync()
|
||||
|
||||
fake_file = BytesIO('fakedfile'.encode())
|
||||
path = uploader.upload(fake_file, 'fake_file.txt')
|
||||
full_path = os.path.join(app.config['UPLOAD_FOLDER'], path)
|
||||
|
||||
try:
|
||||
uploader.sync()
|
||||
with open(full_path) as f:
|
||||
assert f.read() == 'fakedfile'
|
||||
finally:
|
||||
rmdir(os.path.dirname(full_path))
|
||||
destroy_ctfd(app)
|
||||
Reference in New Issue
Block a user