From 1975050e491a7bd6ce54a9bf8877c4a1d737b119 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Thu, 8 Nov 2012 23:33:17 +0100 Subject: [PATCH] Basic proxy tests --- ckanext/resourceproxy/tests/file_server.py | 16 ++++ ckanext/resourceproxy/tests/static/test.json | 5 ++ ckanext/resourceproxy/tests/test_proxy.py | 82 ++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 ckanext/resourceproxy/tests/file_server.py create mode 100644 ckanext/resourceproxy/tests/static/test.json diff --git a/ckanext/resourceproxy/tests/file_server.py b/ckanext/resourceproxy/tests/file_server.py new file mode 100644 index 00000000000..fb8f2c31d0e --- /dev/null +++ b/ckanext/resourceproxy/tests/file_server.py @@ -0,0 +1,16 @@ +from flask import Flask +import os + +app = Flask(__name__, + static_folder=os.path.join(os.path.dirname( + os.path.realpath( __file__ )), + "static") + ) + + +@app.route("/", methods=['GET', 'POST']) +def ok(): + return 'ok' + +if __name__ == "__main__": + app.run(port=50001) diff --git a/ckanext/resourceproxy/tests/static/test.json b/ckanext/resourceproxy/tests/static/test.json new file mode 100644 index 00000000000..fbeda23ed5b --- /dev/null +++ b/ckanext/resourceproxy/tests/static/test.json @@ -0,0 +1,5 @@ +{ + "a": "foo", + "bar": "yes, I'm proxied", + "b": 42 +} \ No newline at end of file diff --git a/ckanext/resourceproxy/tests/test_proxy.py b/ckanext/resourceproxy/tests/test_proxy.py index e69de29bb2d..f20d01bbf79 100644 --- a/ckanext/resourceproxy/tests/test_proxy.py +++ b/ckanext/resourceproxy/tests/test_proxy.py @@ -0,0 +1,82 @@ +import os +import json +import subprocess +import requests +import time + +from nose.tools import assert_raises, assert_equal +import ckan.lib.helpers as h +import ckan.logic as l +import ckan.model as model +import ckan.tests as tests +import ckan.plugins as plugins +from ckan.lib.create_test_data import CreateTestData +from ckan.lib.dictization.model_dictize import resource_dictize + +import ckanext.resourceproxy.plugin as proxy + + +class TestProxyBasic(tests.WsgiAppCase): + + @classmethod + def setup_class(cls): + static_files_server = os.path.join(os.path.dirname(__file__), + 'file_server.py') + cls.static_files_server = subprocess.Popen( + ['python', static_files_server]) + + plugins.load('resourceproxy') + + # create test resource + CreateTestData.create() + testpackage = model.Package.get('annakarenina') + + # set the url to a static resource + resource_dict = resource_dictize(testpackage.resources[0], {'model': model}) + resource_dict['url'] = 'http://0.0.0.0:50001/static/test.json' + context = { + 'model': model, + 'session': model.Session, + 'user': model.User.get('testsysadmin').name + } + l.action.update.resource_update(context, resource_dict) + + testpackage = model.Package.get('annakarenina') + assert testpackage.resources[0].url == resource_dict['url'], testpackage.resources[0].url + + cls.data_dict = { + 'resource': { + 'id': testpackage.resources[0].id, + 'url': testpackage.resources[0].url + }, + 'package': { + 'name': testpackage.name + } + } + + #make sure services are running + for i in range(0, 50): + time.sleep(0.1) + response = requests.get('http://0.0.0.0:50001') + if not response: + continue + return + + cls.teardown_class() + raise Exception('services did not start!') + + @classmethod + def teardown_class(cls): + cls.static_files_server.kill() + plugins.reset() + + def test_resource_proxy(self): + url = self.data_dict['resource']['url'] + result = self.app.get(url, status=200) + assert result.status == 200, result.status + assert "yes, I'm proxied" in result.body, result.body + + proxied_url = proxy.get_proxified_resource_url(self.data_dict) + result = self.app.get(proxied_url).follow() + assert result.status == 200, result.status + assert "yes, I'm proxied" in result.body, result.body