Skip to content

Commit

Permalink
Merge pull request #938 from Nandika-A/fix/MultiValueDictKeyError
Browse files Browse the repository at this point in the history
Fixes #920 and #764. Resolved MVDkeyError in Stripe token
  • Loading branch information
amakarudze authored Jan 30, 2024
2 parents e44120b + 0e1e389 commit 1394439
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
14 changes: 11 additions & 3 deletions donations/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import uuid

import stripe
from django.conf import settings
from django.http import HttpResponseForbidden
from django.shortcuts import redirect, render
from django.urls import reverse
from stripe.error import CardError, StripeError
from stripe.error import APIConnectionError, CardError, StripeError

from patreonmanager.models import FundraisingStatus

Expand All @@ -27,11 +29,17 @@ def charge(request):
if form.is_valid():
amount = int(request.POST["amount"])
currency = request.POST["currency"]

key = uuid.uuid4().hex
try:
customer = stripe.Customer.create(
email=request.POST["email"], name=request.POST["name"], source=request.POST["stripeToken"]
email=request.POST["email"],
name=request.POST["name"],
source=request.POST.get("stripeToken"),
idempotency_key=key,
)
except APIConnectionError as err:
request.session["stripe_message"] = err.user_message
return redirect(reverse("donations:error"))
except CardError as err:
request.session["stripe_message"] = err.user_message
return redirect(reverse("donations:error"))
Expand Down
26 changes: 25 additions & 1 deletion tests/donations/test_views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
from unittest.mock import patch

import pytest
from django.test import override_settings
from django.urls import reverse
from pytest_django.asserts import assertContains
from stripe.error import StripeError
from stripe.error import APIConnectionError, StripeError

STRIPE_PUBLIC_KEY = os.environ.get("STRIPE_PUBLIC_KEY", "test_public")
STRIPE_SECRET_KEY = os.environ.get("STRIPE_SECRET_KEY", "test_public")
Expand Down Expand Up @@ -62,3 +63,26 @@ def test_sponsors(client, globalpartner, globalpartner2):
assert resp.status_code == 200
assertContains(resp, "Django Software Foundation")
assertContains(resp, "Caktus Group")


@pytest.mark.django_db
def test_charge_post_api_connection_error(client):
# Mock the stripe.Customer.create method to simulate an API connection error
with patch("stripe.Customer.create", side_effect=APIConnectionError("A Network Connection error occured.")):
form_data = {
"name": "Paul Smith",
"email": "[email protected]",
"currency": "usd",
"amount": "10",
"stripeToken": "test_code",
}

response = client.post(reverse("donations:charge"), data=form_data)

# Check if the response redirects to the error view
assert response.status_code == 302 # 302 is the status code for a redirect
assert response.url == reverse("donations:error")

# Check if the expected session variable is set
assert "stripe_message" in client.session
assert client.session["stripe_message"] == "A Network Connection error occured."

0 comments on commit 1394439

Please sign in to comment.