class TestRelations(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_relation_host_command(self): """ Datamanager objects get - host/command relation """ 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 def test_relation_host_service(self): """ Datamanager objects get - host/services relation """ 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: ", host.__dict__) # Get services of this host service = self.dmg.get_service( {'where': { 'name': 'Shinken2-broker', 'host_name': host.id }}) print("Services: ", service)
class TestLoadCreate(unittest2.TestCase): def setUp(self): self.dmg = DataManager(alignak_webui.app.app) print('Data manager', self.dmg) def test_3_1_load(self): """ Datamanager load """ print('test load as admin') # Initialize and load ... no reset assert self.dmg.user_login('admin', 'admin') result = self.dmg.load() print("Result:", result) # self.assertEqual(result, 0) # No new objects created ... # Initialize and load ... with reset result = self.dmg.load(reset=True) print("Result:", result) # Must not have loaded any objects ... behavior changed, no more objects loading on login # self.assertEqual(result, 0) def test_3_3_get_errors(self): """ Datamanager objects get errors """ print('test get errors') # Initialize and load ... no reset assert self.dmg.user_login('admin', 'admin') result = self.dmg.load() print("Result:", result) assert result == 0 # No new objects created ... # Get elements error item = self.dmg.get_user('unknown') assert not item item = self.dmg.get_userrestrictrole('unknown') assert not item item = self.dmg.get_realm('unknown') assert not item item = self.dmg.get_host('unknown') assert not item item = self.dmg.get_hostgroup('unknown') assert not item item = self.dmg.get_hostdependency('unknown') assert not item item = self.dmg.get_service('unknown') assert not item item = self.dmg.get_servicegroup('unknown') assert not item item = self.dmg.get_servicedependency('unknown') assert not item item = self.dmg.get_command('unknown') assert not item item = self.dmg.get_history('unknown') assert not item item = self.dmg.get_logcheckresult('unknown') assert not item item = self.dmg.get_timeperiod('unknown') assert not item
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')
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
class TestNotAdmin(unittest2.TestCase): def setUp(self): self.dmg = DataManager(alignak_webui.app.app) print('Data manager', self.dmg) @unittest2.skip( "Skipped because creating a new user do not allow him to get its own data (timeperiod get is 404)!" ) def test_4_1_load(self): """ Datamanager load, not admin user """ print('test load not admin user') # Initialize and load ... no reset assert self.dmg.user_login('admin', 'admin') result = self.dmg.load() print("Result:", result) assert result == 0 # No new objects created ... # Get main realm realm_all = self.dmg.get_realm({'where': {'name': 'All'}}) # Get main TP tp_all = self.dmg.get_timeperiod({'where': {'name': '24x7'}}) # Create a non admin user ... # Create a new user print('create a user') data = { "name": "not_admin", "alias": "Testing user - not administrator", "min_business_impact": 0, "email": "*****@*****.**", "is_admin": False, "expert": False, "can_submit_commands": False, "host_notifications_enabled": True, "host_notification_period": tp_all.id, "host_notification_commands": [], "host_notification_options": ["d", "u", "r"], "service_notifications_enabled": True, "service_notification_period": tp_all.id, "service_notification_commands": [], "service_notification_options": ["w", "u", "c", "r"], "definition_order": 100, "address1": "", "address2": "", "address3": "", "address4": "", "address5": "", "address6": "", "pager": "", "notificationways": [], "_realm": realm_all.id } new_user_id = self.dmg.add_user(data) print("New user id: %s" % new_user_id) # Logout # self.dmg.reset(logout=True) # assert not self.dmg.backend.connected # assert self.dmg.logged_in_user is None # assert self.dmg.loaded == False # Login as not_admin created user assert self.dmg.user_login('admin', 'admin', load=False) print("-----") assert self.dmg.user_login('not_admin', 'NOPASSWORDSET', load=False) assert self.dmg.my_backend.connected assert self.dmg.logged_in_user print("Logged-in user: %s" % self.dmg.logged_in_user) assert self.dmg.logged_in_user.get_username() == 'not_admin' print('logged in as not_admin') # Initialize and load ... result = self.dmg.load() print("Result:", result) print("Objects count:", self.dmg.get_objects_count()) print("Objects count:", self.dmg.get_objects_count('host')) print("Objects count:", self.dmg.get_objects_count('service')) print("Objects count (log):", self.dmg.get_objects_count(log=True)) # assert result == 0 # Only the newly created user, so no new objects loaded # assert self.dmg.get_objects_count() == 1 # not_admin user # Initialize and load ... with reset result = self.dmg.load(reset=True) print("Result:", result) print("Objects count:", self.dmg.get_objects_count()) print("Objects count:", self.dmg.get_objects_count('host')) print("Objects count:", self.dmg.get_objects_count('service')) print("Objects count (log):", self.dmg.get_objects_count(log=True)) # assert result == 3 # not_admin user + test_service + relation # assert self.dmg.get_objects_count() == 3 # not_admin user + test_service + relation # Not admin user can see only its own data, ... # ------------------------------------------- # Do not check the length because the backend contains more elements than needed ... # dump_backend(not_admin_user=True, test_service=True) # Get users items = self.dmg.get_users() print("Users:", items) # assert len(items) == 1 # 1 user only ... # Get commands items = self.dmg.get_commands() print("Commands:", items) # assert len(items) == 1 # Get realms items = self.dmg.get_realms() print("Commands:", items) # assert len(items) == 1 # Get timeperiods items = self.dmg.get_timeperiods() print("Commands:", items) # assert len(items) == 1 # Get hosts items = self.dmg.get_hosts() print("Hosts:", items) # assert len(items) == 1 # Get services items = self.dmg.get_services() print("Services:", items) # assert len(items) == 1 result = self.dmg.delete_user(new_user_id) # Cannot delete the current logged-in user assert not result # Logout self.dmg.reset(logout=True) assert not self.dmg.my_backend.connected assert self.dmg.logged_in_user is None assert self.dmg.loaded == False # Login as admin assert self.dmg.user_login('admin', 'admin', load=False) assert self.dmg.my_backend.connected assert self.dmg.logged_in_user.get_username() == 'admin' result = self.dmg.delete_user(new_user_id) # Can delete the former logged-in user assert result