def test_front_error_handlers(rf):
    """
    Test that `SHUUP_ERROR_PAGE_HANDLERS_SPEC` installs error handlers that are overwriting custom ones.
    """
    with override_settings(
            DEBUG=False,
            SHUUP_ERROR_PAGE_HANDLERS_SPEC=[
                "shuup.front.error_handlers.FrontPageErrorHandler"
            ],
            MIDDLEWARE_CLASSES=[],  # For Django < 2
            MIDDLEWARE=[],
            TEMPLATES=
        [  # Overriden to be sure about the contents of our 500.jinja
            {
                "BACKEND":
                "django_jinja.backend.Jinja2",
                "DIRS": [
                    os.path.realpath(
                        os.path.join(os.path.dirname(__file__), "templates"))
                ],
                "OPTIONS": {
                    "match_extension": ".jinja",
                    "newstyle_gettext": True,
                },
                "NAME":
                "jinja2",
            }
        ]):
        with replace_urls([
                url("^aaargh/", errorful_view),
                url("^notfound/", notfound_view),
                url("^dash/", DashboardView.as_view()),
                url("^index/", IndexView.as_view()),
        ], {
                "handler404": four_oh_four,
                "handler500": handler500
        }):
            resolver = get_resolver(None)
            urlconf = resolver.urlconf_module
            handler = BaseHandler()
            handler.load_middleware()

            # test without installing the handler
            assert urlconf.handler404 == four_oh_four
            assert urlconf.handler500 == handler500

            # Test 500
            response = handler.get_response(rf.get("/aaargh/"))
            assert response.status_code == 500
            assert b"The best error" in response.content
            # Test 404
            response = handler.get_response(rf.get("/another_castle/"))
            assert response.status_code == 404
            assert b"flesh wound" in response.content

            # inject our custom error handlers
            install_error_handlers()
            assert urlconf.handler404 != four_oh_four
            assert urlconf.handler500 != handler500
            assert "miss something? 404" in force_text(
                urlconf.handler404(rf.get("/notfound/")).content)
            assert "intergalactic testing 500" in force_text(
                urlconf.handler500(rf.get("/aaargh/")).content)

            # Front must handle all possible apps
            error_handler = FrontPageErrorHandler()

            response = handler.get_response(rf.get("/aaargh/"))
            assert "intergalactic testing 500" in force_text(response.content)

            # simulate a view to check whether the handler can handle an
            # error of a non-front view, a front view and a admin view
            for path in ("/aaargh/", "/index/", "/dash/"):
                request = rf.get(path)
                assert error_handler.can_handle_error(request, 500)
                assert error_handler.can_handle_error(request, 400)
                assert error_handler.can_handle_error(request, 403)
                assert error_handler.can_handle_error(request, 404)

                # check the error handlers return the correct status and text
                for status, content in [
                    (500, "intergalactic testing 500"),
                    (400, "about 400"),
                    (403, "get out 403"),
                    (404, "miss something? 404"),
                ]:
                    response = error_handler.handle_error(request, status)
                    assert response.status_code == status
                    assert content in force_text(response.content)

            from django.conf import settings
            # front can't handle static and media paths
            for path in (settings.STATIC_URL + "mystaticfile",
                         settings.MEDIA_URL + "mymediafile"):
                request = rf.get(path)
                assert error_handler.can_handle_error(request, 500) is False
                assert error_handler.can_handle_error(request, 400) is False
                assert error_handler.can_handle_error(request, 403) is False
                assert error_handler.can_handle_error(request, 404) is False
Beispiel #2
0
def test_front_error_handlers(rf):
    """
    Test that SHUUP_ERROR_PAGE_HANDLERS_SPEC installs error handlers overwriting custom ones
    """
    with override_settings(
        DEBUG=False,
        SHUUP_ERROR_PAGE_HANDLERS_SPEC=["shuup.front.error_handlers.FrontPageErrorHandler"],
        MIDDLEWARE_CLASSES=[],
        TEMPLATES=[  # Overriden to be sure about the contents of our 500.jinja
            {
                "BACKEND": "django_jinja.backend.Jinja2",
                "DIRS": [
                    os.path.realpath(os.path.join(os.path.dirname(__file__), "templates"))
                ],
                "OPTIONS": {
                    "match_extension": ".jinja",
                    "newstyle_gettext": True,
                },
                "NAME": "jinja2",
            }
        ]
    ):
        with replace_urls([
            url("^aaargh/", errorful_view),
            url("^notfound/", notfound_view),
            url("^dash/", DashboardView.as_view()),
            url("^index/", IndexView.as_view()),
        ], {"handler404": four_oh_four, "handler500": handler500}):
            resolver = get_resolver(None)
            urlconf = resolver.urlconf_module
            handler = BaseHandler()
            handler.load_middleware()

            # test without installing the handler
            assert urlconf.handler404 == four_oh_four
            assert urlconf.handler500 == handler500

            # Test 500
            response = handler.get_response(rf.get("/aaargh/"))
            assert response.status_code == 500
            assert b"The best error" in response.content
            # Test 404
            response = handler.get_response(rf.get("/another_castle/"))
            assert response.status_code == 404
            assert b"flesh wound" in response.content

            # inject our custom error handlers
            install_error_handlers()
            assert urlconf.handler404 != four_oh_four
            assert urlconf.handler500 != handler500
            assert "miss something? 404" in force_text(urlconf.handler404(rf.get("/notfound/")).content)
            assert "intergalactic testing 500" in force_text(urlconf.handler500(rf.get("/aaargh/")).content)

            # Front must handle all possible apps
            error_handler = FrontPageErrorHandler()

            response = handler.get_response(rf.get("/aaargh/"))
            assert "intergalactic testing 500" in force_text(response.content)

            # simulate a view to check whether the handler can handle an
            # error of a non-front view, a front view and a admin view
            for path in ("/aaargh/", "/index/", "/dash/"):
                request = rf.get(path)
                assert error_handler.can_handle_error(request, 500)
                assert error_handler.can_handle_error(request, 400)
                assert error_handler.can_handle_error(request, 403)
                assert error_handler.can_handle_error(request, 404)

                # check the error handlers return the correct status and text
                for status, content in [
                    (500, "intergalactic testing 500"),
                    (400, "about 400"),
                    (403, "get out 403"),
                    (404, "miss something? 404"),
                ]:
                    response = error_handler.handle_error(request, status)
                    assert response.status_code == status
                    assert content in force_text(response.content)

            from django.conf import settings
            # front can't handle static and media paths
            for path in (settings.STATIC_URL + "mystaticfile", settings.MEDIA_URL + "mymediafile"):
                request = rf.get(path)
                assert error_handler.can_handle_error(request, 500) is False
                assert error_handler.can_handle_error(request, 400) is False
                assert error_handler.can_handle_error(request, 403) is False
                assert error_handler.can_handle_error(request, 404) is False