From 4ada325ee1c7140775419dd1e93fb421b2aa57be Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Sun, 12 Jun 2022 13:02:23 +0200 Subject: [PATCH] Test slightly more for Query - separately table and raw JSON view, although we can't test the actual response as in both cases it's generated with JavaScript --- test/views/test_query.py | 60 ++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/test/views/test_query.py b/test/views/test_query.py index 653fb427..bfb0a8f0 100644 --- a/test/views/test_query.py +++ b/test/views/test_query.py @@ -18,23 +18,23 @@ def test_query_view(client, mocker, assert len(vals) == 0 -def test_query__some_response(client, mocker, - mock_puppetdb_environments, - mock_puppetdb_default_nodes): +def test_query__some_response__table(client, mocker, + mock_puppetdb_environments, + mock_puppetdb_default_nodes): app.app.config['WTF_CSRF_ENABLED'] = False - query_data = [ - {'certname': 'foobar'}, + result = [ + {'certname': 'foobar', 'catalog_environment': 'qa'}, ] - mocker.patch.object(app.puppetdb, '_query', return_value=query_data) + mocker.patch.object(app.puppetdb, '_query', return_value=result) - data = { + query_data = { 'query': 'nodes[certname] { certname = "foobar" }', 'endpoints': 'pql', } rv = client.post( '/query', - data=data, + data=query_data, content_type='application/x-www-form-urlencoded', ) @@ -48,7 +48,49 @@ def test_query__some_response(client, mocker, vals = soup.find_all('p', {"id": "number_of_results"}) assert len(vals) == 1 - assert str(len(query_data)) in vals[0].string + assert 'Number of results: 1' in vals[0].string + + vals = soup.find_all('table', {"id": "query_table"}) + assert len(vals) == 1 + # we can't test more here as the content of this table is generated with JavaScript... + + +def test_query__some_response__json(client, mocker, + mock_puppetdb_environments, + mock_puppetdb_default_nodes): + app.app.config['WTF_CSRF_ENABLED'] = False + + result = [ + {'certname': 'foobar', 'catalog_environment': 'qa'}, + ] + mocker.patch.object(app.puppetdb, '_query', return_value=result) + + query_data = { + 'query': 'nodes[certname] { certname = "foobar" }', + 'endpoints': 'pql', + 'rawjson': 'y', + } + rv = client.post( + '/query', + data=query_data, + content_type='application/x-www-form-urlencoded', + ) + + assert rv.status_code == 200 + + soup = BeautifulSoup(rv.data, 'html.parser') + assert soup.title.contents[0] == 'Puppetboard' + + vals = soup.find_all('h2', {"id": "results_header"}) + assert len(vals) == 1 + + vals = soup.find_all('p', {"id": "number_of_results"}) + assert len(vals) == 1 + assert 'Number of results: 1' in vals[0].string + + vals = soup.find_all('pre', {"id": "result"}) + assert len(vals) == 1 + # we can't test more here as the content of this tag is generated with JavaScript... def test_query__empty_response(client, mocker,