예제 #1
0
def test_inject(
    config_mocker,
    fs,
    mocked_responses,
    mocker,
):
    config = Config()
    config.load('/tmp')
    config.add_account('VA-000', 'Account 0', 'Api 0',
                       'https://localhost/public/v1')

    cloner = ProductCloner(
        config=config,
        source_account='VA-000',
        destination_account='VA-000',
        product_id='PRD-123',
    )

    os.mkdir(os.path.join(
        cloner.fs.root_path,
        'PRD-123',
    ), )
    wb = load_workbook('./tests/fixtures/comparation_product.xlsx')
    wb.save(os.path.join(
        cloner.fs.root_path,
        'PRD-123',
        'PRD-123.xlsx',
    ), )

    mocker.patch('connect.cli.plugins.product.clone.GeneralSynchronizer', )
    mocker.patch(
        'connect.cli.plugins.product.clone.CapabilitiesSynchronizer', )
    mocker.patch('connect.cli.plugins.product.clone.TemplatesSynchronizer', )
    mocker.patch('connect.cli.plugins.product.clone.ParamsSynchronizer', )
    mocker.patch('connect.cli.plugins.product.clone.ActionsSynchronizer', )
    mocker.patch('connect.cli.plugins.product.clone.MediaSynchronizer', )
    mocker.patch(
        'connect.cli.plugins.product.clone.ItemSynchronizer',
        return_value=FakeItemSynchronizer,
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/items',
        json=[],
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/public/v1/products/PRD-276-377-545/templates?limit=100&offset=0',
        json=[],
    )
    cloner.inject()
예제 #2
0
def test_dump(mocker, config_mocker):
    mock = mocker.patch('connect.cli.plugins.product.clone.dump_product', )
    config = Config()
    config.load('/tmp')
    cloner = ProductCloner(
        config=config,
        source_account='VA-000',
        destination_account='VA-000',
        product_id='PRD-123',
    )

    cloner.dump()

    mock.assert_called_once()
예제 #3
0
def test_create_product(
    config_mocker,
    fs,
    mocked_responses,
    mocked_categories_response,
    mocked_product_response,
):
    config = Config()
    config.load('/tmp')
    config.add_account('VA-000', 'Account 0', 'Api 0',
                       'https://localhost/public/v1')

    cloner = ProductCloner(
        config=config,
        source_account='VA-000',
        destination_account='VA-000',
        product_id='PRD-123',
    )

    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/categories?limit=100&offset=0',
        json=mocked_categories_response,
    )
    mocked_responses.add(
        method='POST',
        url='https://localhost/public/v1/products',
        json=mocked_product_response,
    )

    os.mkdir(os.path.join(
        cloner.fs.root_path,
        'PRD-123',
    ), )
    wb = load_workbook('./tests/fixtures/comparation_product.xlsx')
    wb.save(os.path.join(
        cloner.fs.root_path,
        'PRD-123',
        'PRD-123.xlsx',
    ), )
    cloner.load_wb()
    cloner.create_product()

    wb = load_workbook(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
            'PRD-123.xlsx',
        ), )

    assert wb['General Information']['B5'].value == 'PRD-276-377-545'
예제 #4
0
def test_create_product_errordef(
    config_mocker,
    fs,
    mocked_responses,
    mocked_categories_response,
    mocked_product_response,
):
    config = Config()
    config.load('/tmp')
    config.add_account('VA-000', 'Account 0', 'Api 0', 'https://localhost/public/v1')

    stats = SynchronizerStats()
    cloner = ProductCloner(
        config=config,
        source_account='VA-000',
        destination_account='VA-000',
        product_id='PRD-123',
        stats=stats,
    )

    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/categories?limit=100&offset=0',
        json=mocked_categories_response,
    )
    mocked_responses.add(
        method='POST',
        url='https://localhost/public/v1/products',
        status=500,
    )

    os.mkdir(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
        ),
    )
    wb = load_workbook('./tests/fixtures/comparation_product.xlsx')
    wb.save(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
            'PRD-123.xlsx',
        ),
    )
    cloner.load_wb()
    with pytest.raises(ClickException) as e:
        cloner.create_product()

    assert 'Error on product creation' in str(e)
예제 #5
0
def test_clean_wb(
    config_mocker,
    fs,
):
    config = Config()
    config.load('/tmp')

    stats = SynchronizerStats()
    cloner = ProductCloner(
        config=config,
        source_account='VA-000',
        destination_account='VA-000',
        product_id='PRD-123',
        stats=stats,
    )

    os.mkdir(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
        ),
    )
    wb = load_workbook('./tests/fixtures/comparation_product.xlsx')
    wb.save(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
            'PRD-123.xlsx',
        ),
    )
    cloner.load_wb()
    cloner.clean_wb()

    cloned_wb = load_workbook(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
            'PRD-123.xlsx',
        ),
    )

    for row in range(2, 11):
        assert cloned_wb['Capabilities'][f'B{row}'].value == 'update'
예제 #6
0
def cmd_clone_products(config, source_product_id, source_account,
                       destination_account, name, yes):
    if name and len(name) > 32:
        click.echo(
            click.style(
                f'New product name can not exceed 32 chracters, provided as name{name}',
                fg='red',
            ), )
        exit(-1)
    if destination_account:
        config.activate(destination_account)
        config.validate()
    else:
        destination_account = config.active.id

    if source_account:
        config.activate(source_account)
        config.validate()
    else:
        source_account = config.active.id

    config.validate()

    acc_id = config.active.id
    acc_name = config.active.name

    if not config.silent:
        click.echo(
            click.style(
                f'Current active account: {acc_id} - {acc_name}\n',
                fg='blue',
            ), )

    client = ConnectClient(
        api_key=config.active.api_key,
        endpoint=config.active.endpoint,
        use_specs=False,
        max_retries=3,
    )

    if not yes:
        click.confirm(
            'Are you sure you want to Clone '
            f'the product {source_product_id} ?',
            abort=True,
        )
        click.echo('')

    try:
        client.products[source_product_id].get()
    except ClientError:
        click.echo(
            click.style(
                f'Product {source_product_id} does not exist',
                fg='red',
            ), )
        exit(-1)

    synchronizer = ProductCloner(
        config=config,
        source_account=source_account,
        destination_account=destination_account,
        product_id=source_product_id,
    )

    if not config.silent:
        click.echo(
            click.style(
                f'Dumping Product {synchronizer.product_id} from account '
                f'{synchronizer.source_account}\n',
                fg='blue',
            ), )

    synchronizer.dump()
    synchronizer.load_wb()

    if not config.silent:
        click.echo(
            click.style(
                f'Creating new Product on account {synchronizer.destination_account}',
                fg='blue',
            ), )

    synchronizer.create_product(name=name)
    synchronizer.clean_wb()

    if not config.silent:
        click.echo(click.style(
            'Injecting Product information',
            fg='blue',
        ), )

    synchronizer.inject()

    if not config.silent:
        click.echo(
            click.style(
                f'Finished cloning product {source_product_id} from account '
                f'{synchronizer.source_account} to {synchronizer.destination_account}\n',
                fg='green',
            ), )

        click.echo(
            click.style(
                f'New product id {synchronizer.destination_product}',
                fg='green',
            ), )
예제 #7
0
def cmd_clone_products(config, source_product_id, source_account,
                       destination_account, name, yes):
    if not config.active.is_vendor():
        raise ClickException(
            'The clone command is only available for vendor accounts.', )
    if name and len(name) > 32:
        click.secho(
            f'New product name can not exceed 32 chracters, provided as name {name}',
            fg='red',
        )
        exit(-1)
    if destination_account:
        config.activate(destination_account)
    else:
        destination_account = config.active.id

    if source_account:
        config.activate(source_account)
    else:
        source_account = config.active.id

    acc_id = config.active.id
    acc_name = config.active.name

    if not config.silent:
        click.secho(
            f'Current active account: {acc_id} - {acc_name}\n',
            fg='blue',
        )

    client = ConnectClient(
        api_key=config.active.api_key,
        endpoint=config.active.endpoint,
        use_specs=False,
        max_retries=3,
        logger=RequestLogger() if config.verbose else None,
    )

    if not yes:
        click.confirm(
            'Are you sure you want to Clone '
            f'the product {source_product_id} ?',
            abort=True,
        )
        click.echo('')

    try:
        client.products[source_product_id].get()
    except ClientError:
        click.secho(
            f'Product {source_product_id} does not exist',
            fg='red',
        )
        exit(-1)
    stats = SynchronizerStats(operation='Clone')
    stats.RESULTS_HEADER = stats.RESULTS_HEADER.replace(
        "synchronization",
        f"cloning {source_product_id}",
    )
    synchronizer = ProductCloner(
        config=config,
        source_account=source_account,
        destination_account=destination_account,
        product_id=source_product_id,
        stats=stats,
    )

    if not config.silent:
        click.secho(
            f'Dumping Product {synchronizer.product_id} from account '
            f'{synchronizer.source_account}\n',
            fg='blue',
        )

    synchronizer.dump()
    synchronizer.load_wb()

    if not config.silent:
        click.secho(
            f'Creating new Product on account {synchronizer.destination_account}',
            fg='blue',
        )

    synchronizer.create_product(name=name)
    synchronizer.clean_wb()

    if not config.silent:
        click.secho(
            'Injecting Product information',
            fg='blue',
        )

    synchronizer.inject()

    if not config.silent:
        click.secho(
            f'Finished cloning product {source_product_id} from account '
            f'{synchronizer.source_account} to {synchronizer.destination_account}\n',
            fg='green',
        )

        click.secho(
            f'New product id {synchronizer.destination_product}',
            fg='green',
        )
    stats.print()
예제 #8
0
def test_create_product(
    mocked_responses,
    mocked_categories_response,
    mocked_product_response,
):
    config = Config()
    config.load('/tmp')
    config.add_account('VA-000', 'Account 0', 'Api 0', 'https://localhost/public/v1')

    stats = SynchronizerStats()
    cloner = ProductCloner(
        config=config,
        source_account='VA-000',
        destination_account='VA-000',
        product_id='PRD-123',
        stats=stats,
    )

    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/categories?limit=100&offset=0',
        json=mocked_categories_response,
    )
    mocked_responses.add(
        method='POST',
        url='https://localhost/public/v1/products',
        match=[
            matchers.json_params_matcher({
                'name': 'Clone of PRD-123 2022-04-05-20:15:00',
                'category': {
                    'id': 'CAT-59128',
                },
                'translations': [
                    {'locale': {'id': 'FA'}, 'primary': True},
                ],
            }),
        ],
        json=mocked_product_response,
    )

    os.mkdir(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
        ),
    )
    wb = load_workbook('./tests/fixtures/comparation_product.xlsx')
    wb.save(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
            'PRD-123.xlsx',
        ),
    )
    cloner.load_wb()
    cloner.create_product()

    wb = load_workbook(
        os.path.join(
            cloner.fs.root_path,
            'PRD-123',
            'PRD-123.xlsx',
        ),
    )

    assert wb['General Information']['B5'].value == 'PRD-276-377-545'