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
def test_command(self): """ Actions - command""" print('test command') print('get page /command/form/add') response = self.app.get('/command/form/add') response.mustcontain( '<form class="form-horizontal" data-item="command" data-action="add" ' ) print('get page /command/parameters - bad parameters') response = self.app.get('/command/parameters', status=409) assert response.json == {'error': "the command 'None' does not exist"} response = self.app.get( '/command/parameters?command=fake&elements_type=host', status=409) assert response.json == {'error': "the command 'fake' does not exist"} response = self.app.get( '/command/parameters?command=process_host_check_result&elements_type=fake', status=409) assert response.json == { 'error': "the plugin for 'fake' is not existing or not installed" } print('get page /command/parameters') response = self.app.get( '/command/parameters?elements_type=host&command=process_host_check_result' ) expected = { "ls_state_id": { "allowed": { "0": "Up", "1": "Down (1)", "2": "Not used (2)", "3": "Not used (3)", "4": "Unreachable" }, "allowed_0": "Up", "allowed_1": "Down (1)", "allowed_2": "Not used (2)", "allowed_3": "Not used (3)", "allowed_4": "Unreachable", "comment": "Current state identifier. O: UP, 1: DOWN, 2/3: NOT USED, 4: UNREACHABLE", "default": 3, "title": "State identifier", "editable": True, "hidden": True, "type": "integer" }, "ls_output": { "default": "Check output from WebUI", "type": "string", "title": "Output", "editable": True, "comment": "Last check output" }, "ls_long_output": { "default": "", "type": "string", "title": "Long output", "editable": True, "visible": False, "comment": "Last check long output" }, "ls_perf_data": { "default": "", "type": "string", "title": "Performance data", "editable": True, "visible": False, "comment": "Last check performance data" } } assert expected == response.json # Current user is admin session = response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' # Data manager datamgr = DataManager(alignak_webui.app.app, session=session) # Get host and user in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) # ------------------------------------------- # Add a command # Missing or invalid parameters! data = { # "command": "test", "elements_type": 'host', "element_id": host.id } response = self.app.post('/command/add', data, status=400) print(response) # Unknown command data = { "command": "test", "elements_type": 'host', "element_id": host.id } response = self.app.post('/command/add', data, status=400) # Missing command parameter data = { "command": "process_host_check_result", "elements_type": 'host', "element_id": host.id } response = self.app.post('/command/add', data, status=400) # Command for an host data = { "command": "process_host_check_result", "elements_type": 'host', "element_id": host.id, "ls_state_id": '0', "ls_output": "New output...", "ls_long_output": "", "ls_perf_data": "", } response = self.app.post('/command/add', data, status=409) # As of #193... assert response.json['status'] == "ko" assert response.json[ 'message'] == "Failed sending a command for localhost. "
def test_recheck(self): """ Actions - recheck""" print('test recheck') print('get page /recheck/form/add') response = self.app.get('/recheck/form/add') response.mustcontain( '<form class="form-horizontal" data-item="recheck" data-action="add" ' ) # Current user is admin session = response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' # Data manager datamgr = DataManager(alignak_webui.app.app, session=session) # Get host, user and realm in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) # ------------------------------------------- # Add a recheck # Missing livestate_id! data = { "host": host.id, "service": None, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/recheck/add', data, status=400) # Recheck an host data = { "elements_type": 'host', "element_id": host.id, "comment": "User comment", } response = self.app.post('/recheck/add', data) assert response.json['status'] == "ok" assert response.json['message'] == "Check request sent for localhost. " # Recheck a service service = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) data = { "elements_type": 'service', "element_id": service.id, "comment": "User comment", } response = self.app.post('/recheck/add', data) assert response.json['status'] == "ok" assert response.json[ 'message'] == "Check request sent for localhost/Cpu. " # Recheck several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id, 'test'], "comment": "User comment", } response = self.app.post('/recheck/add', data) assert response.json['status'] == "ok" assert response.json[ 'message'] == "Check request sent for localhost/Cpu. Check request sent for localhost/Memory. service element test does not exist. "
def test_downtime(self): """ Actions - downtime""" print('test actions') print('get page /downtime/form/add') response = self.app.get('/downtime/form/add') response.mustcontain( '<form class="form-horizontal" data-item="downtime" data-action="add"' ) # Current user is admin session = response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' # Data manager datamgr = DataManager(alignak_webui.app.app, session=session) # Get host, user and realm in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) now = datetime.utcnow() later = now + timedelta(days=2, hours=4, minutes=3, seconds=12) now = timegm(now.timetuple()) later = timegm(later.timetuple()) # ------------------------------------------- # Add an downtime # Missing livestate_id! data = { "action": "add", "host": host.id, "service": None, "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } self.app.post('/downtime/add', data, status=400) # downtime an host data = { "action": "add", "element_id": host.id, "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } response = self.app.post('/downtime/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Downtime sent for localhost. " # downtime a service service = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) data = { "action": "add", "elements_type": 'service', "element_id": service.id, "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } response = self.app.post('/downtime/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Downtime sent for localhost/Cpu. " # downtime several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id, 'test'], "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } response = self.app.post('/downtime/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Downtime sent for localhost/Cpu. " \ "Downtime sent for localhost/Memory. " \ "service element test does not exist. "
def test_acknowledge(self): """ Actions - acknowledge""" print('test actions') print('get page /acknowledge/form/add') response = self.app.get('/acknowledge/form/add') response.mustcontain( '<form class="form-horizontal" data-item="acknowledge" data-action="add" ' ) # Get Data manager in the session session = 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 user in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) # ------------------------------------------- # Add an acknowledge # Missing element_id! data = { "elements_type": "host", "element_id": host.id, "service": None, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } self.app.post('/acknowledge/add', data, status=400) # Acknowledge an host data = { "action": "add", "elements_type": 'host', # Default value, can be omitted ... "element_id": host.id, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost. " # Acknowledge a service service = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) data = { "action": "add", "elements_type": 'service', "element_id": service.id, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost/Cpu. " # Acknowledge several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id], "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost/Cpu. " \ "Acknowledge sent for localhost/Memory. " \ # Acknowledge several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id, 'test'], "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost/Cpu. " \ "Acknowledge sent for localhost/Memory. " \ "service element test does not exist. "
def test_change_password(self): """ Actions - acknowledge""" print('test password change') print('get page /password_change_request') response = self.app.get('/password_change_request') response.mustcontain( '<form id="password_change" class="form-horizontal" data-action="password" method="post" action="/change_password" role="form">' ) # Get Data manager in the session session = 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 guest user in the backend user = datamgr.get_user({'where': {'name': 'guest'}}) # ------------------------------------------- # Change a password # Missing element_id! data = { # "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "NewPassword2017", "password2": "NewPassword2017", "valid_form": "true" } self.app.post('/change_password', data, status=400) # Empty passwords data = { "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "", "password2": "", "valid_form": "true" } self.app.post('/change_password', data, status=400) data = { "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "NewPassword2017", "password2": "", "valid_form": "true" } self.app.post('/change_password', data, status=400) # Invalid form data = { "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "NewPassword2017", "password2": "NewPassword2017", "valid_form": "false" } self.app.post('/change_password', data, status=400) # ------------------------------------------- # Change a password data = { "element_id": user.id, "elements_type": 'user', # Default value, can be omitted ... "elements_name": 'guest', "password1": "NewPassword2017", "password2": "NewPassword2017", "valid_form": "true" } response = self.app.post('/change_password', data) assert response.json['status'] == "ok" assert response.json['message'] == "User guest updated." # ------------------------------------------- # Log-out the admin user response = self.app.get('/logout') redirected_response = response.follow() redirected_response.mustcontain('<form role="form" method="post" action="/login">') # Log-in with the new password 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="dashboard">') self.stored_response = redirected_response # A host cookie now exists assert self.app.cookies['Alignak-WebUI'] # Redirected twice: /login -> / -> /livestate redirected_response = response.follow() redirected_response = redirected_response.follow() redirected_response.mustcontain('<div id="livestate">')
def test_user_widgets(self): """External - user widgets""" print('allowed user 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 user in the backend user = datamgr.get_user({'where': {'name': 'imported_admin'}}) # Get external user widget - no widget identifier self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/user/%s' % user.id, status=409 ) response.mustcontain( '<div><h1>Missing user widget name.</h1><p>You must provide a widget name</p></div>') # Get external user widget - unknown widget self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/user/%s/unknown' % user.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 user widget self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/user/%s/information?page' % user.id ) print(response) response.mustcontain( '<!DOCTYPE html>', '<html lang="en">', '<body>', '<section>', '<div id="wd_panel_information" class="panel panel-default alignak_webui_widget embedded">', '<!-- User information widget -->', '</section>', '</body>' ) # Get external user widget, no page parameter self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/user/%s/information' % user.id ) response.mustcontain( '<div id="wd_panel_information" class="panel panel-default alignak_webui_widget embedded">', '<!-- User information widget -->', )
def test_change_password(self): """ Actions - acknowledge""" print('test password change') print('get page /password_change_request') response = self.app.get('/password_change_request') response.mustcontain( '<form id="password_change" class="form-horizontal" data-action="password" method="post" action="/change_password" role="form">' ) # Get Data manager in the session session = 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 guest user in the backend user = datamgr.get_user({'where': {'name': 'guest'}}) # ------------------------------------------- # Change a password # Missing element_id! data = { # "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "NewPassword2017", "password2": "NewPassword2017", "valid_form": "true" } self.app.post('/change_password', data, status=400) # Empty passwords data = { "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "", "password2": "", "valid_form": "true" } self.app.post('/change_password', data, status=400) data = { "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "NewPassword2017", "password2": "", "valid_form": "true" } self.app.post('/change_password', data, status=400) # Invalid form data = { "element_id": user.id, "elements_type": 'user', "elements_name": 'guest', "password1": "NewPassword2017", "password2": "NewPassword2017", "valid_form": "false" } self.app.post('/change_password', data, status=400) # ------------------------------------------- # Change a password data = { "element_id": user.id, "elements_type": 'user', # Default value, can be omitted ... "elements_name": 'guest', "password1": "NewPassword2017", "password2": "NewPassword2017", "valid_form": "true" } response = self.app.post('/change_password', data) assert response.json['status'] == "ok" assert response.json['message'] == "User guest updated." # ------------------------------------------- # Log-out the admin user response = self.app.get('/logout') redirected_response = response.follow() redirected_response.mustcontain( '<form role="form" method="post" action="/login">') # Log-in with the new password 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="dashboard">') self.stored_response = redirected_response # A host cookie now exists assert self.app.cookies['Alignak-WebUI'] # Redirected twice: /login -> / -> /livestate redirected_response = response.follow() redirected_response = redirected_response.follow() redirected_response.mustcontain('<div id="livestate">')