App Engine Python SDK version 1.7.7
[gae.git] / python / google / appengine / tools / devappserver2 / admin / admin_request_handler_test.py
blob439804abf31f8e2db38eab221df98f534dd203f0
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Tests for devappserver2.admin.admin_request_handler."""
20 import os.path
21 import tempfile
22 import unittest
23 import urlparse
25 import google
26 import mox
27 import webapp2
29 from google.appengine.tools import sdk_update_checker
30 from google.appengine.tools.devappserver2.admin import admin_request_handler
33 class XSRFHandler(admin_request_handler.AdminRequestHandler):
35 def get(self):
36 pass
38 def post(self):
39 pass
41 APP = webapp2.WSGIApplication([('/.*', XSRFHandler)])
44 class XSRFTest(unittest.TestCase):
45 """Tests for the admin_request_handler XSRF protection."""
47 def test_init_xsrf(self):
48 xsrf_path = os.path.join(tempfile.mkdtemp(), 'xsrf')
49 admin_request_handler.AdminRequestHandler.init_xsrf(xsrf_path)
50 xsrf_token = admin_request_handler.AdminRequestHandler.xsrf_token
51 self.assertEqual(10, len(xsrf_token))
53 admin_request_handler.AdminRequestHandler.init_xsrf(xsrf_path)
54 self.assertEqual(xsrf_token,
55 admin_request_handler.AdminRequestHandler.xsrf_token)
57 def test_xsrf_required_not_set(self):
58 admin_request_handler.AdminRequestHandler.xsrf_token = '123456789'
60 response = APP.get_response('/test', method='POST')
61 self.assertEqual(403, response.status_int)
63 def test_xsrf_required_and_correct(self):
64 admin_request_handler.AdminRequestHandler.xsrf_token = '123456789'
66 response = APP.get_response('/test',
67 method='POST',
68 POST={'xsrf_token': '123456789'})
69 self.assertEqual(200, response.status_int)
71 def test_xsrf_required_and_wrong(self):
72 admin_request_handler.AdminRequestHandler.xsrf_token = '123456789'
74 response = APP.get_response('/test',
75 method='POST',
76 POST={'xsrf_token': 'wrong'})
77 self.assertEqual(403, response.status_int)
79 def test_xsrf_not_required(self):
80 response = APP.get_response('/test')
81 self.assertEqual(200, response.status_int)
84 class ConstructUrlTest(unittest.TestCase):
85 """Tests for AdminRequestHandler._construct_url."""
87 def test_construct_url_no_args(self):
88 request = webapp2.Request.blank('/foo', POST={'arg1': 'value1',
89 'arg2': 'value2'})
90 response = webapp2.Response()
91 handler = admin_request_handler.AdminRequestHandler(request, response)
92 url = handler._construct_url()
93 parsed_url = urlparse.urlparse(url)
94 self.assertEqual('/foo', parsed_url.path)
95 self.assertEqual({'arg1': ['value1'], 'arg2': ['value2']},
96 urlparse.parse_qs(parsed_url.query))
98 def test_construct_url_remove(self):
99 request = webapp2.Request.blank('/foo', POST={'arg1': 'value1',
100 'arg2': 'value2'})
101 response = webapp2.Response()
102 handler = admin_request_handler.AdminRequestHandler(request, response)
103 url = handler._construct_url(remove=['arg1'])
104 parsed_url = urlparse.urlparse(url)
105 self.assertEqual('/foo', parsed_url.path)
106 self.assertEqual({'arg2': ['value2']},
107 urlparse.parse_qs(parsed_url.query))
109 def test_construct_url_add(self):
110 request = webapp2.Request.blank('/foo', POST={'arg1': 'value1',
111 'arg2': 'value2'})
112 response = webapp2.Response()
113 handler = admin_request_handler.AdminRequestHandler(request, response)
114 url = handler._construct_url(add={'arg2': 'new2', 'arg3': 'new3'})
115 parsed_url = urlparse.urlparse(url)
116 self.assertEqual('/foo', parsed_url.path)
117 self.assertEqual({'arg1': ['value1'], 'arg2': ['new2'], 'arg3': ['new3']},
118 urlparse.parse_qs(parsed_url.query))
121 class GetSDKVersionTest(unittest.TestCase):
122 """Tests for _get_sdk_version."""
124 def setUp(self):
125 self.mox = mox.Mox()
127 def tearDown(self):
128 self.mox.UnsetStubs()
130 def test_version_file_exists(self):
131 self.assertNotEqual(admin_request_handler._DEFAULT_SDK_VERSION,
132 admin_request_handler._get_sdk_version())
134 def test_version_file_missing(self):
135 self.mox.StubOutWithMock(sdk_update_checker, 'GetVersionObject')
136 sdk_update_checker.GetVersionObject().AndReturn(None)
138 self.mox.ReplayAll()
139 self.assertEqual(admin_request_handler._DEFAULT_SDK_VERSION,
140 admin_request_handler._get_sdk_version())
141 self.mox.VerifyAll()
144 class ByteSizeFormatTest(unittest.TestCase):
145 """Tests for the _byte_size_format jinja2 filter."""
147 def testOneByte(self):
148 self.assertEqual('1 Byte',
149 admin_request_handler._byte_size_format('1'))
151 def testLessThan1KiB(self):
152 self.assertEqual('123 Bytes',
153 admin_request_handler._byte_size_format('123'))
155 def testLessThan1MiB(self):
156 self.assertEqual('5.5 KiB (5678 Bytes)',
157 admin_request_handler._byte_size_format('5678'))
159 def testLessThan1GiB(self):
160 self.assertEqual('11.8 MiB (12345678 Bytes)',
161 admin_request_handler._byte_size_format('12345678'))
163 def testGreaterThan1GiB(self):
164 self.assertEqual('1.1 GiB (1234567890 Bytes)',
165 admin_request_handler._byte_size_format('1234567890'))
167 if __name__ == '__main__':
168 unittest.main()