@@ -0,0 +1,4073 @@
+# Copyright 2015 Google Inc. All rights reserved.%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A#     Unless required by applicable law or agreed to in writing, software%0A#     distributed under the License is distributed on an %22AS IS%22 BASIS,%0A#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A#     See the License for the specific language governing permissions and%0A#     limitations under the License.%0A%22%22%22Tests for main.%22%22%22%0A%0Aimport webapp2%0Afrom webapp2_extras.routes import MultiRoute%0A%0Afrom app import routes%0Afrom app.base import handlers%0Afrom tests.testcases import BaseTestCase%0A%0A%0Aclass MainTest(BaseTestCase):%0A%0A    %22%22%22Test cases for main.%22%22%22%0A%0A    def _VerifyInheritance(self, routes_list, base_class):%0A        %22%22%22Checks that the handlers of the given routes inherit from base_class.%22%22%22%0A%0A        router = webapp2.Router(routes_list)%0A        route_list = router.match_routes + router.build_routes.values()%0A        inheritance_errors = ''%0A        for route in route_list:%0A            if issubclass(route.__class__, MultiRoute):%0A                self._VerifyInheritance(list(route.get_routes()), base_class)%0A                continue%0A%0A            if issubclass(route.handler, webapp2.RedirectHandler):%0A                continue%0A%0A            if not issubclass(route.handler, base_class):%0A                inheritance_errors += '* %25s does not inherit from %25s.%5Cn' %25 (%0A                    route.handler.__name__, base_class.__name__)%0A%0A        return inheritance_errors%0A%0A    def testRoutesInheritance(self):%0A        errors = ''%0A        errors += self._VerifyInheritance(routes._UNAUTHENTICATED_ROUTES,%0A                                          handlers.BaseHandler)%0A        errors += self._VerifyInheritance(routes._UNAUTHENTICATED_AJAX_ROUTES,%0A                                          handlers.BaseAjaxHandler)%0A        errors += self._VerifyInheritance(routes._USER_ROUTES,%0A                                          handlers.AuthenticatedHandler)%0A        errors += self._VerifyInheritance(routes._AJAX_ROUTES,%0A                                          handlers.AuthenticatedAjaxHandler)%0A        errors += self._VerifyInheritance(routes._ADMIN_ROUTES,%0A                                          handlers.AdminHandler)%0A        errors += self._VerifyInheritance(routes._ADMIN_AJAX_ROUTES,%0A                                          handlers.AdminAjaxHandler)%0A        errors += self._VerifyInheritance(routes._CRON_ROUTES,%0A                                          handlers.BaseCronHandler)%0A        errors += self._VerifyInheritance(routes._TASK_ROUTES,%0A                                          handlers.BaseTaskHandler)%0A        if errors:%0A            self.fail('Some handlers do not inherit from the correct classes:%5Cn' + errors)%0A%0A    def testStrictHandlerMethodRouting(self):%0A        %22%22%22Checks that handler functions properly limit applicable HTTP methods.%22%22%22%0A        router = webapp2.Router(routes._USER_ROUTES + routes._AJAX_ROUTES +%0A                                routes._ADMIN_ROUTES + routes._ADMIN_AJAX_ROUTES)%0A        route_list = router.match_routes + router.build_routes.values()%0A        failed_routes = %5B%5D%0A        while route_list:%0A            route = route_list.pop()%0A            if issubclass(route.__class__, MultiRoute):%0A                route_list += list(route.get_routes())%0A                continue%0A%0A            if issubclass(route.handler, webapp2.RedirectHandler):%0A                continue%0A%0A            if route.handler_method and not route.methods:%0A                failed_routes.append('%25s (%25s)' %25 (route.template,%0A                                                  route.handler.__name__))%0A%0A        if failed_routes:%0A            self.fail('Some handlers specify a handler_method but are missing a '%0A                      'methods%22 attribute and may be vulnerable to XSRF via GET '%0A                      'requests:%5Cn * ' + '%5Cn * '.join(failed_routes))%0A

Test handler inheritance and MultiRoute support