示例#1
0
def test_cannot_create_model_category_or_manufacturer_not_exists(mocker):
    create_data_at_api_mock = mocker.MagicMock(return_value=MODEL_NOT_CREATED)
    get_model_id_mock = mocker.MagicMock(return_value=-1)

    mocker.patch(
        'Controller.StockAPIController.StockAPIController.create_data_at_api',
        create_data_at_api_mock)
    mocker.patch(
        'Controller.StockAPIController.StockAPIController.get_model_id',
        get_model_id_mock)

    assert APIc.create_model('model_name', -1, 1) == -1
    assert APIc.create_model('model_name', 1, -1) == -1
    assert APIc.create_model('model_name', -1, -1) == -1
示例#2
0
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
示例#4
0
def test_created_model_not_exists(mocker):
    create_data_at_api_mock = mocker.MagicMock(return_value=CATEGORY_CREATED)
    get_model_id_mock = mocker.MagicMock(return_value=-1)

    mocker.patch(
        'Controller.StockAPIController.StockAPIController.create_data_at_api',
        create_data_at_api_mock)
    mocker.patch(
        'Controller.StockAPIController.StockAPIController.get_model_id',
        get_model_id_mock)

    assert APIc.create_model(CATEGORY_CREATED['payload']['name'], 1,
                             1) == CATEGORY_CREATED['payload']['id']
    assert APIc.create_model(CATEGORY_CREATED['payload']['name'].upper(), 1,
                             1) == CATEGORY_CREATED['payload']['id']
    assert APIc.create_model(CATEGORY_CREATED['payload']['name'].lower(), 1,
                             1) == CATEGORY_CREATED['payload']['id']
示例#5
0
def test_cannot_create_category(mocker):
    mock = mocker.MagicMock(return_value=CATEGORY_NOT_CREATED)

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.create_data_at_api',
            mock):
        category_id = APIc.create_category('category_name')

        assert category_id == -1
示例#6
0
def test_get_category_id_by_category_name_not_found(mocker):
    mock = mocker.MagicMock(return_value={'total': 0, 'rows': []})

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.get_data_from_api',
            mock):
        category_id = APIc.get_category_id('category_name')

        assert category_id == -1
示例#7
0
def test_get_model_id_by_model_name_single(mocker):
    mock = mocker.MagicMock(return_value=SINGLE_MODEL_ID_FOUND)

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.get_data_from_api',
            mock):
        model_id = APIc.get_model_id(SINGLE_MODEL_ID_FOUND['rows'][0]['name'])

        assert model_id == SINGLE_MODEL_ID_FOUND['rows'][0]['id']
示例#8
0
def test_get_category_id_by_category_name_duplicated(mocker):
    mock = mocker.MagicMock(return_value=CATEGORY_ID_FOUND_DUPLICATED)

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.get_data_from_api',
            mock):
        category_id = APIc.get_category_id(
            CATEGORY_ID_FOUND_DUPLICATED['rows'][2]['name'])

        assert category_id == CATEGORY_ID_FOUND_DUPLICATED['rows'][2]['id']
示例#9
0
def test_get_category_id_by_category_name_single(mocker):
    mock = mocker.MagicMock(return_value=SINGLE_CATEGORY_ID_FOUND)

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.get_data_from_api',
            mock):
        category_id = APIc.get_category_id(
            SINGLE_CATEGORY_ID_FOUND['rows'][0]['name'])

        assert category_id == SINGLE_CATEGORY_ID_FOUND['rows'][0]['id']
示例#10
0
def test_get_model_id_by_model_name_duplicated(mocker):
    mock = mocker.MagicMock(return_value=MODEL_ID_DUPLICATED_FOUND)

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.get_data_from_api',
            mock):
        model_id = APIc.get_model_id(
            MODEL_ID_DUPLICATED_FOUND['rows'][2]['name'])

        assert model_id == MODEL_ID_DUPLICATED_FOUND['rows'][2]['id']
示例#11
0
    def __auto_add(self):
        for asset in self.assets_to_add:

            category_id = StockAPIController.get_category_id(
                asset.category_name)
            if category_id == -1:
                category_id = StockAPIController.create_category(
                    asset.category_name)

            asset.category_id = category_id

            if category_id == -1:
                logging.error(
                    'Error. Cannot create category in auto-add process for asset: {}'
                    .format(str(asset)))
                continue

            model_id = StockAPIController.get_model_id(asset.model_name)
            if model_id == -1:
                model_id = StockAPIController.create_model(
                    asset.model_name, category_id)

            asset.model_id = model_id

            if model_id == -1:
                logging.error(
                    'Error. Cannot create model in auto-add process for asset: {}'
                    .format(str(asset)))
                continue

            asset_id = StockAPIController.create_hardware(asset, 1)
            if asset_id == -1:
                logging.error(
                    'Error. Cannot create asset in auto-add process for asset: {}'
                    .format(str(asset)))

            else:
                logging.info('Created asset with id: {}, asset: {}'.format(
                    asset_id, str(asset)))

        self.main_view.btn_auto_add.config(state='disable')
        self.__refresh_assets()
示例#12
0
def test_check_in_hardware(mocker):
    a = Asset(id=123)
    a.status = AssetStatus.DEPLOYED
    notes = 'notes'
    a.notes = notes

    response = {
        'status': 'success',
        'messages': 'Asset checked in successfully.',
        'payload': {
            'asset': 'tag'
        }
    }
    mock = mocker.MagicMock(return_value=response)

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.create_data_at_api',
            mock):
        APIc.check_in(a)
        assert a.status != AssetStatus.DEPLOYED  # after check_in status doesn't have to be READY TO DEPLOY
        assert a.notes == notes
示例#13
0
def test_created_hardware(mocker):
    mock = mocker.MagicMock(return_value=HARDWARE_CREATED)
    asset = Asset()

    asset.id = HARDWARE_CREATED['payload']['id']
    asset.name = HARDWARE_CREATED['payload']['name']
    asset.tag = HARDWARE_CREATED['payload']['asset_tag']
    asset.model_id = HARDWARE_CREATED['payload']['model_id']
    asset.serial_number = HARDWARE_CREATED['payload']['serial']

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.create_data_at_api',
            mock):
        asset_id = APIc.create_hardware(asset, 1)

        assert asset_id == HARDWARE_CREATED['payload']['id']
示例#14
0
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
示例#15
0
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
示例#16
0
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
示例#17
0
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
示例#18
0
def test_cannot_create_hardware_duplicated(mocker):
    response = {
        'status': 'error',
        'messages': {
            'asset_tag': ['The asset tag must be unique.']
        },
        'payload': None
    }

    mock = mocker.MagicMock(return_value=response)
    asset = Asset()

    asset.id = HARDWARE_CREATED['payload']['id']
    asset.name = HARDWARE_CREATED['payload']['name']
    asset.tag = HARDWARE_CREATED['payload']['asset_tag']
    asset.model_id = HARDWARE_CREATED['payload']['model_id']
    asset.serial_number = HARDWARE_CREATED['payload']['serial']

    with mocker.patch(
            'Controller.StockAPIController.StockAPIController.create_data_at_api',
            mock):
        asset_id = APIc.create_hardware(asset, -1)
        assert asset_id == -1
示例#19
0
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
示例#20
0
 def __check_in(self):
     for asset in self.assets:
         StockAPIController.check_in(asset)
     self.__refresh_assets()