def test_getAvailableActions_DeleteTicket(self): # http://trac-hacks.org/ticket/5387 tid = self.admin.ticket.create('abc', 'def', {}) try: self.assertEquals(False, 'delete' in self.admin.ticket.getAvailableActions(tid)) env = rpc_testenv.get_trac_environment() delete_plugin = os.path.join(rpc_testenv.tracdir, 'plugins', 'DeleteTicket.py') shutil.copy(os.path.join( rpc_testenv.trac_src, 'sample-plugins', 'workflow', 'DeleteTicket.py'), delete_plugin) env.config.set('ticket', 'workflow', 'ConfigurableTicketWorkflow,DeleteTicketActionController') env.config.save() self.assertEquals(True, 'delete' in self.admin.ticket.getAvailableActions(tid)) self.assertEquals(False, 'delete' in self.user.ticket.getAvailableActions(tid)) env.config.set('ticket', 'workflow', 'ConfigurableTicketWorkflow') env.config.save() rpc_testenv.restart() self.assertEquals(False, 'delete' in self.admin.ticket.getAvailableActions(tid)) finally: # Clean up try: os.unlink(delete_plugin) except: pass rpc_testenv.restart() self.assertEquals(0, self.admin.ticket.delete(tid))
def test_search_none_result(self): # Some plugins may return None instead of empty iterator # https://trac-hacks.org/ticket/12950 # Add custom plugin to provoke error plugin = os.path.join(rpc_testenv.tracdir, 'plugins', 'NoneSearchPlugin.py') open(plugin, 'w').write( "from trac.core import *\n" "from trac.search.api import ISearchSource\n" "class NoneSearch(Component):\n" " implements(ISearchSource)\n" " def get_search_filters(self, req):\n" " yield ('test', 'Test')\n" " def get_search_results(self, req, terms, filters):\n" " self.log.debug('Search plugin returning None')\n" " return None") rpc_testenv.restart() # Test results = self.user.search.performSearch("nothing_should_be_found") self.assertEquals([], results) # Remove plugin and restart os.unlink(plugin) rpc_testenv.restart()
def test_general_provider_error(self): # Make a new plugin and restart server provider = os.path.join(rpc_testenv.tracdir, 'plugins', 'DummyProvider.py') open(provider, 'w').write( "from trac.core import *\n" "from tracrpc.api import *\n" "class DummyProvider(Component):\n" " implements(IRPCProtocol)\n" " def rpc_info(self):\n" " return ('TEST-RPC', 'No Docs!')\n" " def rpc_match(self):\n" " yield ('rpc', 'application/x-tracrpc-test')\n" " def parse_rpc_request(self, req, content_type):\n" " return {'method' : 'system.getAPIVersion'}\n" " def send_rpc_error(self, req, e):\n" " if isinstance(e, RPCError) :\n" " req.send_error(None, template='', \n" " content_type='text/plain',\n" " status=500, env=None, data=e.message)\n" " else :\n" " req.send_error(None, template='', \n" " content_type='text/plain',\n" " status=500, env=None, data='Test failure')\n" " def send_rpc_result(self, req, result):\n" " raise RPCError('No good.')") rpc_testenv.restart() # Make the request try: req = urllib2.Request(rpc_testenv.url_anon, headers={'Content-Type': 'application/x-tracrpc-test'}) resp = urllib2.urlopen(req) except urllib2.HTTPError, e: self.assertEquals(500, e.code) self.assertEquals("No good.", e.fp.read()) self.assertTrue(e.hdrs['Content-Type'].startswith('text/plain'))
def test_xml_encoding_invalid_characters(self): # Enable ticket manipulator plugin = os.path.join(rpc_testenv.tracdir, 'plugins', 'InvalidXmlCharHandler.py') open(plugin, 'w').write( "from trac.core import *\n" "from tracrpc.api import IXMLRPCHandler\n" "class UniChr(Component):\n" " implements(IXMLRPCHandler)\n" " def xmlrpc_namespace(self):\n" " return 'test_unichr'\n" " def xmlrpc_methods(self):\n" " yield ('XML_RPC', ((str, int),), self.unichr)\n" " def unichr(self, req, code):\n" " return (r'\U%08x' % code).decode('unicode-escape')\n") rpc_testenv.restart() from tracrpc.xml_rpc import _illegal_unichrs, REPLACEMENT_CHAR for low, high in _illegal_unichrs: for x in (low, (low + high) / 2, high): self.assertEquals(REPLACEMENT_CHAR, self.user.test_unichr.unichr(x), "Failed unichr with U+%04X" % (x, )) # surrogate pair on narrow build self.assertEquals(u'\U0001D4C1', self.user.test_unichr.unichr(0x1D4C1)) # Remove plugin and restart os.unlink(plugin) rpc_testenv.restart()
def test_xml_encoding_invalid_characters(self): # Enable ticket manipulator plugin = os.path.join(rpc_testenv.tracdir, 'plugins', 'InvalidXmlCharHandler.py') open(plugin, 'w').write( "from trac.core import *\n" "from tracrpc.api import IXMLRPCHandler\n" "class UniChr(Component):\n" " implements(IXMLRPCHandler)\n" " def xmlrpc_namespace(self):\n" " return 'test_unichr'\n" " def xmlrpc_methods(self):\n" " yield ('XML_RPC', ((str, int),), self.unichr)\n" " def unichr(self, req, code):\n" " return (r'\U%08x' % code).decode('unicode-escape')\n") rpc_testenv.restart() from tracrpc.xml_rpc import _illegal_unichrs, REPLACEMENT_CHAR for low, high in _illegal_unichrs: for x in (low, (low + high) / 2, high): self.assertEquals(REPLACEMENT_CHAR, self.user.test_unichr.unichr(x), "Failed unichr with U+%04X" % (x,)) # surrogate pair on narrow build self.assertEquals(u'\U0001D4C1', self.user.test_unichr.unichr(0x1D4C1)) # Remove plugin and restart os.unlink(plugin) rpc_testenv.restart()
def test_FineGrainedSecurity(self): self.assertEquals(1, self.admin.ticket.create('abc', '123', {})) self.assertEquals(2, self.admin.ticket.create('def', '456', {})) # First some non-restricted tests for comparison: self.assertRaises(xmlrpclib.Fault, self.anon.ticket.create, 'abc', 'def') self.assertEquals([1, 2], self.user.ticket.query()) self.assertTrue(self.user.ticket.get(2)) self.assertTrue(self.user.ticket.update(1, "ok")) self.assertTrue(self.user.ticket.update(2, "ok")) # Enable security policy and test from trac.core import Component, implements from trac.perm import IPermissionPolicy policy = os.path.join(rpc_testenv.tracdir, 'plugins', 'TicketPolicy.py') open(policy, 'w').write( "from trac.core import *\n" "from trac.perm import IPermissionPolicy\n" "class TicketPolicy(Component):\n" " implements(IPermissionPolicy)\n" " def check_permission(self, action, username, resource, perm):\n" " if username == 'user' and resource and resource.id == 2:\n" " return False\n" " if username == 'anonymous' and action == 'TICKET_CREATE':\n" " return True\n") env = rpc_testenv.get_trac_environment() _old_conf = env.config.get('trac', 'permission_policies') env.config.set('trac', 'permission_policies', 'TicketPolicy,' + _old_conf) env.config.save() rpc_testenv.restart() self.assertEquals([1], self.user.ticket.query()) self.assertTrue(self.user.ticket.get(1)) self.assertRaises(xmlrpclib.Fault, self.user.ticket.get, 2) self.assertTrue(self.user.ticket.update(1, "ok")) self.assertRaises(xmlrpclib.Fault, self.user.ticket.update, 2, "not ok") self.assertEquals(3, self.anon.ticket.create('efg', '789', {})) # Clean, reset and simple verification env.config.set('trac', 'permission_policies', _old_conf) env.config.save() os.unlink(policy) rpc_testenv.restart() self.assertEquals([1, 2, 3], self.user.ticket.query()) self.assertEquals(0, self.admin.ticket.delete(1)) self.assertEquals(0, self.admin.ticket.delete(2)) self.assertEquals(0, self.admin.ticket.delete(3))
def test_FineGrainedSecurity(self): self.assertEquals(1, self.admin.ticket.create('abc', '123', {})) self.assertEquals(2, self.admin.ticket.create('def', '456', {})) # First some non-restricted tests for comparison: self.assertRaises(xmlrpclib.Fault, self.anon.ticket.create, 'abc', 'def') self.assertEquals([1,2], self.user.ticket.query()) self.assertTrue(self.user.ticket.get(2)) self.assertTrue(self.user.ticket.update(1, "ok")) self.assertTrue(self.user.ticket.update(2, "ok")) # Enable security policy and test from trac.core import Component, implements from trac.perm import IPermissionPolicy policy = os.path.join(rpc_testenv.tracdir, 'plugins', 'TicketPolicy.py') open(policy, 'w').write( "from trac.core import *\n" "from trac.perm import IPermissionPolicy\n" "class TicketPolicy(Component):\n" " implements(IPermissionPolicy)\n" " def check_permission(self, action, username, resource, perm):\n" " if username == 'user' and resource and resource.id == 2:\n" " return False\n" " if username == 'anonymous' and action == 'TICKET_CREATE':\n" " return True\n") env = rpc_testenv.get_trac_environment() _old_conf = env.config.get('trac', 'permission_policies') env.config.set('trac', 'permission_policies', 'TicketPolicy,'+_old_conf) env.config.save() rpc_testenv.restart() self.assertEquals([1], self.user.ticket.query()) self.assertTrue(self.user.ticket.get(1)) self.assertRaises(xmlrpclib.Fault, self.user.ticket.get, 2) self.assertTrue(self.user.ticket.update(1, "ok")) self.assertRaises(xmlrpclib.Fault, self.user.ticket.update, 2, "not ok") self.assertEquals(3, self.anon.ticket.create('efg', '789', {})) # Clean, reset and simple verification env.config.set('trac', 'permission_policies', _old_conf) env.config.save() os.unlink(policy) rpc_testenv.restart() self.assertEquals([1,2,3], self.user.ticket.query()) self.assertEquals(0, self.admin.ticket.delete(1)) self.assertEquals(0, self.admin.ticket.delete(2)) self.assertEquals(0, self.admin.ticket.delete(3))
def test_general_provider_error(self): # Make a new plugin and restart server provider = os.path.join(rpc_testenv.tracdir, 'plugins', 'DummyProvider.py') open(provider, 'w').write( "from trac.core import *\n" "from tracrpc.api import *\n" "class DummyProvider(Component):\n" " implements(IRPCProtocol)\n" " def rpc_info(self):\n" " return ('TEST-RPC', 'No Docs!')\n" " def rpc_match(self):\n" " yield ('rpc', 'application/x-tracrpc-test')\n" " def parse_rpc_request(self, req, content_type):\n" " return {'method' : 'system.getAPIVersion'}\n" " def send_rpc_error(self, req, e):\n" " if isinstance(e, RPCError) :\n" " req.send_error(None, template='', \n" " content_type='text/plain',\n" " status=500, env=None, data=e.message)\n" " else :\n" " req.send_error(None, template='', \n" " content_type='text/plain',\n" " status=500, env=None, data='Test failure')\n" " def send_rpc_result(self, req, result):\n" " raise RPCError('No good.')") rpc_testenv.restart() # Make the request try: try: req = urllib2.Request( rpc_testenv.url_anon, headers={'Content-Type': 'application/x-tracrpc-test'}) resp = urllib2.urlopen(req) except urllib2.HTTPError, e: self.assertEquals(500, e.code) self.assertEquals("No good.", e.fp.read()) self.assertTrue( e.hdrs['Content-Type'].startswith('text/plain')) finally: # Clean up so that provider don't affect further tests os.unlink(provider) rpc_testenv.restart()
def test_getPageHTMLWithManipulator(self): self.admin.wiki.putPage('FooBar', 'foo bar', {}) # Enable wiki manipulator plugin = os.path.join(rpc_testenv.tracdir, 'plugins', 'Manipulator.py') open(plugin, 'w').write("from trac.core import *\n" "from trac.wiki.api import IWikiPageManipulator\n" "class WikiManipulator(Component):\n" " implements(IWikiPageManipulator)\n" " def prepare_wiki_page(self, req, page, fields):\n" " fields['text'] = 'foo bar baz'\n" " def validate_wiki_page(req, page):\n" " return []\n") rpc_testenv.restart() # Perform tests self.assertEquals('<html><body><p>\nfoo bar baz\n</p>\n</body></html>', self.admin.wiki.getPageHTML('FooBar')) # Remove plugin and restart os.unlink(plugin) rpc_testenv.restart()
def test_getPageHTMLWithManipulator(self): self.admin.wiki.putPage('FooBar', 'foo bar', {}) # Enable wiki manipulator plugin = os.path.join(rpc_testenv.tracdir, 'plugins', 'Manipulator.py') open(plugin, 'w').write( "from trac.core import *\n" "from trac.wiki.api import IWikiPageManipulator\n" "class WikiManipulator(Component):\n" " implements(IWikiPageManipulator)\n" " def prepare_wiki_page(self, req, page, fields):\n" " fields['text'] = 'foo bar baz'\n" " def validate_wiki_page(req, page):\n" " return []\n") rpc_testenv.restart() # Perform tests self.assertEquals('<html><body><p>\nfoo bar baz\n</p>\n</body></html>', self.admin.wiki.getPageHTML('FooBar')) # Remove plugin and restart os.unlink(plugin) rpc_testenv.restart()
" return ('TEST-RPC', 'No Docs!')\n" " def rpc_match(self):\n" " yield ('rpc', 'application/x-tracrpc-test')\n" " def parse_rpc_request(self, req, content_type):\n" " return {'method' : 'system.getAPIVersion'}\n" " def send_rpc_error(self, req, e):\n" " rpcreq = req.rpc\n" " req.send_error(None, template='', content_type=rpcreq['mimetype'],\n" " status=500, env=None,\n" " data='Test failure: %s' % str(e))\n" " def send_rpc_result(self, req, result):\n" " rpcreq = req.rpc\n" " # raise KeyError('Here')\n" " response = 'Got a result!'\n" " req.send(response, rpcreq['mimetype'], 200)\n") rpc_testenv.restart() try: req = urllib2.Request(rpc_testenv.url_anon, headers={'Content-Type': 'application/x-tracrpc-test'}) resp = urllib2.urlopen(req) self.assertEquals(200, resp.code) self.assertEquals("Got a result!", resp.read()) self.assertEquals(resp.headers['Content-Type'], 'application/x-tracrpc-test;charset=utf-8') finally: # Clean up so that provider don't affect further tests os.unlink(provider) rpc_testenv.restart() def test_general_provider_error(self): # Make a new plugin and restart server