Best Python code snippet using yandex-tank
test_stripe.py
Source:test_stripe.py
1import os2from decimal import Decimal3from math import isclose4import pytest5from .... import ChargeStatus6from ....interface import CreditCardInfo, CustomerSource, GatewayConfig7from ....utils import create_payment_information8from .. import (9 TransactionKind,10 _get_client,11 authorize,12 capture,13 confirm,14 get_client_token,15 list_client_sources,16 refund,17 void,18)19TRANSACTION_AMOUNT = Decimal(42.42)20TRANSACTION_REFUND_AMOUNT = Decimal(24.24)21TRANSACTION_CURRENCY = "USD"22PAYMENT_METHOD_CARD_SIMPLE = "pm_card_pl"23CARD_SIMPLE_DETAILS = CreditCardInfo(24 last_4="0005", exp_year=2020, exp_month=8, brand="visa"25)26PAYMENT_METHOD_CARD_3D_SECURE = "pm_card_threeDSecure2Required"27# Set to True if recording new cassette with sandbox using credentials in env28RECORD = False29@pytest.fixture()30def gateway_config():31 return GatewayConfig(32 gateway_name="stripe",33 auto_capture=True,34 supported_currencies="USD",35 connection_params={36 "public_key": "public",37 "private_key": "secret",38 "store_name": "Saleor",39 "store_image": "image.gif",40 "prefill": True,41 "remember_me": True,42 "locale": "auto",43 "enable_billing_address": False,44 "enable_shipping_address": False,45 },46 )47@pytest.fixture()48def sandbox_gateway_config(gateway_config):49 if RECORD:50 connection_params = {51 "public_key": os.environ.get("STRIPE_PUBLIC_KEY"),52 "private_key": os.environ.get("STRIPE_SECRET_KEY"),53 }54 gateway_config.connection_params.update(connection_params)55 return gateway_config56@pytest.fixture()57def stripe_payment(payment_dummy):58 payment_dummy.total = TRANSACTION_AMOUNT59 payment_dummy.currency = TRANSACTION_CURRENCY60 return payment_dummy61@pytest.mark.integration62@pytest.mark.vcr(filter_headers=["authorization"])63def test_authorize(sandbox_gateway_config, stripe_payment):64 payment_info = create_payment_information(65 stripe_payment, PAYMENT_METHOD_CARD_SIMPLE66 )67 response = authorize(payment_info, sandbox_gateway_config)68 assert not response.error69 assert response.kind == TransactionKind.CAPTURE70 assert isclose(response.amount, TRANSACTION_AMOUNT)71 assert response.currency == TRANSACTION_CURRENCY72 assert response.is_success is True73 assert response.card_info == CARD_SIMPLE_DETAILS74 assert not response.action_required75@pytest.mark.integration76@pytest.mark.vcr(filter_headers=["authorization"])77def test_authorize_error_response(stripe_payment, sandbox_gateway_config):78 INVALID_METHOD = "abcdefghijklmnoprstquwz"79 payment_info = create_payment_information(stripe_payment, INVALID_METHOD)80 response = authorize(payment_info, sandbox_gateway_config)81 assert response.error == "No such payment_method: " + INVALID_METHOD82 assert response.transaction_id == INVALID_METHOD83 assert response.kind == TransactionKind.CAPTURE84 assert not response.is_success85 assert response.amount == stripe_payment.total86 assert response.currency == stripe_payment.currency87 assert not response.action_required88@pytest.mark.integration89@pytest.mark.vcr(filter_headers=["authorization"])90def test_authorize_3d_secure(stripe_payment, sandbox_gateway_config):91 payment_info = create_payment_information(92 stripe_payment, PAYMENT_METHOD_CARD_3D_SECURE93 )94 response = authorize(payment_info, sandbox_gateway_config)95 assert not response.error96 assert response.kind == TransactionKind.CAPTURE97 assert isclose(response.amount, TRANSACTION_AMOUNT)98 assert response.currency == TRANSACTION_CURRENCY99 assert response.is_success is True100 assert response.action_required101@pytest.mark.integration102@pytest.mark.vcr(filter_headers=["authorization"])103def test_authorize_without_capture(stripe_payment, sandbox_gateway_config):104 sandbox_gateway_config.auto_capture = False105 payment_info = create_payment_information(106 stripe_payment, PAYMENT_METHOD_CARD_SIMPLE107 )108 response = authorize(payment_info, sandbox_gateway_config)109 assert not response.error110 assert response.kind == TransactionKind.AUTH111 assert isclose(response.amount, TRANSACTION_AMOUNT)112 assert response.currency == TRANSACTION_CURRENCY113 assert response.is_success is True114@pytest.mark.integration115@pytest.mark.vcr(filter_headers=["authorization"])116def test_authorize_and_save_customer_id(payment_dummy, sandbox_gateway_config):117 CUSTOMER_ID = "cus_FbquUfgBnLdlsY" # retrieved from sandbox118 payment = payment_dummy119 payment_info = create_payment_information(payment, PAYMENT_METHOD_CARD_SIMPLE)120 sandbox_gateway_config.store_customer = True121 response = authorize(payment_info, sandbox_gateway_config)122 assert not response.error123 assert response.customer_id == CUSTOMER_ID124@pytest.mark.integration125@pytest.mark.vcr(filter_headers=["authorization"])126def test_authorize_with_customer_id(payment_dummy, sandbox_gateway_config):127 CUSTOMER_ID = "cus_FbquUfgBnLdlsY" # retrieved from sandbox128 payment = payment_dummy129 payment_info = create_payment_information(payment, "pm_card_visa")130 payment_info.amount = TRANSACTION_AMOUNT131 payment_info.customer_id = CUSTOMER_ID132 payment_info.reuse_source = True133 response = authorize(payment_info, sandbox_gateway_config)134 assert not response.error135 assert response.is_success136@pytest.fixture()137def stripe_authorized_payment(stripe_payment):138 stripe_payment.charge_status = ChargeStatus.NOT_CHARGED139 stripe_payment.save(update_fields=["charge_status"])140 return stripe_payment141@pytest.mark.integration142@pytest.mark.vcr(filter_headers=["authorization"])143def test_capture(stripe_authorized_payment, sandbox_gateway_config):144 # Get id from sandbox for intent not yet captured145 INTENT_ID = "pi_1F5BsRIUmJaD6Oqvz2XMKZCD"146 payment_info = create_payment_information(147 stripe_authorized_payment, payment_token=INTENT_ID148 )149 response = capture(payment_info, sandbox_gateway_config)150 assert not response.error151 assert response.transaction_id == INTENT_ID152 assert response.kind == TransactionKind.CAPTURE153 assert response.is_success154 assert isclose(response.amount, TRANSACTION_AMOUNT)155 assert response.currency == TRANSACTION_CURRENCY156 assert response.card_info == CARD_SIMPLE_DETAILS157@pytest.mark.integration158@pytest.mark.vcr(filter_headers=["authorization"])159def test_capture_3d_secure(stripe_payment, sandbox_gateway_config):160 PAYMENT_INTENT = "pi_1F6YmgIUmJaD6Oqv77HUh6qq"161 ERROR = (162 "This PaymentIntent could not be captured because it"163 " has a status of requires_action."164 " Only a PaymentIntent with one of the following "165 "statuses may be captured: requires_capture."166 )167 payment_info = create_payment_information(stripe_payment, PAYMENT_INTENT)168 response = capture(payment_info, sandbox_gateway_config)169 assert response.error == ERROR170 assert response.kind == TransactionKind.CAPTURE171 assert isclose(response.amount, TRANSACTION_AMOUNT)172 assert response.currency == TRANSACTION_CURRENCY173 assert not response.is_success174 assert response.action_required175@pytest.mark.integration176@pytest.mark.vcr(filter_headers=["authorization"])177def test_capture_error_response(stripe_payment, sandbox_gateway_config):178 INVALID_INTENT = "THIS_INTENT_DOES_NOT_EXISTS"179 payment_info = create_payment_information(stripe_payment, INVALID_INTENT)180 response = capture(payment_info, sandbox_gateway_config)181 assert response.error == "No such payment_intent: " + INVALID_INTENT182 assert response.transaction_id == INVALID_INTENT183 assert response.kind == TransactionKind.CAPTURE184 assert not response.is_success185 assert response.amount == stripe_payment.total186 assert response.currency == stripe_payment.currency187@pytest.fixture()188def stripe_paid_payment(stripe_payment):189 stripe_payment.charge_status = ChargeStatus.FULLY_CHARGED190 stripe_payment.save(update_fields=["charge_status"])191 return stripe_payment192@pytest.mark.integration193@pytest.mark.vcr(filter_headers=["authorization"])194def test_refund(stripe_paid_payment, sandbox_gateway_config):195 # Get id from sandbox for succeeded payment196 REFUND_AMOUNT = Decimal(10.0) # partial refund197 INTENT_ID = "pi_1F5BsRIUmJaD6Oqvz2XMKZCD"198 payment_info = create_payment_information(199 stripe_paid_payment, amount=REFUND_AMOUNT, payment_token=INTENT_ID200 )201 response = refund(payment_info, sandbox_gateway_config)202 assert not response.error203 assert response.transaction_id == INTENT_ID204 assert response.kind == TransactionKind.REFUND205 assert response.is_success206 assert isclose(response.amount, REFUND_AMOUNT)207 assert response.currency == TRANSACTION_CURRENCY208@pytest.mark.integration209@pytest.mark.vcr(filter_headers=["authorization"])210def test_refund_error_response(stripe_payment, sandbox_gateway_config):211 INVALID_INTENT = "THIS_INTENT_DOES_NOT_EXISTS"212 payment_info = create_payment_information(stripe_payment, INVALID_INTENT)213 response = refund(payment_info, sandbox_gateway_config)214 assert response.error == "No such payment_intent: " + INVALID_INTENT215 assert response.transaction_id == INVALID_INTENT216 assert response.kind == TransactionKind.REFUND217 assert not response.is_success218 assert response.amount == stripe_payment.total219 assert response.currency == stripe_payment.currency220@pytest.mark.integration221@pytest.mark.vcr(filter_headers=["authorization"])222def test_void(stripe_paid_payment, sandbox_gateway_config):223 # Get id from sandbox for succedeed payment224 INTENT_ID = "pi_1F5BsOIUmJaD6Oqvmh5vBJIA"225 payment_info = create_payment_information(226 stripe_paid_payment, payment_token=INTENT_ID227 )228 response = void(payment_info, sandbox_gateway_config)229 assert not response.error230 assert response.transaction_id == INTENT_ID231 assert response.kind == TransactionKind.VOID232 assert response.is_success233 assert isclose(response.amount, TRANSACTION_AMOUNT)234 assert response.currency == TRANSACTION_CURRENCY235@pytest.mark.integration236@pytest.mark.vcr(filter_headers=["authorization"])237def test_void_error_response(stripe_payment, sandbox_gateway_config):238 INVALID_INTENT = "THIS_INTENT_DOES_NOT_EXISTS"239 payment_info = create_payment_information(stripe_payment, INVALID_INTENT)240 response = void(payment_info, sandbox_gateway_config)241 assert response.error == "No such payment_intent: " + INVALID_INTENT242 assert response.transaction_id == INVALID_INTENT243 assert response.kind == TransactionKind.VOID244 assert not response.is_success245 assert response.amount == stripe_payment.total246 assert response.currency == stripe_payment.currency247@pytest.mark.integration248@pytest.mark.vcr(filter_headers=["authorization"])249def test_confirm__intent(stripe_payment, sandbox_gateway_config):250 PAYMENT_INTENT = (251 "pi_1F6bslIUmJaD6Oqv1MNDaBSv" # PI with status "requires_confirmation"252 )253 payment_info = create_payment_information(stripe_payment, PAYMENT_INTENT)254 response = confirm(payment_info, sandbox_gateway_config)255 assert not response.error256 assert response.kind == TransactionKind.CONFIRM257 assert isclose(response.amount, 45.0)258 assert response.currency == TRANSACTION_CURRENCY259 assert response.is_success260 assert not response.action_required261@pytest.mark.integration262@pytest.mark.vcr(filter_headers=["authorization"])263def test_confirm_error_response(stripe_payment, sandbox_gateway_config):264 INVALID_INTENT = "THIS_INTENT_DOES_NOT_EXISTS"265 payment_info = create_payment_information(stripe_payment, INVALID_INTENT)266 response = confirm(payment_info, sandbox_gateway_config)267 assert response.error == "No such payment_intent: " + INVALID_INTENT268 assert response.transaction_id == INVALID_INTENT269 assert response.kind == TransactionKind.CONFIRM270 assert not response.is_success271 assert response.amount == stripe_payment.total272 assert response.currency == stripe_payment.currency273@pytest.mark.integration274@pytest.mark.vcr(filter_headers=["authorization"])275def test_list_customer_sources(sandbox_gateway_config):276 CUSTOMER_ID = "cus_FbquUfgBnLdlsY" # retrieved from sandbox277 expected_credit_card = CreditCardInfo(278 last_4="0005", exp_year=2020, exp_month=8, name_on_card=None279 )280 expected_customer_source = CustomerSource(281 id="pm_1F6dCWIUmJaD6OqvCtcAnPSq",282 gateway="stripe",283 credit_card_info=expected_credit_card,284 )285 sources = list_client_sources(sandbox_gateway_config, CUSTOMER_ID)286 assert sources == [expected_customer_source]287def test_get_client(gateway_config):288 assert _get_client(**gateway_config.connection_params).api_key == "secret"289def test_get_client_token():...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!