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.

317 lines
12 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. # Privs:
  17. #
  18. # Unauthed users:
  19. # * Registration: no Anon_MumbleUser{,Link}FormTestCase
  20. # * Administration: no Anon_MumbleFormTestCase
  21. # * User texture: no
  22. # * User list: no
  23. # * Log messages: no
  24. # * Instance scope: ALL
  25. #
  26. # Authed users, unregistered:
  27. # * Registration: self, User{,Password,Link}Form User_MumbleUserLinkFormTestCase
  28. # * Administration: no
  29. # * User texture: no
  30. # * User list: no
  31. # * Log messages: no
  32. # * Instance scope: CURRENT
  33. #
  34. # Authed users, not admins:
  35. # * Registration: self, UserForm
  36. # * Administration: no
  37. # * User texture: self
  38. # * User list: no
  39. # * Log messages: no
  40. # * Instance scope: CURRENT
  41. #
  42. # Authed users, admins:
  43. # * Registration: everyone
  44. # * Administration: yes
  45. # * User texture: everyone
  46. # * User list: yes
  47. # * Log messages: yes
  48. # * Instance scope: CURRENT
  49. #
  50. # Authed users, superadmins:
  51. # * Registration: everyone Super_MumbleUser{,Link}FormTestCase
  52. # * Administration: yes Super_MumbleFormTestCase
  53. # * User texture: everyone
  54. # * User list: yes
  55. # * Log messages: yes
  56. # * Instance scope: ALL
  57. import simplejson
  58. from django.conf import settings
  59. from django.test import TestCase
  60. from django.test.client import Client
  61. from django.contrib.auth.models import User
  62. from models import Mumble, MumbleUser
  63. from utils import ObjectInfo
  64. class ExtDirectFormTestMixin(object):
  65. """ Methods for testing a Form exported via Ext.Direct.
  66. These only define the methods, you will need to inherit your TestCase
  67. from this class and set the following class attributes:
  68. api_baseurl:
  69. The URL under which the Ext.Direct provider has been registered.
  70. formname:
  71. The name of the exported form class.
  72. """
  73. def setUp(self):
  74. self.cl = Client()
  75. super(ExtDirectFormTestMixin, self).setUp()
  76. self.tid = 1
  77. def testFormApi(self):
  78. rawr = self.cl.get( "%s/%s.js" % ( self.api_baseurl, self.formname.lower() ) )
  79. self.assertEquals( rawr.status_code, 200 )
  80. def formGet( self, data=[] ):
  81. rawr = self.cl.post( self.api_baseurl+'/router',
  82. data=simplejson.dumps({
  83. 'tid': self.tid,
  84. 'action': ('XD_%s' % self.formname),
  85. 'method': 'get',
  86. 'data': data,
  87. 'type': 'rpc'
  88. }),
  89. content_type='application/json' )
  90. self.tid += 1
  91. response = simplejson.loads(rawr.content)
  92. if response['type'] == "exception":
  93. raise Exception( response["message"] )
  94. self.assert_( "result" in response )
  95. return response['result']
  96. def formPost( self, data={} ):
  97. postdata={
  98. 'extAction': ('XD_%s' % self.formname),
  99. 'extMethod': 'update',
  100. 'extTID': self.tid,
  101. 'extType': 'rpc',
  102. 'extUpload': 'false',
  103. }
  104. self.tid += 1
  105. postdata.update( data )
  106. rawr = self.cl.post( self.api_baseurl+'/router', data=postdata )
  107. response = simplejson.loads(rawr.content)
  108. if response['type'] == "exception":
  109. raise Exception( response["message"] )
  110. self.assert_( "result" in response )
  111. return response['result']
  112. def generateTestCase( name, formname, data, login=None ):
  113. attrs = {
  114. 'fixtures': ['testdb.json'],
  115. 'formname': formname,
  116. 'api_baseurl': '/mumble/forms',
  117. }
  118. if login:
  119. def setUp( self ):
  120. ExtDirectFormTestMixin.setUp( self )
  121. if not self.cl.login( **login ):
  122. raise Exception( "Login failed" )
  123. attrs['setUp'] = setUp
  124. def mkGet( data, result ):
  125. def testFormGet( self ):
  126. callresult = self.formGet( [{ 'pk': data['pk'] }] )
  127. if "data" in callresult:
  128. del callresult['data'] # don't care
  129. self.assertEquals( callresult, result )
  130. return testFormGet
  131. def mkPost( data, result ):
  132. def testFormPost( self ):
  133. callresult = self.formPost( data )
  134. self.assertEquals( callresult, result,
  135. ("errors" in callresult and callresult['errors'] or None)
  136. )
  137. return testFormPost
  138. for testname in data:
  139. if len(data[testname]) == 3:
  140. testdata, getresult, postresult = data[testname]
  141. else:
  142. testdata, getresult = data[testname]
  143. postresult = getresult
  144. attrs.update({
  145. ('testForm%sGet' % testname): mkGet( testdata, getresult ),
  146. ('testForm%sPost' % testname): mkPost( testdata, postresult ),
  147. })
  148. return type( name, (ExtDirectFormTestMixin, TestCase), attrs )
  149. RES_SUCCESS = {'success': True}
  150. RES_ACCESSDENIED = {'success': False, 'errors': {'': 'access denied'}}
  151. RES_PREVALFAIL = {'success': False, 'errors': {'': 'pre-validation failed'}}
  152. LOGIN_SUPERADMIN = {'username': 'svedrin', 'password': 'passwort'}
  153. LOGIN_UNREGUSER = {'username': 'nocheinuser', 'password': 'passwort'}
  154. #############################################################
  155. ### ANON: Unauthed (not logged in) users ###
  156. #############################################################
  157. Anon_Registration = generateTestCase(
  158. name = "Anon_Registration",
  159. formname = "MumbleUserForm",
  160. data = {
  161. 'My': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_ACCESSDENIED ),
  162. 'Other': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort'}, RES_ACCESSDENIED )
  163. }
  164. )
  165. Anon_Administration = generateTestCase(
  166. name = "Anon_Administration",
  167. formname = "MumbleForm",
  168. data = { 'My': ( {'pk': 1, 'url': '', 'player': ''}, RES_ACCESSDENIED ) },
  169. )
  170. Anon_UserLink = generateTestCase(
  171. name = "Anon_UserLink",
  172. formname = "MumbleUserLinkForm",
  173. data = {
  174. 'My': ( {'pk': 1, 'name': "ohai", 'password': 'failfail', 'serverid': 1}, RES_ACCESSDENIED ),
  175. }
  176. )
  177. #############################################################
  178. ### UNREG: Authenticated but no MumbleUser avail ###
  179. #############################################################
  180. Unreg_Registration = generateTestCase(
  181. name = "Unreg_Registration",
  182. formname = "MumbleUserForm",
  183. data = {
  184. 'My': ( {'pk': -1, 'name': "neueruser", 'password': 'passwort', 'serverid': 1}, RES_SUCCESS ),
  185. 'Taken': ( {'pk': -1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_SUCCESS,
  186. {'success': False, 'errors': {'name': 'Another player already registered that name.'}} ),
  187. 'Other': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort'}, RES_ACCESSDENIED )
  188. },
  189. login = LOGIN_UNREGUSER
  190. )
  191. if settings.ALLOW_ACCOUNT_LINKING and settings.ALLOW_ACCOUNT_LINKING_ADMINS:
  192. unreg_adminlinkresult = RES_SUCCESS
  193. else:
  194. unreg_adminlinkresult = {'success': False, 'errors': {'__all__': 'Linking Admin accounts is not allowed.'}}
  195. Unreg_UserLink = generateTestCase(
  196. name = "User_UserLink",
  197. formname = "MumbleUserLinkForm",
  198. data = {
  199. 'My': ( {'pk': -1, 'name': "nichtadmin", 'password': 'nichtadmin', 'serverid': 1, 'linkacc': 'on'},
  200. RES_ACCESSDENIED, RES_SUCCESS if settings.ALLOW_ACCOUNT_LINKING else RES_ACCESSDENIED ),
  201. 'Admin': ( {'pk': -1, 'name': "dochadmin", 'password': 'dochadmin', 'serverid': 1, 'linkacc': 'on'},
  202. RES_ACCESSDENIED, unreg_adminlinkresult ),
  203. 'Other': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_ACCESSDENIED ),
  204. 'Taken': ( {'pk': -1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_ACCESSDENIED,
  205. {'success': False, 'errors':{'name': 'Another player already registered that name.'}} ),
  206. },
  207. login = LOGIN_UNREGUSER
  208. )
  209. Unreg_Administration = generateTestCase(
  210. name = "Unreg_Administration",
  211. formname = "MumbleForm",
  212. data = {
  213. 'My': ( {'pk': 1, 'name': 'test server', 'url': '', 'player': ''}, RES_ACCESSDENIED ),
  214. 'Other': ( {'pk': 2, 'name': 'alealejandro', 'url': '', 'player': ''}, RES_ACCESSDENIED ),
  215. },
  216. login = LOGIN_UNREGUSER,
  217. )
  218. #############################################################
  219. ### USER: MumbleUser but not a server admin ###
  220. #############################################################
  221. #############################################################
  222. ### ADMIN: MumbleUser is a server admin ###
  223. #############################################################
  224. #############################################################
  225. ### SUPER: User is superadmin, MumbleUser irrelevant ###
  226. #############################################################
  227. Super_Registration = generateTestCase(
  228. name = "Super_Registration",
  229. formname = "MumbleUserForm",
  230. data = {
  231. 'My': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_SUCCESS ),
  232. 'Fail': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort'}, RES_SUCCESS, RES_PREVALFAIL ),
  233. },
  234. login = LOGIN_SUPERADMIN
  235. )
  236. Super_Administration = generateTestCase(
  237. name = "Super_Administration",
  238. formname = "MumbleForm",
  239. data = {
  240. 'My': ( {'pk': 1, 'name': 'test server', 'url': '', 'player': ''}, RES_SUCCESS ),
  241. 'Other': ( {'pk': 2, 'name': 'alealejandro', 'url': '', 'player': ''}, RES_SUCCESS ),
  242. },
  243. login = LOGIN_SUPERADMIN,
  244. )
  245. Super_UserLink = generateTestCase(
  246. name = "Super_UserLink",
  247. formname = "MumbleUserLinkForm",
  248. data = {
  249. 'My': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1},
  250. RES_ACCESSDENIED ),
  251. 'Link': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1, 'linkacc': 'on'},
  252. RES_ACCESSDENIED )
  253. },
  254. )
  255. class ExportedForms(TestCase):
  256. """ Makes sure needed forms are exported, and admin forms are not. """
  257. def setUp(self):
  258. self.cl = Client()
  259. def testMumbleUserFormApi(self):
  260. rawr = self.cl.get( '/mumble/forms/mumbleuserform.js' )
  261. self.assertEquals( rawr.status_code, 200 )
  262. def testMumbleUserPasswordFormApi(self):
  263. rawr = self.cl.get( '/mumble/forms/mumbleuserpasswordform.js' )
  264. self.assertEquals( rawr.status_code, 200 )
  265. def testMumbleUserLinkFormApi(self):
  266. rawr = self.cl.get( '/mumble/forms/mumbleuserlinkform.js' )
  267. self.assertEquals( rawr.status_code, 200 )
  268. def testMumbleAdminFormApi(self):
  269. # This form is NOT exported (and shouldn't be) because it's only used in the admin
  270. rawr = self.cl.get( '/mumble/forms/mumbleadminform.js' )
  271. self.assertEquals( rawr.status_code, 404 )
  272. def testMumbleUserAdminFormApi(self):
  273. # This form is NOT exported (and shouldn't be) because it's only used in the admin
  274. rawr = self.cl.get( '/mumble/forms/mumbleuseradminform.js' )
  275. self.assertEquals( rawr.status_code, 404 )