def test_connectors_cisco_ios_get_version_bad_creds(self, mock_connect): """ Test Cisco IOS get version with bad device credentials """ mock_connect.side_effect = netmiko.ssh_exception.NetMikoAuthenticationException device = self.db.get_device_name('TEST-Router2') res = get_version_from_device(device, 'username', 'badpass') self.assertEqual(res, None)
def test_connectors_cisco_asa_get_version_bad_creds(self, mock_get): """ Test Cisco ASA Get version with bad device credentials """ # ASA API returns status code 401 when authentication fails mock_get.return_value.status_code = 401 device = self.db.get_device_name('TEST-firewall1') res = get_version_from_device(device, 'username', 'badpass') self.assertEqual(res, None)
def test_connectors_cisco_ios_get_version(self, mock_connect): """ Test CiscoIOS get version """ mock_output = self.load_text_fixture('ios_show_version.txt') # Double return_value to mock the instance returned by ConnectHandler mock_connect.return_value.send_command.return_value = mock_output device = self.db.get_device_name('TEST-Router2') res = get_version_from_device(device, 'username', 'password') self.assertEqual(res, '12.4(24)T6')
def test_connectors_cisco_asa_get_version(self, mock_get): """ Test CiscoASA get version """ mock_asa_resp = self.load_json_fixture('asa_api_get_version.json') mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = mock_asa_resp device = self.db.get_device_name('TEST-firewall1') res = get_version_from_device(device, 'username', 'password') self.assertEqual(res, mock_asa_resp['asaVersion'])
def update_device(device_id): """ Update device with id device_id. If JSON value = None for a supported key, connect to device to retrieve updated value. Also requires keys device_username and device_password Supported keys for device update: sw_version, serial_number """ netAdminToolDB = app.config['DATABASE'] device = netAdminToolDB.get_device(device_id) #print(f'update_device request = {request.get_data()}') if device == None: return jsonify({'error': 'Device_id not found'}), 404 input = request.get_json() if input == None: return jsonify({'error': 'Invalid PUT request'}), 400 # Get update values from device for supported keys with value None if 'sw_version' in input and input['sw_version'] == None: # If device credentials were provided if 'device_username' and 'device_password' in input: input['sw_version'] = get_version_from_device(device, input['device_username'], input['device_password']) if input['sw_version'] == None: return jsonify({'error': 'Unable to retrieve sw_version from device.'}), 404 # Device credentials not provided, return error else: return jsonify({'error': 'Updates from device require credentials.'}), 400 if 'serial_number' in input and input['serial_number'] == None: # If device credentials were provided if 'device_username' and 'device_password' in input: input['serial_number'] = get_serial_from_device(device, input['device_username'], input['device_password']) if input['serial_number'] == None: return jsonify({'error': 'Unable to retrieve serial_number from device.'}), 404 # Device credentials not provided, return error else: return jsonify({'error': 'Updates from device require credentials.'}), 400 # Send input directly to update_device function, which checks each key. netAdminToolDB.update_device(device_id, **input) device = netAdminToolDB.get_device(device_id) deviceDict = dict(device) uri = url_for('get_device',device_id=device.id,_external=True) deviceDict['uri'] = uri return jsonify({'device': deviceDict}), 200