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