2011-09-27 12:48:11 +00:00
|
|
|
"""
|
|
|
|
This file demonstrates writing tests using the unittest module. These will pass
|
|
|
|
when you run "manage.py test".
|
|
|
|
|
|
|
|
Replace this with more appropriate tests for your application.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.test import TestCase
|
2011-10-07 21:17:54 +00:00
|
|
|
from django.utils import unittest
|
2012-01-04 15:52:19 +00:00
|
|
|
from django.conf import settings
|
2011-10-04 11:55:39 +00:00
|
|
|
from regluit.payment.manager import PaymentManager
|
2012-01-04 15:52:19 +00:00
|
|
|
from regluit.payment.paypal import IPN, IPN_PAY_STATUS_ACTIVE, IPN_PAY_STATUS_COMPLETED, IPN_TXN_STATUS_COMPLETED, IPN_PAY_STATUS_COMPLETED, IPN_TXN_STATUS_COMPLETED
|
2011-10-04 11:55:39 +00:00
|
|
|
from noseselenium.cases import SeleniumTestCaseMixin
|
|
|
|
from regluit.payment.models import Transaction
|
2011-10-08 03:11:57 +00:00
|
|
|
from regluit.core.models import Campaign, Wishlist, Work
|
2011-10-04 11:55:39 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from regluit.payment.parameters import *
|
|
|
|
import traceback
|
|
|
|
from django.core.validators import URLValidator
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
import time
|
|
|
|
from selenium import selenium, webdriver
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2011-10-08 03:11:57 +00:00
|
|
|
from decimal import Decimal as D
|
|
|
|
import datetime
|
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
def loginSandbox(test, selenium):
|
|
|
|
|
|
|
|
print "LOGIN SANDBOX"
|
|
|
|
|
|
|
|
try:
|
|
|
|
selenium.open('https://developer.paypal.com/')
|
|
|
|
time.sleep(5)
|
|
|
|
test.failUnless(selenium.is_text_present('Member Log In'))
|
2012-01-04 15:52:19 +00:00
|
|
|
selenium.type('login_email', settings.PAYPAL_SANDBOX_LOGIN)
|
|
|
|
selenium.type('login_password', settings.PAYPAL_SANDBOX_PASSWORD)
|
2011-10-04 11:55:39 +00:00
|
|
|
time.sleep(2)
|
|
|
|
selenium.click('css=input[class=\"formBtnOrange\"]')
|
|
|
|
time.sleep(5)
|
|
|
|
test.failUnless(selenium.is_text_present('Test Accounts'))
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
def authorizeSandbox(test, selenium, url):
|
|
|
|
|
|
|
|
print "AUTHORIZE SANDBOX"
|
|
|
|
|
|
|
|
try:
|
|
|
|
selenium.open(url)
|
|
|
|
time.sleep(5)
|
|
|
|
test.failUnless(selenium.is_text_present('Your preapproved payment summary'))
|
|
|
|
selenium.click('loadLogin')
|
|
|
|
time.sleep(5)
|
2012-01-04 15:52:19 +00:00
|
|
|
selenium.type('id=login_email', settings.PAYPAL_BUYER_LOGIN)
|
|
|
|
selenium.type('id=login_password', settings.PAYPAL_BUYER_PASSWORD)
|
2011-10-04 11:55:39 +00:00
|
|
|
time.sleep(2)
|
|
|
|
selenium.click('submitLogin')
|
|
|
|
time.sleep(5)
|
|
|
|
test.failUnless(selenium.is_text_present('Review your information'))
|
|
|
|
selenium.click('submit.x')
|
|
|
|
time.sleep(10)
|
|
|
|
selenium.click('returnToMerchant')
|
|
|
|
time.sleep(15)
|
|
|
|
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2012-01-04 15:52:19 +00:00
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
def paySandbox(test, selenium, url):
|
|
|
|
|
|
|
|
print "PAY SANDBOX"
|
|
|
|
|
|
|
|
try:
|
|
|
|
selenium.open(url)
|
|
|
|
time.sleep(5)
|
|
|
|
test.failUnless(selenium.is_text_present('Your payment summary'))
|
|
|
|
selenium.click('loadLogin')
|
|
|
|
time.sleep(5)
|
2012-01-04 15:52:19 +00:00
|
|
|
selenium.type('id=login_email', settings.PAYPAL_BUYER_LOGIN)
|
|
|
|
selenium.type('id=login_password', settings.PAYPAL_BUYER_PASSWORD)
|
2011-10-04 11:55:39 +00:00
|
|
|
time.sleep(2)
|
|
|
|
selenium.click('submitLogin')
|
|
|
|
time.sleep(5)
|
|
|
|
test.failUnless(selenium.is_text_present('Review your information'))
|
|
|
|
selenium.click('submit.x')
|
|
|
|
time.sleep(10)
|
|
|
|
selenium.click('returnToMerchant')
|
|
|
|
time.sleep(15)
|
|
|
|
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
class PledgeTest(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.verificationErrors = []
|
|
|
|
# This is an empty array where we will store any verification errors
|
|
|
|
# we find in our tests
|
|
|
|
|
|
|
|
self.selenium = selenium("localhost", 4444, "*firefox",
|
|
|
|
"http://www.google.com/")
|
|
|
|
self.selenium.start()
|
|
|
|
|
|
|
|
def validateRedirect(self, t, url, count):
|
|
|
|
|
|
|
|
self.assertNotEqual(url, None)
|
|
|
|
self.assertNotEqual(t, None)
|
|
|
|
self.assertEqual(t.receiver_set.all().count(), count)
|
|
|
|
self.assertEqual(t.receiver_set.all()[0].amount, t.amount)
|
|
|
|
self.assertEqual(t.receiver_set.all()[0].currency, t.currency)
|
2012-01-04 15:52:19 +00:00
|
|
|
# self.assertNotEqual(t.reference, None)
|
2011-10-04 11:55:39 +00:00
|
|
|
self.assertEqual(t.error, None)
|
2012-01-04 15:52:19 +00:00
|
|
|
self.assertEqual(t.status, IPN_PAY_STATUS_CREATED)
|
2011-10-04 11:55:39 +00:00
|
|
|
|
|
|
|
valid = URLValidator(verify_exists=True)
|
|
|
|
try:
|
|
|
|
valid(url)
|
|
|
|
except ValidationError, e:
|
|
|
|
print e
|
|
|
|
|
2012-01-04 15:52:19 +00:00
|
|
|
@unittest.expectedFailure
|
2011-10-04 11:55:39 +00:00
|
|
|
def test_pledge_single_receiver(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
# Note, set this to 1-5 different receivers with absolute amounts for each
|
2012-01-04 15:52:19 +00:00
|
|
|
receiver_list = [{'email':settings.PAYPAL_GLUEJAR_EMAIL, 'amount':20.00}]
|
2011-10-04 11:55:39 +00:00
|
|
|
t, url = p.pledge('USD', TARGET_TYPE_NONE, receiver_list, campaign=None, list=None, user=None)
|
|
|
|
|
|
|
|
self.validateRedirect(t, url, 1)
|
|
|
|
|
|
|
|
loginSandbox(self, self.selenium)
|
|
|
|
paySandbox(self, self.selenium, url)
|
2012-01-04 15:52:19 +00:00
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
# by now we should have received the IPN
|
2012-01-04 15:52:19 +00:00
|
|
|
# right now, for running on machine with no acess to IPN, we manually update statuses
|
|
|
|
p.checkStatus()
|
|
|
|
t = Transaction.objects.get(id=t.id)
|
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
self.assertEqual(t.status, IPN_PAY_STATUS_COMPLETED)
|
|
|
|
self.assertEqual(t.receiver_set.all()[0].status, IPN_TXN_STATUS_COMPLETED)
|
|
|
|
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2012-01-04 15:52:19 +00:00
|
|
|
|
|
|
|
@unittest.expectedFailure
|
2011-10-04 11:55:39 +00:00
|
|
|
def test_pledge_mutiple_receiver(self):
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
# Note, set this to 1-5 different receivers with absolute amounts for each
|
2012-01-04 15:52:19 +00:00
|
|
|
receiver_list = [{'email':settings.PAYPAL_GLUEJAR_EMAIL, 'amount':20.00},
|
|
|
|
{'email':settings.PAYPAL_TEST_RH_EMAIL, 'amount':10.00}]
|
2011-10-04 11:55:39 +00:00
|
|
|
|
|
|
|
t, url = p.pledge('USD', TARGET_TYPE_NONE, receiver_list, campaign=None, list=None, user=None)
|
|
|
|
|
|
|
|
self.validateRedirect(t, url, 2)
|
|
|
|
|
|
|
|
loginSandbox(self, self.selenium)
|
|
|
|
paySandbox(self, self.selenium, url)
|
|
|
|
|
|
|
|
# by now we should have received the IPN
|
2012-01-04 15:52:19 +00:00
|
|
|
# right now, for running on machine with no acess to IPN, we manually update statuses
|
|
|
|
p.checkStatus()
|
|
|
|
|
|
|
|
t = Transaction.objects.get(id=t.id)
|
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
self.assertEqual(t.status, IPN_PAY_STATUS_COMPLETED)
|
|
|
|
self.assertEqual(t.receiver_set.all()[0].status, IPN_TXN_STATUS_COMPLETED)
|
|
|
|
self.assertEqual(t.receiver_set.all()[1].status, IPN_TXN_STATUS_COMPLETED)
|
|
|
|
|
2012-01-04 15:52:19 +00:00
|
|
|
@unittest.expectedFailure
|
2011-10-04 11:55:39 +00:00
|
|
|
def test_pledge_too_much(self):
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
# Note, set this to 1-5 different receivers with absolute amounts for each
|
2012-01-04 15:52:19 +00:00
|
|
|
receiver_list = [{'email':settings.PAYPAL_GLUEJAR_EMAIL, 'amount':50000.00}]
|
2011-10-04 11:55:39 +00:00
|
|
|
t, url = p.pledge('USD', TARGET_TYPE_NONE, receiver_list, campaign=None, list=None, user=None)
|
|
|
|
|
|
|
|
self.validateRedirect(t, url, 1)
|
2012-01-04 15:52:19 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.selenium.stop()
|
2011-10-04 11:55:39 +00:00
|
|
|
|
|
|
|
class AuthorizeTest(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.verificationErrors = []
|
|
|
|
# This is an empty array where we will store any verification errors
|
|
|
|
# we find in our tests
|
|
|
|
|
|
|
|
self.selenium = selenium("localhost", 4444, "*firefox",
|
|
|
|
"http://www.google.com/")
|
|
|
|
self.selenium.start()
|
|
|
|
|
|
|
|
def validateRedirect(self, t, url):
|
|
|
|
|
|
|
|
self.assertNotEqual(url, None)
|
|
|
|
self.assertNotEqual(t, None)
|
2012-01-04 15:52:19 +00:00
|
|
|
#self.assertNotEqual(t.reference, None)
|
2011-10-04 11:55:39 +00:00
|
|
|
self.assertEqual(t.error, None)
|
|
|
|
self.assertEqual(t.status, 'NONE')
|
|
|
|
|
|
|
|
valid = URLValidator(verify_exists=True)
|
|
|
|
try:
|
|
|
|
valid(url)
|
|
|
|
except ValidationError, e:
|
|
|
|
print e
|
|
|
|
|
|
|
|
def test_authorize(self):
|
|
|
|
|
|
|
|
print "RUNNING TEST: test_authorize"
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
# Note, set this to 1-5 different receivers with absolute amounts for each
|
|
|
|
|
|
|
|
t, url = p.authorize('USD', TARGET_TYPE_NONE, 100.0, campaign=None, list=None, user=None)
|
|
|
|
|
|
|
|
self.validateRedirect(t, url)
|
|
|
|
|
|
|
|
loginSandbox(self, self.selenium)
|
|
|
|
authorizeSandbox(self, self.selenium, url)
|
|
|
|
|
2012-01-04 15:52:19 +00:00
|
|
|
# stick in a getStatus to update statuses in the absence of IPNs
|
|
|
|
p.checkStatus()
|
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
t = Transaction.objects.get(id=t.id)
|
|
|
|
|
|
|
|
self.assertEqual(t.status, IPN_PAY_STATUS_ACTIVE)
|
|
|
|
|
2012-01-04 15:52:19 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.selenium.stop()
|
|
|
|
|
2011-10-07 21:17:54 +00:00
|
|
|
class TransactionTest(TestCase):
|
2011-10-08 03:11:57 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
pass
|
2011-10-07 21:17:54 +00:00
|
|
|
def testSimple(self):
|
2011-10-08 03:11:57 +00:00
|
|
|
"""
|
2012-01-11 23:31:26 +00:00
|
|
|
create a single transaction with PAYMENT_TYPE_AUTHORIZATION / ACTIVE with a $12.34 pledge and see whether the payment
|
2011-10-08 03:11:57 +00:00
|
|
|
manager can query and get the right amount.
|
|
|
|
"""
|
|
|
|
|
|
|
|
w = Work()
|
|
|
|
w.save()
|
2011-12-29 15:17:11 +00:00
|
|
|
c = Campaign(target=D('1000.00'),deadline=datetime.datetime.utcnow() + datetime.timedelta(days=180),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c.save()
|
|
|
|
|
|
|
|
t = Transaction()
|
|
|
|
t.amount = D('12.34')
|
|
|
|
t.type = PAYMENT_TYPE_AUTHORIZATION
|
|
|
|
t.status = 'ACTIVE'
|
2012-01-11 23:31:26 +00:00
|
|
|
t.approved = True
|
2011-10-08 03:11:57 +00:00
|
|
|
t.campaign = c
|
|
|
|
t.save()
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
results = p.query_campaign(campaign=c)
|
|
|
|
self.assertEqual(results[0].amount, D('12.34'))
|
2011-12-14 05:52:42 +00:00
|
|
|
self.assertEqual(c.left,c.target-D('12.34'))
|
2011-10-07 21:17:54 +00:00
|
|
|
|
|
|
|
def suite():
|
|
|
|
|
2012-01-04 15:52:19 +00:00
|
|
|
#testcases = [PledgeTest, AuthorizeTest, TransactionTest]
|
2012-01-04 16:04:18 +00:00
|
|
|
testcases = [TransactionTest]
|
2011-10-07 21:17:54 +00:00
|
|
|
suites = unittest.TestSuite([unittest.TestLoader().loadTestsFromTestCase(testcase) for testcase in testcases])
|
|
|
|
return suites
|
2011-10-04 11:55:39 +00:00
|
|
|
|
|
|
|
|