Add unit tests for api

This commit is contained in:
Trever Fischer
2012-03-05 17:58:42 -05:00
parent e21401c0fd
commit 2808a093b9

55
api/tests.py Normal file
View File

@@ -0,0 +1,55 @@
from django.utils import unittest
import json
from django.test.client import Client
from django.contrib.auth.models import User
from minecraft.models import MinecraftProfile
class MOTDTest(unittest.TestCase):
def setUp(self):
self.client = Client()
def testUnregisteredUser(self):
response = json.loads(self.client.get('/api/motd/NewUser').content)
self.assertIsInstance(response['motd'], list)
class BalanceTest(unittest.TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create_user('ValidUsername', 'test@example.com')
self.user.set_password('password')
self.user.save()
self.user.minecraftprofile.mc_username = "ValidUsername"
self.user.minecraftprofile.save()
self.user.minecraftprofile.currencyaccount.balance = 1000
self.user.minecraftprofile.currencyaccount.save()
def tearDown(self):
self.user.delete()
def testWithoutLogin(self):
response = json.loads(self.client.get('/api/balance'))
def testWithLogin(self):
self.client.login(username=self.user.username, password='password')
response = json.loads(self.client.get('/api/balance'))
self.assertEqual(response['balance'], 1000)
class WhitelistTest(unittest.TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create_user('ValidUsername', 'test@example.com')
self.user.minecraftprofile.mc_username = "ValidUsername"
self.user.minecraftprofile.save()
def tearDown(self):
self.user.delete()
def testValidProfile(self):
response = json.loads(self.client.get('/api/validate/ValidUsername').content)
self.assertEqual(response['valid'], True)
def testInvalidProfile(self):
response = json.loads(self.client.get('/api/validate/InvalidUser').content)
self.assertEqual(response['valid'], False)