From cd29651fb7f424134b842620071611ba78db1b30 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 26 2017 12:54:23 +0000 Subject: [PATCH 1/2] Specify for which production_version and decision_context no policies were found This it is the combo of both that is used to determine the policies to apply Signed-off-by: Pierre-Yves Chibon --- diff --git a/greenwave/api_v1.py b/greenwave/api_v1.py index 650bf17..b8ed6f2 100644 --- a/greenwave/api_v1.py +++ b/greenwave/api_v1.py @@ -96,7 +96,9 @@ def make_decision(): applicable_policies = [policy for policy in current_app.config['policies'] if policy.applies_to(decision_context, product_version)] if not applicable_policies: - raise NotFound('Cannot find any applicable policies for %s' % product_version) + raise NotFound( + 'Cannot find any applicable policies for %s and %s' % ( + product_version, decision_context)) subjects = [item for item in request.get_json()['subject'] if isinstance(item, dict)] if not subjects: raise BadRequest('Invalid subject, must be a list of dicts') From a62924c5b723c0690973d51ddb339bf9932deb2f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 26 2017 12:54:23 +0000 Subject: [PATCH 2/2] Extend the unit-tests for greenwave to check the error on invalid inputs Signed-off-by: Pierre-Yves Chibon --- diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 7af3b48..47082d2 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0+ +import json + from greenwave.app_factory import create_app from greenwave.policies import summarize_answers, RuleSatisfied, TestResultMissing, TestResultFailed @@ -27,3 +29,128 @@ def test_load_policies(): app.config['policies']) assert any(rule.test_case_name == 'dist.rpmdiff.analysis.abi_symbols' for policy in app.config['policies'] for rule in policy.rules) + + +def test_invalid_payload(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post('/api/v1.0/decision', data='not a json') + assert output.status_code == 415 + assert output.data == '{\n "message": "No JSON payload in request"\n}\n' + + +def test_missing_content_type(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({"subject": "foo"}), + ) + assert output.status_code == 415 + assert output.data == '{\n "message": "No JSON payload in request"\n}\n' + + +def test_missing_product_version(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({"subject": "foo"}), + headers={"content-type": "application/json"} + ) + assert output.status_code == 400 + assert output.data == '{\n "message": "Missing required product version"\n}\n' + + + +def test_missing_decision_context(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({"subject": "foo", "product_version": "f26"}), + headers={"content-type": "application/json"} + ) + assert output.status_code == 400 + assert output.data == '{\n "message": "Missing required decision context"\n}\n' + + +def test_missing_subject(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({ + "decision_context": "bodhi_push_stable", + "product_version": "f26" + }), + headers={"content-type": "application/json"} + ) + assert output.status_code == 400 + assert output.data == '{\n "message": "Missing required subject"\n}\n' + + +def test_invalid_subect_list(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({ + "decision_context": "bodhi_push_stable", + "product_version": "f26", + "subject": "foo", + }), + headers={"content-type": "application/json"} + ) + assert output.status_code == 400 + assert output.data == '{\n "message": "Invalid subject, must be a list of items"\n}\n' + + +def test_invalid_subect_list_content(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({ + "decision_context": "bodhi_update_push_stable", + "product_version": "fedora-26", + "subject": ["foo"], + }), + headers={"content-type": "application/json"} + ) + assert output.status_code == 400 + assert output.data == '{\n "message": "Invalid subject, must be a list of dicts"\n}\n' + + +def test_invalid_product_version(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({ + "decision_context": "bodhi_update_push_stable", + "product_version": "f26", + "subject": ["foo"], + }), + headers={"content-type": "application/json"} + ) + assert output.status_code == 404 + assert output.data == '{\n "message": "Cannot find any applicable '\ + 'policies for f26 and bodhi_update_push_stable"\n}\n' + + +def test_invalid_decision_context(): + app = create_app('greenwave.config.TestingConfig') + test_app = app.test_client() + output = test_app.post( + '/api/v1.0/decision', + data=json.dumps({ + "decision_context": "bodhi_update_push", + "product_version": "fedora-26", + "subject": ["foo"], + }), + headers={"content-type": "application/json"} + ) + assert output.status_code == 404 + assert output.data == '{\n "message": "Cannot find any applicable '\ + 'policies for fedora-26 and bodhi_update_push"\n}\n'