Source

bloodhound-rpc / trunk / tracrpc / tests / web_ui.py

Full commit
# -*- coding: utf-8 -*-
"""
License: BSD

(c) 2009      ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
"""

import unittest
import urllib2

from tracrpc.tests import TracRpcTestCase

class DocumentationTestCase(TracRpcTestCase):

    def setUp(self):
        TracRpcTestCase.setUp(self)
        self.opener_user = self._tester.build_opener(self.url_auth, 'user')

    def tearDown(self):
        TracRpcTestCase.tearDown(self)

    def test_get_with_content_type(self):
        req = urllib2.Request(self.url_auth,
                    headers={'Content-Type': 'text/html'})
        self.assert_rpcdocs_ok(self.opener_user, req)

    def test_get_no_content_type(self):
        req = urllib2.Request(self.url_auth)
        self.assert_rpcdocs_ok(self.opener_user, req)

    def test_post_accept(self):
        req = urllib2.Request(self.url_auth, 
                    headers={'Content-Type' : 'text/plain',
                              'Accept': 'application/x-trac-test,text/html'},
                    data='Pass since client accepts HTML')
        self.assert_rpcdocs_ok(self.opener_user, req)

        req = urllib2.Request(self.url_auth, 
                    headers={'Content-Type' : 'text/plain'},
                    data='Fail! No content type expected')
        self.assert_unsupported_media_type(self.opener_user, req)

    def test_form_submit(self):
        from urllib import urlencode
        # Explicit content type
        form_vars = {'result' : 'Fail! __FORM_TOKEN protection activated'}
        req = urllib2.Request(self.url_auth, 
                    headers={'Content-Type': 'application/x-www-form-urlencoded'},
                    data=urlencode(form_vars))
        self.assert_form_protect(self.opener_user, req)

        # Implicit content type
        req = urllib2.Request(self.url_auth, 
                    headers={'Accept': 'application/x-trac-test,text/html'},
                    data='Pass since client accepts HTML')
        self.assert_form_protect(self.opener_user, req)

    def test_get_dont_accept(self):
        req = urllib2.Request(self.url_auth, 
                    headers={'Accept': 'application/x-trac-test'})
        self.assert_unsupported_media_type(self.opener_user, req)

    def test_post_dont_accept(self):
        req = urllib2.Request(self.url_auth, 
                    headers={'Content-Type': 'text/plain',
                              'Accept': 'application/x-trac-test'},
                    data='Fail! Client cannot process HTML')
        self.assert_unsupported_media_type(self.opener_user, req)

    # Custom assertions
    def assert_rpcdocs_ok(self, opener, req):
        """Determine if RPC docs are ok"""
        try :
            resp = opener.open(req)
        except urllib2.HTTPError, e :
            self.fail("Request to '%s' failed (%s) %s" % (e.geturl(),
                                                          e.code,
                                                          e.fp.read()))
        else :
            self.assertEquals(200, resp.code)
            body = resp.read()
            self.assertTrue('<h3 id="XML-RPC">XML-RPC</h3>' in body)
            self.assertTrue('<h3 id="rpc.ticket.status">' in body)

    def assert_unsupported_media_type(self, opener, req):
        """Ensure HTTP 415 is returned back to the client"""
        try :
            opener.open(req)
        except urllib2.HTTPError, e:
            self.assertEquals(415, e.code)
            expected = "No protocol matching Content-Type '%s' at path '%s'." % \
                                (req.headers.get('Content-Type', 'text/plain'),
                                  '/login/rpc')
            got = e.fp.read()
            self.assertEquals(expected, got)
        except Exception, e:
            self.fail('Expected HTTP error but %s raised instead' % \
                                              (e.__class__.__name__,))
        else :
            self.fail('Expected HTTP error (415) but nothing raised')

    def assert_form_protect(self, opener, req):
        e = self.assertRaises(urllib2.HTTPError, opener.open, req)
        self.assertEquals(400, e.code)
        msg = e.fp.read()
        self.assertTrue("Missing or invalid form token. "
                                "Do you have cookies enabled?" in msg)

def test_suite(suite=None):
    if suite is None:
        from tracrpc.tests import functionalSuite
        suite = functionalSuite()
    # FIXME: Improve
    ts = unittest.makeSuite(DocumentationTestCase)
    for test in ts._tests:
        suite.addTest(test)
    return suite
    

if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')