import unittest
import random
import sys
if sys.version_info < (2, 6):
from django.utils import simplejson as json
else:
import json
from django.db import IntegrityError, DatabaseError
from django.test.client import Client
from django.http import HttpResponse
from django.core.handlers.wsgi import WSGIRequest
from models import BillingUser, Rental, Wallet, Currency, Package, PriceCategory
from cms_sample.cms_models import Item, Media_biz
from exceptions import BillExcep_rental, BillExcep_noWallet, BillExcep_notEnoughMoney
from utils import rent, is_rented, time_from_end, tokenize, untokenize, is_subscribed, subscribe
from webmx_views import rent_item
# in order to run these tests, run: "python manage.py test billing.Funtionality"
# test functions must start with 'test'. For example testMyPlan
class Funtionality(unittest.TestCase):
item = Item.objects.all()[0]
item2 = Item.objects.all()[1]
def setUp(self):
pass
def tearDown(self):
pass
#self.user.delete()
def test_Rental_Item(self):
user = BillingUser(username='AtestsUser76212', pin='1111', sbox_id='7777777')
user.save()
currency = Currency.objects.all()[0]
wallet = Wallet(user=user, currency=currency)
wallet.save()
rent = Rental(wallet=wallet, item=self.item, value=10.0, index=1)
rent.save()
self.assertTrue(rent.id)
# Test that rental objects can not re-save
try:
rent.save()
except BillExcep_rental:
self.assertTrue(1)
# Test that the user can not rent same item twice with the same index
rent2 = Rental(wallet=wallet, item=self.item, value=10.0, index=1)
try:
rent2.save()
except IntegrityError:
self.assertTrue(1)
def test_Rental_AntherItem(self):
user = BillingUser(username='AtestsUser812228', pin='1111', sbox_id='7777777')
user.save()
currency = Currency.objects.all()[0]
wallet = Wallet(user=user, currency=currency)
wallet.save()
rent = Rental(wallet=wallet, item=self.item, value=10.0, index=1)
rent.save()
self.assertTrue(rent.id)
# Test that anther item can be rent by the user
rent2 = Rental(wallet=wallet, item=self.item2, value=10.0, index=1)
rent2.save()
self.assertTrue(rent2.id)
def test_Rent(self):
user = BillingUser(username='AtestsUser812229', pin='1111', sbox_id='7777777')
user.save()
try:
rent(self.item, user)
except BillExcep_noWallet:
self.assertTrue(1) # Make sure that rent will not work without wallet
currency = Currency.objects.all()[2]
if not Wallet.objects.filter(user=user):
wallet = Wallet(user=user, currency=currency)
wallet.save()
self.assertFalse(is_rented(self.item, user))
try:
rent(self.item, user)
except BillExcep_notEnoughMoney:
self.assertTrue(1) # Can't rent with no money
user.wallet.amount = 12.0
user.wallet.save()
self.assertTrue(rent(self.item, user))
self.assertTrue(is_rented(self.item, user))
try:
rent(self.item2, user)
except BillExcep_notEnoughMoney:
self.assertTrue(1) # Can rent with no money
user.wallet.amount = 0
#self.assertFalse(is_rented(self.item2, user), 'Wallet suppose to be empty now')
def test_tokenize(self):
print BillingUser.objects.all()
user = BillingUser(username='AtestsUser812230', pin='1111', sbox_id='7777777')
user.save()
try:
rent(self.item, user)
except BillExcep_noWallet:
self.assertTrue(1, 'Wallet.DoesNotExist') # Make sure that rent will not work without wallet
currency = Currency.objects.all()[0]
wallet = Wallet(user=user, currency=currency)
wallet.save()
token = tokenize(username, self.item.id)
tmp_username, tmp_item_id = untokenize(token)
self.assertTrue(tmp_username == user.username, tmp_username + '==' + user.username)
self.assertTrue(int(tmp_item_id) == self.item.id)
def test_subscription(self):
username = 'AtestsUser_4123109_subscription'
sid = '7777777'
user = BillingUser(username=username, pin='1111', sbox_id=sid, parental_code='1111')
user.save()
self.assertFalse(is_subscribed(self.item, user), 'User not suppose to be subscribed to random item')
currency = Currency.objects.all()[0]
price_cat = PriceCategory(name = 'Price Cat', price = 100.0, currency = currency )
price_cat.save()
package = Package(name = 'test package', pricecategory = price_cat)
package.save()
package.items.add(self.item)
subscrition = subscribe(package, user)
self.assertTrue(is_subscribed(self.item, user), 'User suppose to be subscribed item')
try:
subscribe(package, user)
except IntegrityError: # Second attempt must raise Exception
self.assertTrue(1)
child_package = Package(name = 'test package 2', pricecategory = price_cat)
child_package.save()
self.assertFalse(is_subscribed(self.item2, user), 'User not suppose to be subscribed item')
child_package.parent.add(package)
package.items.add(self.item2)
self.assertTrue(is_subscribed(self.item2, user), 'User suppose to be subscribed to item within sub-package')
subscrition.delete()
self.assertFalse(is_subscribed(self.item2, user), 'User not suppose to be subscribed to item')
item_subscrition = subscribe(self.item2, user)
self.assertTrue(is_subscribed(self.item2, user), 'User suppose to be subscribed to item')
#in order to run these tests, run: "python manage.py test billing.WebMX_view"
#class WebMX_view(unittest.TestCase):
#def setUp(self):
#self.client = Client()
#def test_rent(self):
#username = 'AtestsUser52825_92'
#sid = '7777777'
#pin_code = '1234'
#user = BillingUser(username=username, pin=pin_code, sbox_id=sid)
#user.save()
#currency = Currency.objects.all()[0]
#wallet = Wallet(user=user, currency=currency)
#wallet.save()
#item_id = '225'
## Purche transaction (1/2)
#response = self.client.get('/billing/purchase/' + item_id, {'user': username, 'sid': '7777777'})
#self.assertEquals(response.status_code, 200)
#j = json.loads(response.content)
#self.assertTrue('section' in j.keys(), 'No "section" in the WebMX response')
#self.assertTrue('items' in j['section'].keys(), 'No "items" in the WebMX response')
#self.assertTrue('link' in j['section']['items'][0].keys(), 'No "items" in the WebMX response')
#link = j['section']['items'][0]['link']
## Rent transaction (2/2)
#path = '/' + link
#response2 = self.client.get(path, {'user': username, 'sid': '7777777', 'code': pin_code})
#print response2
#jj = json.loads(response2.content)
#print j['section']['items']
#self.assertTrue('section' in jj.keys(), 'No "section" in the WebMX response')
#self.assertTrue('items' in j['section'].keys(), 'No "items" in the WebMX response')
#self.assertTrue('link' in j['section']['items'][0].keys(), 'No "items" in the WebMX response')