more coverage stuff
This commit is contained in:
127
rowers/tests/test_braintree.py
Normal file
127
rowers/tests/test_braintree.py
Normal file
@@ -0,0 +1,127 @@
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import transaction
|
||||
|
||||
#from __future__ import print_function
|
||||
from .statements import *
|
||||
nu = datetime.datetime.now()
|
||||
|
||||
|
||||
import rowers
|
||||
from rowers import dataprep
|
||||
from rowers import tasks
|
||||
from rowers import c2stuff
|
||||
from rowers import stravastuff
|
||||
import urllib
|
||||
import json
|
||||
|
||||
from rowers.braintreestuff import *
|
||||
|
||||
class transaction:
|
||||
def __init__(self,*args, **kwargs):
|
||||
self.amount = kwargs.get('amount',25)
|
||||
|
||||
class subscription:
|
||||
def __init__(self,*args, **kwargs):
|
||||
self.id = kwargs.get('id',1)
|
||||
self.transactions = [transaction(amount=25)]
|
||||
self.billing_period_end_date = datetime.datetime.now()+datetime.timedelta(days=365)
|
||||
|
||||
class notification:
|
||||
def __init__(self,*args, **kwargs):
|
||||
self.kind = kwargs.get('kind','subscription_charged_successfully')
|
||||
self.subscription = subscription(id=1)
|
||||
|
||||
class mycustomer:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.id = kwargs.get('id',1)
|
||||
|
||||
class mycreatecustomer:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.customer = mycustomer(id=1)
|
||||
self.is_success = True
|
||||
self.customer_id = 1
|
||||
|
||||
class myupgraderesult:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.subscription = subscription()
|
||||
self.is_success = True
|
||||
|
||||
class paymentmethod:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.token = 'aa'
|
||||
|
||||
class mypaymentmethod:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.is_success = True
|
||||
self.payment_method = paymentmethod()
|
||||
|
||||
class BraintreeUnits(TestCase):
|
||||
def setUp(self):
|
||||
self.u = UserFactory()
|
||||
|
||||
self.r = Rower.objects.create(user=self.u,
|
||||
birthdate=faker.profile()['birthdate'],
|
||||
gdproptin=True,surveydone=True,
|
||||
gdproptindate=timezone.now(),
|
||||
rowerplan='coach',subscription_id=1)
|
||||
|
||||
workoutsbox = Mailbox.objects.create(name='workouts')
|
||||
workoutsbox.save()
|
||||
failbox = Mailbox.objects.create(name='Failed')
|
||||
failbox.save()
|
||||
|
||||
self.pp = PaidPlan.objects.create(price=0,paymentprocessor='braintree')
|
||||
self.p2 = PaidPlan.objects.create(price=25,paymentprocessor='braintree')
|
||||
|
||||
|
||||
@patch('rowers.fakturoid.requests.get',side_effect=mocked_requests)
|
||||
@patch('rowers.fakturoid.requests.post',side_effect=mocked_requests)
|
||||
@patch('rowers.braintreestuff.gateway', side_effect=MockBraintreeGateway)
|
||||
def test_process_webhook(self,mock_get,mockpost,mocked_gateway):
|
||||
n = notification()
|
||||
res = process_webhook(n)
|
||||
self.assertEqual(res,1)
|
||||
|
||||
n = notification(kind='subscription_canceled')
|
||||
res = process_webhook(n)
|
||||
self.assertEqual(res,1)
|
||||
|
||||
def test_create_customer(self):
|
||||
with patch('rowers.braintreestuff.gateway') as mocked_gateway:
|
||||
mocked_gateway.customer.create.return_value = mycreatecustomer()
|
||||
self.r.customer_id = 0
|
||||
self.r.save()
|
||||
|
||||
res = create_customer(self.r)
|
||||
self.assertEqual(res,1)
|
||||
|
||||
def test_update_subscription(self):
|
||||
data = {
|
||||
'plan':self.pp.id,
|
||||
'payment_method_nonce':'aap',
|
||||
'amount':24,
|
||||
}
|
||||
|
||||
with patch('rowers.braintreestuff.gateway') as mocked_gateway:
|
||||
mocked_gateway.subscription.update.return_value = myupgraderesult()
|
||||
success,amount = update_subscription(self.r,data)
|
||||
self.assertTrue(success)
|
||||
self.assertEqual(amount,25)
|
||||
|
||||
def test_create_subscription(self):
|
||||
data = {
|
||||
'plan':self.p2.id,
|
||||
'payment_method_nonce':'aap',
|
||||
'amount':24,
|
||||
}
|
||||
|
||||
with patch('rowers.braintreestuff.gateway') as mocked_gateway:
|
||||
mocked_gateway.subscription.create.return_value = myupgraderesult()
|
||||
mocked_gateway.payment_method.create.return_value = mypaymentmethod()
|
||||
success,amount = create_subscription(self.r,data)
|
||||
self.assertTrue(success)
|
||||
self.assertEqual(amount,25)
|
||||
Reference in New Issue
Block a user