From c729b4d88d37b6ef377b6a46cc3b1240bc9cba70 Mon Sep 17 00:00:00 2001 From: Mike Terzo Date: Fri, 23 Dec 2016 03:54:09 -0500 Subject: [PATCH] Adding testing for Puppetboard app using flask client. Adding offline / online mode testing for validation. This is the start of adding a ton of tests with the start to mocking for pypupppetdb --- requirements-test.txt | 1 + test/test_app.py | 96 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/requirements-test.txt b/requirements-test.txt index 5fc11d5..021a97c 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -8,3 +8,4 @@ pytest-mock==1.5.0 cov-core==1.15.0 unittest2==1.1.0; python_version < '2.7' bandit +beautifulsoup4==4.5.3 diff --git a/test/test_app.py b/test/test_app.py index 2fb3606..21d8565 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -1,8 +1,100 @@ import pytest -import tempfile - from puppetboard import app +from pypuppetdb.types import Node +from puppetboard import default_settings + +from bs4 import BeautifulSoup + + +class MockDbQuery(object): + def __init__(self, responses): + self.responses = responses + + def get(self, method, **kws): + resp = None + if method in self.responses: + resp = self.responses[method].pop(0) + return resp + + +@pytest.fixture +def mock_puppetdb_environments(mocker): + environemnts = [ + {'name': 'production'}, + {'name': 'staging'} + ] + return mocker.patch.object(app.puppetdb, 'environments', + return_value=environemnts) + + +@pytest.fixture +def mock_puppetdb_default_nodes(mocker): + node_list = [ + Node('_', 'node', + report_timestamp='2013-08-01T09:57:00.000Z', + latest_report_hash='1234567', + catalog_timestamp='2013-08-01T09:57:00.000Z', + facts_timestamp='2013-08-01T09:57:00.000Z',) + ] + return mocker.patch.object(app.puppetdb, 'nodes', + return_value=node_list) + + +@pytest.fixture +def client(): + client = app.app.test_client() + return client def test_first_test(): assert app is not None, ("%s" % reg.app) + + +def test_no_env(client, mock_puppetdb_environments): + rv = client.get('/nonexsistenv/') + + assert rv.status_code == 404 + + +def test_get_index(client, mocker, + mock_puppetdb_environments, + mock_puppetdb_default_nodes): + query_data = { + 'nodes': [[{'count': 10}]], + 'resources': [[{'count': 40}]], + } + + dbquery = MockDbQuery(query_data) + + mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get) + rv = client.get('/') + soup = BeautifulSoup(rv.data, 'html.parser') + assert soup.title.contents[0] == 'Puppetboard' + assert rv.status_code == 200 + + +def test_offline_mode(client, mocker): + app.app.config['OFFLINE_MODE'] = True + + mock_puppetdb_environments(mocker) + mock_puppetdb_default_nodes(mocker) + + query_data = { + 'nodes': [[{'count': 10}]], + 'resources': [[{'count': 40}]], + } + + dbquery = MockDbQuery(query_data) + + mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get) + rv = client.get('/') + soup = BeautifulSoup(rv.data, 'html.parser') + assert soup.title.contents[0] == 'Puppetboard' + for link in soup.find_all('link'): + assert "//" not in link['href'] + + for script in soup.find_all('script'): + if "src" in script.attrs: + assert "//" not in script['src'] + + assert rv.status_code == 200