Exemple #1
0
def test_validate_store_keys(name, valid):
    ct = types.CommercetoolsSettings(
        project_key="ct-unit-test",
        client_id="a96e59be-24da-4f41-a6cf-d61d7b6e1766",
        client_secret="98c32de8-1a6c-45a9-a718-d3cce5201799",
        scopes="manage_project:ct-unit-test",
        project_settings=types.CommercetoolsProjectSettings(
            languages=["nl-NL"],
            countries=["NL"],
            currencies=["EUR"],
        ),
        stores=[
            types.CommercetoolsStore(
                key=name,
                name={
                    "en-GB": "Default store",
                },
            ),
        ],
    )

    if not valid:
        with pytest.raises(ValidationError):
            validate.validate_store_keys(ct)
    else:
        validate.validate_store_keys(ct)

    ct.stores[0].key = "main-store"
    validate.validate_store_keys(ct)

    ct.stores.append(
        types.CommercetoolsStore(
            key="main-store",
            name={
                "en-GB": "Another store",
            },
        ), )

    with pytest.raises(ValidationError):
        # Duplicate key
        validate.validate_store_keys(ct)

    ct.stores[1].key = "other-store"
    validate.validate_store_keys(ct)
Exemple #2
0
def test_validate_taxes_tax_categories(parsed_config: types.MachConfig):
    """Tests if an error is raised when taxes and tax_categories are used in conjunction."""
    config = parsed_config
    site = config.sites[0]

    site.commercetools = types.CommercetoolsSettings(
        project_key="ct-unit-test",
        client_id="a96e59be-24da-4f41-a6cf-d61d7b6e1766",
        client_secret="98c32de8-1a6c-45a9-a718-d3cce5201799",
        scopes="manage_project:ct-unit-test",
        project_settings=types.CommercetoolsProjectSettings(
            languages=["nl-NL"],
            countries=["NL"],
            currencies=["EUR"],
        ),
        taxes=[types.CommercetoolsTax(name="foo tax", amount=1, country="NL")],
    )

    validate.validate_commercetools(site)

    site.commercetools.tax_categories = [
        types.CommercetoolsTaxCategory(
            name="foo category",
            key="foo-key",
            rates=[
                types.CommercetoolsTax(
                    name="foo category tax",
                    amount=2,
                    country="NL",
                    included_in_price=True,
                )
            ],
        )
    ]

    with pytest.raises(ValidationError):
        validate.validate_commercetools(site)
Exemple #3
0
def test_validate_stores(parsed_config: types.MachConfig):
    """Tests if the stores used in the store variables match the defined commercetools stores."""
    config = parsed_config
    site = config.sites[0]

    site.components[0].store_variables = {
        "main-store": {
            "FOO": "BAR",
        }
    }

    # It should fail because we refer a store that hasnt been defined yet
    with pytest.raises(ValidationError):
        validate.validate_config(config)

    site.commercetools = types.CommercetoolsSettings(
        project_key="ct-unit-test",
        client_id="a96e59be-24da-4f41-a6cf-d61d7b6e1766",
        client_secret="98c32de8-1a6c-45a9-a718-d3cce5201799",
        scopes="manage_project:ct-unit-test",
        project_settings=types.CommercetoolsProjectSettings(
            languages=["nl-NL"],
            countries=["NL"],
            currencies=["EUR"],
        ),
        stores=[
            types.CommercetoolsStore(
                name={
                    "en-GB": "Default store",
                },
                key="main-store",
            ),
        ],
    )

    validate.validate_config(config)
Exemple #4
0
def test_generate_w_stores(config: types.MachConfig, tf_mock):
    config.sites[0].commercetools = types.CommercetoolsSettings(
        project_key="ct-unit-test",
        client_id="a96e59be-24da-4f41-a6cf-d61d7b6e1766",
        client_secret="98c32de8-1a6c-45a9-a718-d3cce5201799",
        scopes="manage_project:ct-unit-test",
        project_settings=types.CommercetoolsProjectSettings(
            languages=["nl-NL"],
            countries=["NL"],
            currencies=["EUR"],
        ),
        stores=[
            types.CommercetoolsStore(
                name={
                    "en-GB": "Default store",
                },
                key="main-store",
            ),
            types.CommercetoolsStore(
                name={
                    "en-GB": "Some other store",
                },
                key="other-store",
            ),
            types.CommercetoolsStore(
                name={
                    "en-GB": "Forgotten store",
                },
                key="forgotten-store",
            ),
        ],
    )
    config.components[0].integrations = ["aws", "commercetools"]
    data = tf.generate(parse.parse_config(config))

    assert len(data.resource.commercetools_store) == 3
    assert "main-store" in data.resource.commercetools_store
    assert "other-store" in data.resource.commercetools_store
    assert "forgotten-store" in data.resource.commercetools_store

    assert len(data.module["api-extensions"].ct_stores) == 3

    for store_key, store in data.module["api-extensions"].ct_stores.items():
        assert store["key"] == store_key
        assert not store["variables"]
        assert not store["secrets"]

    config.sites[0].components[0].store_variables = {
        "main-store": {
            "FOO": "BAR",
            "EXTRA": "VALUES",
        },
        "other-store": {
            "FOO": "SOMETHING ELSE",
        },
    }
    config.sites[0].components[0].store_secrets = {
        "main-store": {
            "PAYMENT_KEY": "TLrlDf6XhKkXFGGHeQGY",
        },
    }

    data = tf.generate(parse.parse_config(config))
    main_store = data.module["api-extensions"].ct_stores["main-store"]
    other_store = data.module["api-extensions"].ct_stores["other-store"]
    assert len(main_store.variables) == 2
    assert len(other_store.variables) == 1
    assert len(main_store.secrets) == 1
    assert not other_store.secrets
Exemple #5
0
def test_commercetools_frontend_credentials(config: types.MachConfig, tf_mock):
    config.sites[0].commercetools = types.CommercetoolsSettings(
        project_key="ct-unit-test",
        client_id="a96e59be-24da-4f41-a6cf-d61d7b6e1766",
        client_secret="98c32de8-1a6c-45a9-a718-d3cce5201799",
        scopes="manage_project:ct-unit-test",
        project_settings=types.CommercetoolsProjectSettings(
            languages=["nl-NL"],
            countries=["NL"],
            currencies=["EUR"],
        ),
        stores=[
            types.CommercetoolsStore(
                name={
                    "en-GB": "Default store",
                },
                key="main-store",
            ),
            types.CommercetoolsStore(
                name={
                    "en-GB": "Some other store",
                },
                key="other-store",
            ),
            types.CommercetoolsStore(
                name={
                    "en-GB": "Forgotten store",
                },
                key="forgotten-store",
            ),
        ],
    )
    data = tf.generate(parse.parse_config(config))
    assert list(data.resource.commercetools_api_client.keys()) == [
        "frontend_credentials_main-store",
        "frontend_credentials_other-store",
        "frontend_credentials_forgotten-store",
    ]

    assert data.resource.commercetools_api_client[
        "frontend_credentials_main-store"].scope == [
            "create_anonymous_token:ct-unit-test",
            "manage_my_profile:ct-unit-test",
            "manage_my_profile:ct-unit-test:main-store",
            "manage_my_orders:ct-unit-test",
            "manage_my_orders:ct-unit-test:main-store",
            "manage_my_shopping_lists:ct-unit-test",
            "manage_my_payments:ct-unit-test",
            "view_products:ct-unit-test",
            "view_project_settings:ct-unit-test",
        ]

    config.sites[0].commercetools.frontend.create_credentials = False
    data = tf.generate(parse.parse_config(config))
    assert "commercetools_api_client" not in data.resource

    config.sites[0].commercetools.frontend.create_credentials = True
    config.sites[0].commercetools.frontend.permission_scopes = [
        "manage_my_profile",
        "manage_my_orders",
        "view_products",
        "manage_my_payments",
        "create_anonymous_token",
        "view_stores",
    ]

    data = tf.generate(parse.parse_config(config))
    assert data.resource.commercetools_api_client[
        "frontend_credentials_main-store"].scope == [
            "manage_my_profile:ct-unit-test",
            "manage_my_profile:ct-unit-test:main-store",
            "manage_my_orders:ct-unit-test",
            "manage_my_orders:ct-unit-test:main-store",
            "view_products:ct-unit-test",
            "manage_my_payments:ct-unit-test",
            "create_anonymous_token:ct-unit-test",
            "view_stores:ct-unit-test",
        ]
Exemple #6
0
def _create_config() -> types.MachConfig:  # noqa: C901
    environment = click.prompt("Environment", "test")
    cloud = click.prompt("Cloud environment",
                         type=click.Choice(["aws", "azure"]),
                         default="aws")
    site_id = click.prompt("Site identifier")
    use_commercetools = click.confirm("Use commercetools?", default=True)
    ct_project = ""
    if use_commercetools:
        ct_project = click.prompt("commercetools project name",
                                  default=site_id)
    use_sentry = click.confirm("Use Sentry?", default=False)
    use_contentful = click.confirm("Use Contentful?", default=False)
    use_amplience = click.confirm("Use Amplience?", default=False)

    integrations = []
    if use_commercetools:
        integrations.append("commercetools")
    if use_sentry:
        integrations.append("sentry")
    if use_contentful:
        integrations.append("contentful")
    if use_amplience:
        integrations.append("amplience")
    # If we do have integrations, add the default (cloud) integration here as well
    if integrations:
        integrations = [cloud] + integrations

    if cloud == "aws":
        tf_config = types.TerraformConfig(aws_remote_state=types.AWSTFState(
            bucket="<your bucket>",
            key_prefix="mach",
            region="eu-central-1",
        ))
    else:
        tf_config = types.TerraformConfig(
            azure_remote_state=types.AzureTFState(
                resource_group="<your-resource-group>",
                storage_account="<your-storage-account>",
                container_name="<your-container-name>",
                state_folder=environment,
            ))

    general_config_kwargs = dict(
        environment=environment,
        terraform_config=tf_config,
        cloud=cloud,
    )

    if cloud == "azure":
        general_config_kwargs["azure"] = types.AzureConfig(
            tenant_id="<your-tenant-id>",
            subscription_id="<your-subscription-id>",
            region="westeurope",
        )

    if use_sentry:
        general_config_kwargs["sentry"] = types.SentryConfig(
            auth_token="<your-auth-token>",
            project="<your-project>",
            organization="<your-organization>",
        )

    if use_contentful:
        general_config_kwargs["contentful"] = types.ContentfulConfig(
            cma_token="<your-cma-token>",
            organization_id="<your-organization-id>",
        )

    if use_amplience:
        general_config_kwargs["amplience"] = types.AmplienceConfig(
            client_id="<your-client-id>",
            client_secret="<your-client-secret>",
        )

    site = types.Site(
        identifier=site_id,
        components=[
            types.Component(
                name="your-component",
                variables={"FOO_VAR": "my-value"},
                secrets={"MY_SECRET": "secretvalue"},
            )
        ],
    )

    if cloud == "aws":
        site.aws = types.SiteAWSSettings(
            account_id=123456789,
            region="eu-central-1",
        )

    if use_commercetools:
        site.commercetools = types.CommercetoolsSettings(
            project_key=ct_project,
            client_id="<client-id>",
            client_secret="<client-secret>",
            scopes=(f"manage_api_clients:{ct_project} "
                    f"manage_project:{ct_project} "
                    f"view_api_clients:{ct_project}"),
            project_settings=types.CommercetoolsProjectSettings(
                # TODO: Improve this by letting user select one or more countries
                # and generate/guess the correct languages and currencies that probably
                # need to be applied for that project
                languages=["nl-NL"],
                countries=["NL"],
                currencies=["EUR"],
            ),
        )

    component_config = types.ComponentConfig(
        name="your-component",
        source=
        "git::https://github.com/<username>/<your-component>.git//terraform",
        version="0.1.0",
        integrations=integrations,
    )
    if cloud == "azure":
        component_config.azure = types.ComponentAzureConfig(
            short_name="yourcomp", )

    return types.MachConfig(
        mach_composer=types.MachComposerConfig(version=__version__),
        general_config=types.GlobalConfig(**general_config_kwargs, ),
        sites=[site],
        components=[component_config],
    )