def test_render(): ''' Tests the render function ''' test_resp = FlaskResponse() data_dict = {"test": test_resp, "file": "home.html.j2"} # Test passing data without the FlaskResponse with app.test_request_context('/home'): resp = template.render(data_dict) assert isinstance(resp, FlaskResponse) # Test passing in an invalid template data_dict["file"] = "not-there-file.html.j2" with app.test_request_context('/home'): with raises(HTTPException) as http_exc: resp = template.render(data_dict) assert http_exc.type.code == 501 # Test with a syntax error in the template data_dict["file"] = "invalid.html.j2" with app.test_request_context('/home'): with raises(HTTPException) as http_exc: resp = template.render(data_dict) assert http_exc.type.code == 500 # Test a template render where a file was not given del data_dict["file"] with app.test_request_context('/home'): with raises(HTTPException) as http_exc: resp = template.render(data_dict) assert http_exc.type.code == 500
def test_match_request_format(): # Test standard search request using default format with app.test_request_context('/search?q=elephant'): result_format = request.match_request_format( "format", ["text/html", "application/json"]) assert result_format == "text/html" # Test standard search requesting json response with '.json' with app.test_request_context('/search.json?q=elephant'): result_format = request.match_request_format( "format", ["text/html", "application/json"]) assert result_format == "application/json" # Test standard search requesting json via Accept header with app.test_request_context('/search?q=elephant', headers=[("Accept", "application/json")]): result_format = request.match_request_format( "format", ["text/html", "application/json"]) assert result_format == "application/json" # Test passing in invalid format with app.test_request_context('/search.xml?q=elephant'): with raises(HTTPException) as http_error: result_format = request.match_request_format( "format", ["text/html", "application/json"]) assert http_error.type.code == 501
def test_search(): data = { "name": "search", "processor": "solr.search", "paths": ['config/search/main.json'] } # Test for successful request with a dict response with app.test_request_context('/search'): response = solr.search(data, url="https://test.example.edu", api_get_function=_test_api_get_json) assert isinstance(response, dict) assert response # Test for successful request with a jsonify'ed response with app.test_request_context('/search.json'): response = solr.search(data, url="https://test.example.edu", api_get_function=_test_api_get_json) assert isinstance(response, FlaskResponse) assert response.status_code == 200 # Test for passing a url parameter to overrride default with app.test_request_context('/search?q=changed&start=20'): response = solr.search(data, url="https://test.example.edu", api_get_function=_test_api_get_json) assert isinstance(response, dict) assert response # Test for missing solr params with app.test_request_context('/search'): data['paths'] = ['config/search/invalid.json'] with raises(HTTPException) as http_error: response = solr.search(data, url="https://test.example.edu", api_get_function=_test_api_get_json) assert http_error.type.code == 500 # Test for missing paths in data del data['paths'] with app.test_request_context('/search.json'): with raises(HTTPException) as http_error: response = solr.search(data, url="https://test.example.edu", api_get_function=_test_api_get_json) assert http_error.type.code == 500 data["paths"] = ['config/search/main.json']
def test_eval_when(): route_data = OrderedDict({ "name": "test_eval_when", "when": "True" }) loaded_data = {}; with app.test_request_context('/dummy'): assert base.eval_when(route_data, loaded_data) is True route_data['when'] = "{{ 1 == 1 }}" assert base.eval_when(route_data, loaded_data) is True route_data['when'] = "{{ 1 == 0 }}" assert base.eval_when(route_data, loaded_data) is False route_data['when'] = "{{ 1 == 0 }}" assert base.eval_when(route_data, loaded_data) is False route_data['when'] = "{{ mylist[0] == 1 }}" with raises(HTTPException) as http_error: base.eval_when(route_data, loaded_data) assert 500 == http_error.type.code route_data['when'] = "{{ invalid.var }}" with raises(HTTPException) as http_error: base.eval_when(route_data, loaded_data) assert 500 == http_error.type.code route_data['when'] = "{{ test } bad curlies" with raises(HTTPException) as http_error: base.eval_when(route_data, loaded_data) assert 500 == http_error.type.code
def test_stream(): ''' Tests the stream function ''' test_resp = RequestsResponse() test_resp.raw = io.StringIO("This is a test") test_resp.status_code = 200 test_resp.headers['Content-Type'] = 'application/json' test_resp.headers['Range'] = '0-31' test_resp.headers['Invalid-Header'] = '10.0.0.10' data = {"test": test_resp, "response": 'test', "on_fail": 0} with app.test_request_context('/home'): # Test passing it the correct information resp = stream.response(data) assert test_resp.status_code == resp.status_code assert isinstance(resp, FlaskResponse) assert test_resp.headers['Content-Type'] == resp.headers[ 'Content-Type'] assert test_resp.headers['Range'] == resp.headers['Range'] # Test valid RequestsResponse, but >= 400 http code test_resp.status_code = 401 del data['on_fail'] assert stream.response(data) == None # Test not a valid RequestResponse data["test"] = "string is not allowed" assert stream.response(data) == None # Test returning a non-OK status code data['test'] = test_resp data['on_fail'] = 0 test_resp.status_code = 400 with raises(HTTPException) as http_exc: resp = stream.response(data) assert http_exc.type.code == 400 # Test not giving the correct key in data data['response'] = 'invalid' with raises(HTTPException) as http_exc: resp = stream.response(data) assert http_exc.type.code == 503 # Test giving no data in the key data["test2"] = None data['response'] = 'test2' with raises(HTTPException) as http_exc: resp = stream.response(data) assert http_exc.type.code == 503 # Test disallowed header assert 'Invalid-Header' not in resp.headers # Test missing 'response' key in data del data['response'] with raises(HTTPException) as http_exc: resp = stream.response(data) assert http_exc.type.code == 500
def test_handle_http_abort(): ''' Test the handle_http_abort function ''' # Test an exception with a accpet header of json with app.test_request_context("/", headers={'Accept':'application/json'}): resp, code = error.handle_http_abort(NotFound()) assert resp.headers['Content-Type'] == 'application/json' assert resp.get_json()['code'] == 404 assert code == 404 # Test an exception with no extra headers passed with app.test_request_context("/"): resp, code = error.handle_http_abort(NotFound()) assert isinstance(resp, str) assert code == 404 assert "404 Not Found" in resp
def test_urlcomponents_context_processor(): ctx = context.context_processors() # testing a basic path to make sure url components are collected, assembled, passed correctly with app.test_request_context('/etd/1000'): app.preprocess_request() result = ctx['urlcomponents']() assert isinstance(result, dict) assert result['path'] == '/etd/1000' assert result['full_path'] == '/etd/1000?' assert result['base_url'] == flask.request.url_root + 'etd/1000' assert result['url'] == flask.request.url_root + 'etd/1000' assert result['url_root'] == flask.request.url_root assert isinstance(result['query_args'], dict) assert not result['query_args'] # testing a path with a query to make sure the parts are structured correctly with app.test_request_context('/search?q=frogs&fq=absek&fq=chocolate'): app.preprocess_request() result = ctx['urlcomponents']() assert isinstance(result, dict) assert result['path'] == '/search' assert result['full_path'] == '/search?q=frogs&fq=absek&fq=chocolate' assert result['base_url'] == flask.request.url_root + 'search' assert result[ 'url'] == flask.request.url_root + 'search?q=frogs&fq=absek&fq=chocolate' assert isinstance(result['query_args'], dict) assert 'q' in result['query_args'] assert result['query_args']['q'] == ['frogs'] assert len(result['query_args']) == 2 assert 'fq' in result['query_args'] assert result['query_args']['fq'] == ['absek', 'chocolate'] # testing that a missing request fails correctly with raises(RuntimeError) as run_error: result = ctx['urlcomponents']()
def test_overlay_with_query_args(): # Test configs analagous to those in config/search/ query_config = { "q": { "base": ["elephant"] }, "rows": { "default": 20, "max": 100 }, "rows_min": { "default": 20, "min": 5 }, "start": { "default": 0 }, "fl": { "default": ["PID", "title"], "base": "PID" }, "q.alt": { "default": "" }, "fq": { "default": ["name:Pat"] } } # Passing in params to combine with default configs params = { "fq": "title:Hello", "q": "*", "json.facet": { "type": "terms", "field": "subject" } } query_params = request.overlay_with_query_args(query_config, request_args=params) assert ["title:Hello"] == query_params["fq"] assert len(query_params["fq"]) == 1 assert query_params["q"] == ["elephant"] assert "json.facet" not in query_params # Allowing unknown fields query_params = request.overlay_with_query_args(query_config, request_args=params, allow_undefined=True) assert "json.facet" in query_params # Base values will override user-input params; max is implemented; default is overwritten; lists are combined as expected with app.test_request_context( '/search?q=antelope&rows=120&rows_min=abc&start=5&fl=author&fl=date' ): query_params = request.overlay_with_query_args(query_config) assert query_params["q"] == ['elephant'] assert query_params["rows"] == ['100'] assert query_params["start"] == ['5'] assert "PID" in query_params["fl"] assert "author" in query_params["fl"] assert "date" in query_params["fl"] assert "title" not in query_params["fl"] assert query_params["rows_min"] == ["5"] # Min is applied correctly; that defaults are used if not overwritten by user; that only unique values are returned; # that empty strings don't get included from configs; that user params not in configs are ignored. with app.test_request_context('/search?rows=1&rows_min=1&hello=world'): query_params = request.overlay_with_query_args(query_config) assert query_params["rows"] == ['1'] assert query_params["rows_min"] == ['5'] assert "PID" in query_params["fl"] assert "title" in query_params["fl"] assert len(query_params["fl"]) == 2 assert "hello" not in query_params assert "q.alt" not in query_params assert query_params["q"] == ["elephant"]
def test_load_route_data(): route_data = [ OrderedDict({ "processor": "file.load_json", "name": "search_conf", "on_fail": 501, "paths": [ "config/search/main.json" ] }), OrderedDict({ "processor": "template.render_string", "name": "string1", "value": "A MISING STRING", "when": "{{ 1 == 0 }}" }), OrderedDict({ "processor": "template.render_string", "name": "string2", "value": "A REAL STRING", "when": "{{ 1 == 1 }}" }) ] # Test positive route data load with app.test_request_context('/etd/1000'): app.preprocess_request() loaded = base.load_route_data(route_data) assert isinstance(loaded, dict) assert loaded # Validate when conditions assert 'string1' not in loaded assert 'string2' in loaded assert loaded['string2'] == "A REAL STRING" # Test of the on fail error code is valid route_data = [ OrderedDict({ "processor": "request.invalid_action", "name": "urlcomponents", "on_fail": 7000 }) ] with app.test_request_context('/etd/1000'): with raises(LookupError) as lookup_error: loaded = base.load_route_data(route_data) assert "no exception for" in str(lookup_error.value) # Test invalid on_fail route_data = [ OrderedDict({ "processor": "file.invalid_teapot", "name": "invalid", "on_fail": "teapot" }) ] with app.test_request_context('/etd/1000'): with raises(HTTPException) as http_error: loaded = base.load_route_data(route_data) assert 500 == http_error.type.code # Test loading invalid processors route_data = [ OrderedDict({ "processor": "fake.invalid", "name": "invalid" }) ] with app.test_request_context('/etd/1000'): loaded = base.load_route_data(route_data) assert isinstance(loaded, dict) del loaded['view_args'] assert not loaded # Test invalid function on valid processor with valid on_fail set route_data = [ OrderedDict({ "processor": "file.invalid_teapot", "name": "invalid", "on_fail": 418 }) ] with app.test_request_context('/etd/1000'): with raises(HTTPException) as http_error: loaded = base.load_route_data(route_data) assert 418 == http_error.type.code # Test badly formed route (invalid keys) route_data = [ OrderedDict({ "processor": "file-invalid_teapot", "name": "invalid" }) ] with app.test_request_context('/etd/1000'): loaded = base.load_route_data(route_data) assert isinstance(loaded, dict) del loaded['view_args'] assert not loaded # Test invalid when condition route_data = [ OrderedDict({ "processor": "template.render_string", "name": "string", "value": "dummy", "when": "NOT A TYPE" }) ] with app.test_request_context('/etd/1000'): with raises(HTTPException) as http_error: loaded = base.load_route_data(route_data) assert 500 == http_error.type.code # Test invalid char in request, causing a JSON decode failure route_data = [ OrderedDict({ "processor": "template.render_string", "name": "string", "value": "{{ view_args.namespace }}", }) ] with app.test_request_context('/dummy'): request.view_args = { 'namespace': "a\\.aspx" } with raises(HTTPException) as http_error: loaded = base.load_route_data(route_data) assert 400 == http_error.type.code