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.

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