<commit_before>from decimal import Decimal
from unittest.mock import patch

from stripe.error import AuthenticationError, StripeError
from stripe.stripe_object import StripeObject

from saleor.payment.interface import PaymentMethodInfo
from saleor.payment.utils import price_to_minor_unit

from ..consts import (
    AUTOMATIC_CAPTURE_METHOD,
    MANUAL_CAPTURE_METHOD,
    METADATA_IDENTIFIER,
    STRIPE_API_VERSION,
    WEBHOOK_EVENTS,
)
from ..stripe_api import (
    cancel_payment_intent,
    capture_payment_intent,
    create_payment_intent,
    delete_webhook,
    get_or_create_customer,
    get_payment_method_details,
    is_secret_api_key_valid,
    list_customer_payment_methods,
    refund_payment_intent,
    retrieve_payment_intent,
    subscribe_webhook,
)


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_is_secret_api_key_valid_incorrect_key(mocked_webhook):
    api_key = "incorrect"
    mocked_webhook.list.side_effect = AuthenticationError()
    assert is_secret_api_key_valid(api_key) is False


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_is_secret_api_key_valid_correct_key(mocked_webhook):
    api_key = "correct_key"
    assert is_secret_api_key_valid(api_key) is True

    mocked_webhook.list.assert_called_with(api_key, stripe_version=STRIPE_API_VERSION)


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_subscribe_webhook_returns_webhook_object(mocked_webhook, channel_USD):
    api_key = "api_key"
    expected_url = (
        "http://mirumee.com/plugins/channel/main/saleor.payments.stripe/webhooks/"
    )

    subscribe_webhook(api_key, channel_slug=channel_USD.slug)

    mocked_webhook.create.assert_called_with(
        api_key=api_key,
        url=expected_url,
        enabled_events=WEBHOOK_EVENTS,
        metadata={METADATA_IDENTIFIER: "mirumee.com"},
        stripe_version=STRIPE_API_VERSION,
    )


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_delete_webhook(mocked_webhook):
    api_key = "api_key"

    delete_webhook(api_key, "webhook_id")

    mocked_webhook.delete.assert_called_with(
        "webhook_id", api_key=api_key, stripe_version=STRIPE_API_VERSION
    )


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_returns_intent_object(mocked_payment_intent):
    api_key = "api_key"
    mocked_payment_intent.create.return_value = StripeObject()

    intent, error = create_payment_intent(
        api_key, Decimal(10), "USD", auto_capture=True
    )

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=AUTOMATIC_CAPTURE_METHOD,
        stripe_version=STRIPE_API_VERSION,
    )

    assert isinstance(intent, StripeObject)
    assert error is None


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_with_customer(mocked_payment_intent):
    customer = StripeObject(id="c_ABC")
    api_key = "api_key"
    mocked_payment_intent.create.return_value = StripeObject()

    intent, error = create_payment_intent(
        api_key, Decimal(10), "USD", auto_capture=True, customer=customer
    )

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=AUTOMATIC_CAPTURE_METHOD,
        customer=customer,
        stripe_version=STRIPE_API_VERSION,
    )

    assert isinstance(intent, StripeObject)
    assert error is None


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_manual_auto_capture(mocked_payment_intent):
    api_key = "api_key"
    mocked_payment_intent.create.return_value = StripeObject()

    _intent, _error = create_payment_intent(
        api_key, Decimal(10), "USD", auto_capture=False
    )

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=MANUAL_CAPTURE_METHOD,
        stripe_version=STRIPE_API_VERSION,
    )


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_returns_error(mocked_payment_intent):
    api_key = "api_key"
    mocked_payment_intent.create.side_effect = StripeError(
        json_body={"error": "stripe-error"}
    )

    intent, error = create_payment_intent(api_key, Decimal(10), "USD")

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=AUTOMATIC_CAPTURE_METHOD,
        stripe_version=STRIPE_API_VERSION,
    )
    assert intent is None
    assert error


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_retrieve_payment_intent(mocked_payment_intent):
    api_key = "api_key"
    payment_intent_id = "id1234"

    mocked_payment_intent.retrieve.return_value = StripeObject()

    intent, _ = retrieve_payment_intent(api_key, payment_intent_id)

    mocked_payment_intent.retrieve.assert_called_with(
        payment_intent_id,
        api_key=api_key,
        stripe_version=STRIPE_API_VERSION,
        expand=["payment_method"],
    )
    assert isinstance(intent, StripeObject)


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_retrieve_payment_intent_stripe_returns_error(mocked_payment_intent):
    api_key = "api_key"
    payment_intent_id = "id1234"<commit_msg>Increase code coverage<commit_after>from decimal import Decimal
from unittest.mock import patch

from stripe.error import AuthenticationError, StripeError
from stripe.stripe_object import StripeObject

from saleor.payment.interface import PaymentMethodInfo
from saleor.payment.utils import price_to_minor_unit

from ..consts import (
    AUTOMATIC_CAPTURE_METHOD,
    MANUAL_CAPTURE_METHOD,
    METADATA_IDENTIFIER,
    STRIPE_API_VERSION,
    WEBHOOK_EVENTS,
)
from ..stripe_api import (
    cancel_payment_intent,
    capture_payment_intent,
    create_payment_intent,
    delete_webhook,
    get_or_create_customer,
    get_payment_method_details,
    is_secret_api_key_valid,
    list_customer_payment_methods,
    refund_payment_intent,
    retrieve_payment_intent,
    subscribe_webhook,
    update_payment_method,
)


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_is_secret_api_key_valid_incorrect_key(mocked_webhook):
    api_key = "incorrect"
    mocked_webhook.list.side_effect = AuthenticationError()
    assert is_secret_api_key_valid(api_key) is False


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_is_secret_api_key_valid_correct_key(mocked_webhook):
    api_key = "correct_key"
    assert is_secret_api_key_valid(api_key) is True

    mocked_webhook.list.assert_called_with(api_key, stripe_version=STRIPE_API_VERSION)


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_subscribe_webhook_returns_webhook_object(mocked_webhook, channel_USD):
    api_key = "api_key"
    expected_url = (
        "http://mirumee.com/plugins/channel/main/saleor.payments.stripe/webhooks/"
    )

    subscribe_webhook(api_key, channel_slug=channel_USD.slug)

    mocked_webhook.create.assert_called_with(
        api_key=api_key,
        url=expected_url,
        enabled_events=WEBHOOK_EVENTS,
        metadata={METADATA_IDENTIFIER: "mirumee.com"},
        stripe_version=STRIPE_API_VERSION,
    )


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.WebhookEndpoint",
)
def test_delete_webhook(mocked_webhook):
    api_key = "api_key"

    delete_webhook(api_key, "webhook_id")

    mocked_webhook.delete.assert_called_with(
        "webhook_id", api_key=api_key, stripe_version=STRIPE_API_VERSION
    )


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_returns_intent_object(mocked_payment_intent):
    api_key = "api_key"
    mocked_payment_intent.create.return_value = StripeObject()

    intent, error = create_payment_intent(
        api_key, Decimal(10), "USD", auto_capture=True
    )

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=AUTOMATIC_CAPTURE_METHOD,
        stripe_version=STRIPE_API_VERSION,
    )

    assert isinstance(intent, StripeObject)
    assert error is None


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_with_customer(mocked_payment_intent):
    customer = StripeObject(id="c_ABC")
    api_key = "api_key"
    mocked_payment_intent.create.return_value = StripeObject()

    intent, error = create_payment_intent(
        api_key, Decimal(10), "USD", auto_capture=True, customer=customer
    )

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=AUTOMATIC_CAPTURE_METHOD,
        customer=customer,
        stripe_version=STRIPE_API_VERSION,
    )

    assert isinstance(intent, StripeObject)
    assert error is None


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_manual_auto_capture(mocked_payment_intent):
    api_key = "api_key"
    mocked_payment_intent.create.return_value = StripeObject()

    _intent, _error = create_payment_intent(
        api_key, Decimal(10), "USD", auto_capture=False
    )

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=MANUAL_CAPTURE_METHOD,
        stripe_version=STRIPE_API_VERSION,
    )


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_create_payment_intent_returns_error(mocked_payment_intent):
    api_key = "api_key"
    mocked_payment_intent.create.side_effect = StripeError(
        json_body={"error": "stripe-error"}
    )

    intent, error = create_payment_intent(api_key, Decimal(10), "USD")

    mocked_payment_intent.create.assert_called_with(
        api_key=api_key,
        amount="1000",
        currency="USD",
        capture_method=AUTOMATIC_CAPTURE_METHOD,
        stripe_version=STRIPE_API_VERSION,
    )
    assert intent is None
    assert error


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentMethod",
)
def test_update_payment_method(mocked_payment_method):
    # given
    api_key = "api_key"
    payment_method_id = "1234"
    channel_slug = "channel_slug"
    metadata = {"key": "value"}

    # when
    update_payment_method(api_key, payment_method_id, channel_slug, metadata)

    # then
    mocked_payment_method.modify.assert_called_once_with(
        payment_method_id,
        api_key=api_key,
        metadata={**metadata, "channel": channel_slug},
    )


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_retrieve_payment_intent(mocked_payment_intent):
    api_key = "api_key"
    payment_intent_id = "id1234"

    mocked_payment_intent.retrieve.return_value = StripeObject()

    intent, _ = retrieve_payment_intent(api_key, payment_intent_id)

    mocked_payment_intent.retrieve.assert_called_with(
        payment_intent_id,
        api_key=api_key,
        stripe_version=STRIPE_API_VERSION,
        expand=["payment_method"],
    )
    assert isinstance(intent, StripeObject)


@patch(
    "saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent",
)
def test_retrieve_payment_intent_stripe_returns_error(mocked_payment_intent):
    api_key = "api_key"
    payment_intent_id = "id1234"
{'fraction_non_alphanumeric': 0.08644466868483014, 'fraction_numerical': 0.005718129835183316, 'mean_word_length': 4.302273740526081, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6342141', 'n_tokens_mistral': 4237, 'n_tokens_neox': 4032, 'n_words': 504}