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."""
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
):
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',
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',
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',
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',
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',
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."""
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)
139 self
.assertEqual(admin_request_handler
._DEFAULT
_SDK
_VERSION
,
140 admin_request_handler
._get
_sdk
_version
())
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__':