~ubuntu-branches/ubuntu/wily/trac-xmlrpc/wily

« back to all changes in this revision

Viewing changes to trunk/tracrpc/tests/web_ui.py

  • Committer: Package Import Robot
  • Author(s): W. Martin Borgert, Jakub Wilk
  • Date: 2011-12-31 18:27:57 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111231182757-oe70jynzu0lk5hny
Tags: 1.1.2+r10706-1
* New upstream version (Closes: #653726). Works with Trac 0.12.
* Fixed lintians.
  [Jakub Wilk <jwilk@debian.org>]
* Replace deprecated > operator with >=.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
License: BSD
 
4
 
 
5
(c) 2009      ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
 
6
"""
 
7
 
 
8
import unittest
 
9
import urllib2
 
10
 
 
11
from tracrpc.tests import rpc_testenv, TracRpcTestCase
 
12
 
 
13
class DocumentationTestCase(TracRpcTestCase):
 
14
 
 
15
    def setUp(self):
 
16
        TracRpcTestCase.setUp(self)
 
17
        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
 
18
        handler = urllib2.HTTPBasicAuthHandler(password_mgr)
 
19
        password_mgr.add_password(realm=None,
 
20
                      uri=rpc_testenv.url_auth,
 
21
                      user='user', passwd='user')
 
22
        self.opener_user = urllib2.build_opener(handler)
 
23
 
 
24
    def tearDown(self):
 
25
        TracRpcTestCase.tearDown(self)
 
26
 
 
27
    def test_get_with_content_type(self):
 
28
        req = urllib2.Request(rpc_testenv.url_auth,
 
29
                    headers={'Content-Type': 'text/html'})
 
30
        self.assert_rpcdocs_ok(self.opener_user, req)
 
31
 
 
32
    def test_get_no_content_type(self):
 
33
        req = urllib2.Request(rpc_testenv.url_auth)
 
34
        self.assert_rpcdocs_ok(self.opener_user, req)
 
35
 
 
36
    def test_post_accept(self):
 
37
        req = urllib2.Request(rpc_testenv.url_auth, 
 
38
                    headers={'Content-Type' : 'text/plain',
 
39
                              'Accept': 'application/x-trac-test,text/html'},
 
40
                    data='Pass since client accepts HTML')
 
41
        self.assert_rpcdocs_ok(self.opener_user, req)
 
42
 
 
43
        req = urllib2.Request(rpc_testenv.url_auth, 
 
44
                    headers={'Content-Type' : 'text/plain'},
 
45
                    data='Fail! No content type expected')
 
46
        self.assert_unsupported_media_type(self.opener_user, req)
 
47
 
 
48
    def test_form_submit(self):
 
49
        from urllib import urlencode
 
50
        # Explicit content type
 
51
        form_vars = {'result' : 'Fail! __FORM_TOKEN protection activated'}
 
52
        req = urllib2.Request(rpc_testenv.url_auth, 
 
53
                    headers={'Content-Type': 'application/x-www-form-urlencoded'},
 
54
                    data=urlencode(form_vars))
 
55
        self.assert_form_protect(self.opener_user, req)
 
56
 
 
57
        # Implicit content type
 
58
        req = urllib2.Request(rpc_testenv.url_auth, 
 
59
                    headers={'Accept': 'application/x-trac-test,text/html'},
 
60
                    data='Pass since client accepts HTML')
 
61
        self.assert_form_protect(self.opener_user, req)
 
62
 
 
63
    def test_get_dont_accept(self):
 
64
        req = urllib2.Request(rpc_testenv.url_auth, 
 
65
                    headers={'Accept': 'application/x-trac-test'})
 
66
        self.assert_unsupported_media_type(self.opener_user, req)
 
67
 
 
68
    def test_post_dont_accept(self):
 
69
        req = urllib2.Request(rpc_testenv.url_auth, 
 
70
                    headers={'Content-Type': 'text/plain',
 
71
                              'Accept': 'application/x-trac-test'},
 
72
                    data='Fail! Client cannot process HTML')
 
73
        self.assert_unsupported_media_type(self.opener_user, req)
 
74
 
 
75
    # Custom assertions
 
76
    def assert_rpcdocs_ok(self, opener, req):
 
77
        """Determine if RPC docs are ok"""
 
78
        try :
 
79
            resp = opener.open(req)
 
80
        except urllib2.HTTPError, e :
 
81
            self.fail("Request to '%s' failed (%s) %s" % (e.geturl(),
 
82
                                                          e.code,
 
83
                                                          e.fp.read()))
 
84
        else :
 
85
            self.assertEquals(200, resp.code)
 
86
            body = resp.read()
 
87
            self.assertTrue('<h3 id="XML-RPC">XML-RPC</h3>' in body)
 
88
            self.assertTrue('<h3 id="rpc.ticket.status">' in body)
 
89
 
 
90
    def assert_unsupported_media_type(self, opener, req):
 
91
        """Ensure HTTP 415 is returned back to the client"""
 
92
        try :
 
93
            opener.open(req)
 
94
        except urllib2.HTTPError, e:
 
95
            self.assertEquals(415, e.code)
 
96
            expected = "No protocol matching Content-Type '%s' at path '%s'." % \
 
97
                                (req.headers.get('Content-Type', 'text/plain'),
 
98
                                  '/login/rpc')
 
99
            got = e.fp.read()
 
100
            self.assertEquals(expected, got)
 
101
        except Exception, e:
 
102
            self.fail('Expected HTTP error but %s raised instead' % \
 
103
                                              (e.__class__.__name__,))
 
104
        else :
 
105
            self.fail('Expected HTTP error (415) but nothing raised')
 
106
 
 
107
    def assert_form_protect(self, opener, req):
 
108
        e = self.assertRaises(urllib2.HTTPError, opener.open, req)
 
109
        self.assertEquals(400, e.code)
 
110
        msg = e.fp.read()
 
111
        self.assertTrue("Missing or invalid form token. "
 
112
                                "Do you have cookies enabled?" in msg)
 
113
 
 
114
def test_suite():
 
115
    return unittest.makeSuite(DocumentationTestCase)
 
116
 
 
117
if __name__ == '__main__':
 
118
    unittest.main(defaultTest='test_suite')