def test_2_login(self): print '' print 'test login/logout process' with self.app: print 'get login page' rv = self.app.get('/login') assert '<form id="login"' in rv.data print 'login refused - credentials' rv = self.login('admin', 'default') assert 'Invalid credentials: username is unknown or password is invalid.' in rv.data print 'login refused - credentials' rv = self.login('admin', '') assert 'Invalid credentials: username is unknown or password is invalid.' in rv.data print 'login accepted - home page' rv = self.login('admin', 'admin') assert '<title>Home page</title>' in rv.data print 'login accepted - user attributes' assert current_user.username == 'admin' print 'user:'******'user name:', current_user.get_name() print 'token:', current_user.get_auth_token() print 'username:'******'user role:', current_user.get_role() print 'user picture:', current_user.get_picture() print 'admin:', current_user.can_admin() print 'action:', current_user.can_action() print 'reload home page' rv = self.app.get('/') assert '<title>Home page</title>' in rv.data print 'reload home page' rv = self.app.get('/?search=test') assert '<title>Home page</title>' in rv.data print 'reload home page' rv = self.app.get('/index') assert '<title>Home page</title>' in rv.data print 'refresh header' rv = self.app.get('/refresh_header') assert 'html_livesynthesis' in rv.data print 'refresh livestate' rv = self.app.get('/refresh_livestate') assert 'livestate' in rv.data print 'refresh livesynthesis' rv = self.app.get('/livesynthesis') assert 'livesynthesis' in rv.data print 'logout - go to login page' rv = self.logout() assert '<form id="login"' in rv.data
def get_html_livestate(self, bi=-1, search_filter=None): """ Get HTML formatted live state Update system live synthesis and build header elements :param bi: business impact :type bi: int :return: hosts_states and services_states HTML strings in a dictionary :rtype: dict """ parameters = {} # if bi: # parameters.update({"where": '{"bi":%d}' % bi}) items = self.get_livestate(parameters=parameters) logger.debug( "get_html_livestate, livestate %d (%s), %d elements", bi, search_filter, len(items) ) if bi != -1: items = [item for item in items if item['bi'] == bi] logger.debug( "get_html_livestate, livestate %d (%s), %d elements", bi, search_filter, len(items) ) if search_filter: items = self.search_livestate(items, search=search_filter) rows = [] current_host = '' for item in items: elt_id = self.get_html_id(item['type'], item['name']) host_url = '' if current_host != item['host_name']['host_name']: current_host = item['host_name']['host_name'] host_url = self.get_html_url("host", item['host_name']['host_name']) service_url = 'Host check' if item['type'] == "service": service_url = self.get_html_url( "service", item['service_description']['service_description'] ) long_output = '' if item['long_output']: long_output = """ <button type="button" class="btn btn-xs btn-info" data-toggle="popover" title="Long output" data-content="Long check output ..."> %s </button> """ % item['long_output'] tr = """ <tr data-toggle="collapse" data-target="#details-%s" class="accordion-toggle"> <td> <input id="selector-%s" type="checkbox" class="input-sm" value="" data-type="problem" data-business-impact="0" data-item="xxxx"> </td> <td> %s </td> <td> %s </td> <td> %s </td> <td class="font-%s"><strong>%s</strong></td> <td class="hidden-sm hidden-xs"> %s </td> <td class="hidden-sm hidden-xs"> %s %s </td> </tr>""" % ( elt_id, elt_id, self.get_html_state(item["type"], item["state"]), host_url, service_url, item['state'].lower(), item['state'], self.print_duration(item['last_state_changed'], duration_only=True, x_elts=2), item['output'], long_output ) rows.append(tr) if item['type'] == "host": passive_checks_enabled = item['host_name']['passive_checks_enabled'] freshness_threshold = item['host_name']['freshness_threshold'] active_checks_enabled = item['host_name']['active_checks_enabled'] else: passive_checks_enabled = item['service_description']['passive_checks_enabled'] freshness_threshold = item['service_description']['freshness_threshold'] active_checks_enabled = item['service_description']['passive_checks_enabled'] tr2 = """ <tr id="details-%s" class="collapse"> <td colspan="20"> """ % (elt_id) tr2 += """ <div class="pull-left"> """ if passive_checks_enabled: tr2 += """ <span> <i class="fa fa-arrow-left" title="Passive checks are enabled."></i>""" if freshness_threshold: tr2 += """ <i title="Freshness check is enabled">(Freshness threshold: %s seconds)</i> </span>""" % (freshness_threshold) if active_checks_enabled: tr2 += """ <span> <i class="fa fa-arrow-right" title="Active checks are enabled."></i> <i> Last check <strong>%s</strong>, next check in <strong>%s</strong>, attempt <strong>%d / %d</strong> </i> </span>""" % ( helper.print_duration(item['last_check'], duration_only=True, x_elts=2), helper.print_duration(item['next_check'], duration_only=True, x_elts=2), int(item['current_attempt']), int(item['max_attempts']) ) tr2 += """ </div> """ if current_user and current_user.can_action(): tr2 += """ <div class="pull-right"> <div class="btn-group" role="group" data-type="actions" aria-label="Actions"> <button class="btn btn-default btn-xs" data-type="action" action="event-handler" data-toggle="tooltip" data-placement="bottom" title="Try to fix (launch event handler)" data-element="test"> <i class="fa fa-magic"></i> <span class="hidden-sm hidden-xs"> Try to fix</span> </button> </div> </div>""" tr2 += """ </td> </tr>""" rows.append(tr2) if len(rows): panel_bi = """ <div id="livestate-bi-%d" class="panel panel-default"> <div class="panel-body"> <button type="button" class="btn btn-default btn-xs pull-left" data-type="business-impact" data-business-impact="%d" data-state="off"> Select all elements </button> <i class="pull-right small">%d elements</i> <h3 class="text-center">Business impact: %s</h3> <table class="table table-invisible table-condensed"> <thead><tr> <th width="20px"></th> <th width="40px"></th> <th width="60px">Host</th> <th width="90px">Service</th> <th width="90px">State</th> <th class="hidden-sm hidden-xs" width="90px">Duration</th> <th class="hidden-sm hidden-xs" width="100%%">Output</th> </tr></thead> <tbody> </tbody> </table> </div> </div>""" % ( bi, bi, len(rows) / 2, self.get_html_business_impact(bi, icon=True, text=True) ) else: panel_bi = """ <div id="livestate-bi-%d" class="panel panel-default"> <div class="panel-body" > <h3 class="text-center">Business impact: %s</h3> <div class="alert alert-info"> <p class="font-critical">No elements available.</p> </div> </div> </div>""" % ( bi, self.get_html_business_impact(bi, icon=True, text=True) ) return {'bi': bi, 'rows': rows, 'panel_bi': panel_bi}