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.

339 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': 'unreg', 'password': 'passwort'}
  154. LOGIN_USER = {'username': 'user', 'password': 'passwort'}
  155. LOGIN_ADMIN = {'username': 'admin', 'password': 'passwort'}
  156. #############################################################
  157. ### ANON: Unauthed (not logged in) users ###
  158. #############################################################
  159. Anon_Registration = generateTestCase(
  160. name = "Anon_Registration",
  161. formname = "MumbleUserForm",
  162. data = {
  163. 'My': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_ACCESSDENIED ),
  164. 'Other': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort'}, RES_ACCESSDENIED )
  165. }
  166. )
  167. Anon_Administration = generateTestCase(
  168. name = "Anon_Administration",
  169. formname = "MumbleForm",
  170. data = { 'My': ( {'pk': 1, 'url': '', 'player': ''}, RES_ACCESSDENIED ) },
  171. )
  172. Anon_UserLink = generateTestCase(
  173. name = "Anon_UserLink",
  174. formname = "MumbleUserLinkForm",
  175. data = {
  176. 'My': ( {'pk': 1, 'name': "ohai", 'password': 'failfail', 'serverid': 1}, RES_ACCESSDENIED ),
  177. }
  178. )
  179. #############################################################
  180. ### UNREG: Authenticated but no MumbleUser avail ###
  181. #############################################################
  182. Unreg_Registration = generateTestCase(
  183. name = "Unreg_Registration",
  184. formname = "MumbleUserForm",
  185. data = {
  186. 'My': ( {'pk': -1, 'name': "neueruser", 'password': 'passwort', 'serverid': 1}, RES_SUCCESS ),
  187. 'Taken': ( {'pk': -1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_SUCCESS,
  188. {'success': False, 'errors': {'name': 'Another player already registered that name.'}} ),
  189. 'Other': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort'}, RES_ACCESSDENIED )
  190. },
  191. login = LOGIN_UNREGUSER
  192. )
  193. if settings.ALLOW_ACCOUNT_LINKING and settings.ALLOW_ACCOUNT_LINKING_ADMINS:
  194. unreg_adminlinkresult = RES_SUCCESS
  195. else:
  196. unreg_adminlinkresult = {'success': False, 'errors': {'__all__': 'Linking Admin accounts is not allowed.'}}
  197. Unreg_UserLink = generateTestCase(
  198. name = "User_UserLink",
  199. formname = "MumbleUserLinkForm",
  200. data = {
  201. 'My': ( {'pk': -1, 'name': "nichtadmin", 'password': 'nichtadmin', 'serverid': 1, 'linkacc': 'on'},
  202. RES_ACCESSDENIED, RES_SUCCESS if settings.ALLOW_ACCOUNT_LINKING else RES_ACCESSDENIED ),
  203. 'Admin': ( {'pk': -1, 'name': "dochadmin", 'password': 'dochadmin', 'serverid': 1, 'linkacc': 'on'},
  204. RES_ACCESSDENIED, unreg_adminlinkresult ),
  205. 'Other': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_ACCESSDENIED ),
  206. 'Taken': ( {'pk': -1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_ACCESSDENIED,
  207. {'success': False, 'errors':{'name': 'Another player already registered that name.'}} ),
  208. },
  209. login = LOGIN_UNREGUSER
  210. )
  211. Unreg_Administration = generateTestCase(
  212. name = "Unreg_Administration",
  213. formname = "MumbleForm",
  214. data = {
  215. 'My': ( {'pk': 1, 'name': 'test server', 'url': '', 'player': ''}, RES_ACCESSDENIED ),
  216. 'Other': ( {'pk': 2, 'name': 'alealejandro', 'url': '', 'player': ''}, RES_ACCESSDENIED ),
  217. },
  218. login = LOGIN_UNREGUSER,
  219. )
  220. #############################################################
  221. ### USER: MumbleUser but not a server admin ###
  222. #############################################################
  223. User_Administration = generateTestCase(
  224. name = "User_Administration",
  225. formname = "MumbleForm",
  226. data = {
  227. 'My': ( {'pk': 1, 'name': 'test server', 'url': '', 'player': ''}, RES_ACCESSDENIED ),
  228. 'Other': ( {'pk': 2, 'name': 'alealejandro', 'url': '', 'player': ''}, RES_ACCESSDENIED ),
  229. },
  230. login = LOGIN_USER,
  231. )
  232. #############################################################
  233. ### ADMIN: MumbleUser is a server admin ###
  234. #############################################################
  235. Admin_Administration = generateTestCase(
  236. name = "Admin_Administration",
  237. formname = "MumbleForm",
  238. data = {
  239. 'My': ( {'pk': 1, 'name': 'test server', 'url': '', 'player': ''}, RES_SUCCESS ),
  240. 'Other': ( {'pk': 2, 'name': 'alealejandro', 'url': '', 'player': ''}, RES_ACCESSDENIED ),
  241. },
  242. login = LOGIN_ADMIN,
  243. )
  244. #############################################################
  245. ### SUPER: User is superadmin, MumbleUser irrelevant ###
  246. #############################################################
  247. Super_Registration = generateTestCase(
  248. name = "Super_Registration",
  249. formname = "MumbleUserForm",
  250. data = {
  251. 'My': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1}, RES_SUCCESS ),
  252. 'Fail': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort'}, RES_SUCCESS, RES_PREVALFAIL ),
  253. },
  254. login = LOGIN_SUPERADMIN
  255. )
  256. Super_Administration = generateTestCase(
  257. name = "Super_Administration",
  258. formname = "MumbleForm",
  259. data = {
  260. 'My': ( {'pk': 1, 'name': 'test server', 'url': '', 'player': ''}, RES_SUCCESS ),
  261. 'Other': ( {'pk': 2, 'name': 'alealejandro', 'url': '', 'player': ''}, RES_SUCCESS ),
  262. },
  263. login = LOGIN_SUPERADMIN,
  264. )
  265. Super_UserLink = generateTestCase(
  266. name = "Super_UserLink",
  267. formname = "MumbleUserLinkForm",
  268. data = {
  269. 'My': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1},
  270. RES_ACCESSDENIED ),
  271. 'Link': ( {'pk': 1, 'name': "svedrin", 'password': 'passwort', 'serverid': 1, 'linkacc': 'on'},
  272. RES_ACCESSDENIED )
  273. },
  274. )
  275. class ExportedForms(TestCase):
  276. """ Makes sure needed forms are exported, and admin forms are not. """
  277. def setUp(self):
  278. self.cl = Client()
  279. def testMumbleUserFormApi(self):
  280. rawr = self.cl.get( '/mumble/forms/mumbleuserform.js' )
  281. self.assertEquals( rawr.status_code, 200 )
  282. def testMumbleUserPasswordFormApi(self):
  283. rawr = self.cl.get( '/mumble/forms/mumbleuserpasswordform.js' )
  284. self.assertEquals( rawr.status_code, 200 )
  285. def testMumbleUserLinkFormApi(self):
  286. rawr = self.cl.get( '/mumble/forms/mumbleuserlinkform.js' )
  287. self.assertEquals( rawr.status_code, 200 )
  288. def testMumbleAdminFormApi(self):
  289. # This form is NOT exported (and shouldn't be) because it's only used in the admin
  290. rawr = self.cl.get( '/mumble/forms/mumbleadminform.js' )
  291. self.assertEquals( rawr.status_code, 404 )
  292. def testMumbleUserAdminFormApi(self):
  293. # This form is NOT exported (and shouldn't be) because it's only used in the admin
  294. rawr = self.cl.get( '/mumble/forms/mumbleuseradminform.js' )
  295. self.assertEquals( rawr.status_code, 404 )