From 89c89616d2f00aeb036e447722a69403265030d1 Mon Sep 17 00:00:00 2001 From: Jimmy Jia Date: Mon, 26 Sep 2016 18:46:57 -0400 Subject: [PATCH] Return Flask response directly for upload info --- flask_annex/s3.py | 10 +++++----- tests/conftest.py | 5 +++++ tests/helpers.py | 16 ++++++++++++++++ tests/test_file.py | 6 ++---- tests/test_s3.py | 45 +++++++++++++++++++++------------------------ 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/flask_annex/s3.py b/flask_annex/s3.py index bbb8b1e..bc748bc 100644 --- a/flask_annex/s3.py +++ b/flask_annex/s3.py @@ -112,8 +112,8 @@ def get_upload_info(self, key): ExpiresIn=self._url_expires_in, ) - return { - 'method': 'POST', - 'url': post_info['url'], - 'data': post_info['fields'], - } + return flask.jsonify( + method='POST', + url=post_info['url'], + data=post_info['fields'], + ) diff --git a/tests/conftest.py b/tests/conftest.py index efb9fb3..6a213ee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,3 +10,8 @@ def app(): app.config['TESTING'] = True return app + + +@pytest.fixture +def client(app): + return app.test_client() diff --git a/tests/helpers.py b/tests/helpers.py index 4ad8e68..8de3935 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,4 +1,5 @@ from io import BytesIO +import json import pytest @@ -13,6 +14,11 @@ def assert_key_value(annex, key, value): assert out_file.read() == value +def get_upload_info(client, key): + response = client.get('/upload_info/{}'.format(key)) + return json.loads(response.get_data(as_text=True)) + + # ----------------------------------------------------------------------------- @@ -23,6 +29,16 @@ def annex(self, annex_base): annex_base.save_file('foo/baz.json', BytesIO(b'2\n')) return annex_base + @pytest.fixture(autouse=True) + def routes(self, app, annex): + @app.route('/file/') + def file(key): + return annex.send_file(key) + + @app.route('/upload_info/') + def upload_info(key): + return annex.get_upload_info(key) + def test_get_file(self, annex): assert_key_value(annex, 'foo/bar.txt', b'1\n') diff --git a/tests/test_file.py b/tests/test_file.py index 02f37d0..e0ddd9a 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -26,10 +26,8 @@ def test_save_file_existing_dir(self, annex): annex.save_file('foo/qux.txt', BytesIO(b'6\n')) assert_key_value(annex, 'foo/qux.txt', b'6\n') - def test_send_file(self, app, annex): - with app.test_request_context(): - response = annex.send_file('foo/baz.json') - + def test_send_file(self, client): + response = client.get('/file/foo/baz.json') assert response.status_code == 200 assert response.mimetype == 'application/json' diff --git a/tests/test_s3.py b/tests/test_s3.py index 7afde97..45e5673 100644 --- a/tests/test_s3.py +++ b/tests/test_s3.py @@ -6,7 +6,7 @@ from flask_annex import Annex -from helpers import AbstractTestAnnex, assert_key_value +from helpers import AbstractTestAnnex, assert_key_value, get_upload_info # ----------------------------------------------------------------------------- @@ -60,33 +60,30 @@ def test_save_file_unknown_type(self, annex): annex.save_file('foo/qux', BytesIO(b'6\n')) assert_key_value(annex, 'foo/qux', b'6\n') - def test_send_file(self, app, annex): - with app.test_request_context(): - response = annex.send_file('foo/baz.json') - + def test_send_file(self, client): + response = client.get('/file/foo/baz.json') assert response.status_code == 302 - def test_get_upload_info(self, app, annex): - with app.app_context(): - upload_info = annex.get_upload_info('foo/qux.txt') - assert upload_info['method'] == 'POST' - assert upload_info['url'] == \ - 'https://flask-annex.s3.amazonaws.com/' - assert upload_info['data']['key'] == 'foo/qux.txt' - assert upload_info['data']['Content-Type'] == 'text/plain' - - conditions = get_policy(upload_info)['conditions'] - assert get_condition(conditions, 'bucket') == 'flask-annex' - assert get_condition(conditions, 'key') == 'foo/qux.txt' - assert get_condition(conditions, 'Content-Type') == 'text/plain' - - def test_get_upload_info_max_content_length(self, app, annex): + def test_get_upload_info(self, client): + upload_info = get_upload_info(client, 'foo/qux.txt') + + assert upload_info['method'] == 'POST' + assert upload_info['url'] == 'https://flask-annex.s3.amazonaws.com/' + assert upload_info['data']['key'] == 'foo/qux.txt' + assert upload_info['data']['Content-Type'] == 'text/plain' + + conditions = get_policy(upload_info)['conditions'] + assert get_condition(conditions, 'bucket') == 'flask-annex' + assert get_condition(conditions, 'key') == 'foo/qux.txt' + assert get_condition(conditions, 'Content-Type') == 'text/plain' + + def test_get_upload_info_max_content_length(self, app, client): app.config['MAX_CONTENT_LENGTH'] = 100 - with app.app_context(): - upload_info = annex.get_upload_info('foo/qux.txt') - conditions = get_policy(upload_info)['conditions'] - assert get_condition(conditions, 'content-length-range')[1] == 100 + upload_info = get_upload_info(client, 'foo/qux.txt') + + conditions = get_policy(upload_info)['conditions'] + assert get_condition(conditions, 'content-length-range') == [0, 100] class TestS3AnnexFromEnv(TestS3Annex):