def test_host_delete(self):
        """Delete the newly created hosts"""
        datamgr = DataManager(alignak_webui.app.app, session=self.session)

        # Count hosts
        count = datamgr.count_objects('host')
        print("Host count: %s" % count)

        # Get host and service in the backend
        host = datamgr.get_host({'where': {'name': 'pi1'}})

        assert datamgr.delete_object('service', host) is False
        assert datamgr.delete_object('host', host) is True

        # Count hosts (one less!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count - 1

        # Get host and service in the backend
        host = datamgr.get_host({'where': {'name': 'pi2'}})

        assert datamgr.delete_object('service', host) is False
        assert datamgr.delete_object('host', host) is True

        # Count hosts (one less!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count - 2
    def test_host_delete(self):
        """Delete the newly created hosts"""
        datamgr = DataManager(alignak_webui.app.app, session=self.session)

        # Count hosts
        count = datamgr.count_objects('host')
        print("Host count: %s" % count)

        # Get host and service in the backend
        host = datamgr.get_host({'where': {'name': 'pi1'}})

        assert datamgr.delete_object('service', host) is False
        assert datamgr.delete_object('host', host) is True

        # Count hosts (one less!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count - 1

        # Get host and service in the backend
        host = datamgr.get_host({'where': {'name': 'pi2'}})

        assert datamgr.delete_object('service', host) is False
        assert datamgr.delete_object('host', host) is True

        # Count hosts (one less!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count - 2
class TestCreation(unittest2.TestCase):
    def setUp(self):
        # Test application
        self.app = TestApp(alignak_webui.app.session_app)

        print('login accepted - go to home page')
        self.login_response = self.app.post('/login', {'username': '******', 'password': '******'})

        # A session cookie exist
        assert self.app.cookies['Alignak-WebUI']
        for cookie in self.app.cookiejar:
            if cookie.name=='Alignak-WebUI':
                assert cookie.expires is None

        # A session exists and it contains: current user, his realm and his live synthesis
        self.session = self.login_response.request.environ['beaker.session']
        assert 'current_user' in self.session and self.session['current_user']
        assert self.session['current_user'].name == 'admin'
        assert 'current_realm' in self.session and self.session['current_realm']
        assert self.session['current_realm'].name == 'All'
        # assert 'current_ls' in self.session and self.session['current_ls']

        # edition_mode is defined but not activated in the session...
        assert 'edition_mode' in self.session
        assert False == self.session['edition_mode']

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

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

        # Get realm in the backend
        self.realm = self.datamgr.get_realm({'where': {'name': 'All'}})
        print("Realm: %s" % self.realm)
        assert self.realm.name == 'All'

        # Count hosts templates
        self.hosts_templates_count = self.datamgr.count_objects('host', search={'where': {'_is_template': True}})
        print("Hosts templates count: %s" % self.hosts_templates_count)
        assert self.hosts_templates_count == hosts_templates_count

        # Count services templates
        self.services_templates_count = self.datamgr.count_objects('service', search={'where': {'_is_template': True}})
        print("Services templates count: %s" % self.services_templates_count)
        assert self.services_templates_count == services_templates_count

        # Count users templates
        self.users_templates_count = self.datamgr.count_objects('user', search={'where': {'_is_template': True}})
        print("Users templates count: %s" % self.users_templates_count)
        assert self.users_templates_count == users_templates_count

        # Count hosts
        self.hosts_count = self.datamgr.count_objects('host', search={'where': {'_is_template': False}})
        print("Hosts count: %s" % self.hosts_count)

        # Count services
        self.services_count = self.datamgr.count_objects('service', search={'where': {'_is_template': False}})
        print("Services count: %s" % self.services_count)

        # Count users
        self.users_count = self.datamgr.count_objects('user', search={'where': {'_is_template': False}})
        print("users count: %s" % self.users_count)

    def tearDown(self):
        print("Logout!")
        self.app.get('/logout')
    def test_host_new_host(self):
        """Create a new host edition form"""

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

        # Get realm in the backend
        realm = datamgr.get_realm({'where': {'name': 'All'}})
        # Get host template in the backend
        template = datamgr.get_host({'where': {'name': 'generic-host', '_is_template': True}})

        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'}

        # Count hosts
        count = datamgr.count_objects('host')
        print("Host count: %s" % count)

        print('get page /host_form (edition mode) - for a new host')
        response = self.app.get('/host_form/unknown_host')
        response.mustcontain(
            '''<div id="form_host">''',
            '''<form role="form" data-element="None" class="element_form " method="post" action="/host_form/None">''',
            '''<h4>You are creating a new host.</h4>''',
            '''$('form[data-element="None"]').on("submit", function (evt) {'''
        )

        # A name is enough to create a new host
        print('Host creation - missing name')
        data = {
            "_is_template": False,
        }
        response = self.app.post('/host_form/None', params=data)
        print(response.json)
        assert response.json == {
            '_is_template': False,
            '_message': 'host creation failed!',
            '_errors': ['']
        }

        # A name is enough to create a new host
        print('Host creation without template')
        data = {
            "_is_template": False,
            'name': "New host",
            'alias': "Friendly name"
        }
        response = self.app.post('/host_form/None', params=data)
        # Returns the new item _id
        new_host_id = response.json['_id']
        resp = response.json
        resp.pop('_id')
        assert resp == {
            "_message": "New host created",
            "_realm": realm.id,

            "_is_template": False,
            "name": "New host",
            'alias': "Friendly name"
        }

        # Count hosts (one more!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count + 1

        # Get the new host in the backend
        host = datamgr.get_host({'where': {'name': 'New host'}})
        assert host
        assert host.id == new_host_id
        assert host.name == "New host"
        assert host.alias == "Friendly name"

        print('Host creation with a template')
        data = {
            "_is_template": False,
            "_templates": [template.id],
            'name': "New host 2",
            'alias': "Friendly name 2"
        }
        response = self.app.post('/host_form/None', params=data)
        # Returns the new item _id
        new_host_id = response.json['_id']
        resp = response.json
        resp.pop('_id')
        assert resp == {
            "_message": "New host created",
            "_realm": realm.id,

            "_is_template": False,
            "_templates": [template.id],
            "name": "New host 2",
            'alias': "Friendly name 2"
        }

        # Count hosts (one more!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count + 2

        # Get the new host in the backend
        host = datamgr.get_host({'where': {'name': 'New host 2'}})
        assert host
        assert host.id == new_host_id
        assert host.name == "New host 2"
        assert host.alias == "Friendly name 2"
    def test_host_new_host(self):
        """Create a new host edition form"""

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

        # Get realm in the backend
        realm = datamgr.get_realm({'where': {'name': 'All'}})
        # Get host template in the backend
        template = datamgr.get_host(
            {'where': {
                'name': 'generic-host',
                '_is_template': True
            }})

        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'
        }

        # Count hosts
        count = datamgr.count_objects('host')
        print("Host count: %s" % count)

        print('get page /host_form (edition mode) - for a new host')
        response = self.app.get('/host_form/unknown_host')
        response.mustcontain(
            '''<div id="form_host">''',
            '''<form role="form" data-element="None" class="element_form " method="post" action="/host_form/None">''',
            '''<h4>You are creating a new host.</h4>''',
            '''$('form[data-element="None"]').on("submit", function (evt) {''')

        # A name is enough to create a new host
        print('Host creation - missing name')
        data = {
            "_is_template": False,
        }
        response = self.app.post('/host_form/None', params=data)
        print(response.json)
        assert response.json == {
            '_is_template': False,
            '_message': 'host creation failed!',
            '_errors': ['']
        }

        # A name is enough to create a new host
        print('Host creation without template')
        data = {
            "_is_template": False,
            'name': "New host",
            'alias': "Friendly name"
        }
        response = self.app.post('/host_form/None', params=data)
        # Returns the new item _id
        new_host_id = response.json['_id']
        resp = response.json
        resp.pop('_id')
        assert resp == {
            "_message": "New host created",
            "_realm": realm.id,
            "_is_template": False,
            "name": "New host",
            'alias': "Friendly name"
        }

        # Count hosts (one more!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count + 1

        # Get the new host in the backend
        host = datamgr.get_host({'where': {'name': 'New host'}})
        assert host
        assert host.id == new_host_id
        assert host.name == "New host"
        assert host.alias == "Friendly name"

        print('Host creation with a template')
        data = {
            "_is_template": False,
            "_templates": [template.id],
            'name': "New host 2",
            'alias': "Friendly name 2"
        }
        response = self.app.post('/host_form/None', params=data)
        # Returns the new item _id
        new_host_id = response.json['_id']
        resp = response.json
        resp.pop('_id')
        assert resp == {
            "_message": "New host created",
            "_realm": realm.id,
            "_is_template": False,
            "_templates": [template.id],
            "name": "New host 2",
            'alias': "Friendly name 2"
        }

        # Count hosts (one more!)
        new_count = datamgr.count_objects('host')
        print("Host count: %s" % new_count)
        assert new_count == count + 2

        # Get the new host in the backend
        host = datamgr.get_host({'where': {'name': 'New host 2'}})
        assert host
        assert host.id == new_host_id
        assert host.name == "New host 2"
        assert host.alias == "Friendly name 2"
class TestCreation(unittest2.TestCase):
    def setUp(self):
        # Test application
        self.app = TestApp(alignak_webui.app.session_app)

        print('login accepted - go to home page')
        self.login_response = self.app.post('/login', {
            'username': '******',
            'password': '******'
        })

        # A session cookie exist
        assert self.app.cookies['Alignak-WebUI']
        for cookie in self.app.cookiejar:
            if cookie.name == 'Alignak-WebUI':
                assert cookie.expires is None

        # A session exists and it contains: current user, his realm and his live synthesis
        self.session = self.login_response.request.environ['beaker.session']
        assert 'current_user' in self.session and self.session['current_user']
        assert self.session['current_user'].name == 'admin'
        assert 'current_realm' in self.session and self.session['current_realm']
        assert self.session['current_realm'].name == 'All'
        # assert 'current_ls' in self.session and self.session['current_ls']

        # edition_mode is defined but not activated in the session...
        assert 'edition_mode' in self.session
        assert False == self.session['edition_mode']

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

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

        # Get realm in the backend
        self.realm = self.datamgr.get_realm({'where': {'name': 'All'}})
        print("Realm: %s" % self.realm)
        assert self.realm.name == 'All'

        # Count hosts templates
        self.hosts_templates_count = self.datamgr.count_objects(
            'host', search={'where': {
                '_is_template': True
            }})
        print("Hosts templates count: %s" % self.hosts_templates_count)
        assert self.hosts_templates_count == hosts_templates_count

        # Count services templates
        self.services_templates_count = self.datamgr.count_objects(
            'service', search={'where': {
                '_is_template': True
            }})
        print("Services templates count: %s" % self.services_templates_count)
        assert self.services_templates_count == services_templates_count

        # Count users templates
        self.users_templates_count = self.datamgr.count_objects(
            'user', search={'where': {
                '_is_template': True
            }})
        print("Users templates count: %s" % self.users_templates_count)
        assert self.users_templates_count == users_templates_count

        # Count hosts
        self.hosts_count = self.datamgr.count_objects(
            'host', search={'where': {
                '_is_template': False
            }})
        print("Hosts count: %s" % self.hosts_count)

        # Count services
        self.services_count = self.datamgr.count_objects(
            'service', search={'where': {
                '_is_template': False
            }})
        print("Services count: %s" % self.services_count)

        # Count users
        self.users_count = self.datamgr.count_objects(
            'user', search={'where': {
                '_is_template': False
            }})
        print("users count: %s" % self.users_count)

    def tearDown(self):
        print("Logout!")
        self.app.get('/logout')