Fleshing out exception handling for pledge_cancel
selenium tests work for both amazon and paypal on RY laptop -- now going to test them on ry-dev
pull/1/head
Raymond Yee 2012-05-24 18:47:50 -07:00
parent 60b84b0358
commit 1335b3cb72
3 changed files with 51 additions and 15 deletions

View File

@ -9,19 +9,20 @@
{% block doccontent %}
{% if error_mesg %}
{{error_mesg}}
{% endif %}
{% if error %}
{{error}}
{% else %}
{% if transaction %}
<div>You've asked to cancel your pledge of ${{ transaction.amount|intcomma }} to <a href="{% url work work.id %}">{{ work.title }}</a> Did you mean to do this?</div>
<div>You have asked to cancel your pledge of ${{ transaction.amount|intcomma }} to <a href="{% url work work.id %}">{{ work.title }}</a>. </div>
<form method="post" action="{% url pledge_cancel campaign_id=campaign.id %}">
{% csrf_token %}
<input type="hidden" name="campaign_id" value="{{campaign.id}}" />
<input type="submit" value="Cancel Pledge" id="pledgesubmit" />
<input type="submit" value="Confirm Pledge Cancellation" id="cancelsubmit" />
</form>
<div>or return to <a href="{% url work work.id %}">{{ work.title }}</a> without canceling your pledge.</div>
{% comment %}
"Yes" should trigger whatever functionality we need to complete cancellation -- may differ depending on whether we've hit the back button from Amazon or the cancel-my-pledge link in pledge_modify.
@ -33,8 +34,10 @@
{% endcomment %}
{% else %}
<div>We're sorry; we can't figure out which transaction you're talking about. If you meant to cancel a pledge (though we hope you don't!), please go to the book's page and hit the Modify Pledge button.</div>
<div>No relevant transaction to cancel for this campaign.</div>
{% endif %}
{% endif %}
{% endblock %}

View File

@ -873,7 +873,7 @@ class PledgeCancelView(FormView):
if self.request.user.is_authenticated():
user = self.request.user
else:
context["error"] = "user is not authenticated"
context["error"] = "You are not logged in."
return context
campaign = get_object_or_404(models.Campaign, id=self.kwargs["campaign_id"])
@ -885,14 +885,16 @@ class PledgeCancelView(FormView):
transactions = campaign.transactions().filter(user=user, status=TRANSACTION_STATUS_ACTIVE)
if transactions.count() < 1:
context["error"] = "You don't have an active transaction for this campaign"
context["error"] = "You don't have an active transaction for this campaign."
return context
elif transactions.count() > 1:
logger.error("User {0} has {1} active transactions for campaign id {2}".format(user, transactions.count(), campaign.id))
context["error"] = "You have {0} active transactions for this campaign".format(transactions.count())
return context
transaction = transactions[0]
if transaction.type != PAYMENT_TYPE_AUTHORIZATION:
logger.error("Transaction id {0} transaction type, which should be {1}, is actually {2}".format(transaction.id, PAYMENT_TYPE_AUTHORIZATION, transaction.type))
context["error"] = "Your transaction type, which should be {0}, is actually {1}".format(PAYMENT_TYPE_AUTHORIZATION, transaction.type)
return context
@ -911,7 +913,12 @@ class PledgeCancelView(FormView):
logger.info("arrived at pledge_cancel form_valid")
# pull campaign_id from form, not from URI as we do from GET
campaign_id = self.request.REQUEST.get('campaign_id')
user = self.request.user
# this following logic should be extraneous.
if self.request.user.is_authenticated():
user = self.request.user
else:
return HttpResponse("You need to be logged in.")
try:
# look up the specified campaign and attempt to pull up the appropriate transaction
@ -924,6 +931,7 @@ class PledgeCancelView(FormView):
# to display the success or failure of the cancel operation as a popup in the context of the work page
p = PaymentManager()
result = p.cancel_transaction(transaction)
# put a notification here for pledge cancellation?
if result:
# Now if we redirect the user to the Work page and the IPN hasn't arrived, the status of the
# transaction might be out of date. Let's try an explicit polling of the transaction result before redirecting
@ -932,9 +940,11 @@ class PledgeCancelView(FormView):
update_status = p.update_preapproval(transaction)
return HttpResponseRedirect(reverse('work', kwargs={'work_id': campaign.work.id}))
else:
return HttpResponse("Attempt to cancel your transaction failed")
except Transaction.DoesNotExist, e:
return HttpResponse("Could not find a transaction that you can cancel in this context")
logger.error("Attempt to cancel transaction id {0} failed".format(transaction.id))
return HttpResponse("Our attempt to cancel your transaction failed. We have logged this error.")
except Exception, e:
logger.error("Exception from attempt to cancel pledge for campaign id {0} for username {1}: {2}".format(campaign_id, user.username, e))
return HttpResponse("Sorry, something went wrong in canceling your campaign pledge. We have logged this error.")
class PledgeNeverMindView(TemplateView):

View File

@ -303,12 +303,35 @@ def support_campaign(unglue_it_url = settings.LIVE_SERVER_TEST_URL, do_local=Tru
# wait a bit to allow PayPal sandbox to be update the status of the Transaction
time.sleep(10)
django.db.transaction.commit()
# time out to simulate an IPN -- update all the transactions
if do_local:
django.db.transaction.enter_transaction_management()
pm = PaymentManager()
print pm.checkStatus()
django.db.transaction.commit()
django.db.transaction.enter_transaction_management()
# now go back to the work page, hit modify pledge, and then the cancel link
work_url = WebDriverWait(sel,20).until(lambda d: d.find_element_by_css_selector('p > a[href*="/work/"]'))
work_url.click()
change_pledge_button = WebDriverWait(sel,20).until(lambda d: d.find_element_by_css_selector("input[value*='Modify Pledge']"))
change_pledge_button.click()
cancel_url = WebDriverWait(sel,20).until(lambda d: d.find_element_by_css_selector('a[href*="/pledge/cancel"]'))
cancel_url.click()
# hit the confirm cancellation button
cancel_pledge_button = WebDriverWait(sel,20).until(lambda d: d.find_element_by_css_selector("input[value*='Confirm Pledge Cancellation']"))
cancel_pledge_button.click()
django.db.transaction.commit()
# Why is the status of the new transaction not being updated?
django.db.transaction.commit()
# force a db lookup -- see whether there are 1 or 2 transactions
# they should both be cancelled
if do_local:
transactions = list(Transaction.objects.all())
print "number of transactions", Transaction.objects.count()