def test_host_submit_form(self):
        """Edit and submit host edition form"""
        datamgr = DataManager(alignak_webui.app.app, session=self.session)

        # Get host and service in the backend
        host = datamgr.get_host({'where': {'name': 'alignak_glpi'}})
        services = datamgr.get_host_services(host)
        service = services[0]

        print('Enable edition mode')
        response = self.app.post('/edition_mode', params={'state': 'on'})
        session = response.request.environ['beaker.session']
        # edition_mode is defined and activated in the session...
        assert 'edition_mode' in session
        assert True == session['edition_mode']
        assert response.json == {'edition_mode': True, 'message': 'Edition mode enabled'}

        print('get page /host_form (edition mode) - with an host id')
        response = self.app.get('/host_form/%s' % host.id)
        response.mustcontain(
            '''<div id="form_host">''',
            '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">''' % (
            host.id, host.id),
            '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id
        )

        print('Post form updates')
        data = {
            'name': host.name,
            'alias': "Alias edited"
        }
        response = self.app.post('/host_form/%s' % host.id, params=data)
        assert response.json == {
            "_message": "host 'alignak_glpi' updated", "alias": "Alias edited"
        }
Exemplo n.º 2
0
    def test_host_submit_form(self):
        """Edit and submit host edition form"""
        datamgr = DataManager(alignak_webui.app.app, session=self.session)

        # Get host and service in the backend
        host = datamgr.get_host({'where': {'name': 'alignak_glpi'}})
        services = datamgr.get_host_services(host)
        service = services[0]

        print('Enable edition mode')
        response = self.app.post('/edition_mode', params={'state': 'on'})
        session = response.request.environ['beaker.session']
        # edition_mode is defined and activated in the session...
        assert 'edition_mode' in session
        assert True == session['edition_mode']
        assert response.json == {
            'edition_mode': True,
            'message': 'Edition mode enabled'
        }

        print('get page /host_form (edition mode) - with an host id')
        response = self.app.get('/host_form/%s' % host.id)
        response.mustcontain(
            '''<div id="form_host">''',
            '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">'''
            % (host.id, host.id),
            '''$('form[data-element="%s"]').on("submit", function (evt) {''' %
            host.id)

        print('Post form updates')
        data = {'name': host.name, 'alias': "Alias edited"}
        response = self.app.post('/host_form/%s' % host.id, params=data)
        assert response.json == {
            "_message": "host 'alignak_glpi' updated",
            "alias": "Alias edited"
        }
    def test_service_widgets(self):
        """External - service widgets"""
        print('allowed service widgets external access')

        # Log in to get Data manager in the session
        response = self.app.get('/login')
        response.mustcontain('<form role="form" method="post" action="/login">')

        print('login accepted - go to home page')
        response = self.app.post('/login', {'username': '******', 'password': '******'})
        # Redirected twice: /login -> / -> /dashboard !
        redirected_response = response.follow()
        redirected_response = redirected_response.follow()
        redirected_response.mustcontain('<div id="livestate">')

        session = redirected_response.request.environ['beaker.session']
        assert 'current_user' in session and session['current_user']
        assert session['current_user'].get_username() == 'admin'

        datamgr = DataManager(alignak_webui.app.app, session=session)

        # Get host and service in the backend
        host = datamgr.get_host({'where': {'name': 'KNM-Shinken'}})
        print("Host: %s" % host)
        services = datamgr.get_host_services(host)
        print("Services: %s" % services)
        # service = datamgr.get_service({'where': {'name': 'http', 'host': host.id}})
        service = services[0]
        print("Service: %s" % service)
        # assert False

        # Get external service widget - no widget identifier
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s' % service.id,
            status=409
        )
        response.mustcontain('<div><h1>Missing service widget name.</h1><p>You must provide a widget name</p></div>')

        # Get external service widget - unknown widget
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/unknown' % service.id,
            status=409
        )
        response.mustcontain('<div><h1>Unknown required widget: unknown.</h1><p>The required widget is not available.</p></div>')

        # Service information
        # Get external service widget
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/information?page' % service.id
        )
        response.mustcontain(
            '<!DOCTYPE html>',
            '<html lang="en">',
            '<body>',
            '<section>',
            '<div id="wd_panel_information" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service information widget -->',
            '</section>',
            '</body>'
        )

        # Get external service widget, no page parameter
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/information' % service.id
        )
        response.mustcontain(
            '<div id="wd_panel_information" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service information widget -->',
        )

        # service configuration
        # Get external service widget
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/configuration?page' % service.id
        )
        response.mustcontain(
            '<!DOCTYPE html>',
            '<html lang="en">',
            '<body>',
            '<section>',
            '<div id="wd_panel_configuration" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service configuration widget -->',
            '</section>',
            '</body>'
        )

        # Get external service widget, no page parameter
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/configuration' % service.id
        )
        response.mustcontain(
            '<div id="wd_panel_configuration" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service configuration widget -->',
        )

        # Service timeline
        # Get external service widget
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/timeline?page' % service.id
        )
        response.mustcontain(
            '<!DOCTYPE html>',
            '<html lang="en">',
            '<body>',
            '<section>',
            '<div id="wd_panel_timeline" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service timeline widget -->',
            '</section>',
            '</body>',
            '<div class="alert alert-info">',
            '<p>No available history events.</p>',
            '</div>'
        )

        # Get external service widget, no page parameter
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/timeline' % service.id
        )
        response.mustcontain(
            '<div id="wd_panel_timeline" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service timeline widget -->',
        )

        # service metrics
        # Get external service widget
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/metrics?page' % service.id
        )
        response.mustcontain(
            '<!DOCTYPE html>',
            '<html lang="en">',
            '<body>',
            '<section>',
            '<div id="wd_panel_metrics" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service metrics widget -->',
            '</section>',
            '</body>'
        )

        # Get external service widget, no page parameter
        self.app.authorization = ('Basic', ('admin', 'admin'))
        response = self.app.get(
            '/external/service/%s/metrics' % service.id
        )
        response.mustcontain(
            '<div id="wd_panel_metrics" class="panel panel-default alignak_webui_widget embedded">',
            '<!-- Service metrics widget -->',
        )
Exemplo n.º 4
0
class TestHosts(unittest2.TestCase):
    def setUp(self):
        print("setting up ...")
        self.dmg = DataManager(alignak_webui.app.app)
        print('Data manager', self.dmg)

        # Initialize and do not load
        assert self.dmg.user_login('admin', 'admin', load=False)

    def tearDown(self):
        print("tearing down ...")
        # Logout
        self.dmg.reset(logout=True)

    def test_hosts(self):
        """ Datamanager objects get - hosts """
        print("Get all hosts")

        # Get main realm
        self.dmg.get_realm({'where': {'name': 'All'}})

        # Get main TP
        self.dmg.get_timeperiod({'where': {'name': '24x7'}})

        # Get all hosts
        hosts = self.dmg.get_hosts()
        assert 13 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)

        # Get all hosts (really all...)
        hosts = self.dmg.get_hosts(all_elements=True)
        assert 13 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)

        # Get all hosts (with all embedded relations)
        hosts = self.dmg.get_hosts(embedded=True)
        assert 13 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)

        # Get all hosts templates
        hosts = self.dmg.get_hosts(template=True)
        assert 28 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host template: %s" % host)

        # Get one host
        hosts = self.dmg.get_hosts({'where': {'name': 'alignak_glpi'}})
        assert 1 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)
        assert hosts[0].name == 'alignak_glpi'

        # Get one host
        host = self.dmg.get_host({'where': {'name': 'alignak_glpi'}})
        assert host.name == 'alignak_glpi'

        # Get one host
        host = self.dmg.get_host(host._id)
        assert host.name == 'alignak_glpi'
        assert host.status == 'UNREACHABLE'

        # Get host services
        services = self.dmg.get_host_services({'where': {'name': 'unknown'}})
        assert services == -1

        services = self.dmg.get_host_services(
            {'where': {
                'name': 'alignak_glpi'
            }})
        print("---")
        service_name = ''
        for service in services:
            print("Got service: %s" % service)
            service_name = service['name']
        assert len(services) > 1
        services = self.dmg.get_host_services(
            {'where': {
                'name': 'alignak_glpi'
            }},
            search={'where': {
                'name': service_name
            }})
        services = self.dmg.get_host_services(host)
        assert len(services) > 1

        # Get host overall state
        (state, status) = self.dmg.get_host_overall_state(host)
        print("Host overall state: %s %s" % (state, status))
        assert state == 3
        assert status == 'warning'

    def test_host(self):
        """ Datamanager objects get - host """
        print("--- test Item")

        # Get main realm
        self.dmg.get_realm({'where': {'name': 'All'}})

        # Get main TP
        self.dmg.get_timeperiod({'where': {'name': '24x7'}})

        # Get host
        host = self.dmg.get_host({'where': {'name': 'localhost'}})

        print(host.__dict__)
        print(host.check_period)
        assert isinstance(host.check_command, Command)
        assert host.check_command