def test_stripe_checkout_phase_with_saved_card(rf, stripe_payment_processor): shop = factories.get_default_shop() user = factories.create_random_user() contact = get_person_contact(user) request = apply_request_middleware(rf.post("/"), shop=shop, customer=contact) def return_stripe_mock_data(*args, **kwargs): return StripedData(**CUSTOMER_MOCK_DATA) with mock.patch("stripe.Customer.retrieve", new=return_stripe_mock_data): service = stripe_payment_processor.create_service( "stripe", shop=request.shop, tax_class=factories.get_default_tax_class(), enabled=True) StripeCustomer.objects.create(contact=contact, customer_token="123") checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() context = checkout_phase.get_context_data() assert context["stripe"] request.method = "POST" request.POST = {"stripeCustomer": "1234"} checkout_phase.get_success_url = lambda: reverse("shuup_admin:home") checkout_phase.post(request) assert checkout_phase.is_valid() # We should be valid now assert request.session # And things should've been saved into the session checkout_phase.process() # And this should do things to the basket assert request.basket.payment_data["stripe"]["customer"]
def test_stripe_checkout_phase(rf): request = apply_request_middleware(rf.post("/"), shop=get_default_shop()) request.session = {} request.basket = get_basket(request) payment_processor = StripeCheckoutPaymentProcessor.objects.create( secret_key="secret", publishable_key="12", name="Stripe") service = payment_processor.create_service( "stripe", shop=request.shop, tax_class=get_default_tax_class(), enabled=True) checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() # We can't be valid just yet context = checkout_phase.get_context_data() assert context["stripe"] request.method = "POST" request.POST = { "stripeToken": "1234", "stripeTokenType": "12442", "stripeTokenEmail": "4212342", } checkout_phase.get_success_url = lambda: reverse("shuup_admin:home") checkout_phase.post(request) assert checkout_phase.is_valid() # We should be valid now assert request.session # And things should've been saved into the session checkout_phase.process() # And this should do things to the basket assert request.basket.payment_data.get("stripe")
def test_stripe_checkout_phase(rf): cache.clear() shop = factories.get_default_shop() contact = factories.create_random_person() request = apply_request_middleware(rf.post("/"), shop=get_default_shop()) request.session = {} request.basket = get_basket(request) payment_processor = StripeCheckoutPaymentProcessor.objects.create( secret_key="secret", publishable_key="12", name="Stripe") service = payment_processor.create_service( "stripe", shop=request.shop, tax_class=get_default_tax_class(), enabled=True) checkout_phase = StripeCheckoutPhase(request=request, service=service) with mock.patch.object(checkout_phase, "get_context_data") as mocked_get_context_data: mocked_get_context_data.return_value = { "view": checkout_phase, "stripe": { "publishable_key": "ha", "name": "he", "description": "hi", }, "customer": contact, "stripe_customer_data": { "id": "testing", "sources": { "data": { "?": True } } } } response = checkout_phase.get(request) assert response.status_code == 200 response.render() content = response.content.decode("utf-8") assert "We use Stripe for secure payment handling. You will only be charged when your order completes" in content assert "Click the button below to enter your card details" in content assert "Use saved card details by clicking button below" in content set_checkout_payment_details_message(shop, "ABC123") set_checkout_payment_phase_message(shop, "XYZ987") set_checkout_saved_card_message(shop, "QWERTY456") response = checkout_phase.get(request) assert response.status_code == 200 response.render() content = response.content.decode("utf-8") assert "We use Stripe for secure payment handling. You will only be charged when your order completes" not in content assert "Click the button below to enter your card details" not in content assert "Use saved card details by clicking button below" not in content assert "ABC123" in content assert "XYZ987" in content assert "QWERTY456" in content
def test_stripe_checkout_phase(rf, stripe_payment_processor): request = rf.get("/") request.shop = get_default_shop() request.session = {} request.basket = get_basket(request) service = stripe_payment_processor.create_service( None, shop=request.shop, tax_class=get_default_tax_class(), enabled=True) checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() # We can't be valid just yet context = checkout_phase.get_context_data() assert context["stripe"] request.method = "POST" token = get_stripe_token(stripe_payment_processor) request.POST = { "stripeToken": token["id"], "stripeTokenType": token["type"], "stripeTokenEmail": token.get("email"), } checkout_phase.post(request) assert checkout_phase.is_valid() # We should be valid now assert request.session # And things should've been saved into the session checkout_phase.process() # And this should do things to the basket assert request.basket.payment_data.get("stripe")
def test_stripe_checkout_phase_with_saved_card(rf, stripe_payment_processor): shop = factories.get_default_shop() user = factories.create_random_user() contact = get_person_contact(user) request = apply_request_middleware(rf.post("/"), shop=shop, customer=contact) def return_stripe_mock_data(*args, **kwargs): return StripedData(**CUSTOMER_MOCK_DATA) with mock.patch("stripe.Customer.retrieve", new=return_stripe_mock_data): service = stripe_payment_processor.create_service( "stripe", shop=request.shop, tax_class=factories.get_default_tax_class(), enabled=True) StripeCustomer.objects.create(contact=contact, customer_token="123") checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() context = checkout_phase.get_context_data() assert context["stripe"] request.method = "POST" request.POST = { "stripeCustomer": "1234" } checkout_phase.get_success_url = lambda: reverse("shuup_admin:home") checkout_phase.post(request) assert checkout_phase.is_valid() # We should be valid now assert request.session # And things should've been saved into the session checkout_phase.process() # And this should do things to the basket assert request.basket.payment_data["stripe"]["customer"]
def test_stripe_checkout_phase(rf): cache.clear() shop = factories.get_default_shop() contact = factories.create_random_person() request = apply_request_middleware(rf.post("/"), shop=get_default_shop()) request.session = {} request.basket = get_basket(request) payment_processor = StripeCheckoutPaymentProcessor.objects.create(secret_key="secret", publishable_key="12", name="Stripe") service = payment_processor.create_service("stripe", shop=request.shop, tax_class=get_default_tax_class(), enabled=True) checkout_phase = StripeCheckoutPhase(request=request, service=service) with mock.patch.object(checkout_phase, "get_context_data") as mocked_get_context_data: mocked_get_context_data.return_value = { "view": checkout_phase, "stripe": { "publishable_key": "ha", "name": "he", "description": "hi", }, "customer": contact, "stripe_customer_data": { "id": "testing", "sources": { "data": { "?": True } } } } response = checkout_phase.get(request) assert response.status_code == 200 response.render() content = response.content.decode("utf-8") assert "We use Stripe for secure payment handling. You will only be charged when your order completes" in content assert "Click the button below to enter your card details" in content assert "Use saved card details by clicking button below" in content set_checkout_payment_details_message(shop, "ABC123") set_checkout_payment_phase_message(shop, "XYZ987") set_checkout_saved_card_message(shop, "QWERTY456") response = checkout_phase.get(request) assert response.status_code == 200 response.render() content = response.content.decode("utf-8") assert "We use Stripe for secure payment handling. You will only be charged when your order completes" not in content assert "Click the button below to enter your card details" not in content assert "Use saved card details by clicking button below" not in content assert "ABC123" in content assert "XYZ987" in content assert "QWERTY456" in content
def test_stripe_checkout_phase_with_saved_card_exception( rf, stripe_payment_processor): shop = factories.get_default_shop() user = factories.create_random_user() contact = get_person_contact(user) request = apply_request_middleware(rf.post("/"), shop=shop, customer=contact) def raise_stripe_exc(*args, **kwargs): raise stripe.StripeError("DUMMY") with mock.patch("stripe.Customer.retrieve", new=raise_stripe_exc): service = stripe_payment_processor.create_service( "stripe", shop=request.shop, tax_class=factories.get_default_tax_class(), enabled=True) StripeCustomer.objects.create(contact=contact, customer_token="123") checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() context = checkout_phase.get_context_data() assert context["stripe"] assert not context.get("stripe_customer_data") request.method = "POST" request.POST = {} checkout_phase.get_success_url = lambda: reverse("shuup_admin:home") checkout_phase.post(request) assert not checkout_phase.is_valid()
def test_stripe_checkout_phase_with_misconfigured_module(rf): payment_processor = StripeCheckoutPaymentProcessor.objects.create() request = apply_request_middleware(rf.get("/"), shop=get_default_shop()) request.session = {} request.basket = get_basket(request) service = payment_processor.create_service( "stripe", shop=request.shop, tax_class=get_default_tax_class(), enabled=True) checkout_phase = StripeCheckoutPhase(request=request, service=service) with pytest.raises(Problem): checkout_phase.get_context_data()
def test_stripe_checkout_phase_with_misconfigured_module(rf): stripe_payment_prossor = ( StripeCheckoutPaymentProcessor.objects.create()) request = rf.get("/") request.shop = get_default_shop() request.session = {} request.basket = get_basket(request) service = stripe_payment_prossor.create_service( None, shop=request.shop, tax_class=get_default_tax_class(), enabled=True) checkout_phase = StripeCheckoutPhase( request=request, service=service) with pytest.raises(Problem): checkout_phase.get_context_data()
def test_stripe_checkout_phase(rf, stripe_payment_processor): request = rf.get("/") request.shop = get_default_shop() request.session = {} request.basket = get_basket(request) service = stripe_payment_processor.create_service( None, shop=request.shop, tax_class=get_default_tax_class(), enabled=True) checkout_phase = StripeCheckoutPhase( request=request, service=service) assert not checkout_phase.is_valid() # We can't be valid just yet context = checkout_phase.get_context_data() assert context["stripe"] request.method = "POST" token = get_stripe_token(stripe_payment_processor) request.POST = { "stripeToken": token["id"], "stripeTokenType": token["type"], "stripeTokenEmail": token.get("email"), } checkout_phase.post(request) assert checkout_phase.is_valid() # We should be valid now assert request.session # And things should've been saved into the session checkout_phase.process() # And this should do things to the basket assert request.basket.payment_data.get("stripe")
def test_stripe_checkout_phase_with_saved_card_exception(rf, stripe_payment_processor): shop = factories.get_default_shop() user = factories.create_random_user() contact = get_person_contact(user) request = apply_request_middleware(rf.post("/"), shop=shop, customer=contact) def raise_stripe_exc(*args, **kwargs): raise stripe.StripeError("DUMMY") with mock.patch("stripe.Customer.retrieve", new=raise_stripe_exc): service = stripe_payment_processor.create_service( "stripe", shop=request.shop, tax_class=factories.get_default_tax_class(), enabled=True) StripeCustomer.objects.create(contact=contact, customer_token="123") checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() context = checkout_phase.get_context_data() assert context["stripe"] assert not context.get("stripe_customer_data") request.method = "POST" request.POST = {} checkout_phase.get_success_url = lambda: reverse("shuup_admin:home") checkout_phase.post(request) assert not checkout_phase.is_valid()
def test_stripe_checkout_phase_mocked(rf): shop = get_default_shop() request = apply_request_middleware(rf.post("/")) payment_processor = StripeCheckoutPaymentProcessor.objects.create( secret_key="secret", publishable_key="12", name="Stripe") service = payment_processor.create_service( "stripe", shop=shop, tax_class=get_default_tax_class(), enabled=True, name="Stripe", description="desc") checkout_phase = StripeCheckoutPhase(request=request, service=service) stripe_context = checkout_phase.get_stripe_context() assert stripe_context[ "publishable_key"] == payment_processor.publishable_key assert stripe_context["name"] == shop.name assert stripe_context["description"] == "Purchase" assert not checkout_phase.is_valid()