Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #920 and #764. Resolved MVDkeyError in Stripe token #938

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."
Loading