def test_get_hardware_200_ok(mocker): # Just part of response all is not needed mock = mocker.MagicMock(return_value=SINGLE_HARDWARE_FOUND) with mocker.patch( 'Controller.StockAPIController.StockAPIController.get_data_from_api', mock): asset = APIc.get_hardware(SINGLE_HARDWARE_FOUND['rows'][0]['serial']) assert asset.id == SINGLE_HARDWARE_FOUND['rows'][0]['id'] assert asset.tag == SINGLE_HARDWARE_FOUND['rows'][0]['asset_tag'] assert asset.name == SINGLE_HARDWARE_FOUND['rows'][0]['name'] assert asset.serial_number == SINGLE_HARDWARE_FOUND['rows'][0][ 'serial'] assert asset.model_id == SINGLE_HARDWARE_FOUND['rows'][0]['model'][ 'id'] assert asset.model_name == SINGLE_HARDWARE_FOUND['rows'][0]['model'][ 'name'] assert asset.category_id == SINGLE_HARDWARE_FOUND['rows'][0][ 'category']['id'] assert asset.category_name == SINGLE_HARDWARE_FOUND['rows'][0][ 'category']['name'] assert asset.status == AssetStatus.get_status( SINGLE_HARDWARE_FOUND['rows'][0]['status_label']['id'], SINGLE_HARDWARE_FOUND['rows'][0]['status_label']['status_meta'])
def update_assets(self, serials): assets = [] not_found = [] for serial in serials: asset = StockAPIController.get_hardware(serial) if asset and asset.status == AssetStatus.ASSET_NOT_FOUND or asset.status == AssetStatus.NOT_CONNECTED: logging.info( 'Asset not found, getting data from product info api...') a = self.__product_api_controller.parse_product_data(serial) if a is not None: asset = a else: logging.info( 'Asset exist in local database, skipping query to product info api.' ) assets.append(asset) if asset.status == AssetStatus.ASSET_NOT_FOUND: not_found.append(serial) logging.info('Found {}/{} assets in internal database.'.format( len(assets), len(serials))) if len(not_found) > 0: logging.info('Cannot found following serial numbers: ' + str(not_found)) return assets
def test_parser_hardware_key_error_api_problem(mocker): serial_number = 'Not_important' mock = mocker.MagicMock(side_effect=KeyError('API Problem')) with mocker.patch( 'Controller.StockAPIController.StockAPIController.get_data_from_api', mock): asset = APIc.get_hardware(serial_number) assert asset.id == -1 assert asset.name == '' assert asset.tag == '' assert asset.model_id == -1 assert asset.model_name == '' assert asset.category_id == -1 assert asset.category_name == '' assert asset.serial_number == serial_number assert asset.status == AssetStatus.STATUS_NOT_FOUND
def test_get_hardware_io_error_not_connected(mocker): serial_number = 'Not_important' mock = mocker.MagicMock(side_effect=IOError('IOError')) with mocker.patch( 'Controller.StockAPIController.StockAPIController.get_data_from_api', mock): asset = APIc.get_hardware(serial_number) assert asset.id == -1 assert asset.name == '' assert asset.tag == '' assert asset.model_id == -1 assert asset.model_name == '' assert asset.category_id == -1 assert asset.category_name == '' assert asset.serial_number == serial_number assert asset.status == AssetStatus.NOT_CONNECTED
def test_get_hardware_401_unauthorized(mocker): serial_number = 'Not_important' response = {"status": "error", "message": "Unauthorized."} mock = mocker.MagicMock(return_value=response) with mocker.patch( 'Controller.StockAPIController.StockAPIController.get_data_from_api', mock): asset = APIc.get_hardware(serial_number) assert asset.id == -1 assert asset.name == '' assert asset.tag == '' assert asset.model_id == -1 assert asset.model_name == '' assert asset.category_id == -1 assert asset.category_name == '' assert asset.serial_number == serial_number assert asset.status == AssetStatus.STATUS_NOT_FOUND
def test_get_hardware_200_not_found(mocker): serial_number = 'Not_important' response = {"total": 0, "rows": []} mock = mocker.MagicMock(return_value=response) with mocker.patch( 'Controller.StockAPIController.StockAPIController.get_data_from_api', mock): asset = APIc.get_hardware(serial_number) assert asset.id == -1 assert asset.name == '' assert asset.tag == '' assert asset.model_id == -1 assert asset.model_name == '' assert asset.serial_number == serial_number assert asset.category_id == -1 assert asset.category_name == '' assert asset.status == AssetStatus.ASSET_NOT_FOUND
def test_get_hardware_404_endpoint_not_found(mocker): serial_number = 'Not_important' response = { "status": "error", "messages": "404 endpoint not found", "payload": None } mock = mocker.MagicMock(return_value=response) with mocker.patch( 'Controller.StockAPIController.StockAPIController.get_data_from_api', mock): asset = APIc.get_hardware(serial_number) assert asset.id == -1 assert asset.name == '' assert asset.tag == '' assert asset.model_id == -1 assert asset.model_name == '' assert asset.category_id == -1 assert asset.category_name == '' assert asset.serial_number == serial_number assert asset.status == AssetStatus.STATUS_NOT_FOUND