import boto from io import BytesIO from moto import mock_s3 from werkzeug.test import EnvironBuilder from werkzeug.wrappers import Request from bulkperformancerights.logic import fileupload from bulkperformancerights import config @mock_s3 def test_put_to_s3(): conn = boto.connect_s3() conn.create_bucket(config.bucket_name) # though we can mock the FileStorage object itself, the EnvironBuilder # takes care of the set up and tear down for us # from werkzeug.datastructures import FileStorage # from unittest import mock # test_file = mock.MagicMock(spec=FileStorage(filename='test.txt')) # test_file.data.return_value = (BytesIO(b'my file contents')) # FileStorage(filename='test.txt') # test_file.save('test_new.pdf') test_file_contents = BytesIO(b'my file contents') builder = EnvironBuilder(method='POST', data={ 'file': (test_file_contents, 'test.txt')}) env = builder.get_environ() req = Request(env) test_file = req.files['file'] user_path = 'vendor/1' uploaded_size = fileupload.put_to_s3(test_file, user_path) assert uploaded_size == test_file_contents.getbuffer().nbytes def test_allowed_file(): assert fileupload.allowed_file('foo.xlsx') def test_non_allowed_file(): for filename in ('foo.xls', 'fooxls'): assert not fileupload.allowed_file(filename)