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
This commit is contained in:
@@ -8,3 +8,4 @@ pytest-mock==1.5.0
|
|||||||
cov-core==1.15.0
|
cov-core==1.15.0
|
||||||
unittest2==1.1.0; python_version < '2.7'
|
unittest2==1.1.0; python_version < '2.7'
|
||||||
bandit
|
bandit
|
||||||
|
beautifulsoup4==4.5.3
|
||||||
|
|||||||
@@ -1,8 +1,100 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from puppetboard import app
|
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():
|
def test_first_test():
|
||||||
assert app is not None, ("%s" % reg.app)
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user