Skip to content

Commit

Permalink
Merge pull request #30 from 4Catalyzer/upload_info
Browse files Browse the repository at this point in the history
Return Flask response directly for upload info
  • Loading branch information
taion authored Sep 26, 2016
2 parents 7b76a22 + 89c8961 commit 17e81ba
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
10 changes: 5 additions & 5 deletions flask_annex/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def get_upload_info(self, key):
ExpiresIn=self._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'],
)
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ def app():
app.config['TESTING'] = True

return app


@pytest.fixture
def client(app):
return app.test_client()
16 changes: 16 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from io import BytesIO
import json

import pytest

Expand All @@ -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))


# -----------------------------------------------------------------------------


Expand All @@ -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/<path:key>')
def file(key):
return annex.send_file(key)

@app.route('/upload_info/<path:key>')
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')

Expand Down
6 changes: 2 additions & 4 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
45 changes: 21 additions & 24 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# -----------------------------------------------------------------------------

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 17e81ba

Please sign in to comment.