Beispiel #1
0
def test_no_reports(fs, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()
    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'report',
            'list',
            '-d',
            './tests/fixtures/reports/no_reports',
        ],
    )

    assert result.exit_code == 1
    assert 'Invalid `reports.json`: [] is too short' in result.output
Beispiel #2
0
def test_not_valid_report_dir(fs, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    os.mkdir(f'{fs.root_path}/tmp2')
    runner = CliRunner()
    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'report',
            'list',
            '-d',
            f'{fs.root_path}/tmp2',
        ],
    )

    assert result.exit_code == 1
    assert f"The directory `{fs.root_path}/tmp2` is not a reports project root directory." in result.output
Beispiel #3
0
def test_activate_non_existent_account():
    config = Config()

    with pytest.raises(click.ClickException) as ex:
        config.activate('VA-999')

    assert ex.value.message == 'The account identified by VA-999 does not exist.'
Beispiel #4
0
def test_basic_report_5(fs, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()
    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'report',
            'info',
            'entrypoint_wrong',
            '-d',
            './tests/fixtures/reports/basic_report',
        ],
    )

    assert result.exit_code == 1
    assert 'Error: The report `entrypoint_wrong` does not exist.' in result.output
Beispiel #5
0
def test_basic_report(fs, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()
    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'report',
            'list',
            '-d',
            './tests/fixtures/reports/basic_report',
        ],
    )

    assert "Connect Reports Fixture version 1.0.0" in result.output
Beispiel #6
0
def test_list_products(fs, mocked_responses, ccli):
    with open('./tests/fixtures/product_response.json') as prod_response:
        mocked_responses.add(
            method='GET',
            url='https://localhost/public/v1/products',
            json=[json.load(prod_response)],
        )
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    assert os.path.isfile(f'{fs.root_path}/config.json') is True
    runner = CliRunner()
    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'product',
            'list',
        ],
    )

    assert result.exit_code == 0
    assert "PRD-276-377-545 - My Produc" in result.output
Beispiel #7
0
def test_report_generic_exception(fs, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()

    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'report',
            'execute',
            'entrypoint',
            '-d',
            './tests/fixtures/reports/generic_exception',
        ],
    )

    assert result.exit_code == 1
    assert "Unexpected error while executing the report" in result.output
Beispiel #8
0
def test_add_account():
    config = Config()
    config.add_account('VA-000', 'Account 1', 'ApiKey XXXX:YYYY')

    assert config.active is not None
    assert config.active.id == 'VA-000'
    assert config.active.name == 'Account 1'
    assert config.active.api_key == 'ApiKey XXXX:YYYY'
    assert config.active.endpoint == DEFAULT_ENDPOINT
Beispiel #9
0
def test_add_account_custom_endpoint():
    config = Config()
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://my_custom_endpoint',
    )

    assert config.active.endpoint == 'https://my_custom_endpoint'
Beispiel #10
0
def test_report_client_exception(fs, ccli):
    config = Config()
    config._config_path = f'{fs.root_path}/config.json'
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()

    result = runner.invoke(
        ccli,
        [
            '-c',
            f'{fs.root_path}/',
            'report',
            'execute',
            'entrypoint',
            '-d',
            './tests/fixtures/reports/connect_exception',
        ],
    )

    assert result.exit_code == 1
    assert "Error returned by Connect when executing the report" in result.output
Beispiel #11
0
def test_basic_report_2(fs, ccli):

    config = Config()
    config._config_path = f'{fs.root_path}/config.json'
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()
    result = runner.invoke(
        ccli,
        [
            '-c',
            f'{fs.root_path}/',
            'report',
            'info',
            'entrypoint',
            '-d',
            './tests/fixtures/reports/basic_report',
        ],
    )

    assert result.exit_code == 0
    assert "Basic report info" in result.output
Beispiel #12
0
def test_basic_report_3(fs, ccli):
    config = Config()
    config._config_path = f'{fs.root_path}/config.json'
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()
    result = runner.invoke(
        ccli,
        [
            '-c',
            f'{fs.root_path}/',
            'report',
            'execute',
            'invalid',
            '-d',
            './tests/fixtures/reports/basic_report',
        ],
    )

    assert result.exit_code == 1
    assert 'The report `invalid` does not exist.' in result.output
Beispiel #13
0
def test_remove_account(mocker):
    mock = mocker.patch.object(Config, 'store')
    config = Config()
    config.add_account('VA-000', 'Account 0', 'Api 0')
    config.add_account('VA-001', 'Account 1', 'Api 1')

    assert config.active.id == 'VA-000'

    acc = remove_account(config, 'VA-000')

    assert acc.id == 'VA-000'
    assert config.active.id == 'VA-001'
    mock.assert_called_once()
Beispiel #14
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()
Beispiel #15
0
def test_store(mocker):
    mock_open = mocker.mock_open()
    mocker.patch(
        'connect.cli.core.config.open',
        mock_open,
    )

    config = Config()
    config._config_path = '/tmp'
    config.add_account('VA-000', 'Account 1', 'ApiKey XXXX:YYYY')

    config.store()
    assert mock_open.mock_calls[0][1][1] == 'w'
    expected_output = {
        'active':
        'VA-000',
        'accounts': [
            {
                'id': 'VA-000',
                'name': 'Account 1',
                'api_key': 'ApiKey XXXX:YYYY',
                'endpoint': DEFAULT_ENDPOINT,
            },
        ],
    }
    assert json.loads(mock_open.mock_calls[2][1][0]) == expected_output
Beispiel #16
0
def test_add_account(mocker, mocked_responses):
    config = Config()
    config.add_account = mocker.MagicMock()
    config.store = mocker.MagicMock()
    mocked_responses.add(
        'GET',
        'https://localhost/public/v1/accounts',
        status=200,
        json=[{'id': 'VA-000', 'name': 'Test account'}],
    )
    acc_id, acc_name = add_account(config, 'api_key', 'https://localhost/public/v1')
    config.add_account.assert_called_once_with(
        'VA-000', 'Test account',
        'api_key', 'https://localhost/public/v1',
    )
    assert acc_id == 'VA-000'
    assert acc_name == 'Test account'
    config.store.assert_called()
Beispiel #17
0
def test_input_parameters(mocker, fs, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()

    mocker.patch(
        'connect.cli.plugins.report.wizard.dialogus',
        side_effect=[
            {
                'status': 'Active',
            },
            {
                'date': {
                    'from': '2021-01-01',
                    'to': '2021-02-01',
                },
            },
        ],
    )
    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'report',
            'execute',
            'entrypoint',
            '-d',
            './tests/fixtures/reports/report_with_inputs',
            '-o',
            f'{fs.root_path}/report.xlsx',
        ],
    )

    assert result.exit_code == 0
    assert "100%" in result.output
Beispiel #18
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'
def test_sync_general_sync(fs, get_general_env, mocked_responses, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    assert os.path.isfile(f'{fs.root_path}/config.json') is True

    with open('./tests/fixtures/units_response.json') as units_response:
        mocked_responses.add(
            method='GET',
            url='https://localhost/public/v1/settings/units',
            json=json.load(units_response),
        )

        with open('./tests/fixtures/product_response.json') as prod_response:
            mocked_responses.add(
                method='PUT',
                url='https://localhost/public/v1/products/PRD-276-377-545',
                json=json.load(prod_response),
            )
            get_general_env.save(f'{fs.root_path}/test.xlsx')

            runner = CliRunner()
            result = runner.invoke(
                ccli,
                [
                    '-c',
                    fs.root_path,
                    'product',
                    'sync',
                    '--yes',
                    f'{fs.root_path}/test.xlsx',
                ],
            )

            assert result.exit_code == 0
Beispiel #20
0
def test_product_2(mocked_responses, mocked_product_response):
    param = {
        "id": "product",
        "type": "product",
        "name": "Product list",
        "description": "Select the products you want to include in report",
    }

    config = Config()
    config.load('/tmp')
    config.add_account('PA-000', 'Account 0', 'Api 0',
                       'https://localhost/public/v1')

    client = ConnectClient(
        api_key='ApiKey X',
        endpoint='https://localhost/public/v1',
        use_specs=False,
    )

    mocked_responses.add(
        url='https://localhost/public/v1/products',
        method='GET',
        json=[mocked_product_response],
    )

    result = product_list(config.active, client, param)
    assert result['type'] == 'selectmany'
    assert len(result['values']) == 1
    assert result['values'][0] == ('PRD-276-377-545',
                                   'My Product (PRD-276-377-545)')
Beispiel #21
0
def test_execute_report_fail(mocker):
    mocker.patch('connect.cli.plugins.report.helpers.get_report_inputs')
    mocker.patch('connect.cli.plugins.report.helpers.get_renderer')
    ep_mock = mocker.MagicMock()
    mocker.patch(
        'connect.cli.plugins.report.helpers.get_report_entrypoint',
        return_value=ep_mock,
    )
    ep_mock.side_effect = Exception('this is an error')
    mocked_handle_exc = mocker.patch(
        'connect.cli.plugins.report.helpers.handle_report_exception')
    config = Config()
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
    )
    config.activate('VA-000')

    execute_report(
        config,
        './tests/fixtures/reports/basic_report',
        'entrypoint',
        'out_file',
        None,
    )

    mocked_handle_exc.assert_called_once()
Beispiel #22
0
def test_execute_report_invalid_renderer(mocker):
    config = Config()
    config.add_account(
        'VA-000-001',
        'Account 1',
        'ApiKey XXXX:YYYY',
    )
    config.activate('VA-000-001')

    mocker.patch('connect.cli.plugins.report.helpers.load_repo')

    mocker.patch(
        'connect.cli.plugins.report.helpers.get_report_by_id',
        return_value=mocker.MagicMock(
            renderers=[
                RendererDefinition('path', 'pdf', 'pdf', 'test'),
            ],
            audience=['vendor'],
        ),
    )

    with pytest.raises(ClickException) as cv:
        execute_report(config, 'root_dir', 'local_id', 'out_file', 'out_format')

    assert str(cv.value) == 'The format out_format is not available for report local_id'
Beispiel #23
0
def test_marketplace_list(mocked_responses):
    param = {
        "id": "mkp",
        "type": "marketplace",
        "name": "Marketplaces",
        "description": "Select the marketplaces you want to include in report",
    }

    config = Config()
    config.load('/tmp')
    config.add_account('VA-000', 'Account 0', 'Api 0',
                       'https://localhost/public/v1')

    client = ConnectClient(
        api_key='ApiKey X',
        endpoint='https://localhost/public/v1',
        use_specs=False,
    )

    mocked_responses.add(
        url='https://localhost/public/v1/marketplaces',
        method='GET',
        json=[
            {
                "id": "MKP-1",
                "name": "Marketplace",
            },
        ],
    )

    result = marketplace_list(config, client, param)
    assert result['type'] == 'selectmany'
    assert len(result['values']) == 1
    assert result['values'][0] == ('MKP-1', 'Marketplace (MKP-1)')
Beispiel #24
0
def test_basic_report_4(fs, ccli):
    config = Config()
    config.load(fs.root_path)
    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://localhost/public/v1',
    )
    config.activate('VA-000')
    config.store()
    runner = CliRunner()
    os.mkdir(f'{fs.root_path}/report')
    result = runner.invoke(
        ccli,
        [
            '-c',
            fs.root_path,
            'report',
            'execute',
            'entrypoint',
            '-d',
            './tests/fixtures/reports/basic_report',
            '-o'
            f'{fs.root_path}/report/report',
        ],
    )

    assert result.exit_code == 0
    assert "Processing report test report" in result.output

    wb = load_workbook(f'{fs.root_path}/report/report.xlsx')

    assert wb['Data']['A1'].value == 'Row'
    assert wb['Data']['A2'].value == 1
    assert wb['Data']['A3'].value == 2
    assert wb['Data']['A4'].value is None
Beispiel #25
0
def test_remove_activate_other(config_mocker, mocker):
    config = Config()
    config.load('/tmp')

    assert config.active.id == 'VA-000'

    config.remove_account('VA-000')

    assert config.active.id == 'VA-001'
Beispiel #26
0
def test_add_account_internal_server_error(mocked_responses):
    config = Config()
    mocked_responses.add(
        'GET',
        'https://localhost/public/v1/accounts',
        status=500,
        body=b'Internal Server Error',
    )

    with pytest.raises(click.ClickException) as ex:
        add_account(
            config,
            'ApiKey SU-000:xxxx',
            'https://localhost/public/v1',
        )
    assert ex.value.message == 'Unexpected error: 500 Internal Server Error'
Beispiel #27
0
def test_activate(config_mocker, mocker):
    config = Config()
    config.load('/tmp')

    assert config.active.id == 'VA-000'

    config.activate('VA-001')

    assert config.active is not None
    assert config.active.id == 'VA-001'
    assert config.active.name == 'Account 1'
    assert config.active.api_key == 'ApiKey ZZZZ:SSSS'
Beispiel #28
0
def test_config_validate():
    config = Config()

    with pytest.raises(click.ClickException) as ex:
        config.validate()

    assert ex.value.message == 'connect-cli is not properly configured.'

    config.add_account(
        'VA-000',
        'Account 1',
        'ApiKey XXXX:YYYY',
        endpoint='https://my_custom_endpoint',
    )

    assert config.validate() is None
Beispiel #29
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()
Beispiel #30
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'