2016-08-24 16:55:33 +00:00
|
|
|
import os
|
2018-06-01 21:07:15 +00:00
|
|
|
|
|
|
|
import pytest
|
2016-09-19 20:19:29 +00:00
|
|
|
import requests
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
from flask import Flask
|
2017-04-05 03:29:22 +00:00
|
|
|
from flask_testing import LiveServerTestCase
|
2016-09-19 20:19:29 +00:00
|
|
|
|
2016-08-24 16:55:33 +00:00
|
|
|
from storage import Storage
|
2016-09-19 20:19:29 +00:00
|
|
|
from util.security.instancekeys import InstanceKeys
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
from test.registry.liveserverfixture import *
|
|
|
|
from test.fixtures import *
|
|
|
|
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
@pytest.fixture(params=[True, False])
|
|
|
|
def is_proxying_enabled(request):
|
|
|
|
return request.param
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def server_executor(app):
|
|
|
|
def reload_app(server_hostname):
|
|
|
|
# Close any existing connection.
|
|
|
|
close_db_filter(None)
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
# Reload the database config.
|
|
|
|
app.config['SERVER_HOSTNAME'] = server_hostname[len('http://'):]
|
|
|
|
configure(app.config)
|
|
|
|
return 'OK'
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
executor = LiveServerExecutor()
|
|
|
|
executor.register('reload_app', reload_app)
|
|
|
|
return executor
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def liveserver_app(app, server_executor, init_db_path, is_proxying_enabled):
|
|
|
|
server_executor.apply_blueprint_to_app(app)
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
if os.environ.get('DEBUG') == 'true':
|
|
|
|
app.config['DEBUG'] = True
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
app.config['TESTING'] = True
|
|
|
|
app.config['INSTANCE_SERVICE_KEY_KID_LOCATION'] = 'test/data/test.kid'
|
|
|
|
app.config['INSTANCE_SERVICE_KEY_LOCATION'] = 'test/data/test.pem'
|
|
|
|
app.config['INSTANCE_SERVICE_KEY_SERVICE'] = 'quay'
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
app.config['FEATURE_PROXY_STORAGE'] = is_proxying_enabled
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
app.config['DISTRIBUTED_STORAGE_CONFIG'] = {
|
|
|
|
'test': ['FakeStorage', {}],
|
|
|
|
}
|
|
|
|
app.config['DISTRIBUTED_STORAGE_PREFERENCE'] = ['test']
|
|
|
|
return app
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def instance_keys(liveserver_app):
|
|
|
|
return InstanceKeys(liveserver_app)
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def storage(liveserver_app, instance_keys):
|
|
|
|
return Storage(liveserver_app, instance_keys=instance_keys)
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def app_reloader(liveserver, server_executor):
|
|
|
|
server_executor.on(liveserver).reload_app(liveserver.url)
|
|
|
|
yield
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
|
2018-06-13 16:00:04 +00:00
|
|
|
@pytest.mark.skipif(os.environ.get('TEST_DATABASE_URI') is not None,
|
2018-06-01 21:07:15 +00:00
|
|
|
reason="not supported for non SQLite testing")
|
|
|
|
def test_storage_proxy_auth(storage, liveserver_app, liveserver_session, is_proxying_enabled,
|
|
|
|
app_reloader):
|
|
|
|
# Activate direct download on the fake storage.
|
|
|
|
storage.put_content(['test'], 'supports_direct_download', 'true')
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
# Get the unwrapped URL.
|
|
|
|
direct_download_url = storage.get_direct_download_url(['test'], 'somepath')
|
|
|
|
proxy_index = direct_download_url.find('/_storage_proxy/')
|
|
|
|
if is_proxying_enabled:
|
|
|
|
assert proxy_index >= 0
|
|
|
|
else:
|
|
|
|
assert proxy_index == -1
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
# Ensure that auth returns the expected value.
|
|
|
|
headers = {
|
|
|
|
'X-Original-URI': direct_download_url[proxy_index:] if proxy_index else 'someurihere'
|
|
|
|
}
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2018-06-01 21:07:15 +00:00
|
|
|
resp = liveserver_session.get('_storage_proxy_auth', headers=headers)
|
2018-04-06 17:48:01 +00:00
|
|
|
assert resp.status_code == (500 if not is_proxying_enabled else 200)
|