2017-04-21 19:54:15 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-04-21 20:13:18 +00:00
|
|
|
from mock import Mock
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
from data.userfiles import DelegateUserfiles, Userfiles
|
2017-04-24 17:49:29 +00:00
|
|
|
from test.fixtures import *
|
2017-04-21 20:13:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('prefix,path,expected', [
|
|
|
|
('test', 'foo', 'test/foo'),
|
|
|
|
('test', 'bar', 'test/bar'),
|
|
|
|
('test', '/bar', 'test/bar'),
|
|
|
|
('test', '../foo', 'test/foo'),
|
|
|
|
('test', 'foo/bar/baz', 'test/baz'),
|
|
|
|
('test', 'foo/../baz', 'test/baz'),
|
|
|
|
|
|
|
|
(None, 'foo', 'foo'),
|
|
|
|
(None, 'foo/bar/baz', 'baz'),
|
2017-04-21 19:54:15 +00:00
|
|
|
])
|
2017-04-21 20:13:18 +00:00
|
|
|
def test_filepath(prefix, path, expected):
|
|
|
|
userfiles = DelegateUserfiles(None, None, 'local_us', prefix)
|
2017-04-21 19:54:15 +00:00
|
|
|
assert userfiles.get_file_id_path(path) == expected
|
2017-04-21 20:13:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_lookup_userfile(app, client):
|
|
|
|
uuid = 'deadbeef-dead-beef-dead-beefdeadbeef'
|
|
|
|
bad_uuid = 'deadduck-dead-duck-dead-duckdeadduck'
|
|
|
|
upper_uuid = 'DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF'
|
|
|
|
|
|
|
|
def _stream_read_file(locations, path):
|
|
|
|
if path.find(uuid) > 0 or path.find(upper_uuid) > 0:
|
|
|
|
return BytesIO("hello world")
|
|
|
|
|
|
|
|
raise IOError('Not found!')
|
|
|
|
|
|
|
|
storage_mock = Mock()
|
|
|
|
storage_mock.stream_read_file = _stream_read_file
|
|
|
|
|
|
|
|
app.config['USERFILES_PATH'] = 'foo'
|
|
|
|
Userfiles(app, distributed_storage=storage_mock)
|
|
|
|
|
|
|
|
rv = client.open('/userfiles/' + uuid, method='GET')
|
|
|
|
assert rv.status_code == 200
|
|
|
|
|
|
|
|
rv = client.open('/userfiles/' + upper_uuid, method='GET')
|
|
|
|
assert rv.status_code == 200
|
|
|
|
|
|
|
|
rv = client.open('/userfiles/' + bad_uuid, method='GET')
|
|
|
|
assert rv.status_code == 404
|
|
|
|
|
|
|
|
rv = client.open('/userfiles/foo/bar/baz', method='GET')
|
|
|
|
assert rv.status_code == 404
|