From 5fe3e9888c15e959e3c0ed76fec794f1ca62b312 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: May 02 2018 07:45:15 +0000 Subject: tests: move API test cases out of test_policies.py Some test cases covering parts of the HTTP API had crept into test_policies.py, which is supposed to just contain test cases covering policy loading. Moved them into test_api_v1.py and removed some redundant overlap. --- diff --git a/functional-tests/test_api_v1.py b/functional-tests/test_api_v1.py index b2e2291..4cde16c 100644 --- a/functional-tests/test_api_v1.py +++ b/functional-tests/test_api_v1.py @@ -2,6 +2,8 @@ import json +from greenwave import __version__ + all_rpmdiff_testcase_names = [ 'dist.rpmdiff.analysis.abi_symbols', @@ -115,6 +117,19 @@ def test_inspect_policies(requests_session, greenwave_server): assert any(p['rules'] == expected_rules for p in policies) +def test_version_endpoint(requests_session, greenwave_server): + r = requests_session.get(greenwave_server + 'api/v1.0/version') + assert r.status_code == 200 + assert {'version': __version__} == r.json() + + +def test_version_endpoint_jsonp(requests_session, greenwave_server): + r = requests_session.get(greenwave_server + 'api/v1.0/version?callback=bac123') + assert r.status_code == 200 + assert 'bac123' in r.text + assert '"version": "%s"' % __version__ in r.text + + def test_cannot_make_decision_without_product_version(requests_session, greenwave_server): data = { 'decision_context': 'errata_newfile_to_qe', @@ -155,6 +170,17 @@ def test_cannot_make_decision_with_invalid_subject(requests_session, greenwave_s data = { 'decision_context': 'errata_newfile_to_qe', 'product_version': 'rhel-7', + 'subject': 'foo-1.0.0-1.el7', + } + r = requests_session.post(greenwave_server + 'api/v1.0/decision', + headers={'Content-Type': 'application/json'}, + data=json.dumps(data)) + assert r.status_code == 400 + assert 'Invalid subject, must be a list of items' == r.json()['message'] + + data = { + 'decision_context': 'errata_newfile_to_qe', + 'product_version': 'rhel-7', 'subject': ['foo-1.0.0-1.el7'], } r = requests_session.post(greenwave_server + 'api/v1.0/decision', @@ -164,18 +190,48 @@ def test_cannot_make_decision_with_invalid_subject(requests_session, greenwave_s assert u'Invalid subject, must be a list of dicts' in r.text -def test_404_for_inapplicable_policies(requests_session, greenwave_server): +def test_404_for_invalid_product_version(requests_session, greenwave_server): data = { - 'decision_context': 'dummy_decision', - 'product_version': 'rhel-7', + 'decision_context': 'bodhi_push_update_stable', + 'product_version': 'f26', + 'subject': [{'item': 'foo-1.0.0-1.el7', 'type': 'koji_build'}] + } + r = requests_session.post(greenwave_server + 'api/v1.0/decision', + headers={'Content-Type': 'application/json'}, + data=json.dumps(data)) + assert r.status_code == 404 + expected = u'Cannot find any applicable policies for f26 and bodhi_push_update_stable' + assert expected == r.json()['message'] + + +def test_404_for_invalid_decision_context(requests_session, greenwave_server): + data = { + 'decision_context': 'bodhi_push_update', # missing the _stable part! + 'product_version': 'fedora-26', 'subject': [{'item': 'foo-1.0.0-1.el7', 'type': 'koji_build'}] } r = requests_session.post(greenwave_server + 'api/v1.0/decision', headers={'Content-Type': 'application/json'}, data=json.dumps(data)) assert r.status_code == 404 - expected = u'Cannot find any applicable policies ' + \ - 'for rhel-7 and dummy_decision' + expected = u'Cannot find any applicable policies for fedora-26 and bodhi_push_update' + assert expected == r.json()['message'] + + +def test_415_for_missing_request_content_type(requests_session, greenwave_server): + r = requests_session.post(greenwave_server + 'api/v1.0/decision', + data=json.dumps({})) + assert r.status_code == 415 + expected = "No JSON payload in request" + assert expected == r.json()['message'] + + +def test_invalid_payload(requests_session, greenwave_server): + r = requests_session.post(greenwave_server + 'api/v1.0/decision', + headers={'Content-Type': 'application/json'}, + data='not a json') + assert r.status_code == 400 + expected = "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)" assert expected == r.json()['message'] diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 614b6a3..d54f410 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -1,10 +1,8 @@ # SPDX-License-Identifier: GPL-2.0+ -import json import pytest -from greenwave import __version__ from greenwave.app_factory import create_app from greenwave.policies import ( summarize_answers, @@ -168,130 +166,6 @@ def test_load_policies(): 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 "No JSON payload in request" in output.data - - -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 "No JSON payload in request" in output.data - - -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"}), - content_type='application/json' - ) - assert output.status_code == 400 - assert "Missing required product version" in output.data - - -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"}), - content_type='application/json' - ) - assert output.status_code == 400 - assert "Missing required decision context" in output.data - - -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" - }), - content_type='application/json' - ) - assert output.status_code == 400 - assert "Missing required subject" in output.data - - -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", - }), - content_type='application/json' - ) - assert output.status_code == 400 - assert "Invalid subject, must be a list of items" in output.data - - -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"], - }), - content_type='application/json' - ) - assert output.status_code == 400 - assert "Invalid subject, must be a list of dicts" in output.data - - -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"], - }), - content_type='application/json' - ) - assert output.status_code == 404 - assert "Cannot find any applicable policies " \ - "for f26 and bodhi_update_push_stable" in output.data - - -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"], - }), - content_type='application/json' - ) - assert output.status_code == 404 - assert "Cannot find any applicable policies " \ - "for fedora-26 and bodhi_update_push" in output.data - - def test_misconfigured_policies(tmpdir): p = tmpdir.join('fedora.yaml') p.write(""" @@ -338,29 +212,6 @@ rules: load_policies(tmpdir.strpath) -def test_version_endpoint(): - app = create_app('greenwave.config.TestingConfig') - test_app = app.test_client() - output = test_app.get( - '/api/v1.0/version', - headers={"content-type": "application/json"} - ) - assert output.status_code == 200 - assert '"version": "%s"' % __version__ in output.data - - -def test_version_endpoint_jsonp(): - app = create_app('greenwave.config.TestingConfig') - test_app = app.test_client() - output = test_app.get( - '/api/v1.0/version?callback=bac123', - headers={"content-type": "application/json"} - ) - assert output.status_code == 200 - assert 'bac123' in output.data - assert '"version": "%s"' % __version__ in output.data - - def test_product_versions_pattern(tmpdir): p = tmpdir.join('fedora.yaml') p.write("""