Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
9.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # kate: space-indent on; indent-width 4; replace-tabs on;
  3. """
  4. * Copyright © 2009-2010, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  5. *
  6. * Mumble-Django is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This package is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. """
  16. import simplejson
  17. from django.conf import settings
  18. from django.test import TestCase
  19. from django.test.client import Client
  20. from django.contrib.auth.models import User
  21. from models import Mumble, MumbleUser
  22. from utils import ObjectInfo
  23. class ExtDirectFormTestMixin(object):
  24. """ Methods for testing a Form exported via Ext.Direct.
  25. These only define the methods, you will need to inherit your TestCase
  26. from this class and set the following class attributes:
  27. api_baseurl:
  28. The URL under which the Ext.Direct provider has been registered.
  29. formname:
  30. The name of the exported form class.
  31. """
  32. def setUp(self):
  33. self.cl = Client()
  34. super(ExtDirectFormTestMixin, self).setUp()
  35. self.tid = 1
  36. def testFormApi(self):
  37. rawr = self.cl.get( "%s/%s.js" % ( self.api_baseurl, self.formname.lower() ) )
  38. self.assertEquals( rawr.status_code, 200 )
  39. def formGet( self, data=[] ):
  40. rawr = self.cl.post( self.api_baseurl+'/router',
  41. data=simplejson.dumps({
  42. 'tid': self.tid,
  43. 'action': ('XD_%s' % self.formname),
  44. 'method': 'get',
  45. 'data': data,
  46. 'type': 'rpc'
  47. }),
  48. content_type='application/json' )
  49. self.tid += 1
  50. response = simplejson.loads(rawr.content)
  51. if response['type'] == "exception":
  52. raise Exception( response["message"] )
  53. self.assert_( "result" in response )
  54. return response['result']
  55. def formPost( self, data={} ):
  56. postdata={
  57. 'extAction': ('XD_%s' % self.formname),
  58. 'extMethod': 'update',
  59. 'extTID': self.tid,
  60. 'extType': 'rpc',
  61. 'extUpload': 'false',
  62. }
  63. self.tid += 1
  64. postdata.update( data )
  65. rawr = self.cl.post( self.api_baseurl+'/router', data=postdata )
  66. response = simplejson.loads(rawr.content)
  67. if response['type'] == "exception":
  68. raise Exception( response["message"] )
  69. self.assert_( "result" in response )
  70. return response['result']
  71. class AuthedTestCase( TestCase ):
  72. fixtures = ["testdb.json"]
  73. def setUp( self ):
  74. TestCase.setUp( self )
  75. if not self.cl.login( username="svedrin", password="passwort" ):
  76. raise Exception( "Login failed" )
  77. class UnauthedMumbleFormTestCase( ExtDirectFormTestMixin, TestCase ):
  78. api_baseurl = "/mumble/forms"
  79. formname = "MumbleForm"
  80. def testFormGet( self ):
  81. result = self.formGet( [{'pk': 1}] )
  82. self.assertEquals( result['success'], False )
  83. self.assertEquals( result['errors'][''], 'access denied' )
  84. def testFormPost( self ):
  85. result = self.formPost( {'pk': 1, 'url': '', 'player': ''} )
  86. self.assertEquals( result['success'], False )
  87. self.assertEquals( result['errors'][''], 'access denied' )
  88. class AuthedMumbleFormTestCase( ExtDirectFormTestMixin, AuthedTestCase ):
  89. api_baseurl = "/mumble/forms"
  90. formname = "MumbleForm"
  91. def testFormGet( self ):
  92. result = self.formGet( [{'pk': 1}] )
  93. self.assertEquals( result['success'], True, ("errors" in result and result['errors'] or None) )
  94. def testFormPostSrvAdmin( self ):
  95. result = self.formPost( {'pk': 1, 'name': 'test server', 'url': '', 'player': ''} )
  96. self.assertEquals( result['success'], True, ("errors" in result and result['errors'] or None) )
  97. def testFormPostNonSrvAdmin( self ):
  98. result = self.formPost( {'pk': 2, 'name': 'alealejandro', 'url': '', 'player': ''} )
  99. self.assertEquals( result['success'], True, ("errors" in result and result['errors'] or None) )
  100. class UnauthedMumbleUserFormTestCase( ExtDirectFormTestMixin, TestCase ):
  101. api_baseurl = "/mumble/forms"
  102. formname = "MumbleUserForm"
  103. def testFormGet( self ):
  104. result = self.formGet( [{'pk': 1}] )
  105. self.assertEquals( result['success'], False )
  106. self.assertEquals( result['errors'][''], 'access denied' )
  107. def testFormPostWithoutServer( self ):
  108. result = self.formPost( {'pk': 1, 'name': "ohai", 'password': "failfail"} )
  109. self.assertEquals( result['success'], False )
  110. self.assertEquals( result['errors'][''], 'access denied' )
  111. def testFormPost( self ):
  112. result = self.formPost( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1} )
  113. self.assertEquals( result['success'], False )
  114. self.assertEquals( result['errors'][''], 'access denied' )
  115. class AuthedMumbleUserFormTestCase( ExtDirectFormTestMixin, AuthedTestCase ):
  116. api_baseurl = "/mumble/forms"
  117. formname = "MumbleUserForm"
  118. def testFormGet( self ):
  119. result = self.formGet( [{'pk': 1}] )
  120. self.assertEquals( result['success'], True, ("errors" in result and result['errors'] or None) )
  121. def testFormPostWithoutServer( self ):
  122. result = self.formPost( {'pk': 1, 'name': "svedrin", 'password': 'passwort' } )
  123. self.assertEquals( result['success'], False )
  124. self.assertEquals( result['errors'][''], 'pre-validation failed' )
  125. def testFormPost( self ):
  126. result = self.formPost( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1} )
  127. self.assertEquals( result['success'], True, ("errors" in result and result['errors'] or None) )
  128. class UnauthedMumbleUserLinkFormTestCase( UnauthedMumbleUserFormTestCase ):
  129. api_baseurl = "/mumble/forms"
  130. formname = "MumbleUserLinkForm"
  131. def testFormGet( self ):
  132. result = self.formGet( [{'pk': 1}] )
  133. self.assertEquals( result['success'], False )
  134. self.assertEquals( result['errors'][''], 'access denied' )
  135. def testFormPost( self ):
  136. result = self.formPost( {'pk': 1, 'name': "ohai", 'password': 'failfail', 'serverid': 1} )
  137. self.assertEquals( result['success'], False )
  138. self.assertEquals( result['errors'][''], 'access denied' )
  139. class AuthedMumbleUserLinkFormTestCase( ExtDirectFormTestMixin, AuthedTestCase ):
  140. api_baseurl = "/mumble/forms"
  141. formname = "MumbleUserLinkForm"
  142. def testFormGet( self ):
  143. if settings.ALLOW_ACCOUNT_LINKING:
  144. # Excepts because linkacc can't be retrieved, but this form is for being
  145. # displayed when empty only so retrieval is either forbidden or an error
  146. self.assertRaises( Exception, self.formGet, [{'pk': 1}] )
  147. else:
  148. result = self.formGet( [{'pk': 1}] )
  149. self.assertEquals( result['success'], False )
  150. self.assertEquals( result['errors'][''], 'access denied' )
  151. def testFormPost( self ):
  152. result = self.formPost( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1} )
  153. if settings.ALLOW_ACCOUNT_LINKING:
  154. self.assertEquals( result['success'], True, ("errors" in result and result['errors'] or None) )
  155. else:
  156. self.assertEquals( result['success'], False )
  157. self.assertEquals( result['errors'][''], 'access denied' )
  158. def testFormPostLinking( self ):
  159. result = self.formPost( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1, 'linkacc': 'on'} )
  160. self.assertEquals( result['success'], False )
  161. class UnauthedFormLoading(TestCase):
  162. """ Makes unauthorized requests to forms which require auth, and checks
  163. that those handle auth correctly.
  164. """
  165. def setUp(self):
  166. self.cl = Client()
  167. def testMumbleUserFormApi(self):
  168. rawr = self.cl.get( '/mumble/forms/mumbleuserform.js' )
  169. self.assertEquals( rawr.status_code, 200 )
  170. def testMumbleUserPasswordFormApi(self):
  171. rawr = self.cl.get( '/mumble/forms/mumbleuserpasswordform.js' )
  172. self.assertEquals( rawr.status_code, 200 )
  173. def testMumbleUserLinkFormApi(self):
  174. rawr = self.cl.get( '/mumble/forms/mumbleuserlinkform.js' )
  175. self.assertEquals( rawr.status_code, 200 )
  176. def testMumbleAdminFormApi(self):
  177. # This form is NOT exported (and shouldn't be) because it's only used in the admin
  178. rawr = self.cl.get( '/mumble/forms/mumbleadminform.js' )
  179. self.assertEquals( rawr.status_code, 404 )
  180. def testMumbleUserAdminFormApi(self):
  181. # This form is NOT exported (and shouldn't be) because it's only used in the admin
  182. rawr = self.cl.get( '/mumble/forms/mumbleuseradminform.js' )
  183. self.assertEquals( rawr.status_code, 404 )