Example #1
0
def do_request_and_asserts(rf,
                           contact,
                           maintenance=False,
                           expect_all_seeing=False,
                           expect_toolbar=False):
    request = apply_request_middleware(rf.get("/"),
                                       user=contact.user,
                                       customer=contact)
    response = IndexView.as_view()(request)
    response.render()
    soup = BeautifulSoup(response.content)
    if expect_toolbar:
        toolbar = soup.find("nav", {"class": "navbar-admin-tools"})
        assert toolbar

    if expect_toolbar:
        assert request.shop.maintenance_mode == maintenance
        maintenance_class = "badge-warning" if maintenance else "badge-success"
        assert soup.find("span", {"class": maintenance_class})

    texts = []
    for elem in soup.find_all("a"):
        texts.append(elem.text.strip())

    if contact.user.is_superuser:
        text = "show only visible products and categories" if expect_all_seeing else "show all products and categories"
        assert_text_in_texts(texts, text, True)
    else:
        assert_text_in_texts(texts,
                             "show only visible products and categories",
                             False)
        assert_text_in_texts(texts, "show all products and categories", False)
Example #2
0
def do_request_and_asserts(rf,
                           contact,
                           maintenance=False,
                           expect_all_seeing=False,
                           expect_toolbar=False):
    request = apply_request_middleware(rf.get("/"),
                                       user=contact.user,
                                       customer=contact)
    response = IndexView.as_view()(request)
    response.render()
    soup = BeautifulSoup(response.content)
    if expect_toolbar:
        toolbar = soup.find("nav", {"class": "navbar-admin-tools"})
        assert toolbar

    if maintenance:
        maintenance_class = "maintenance-on" if maintenance else "maintenance-off"
        assert soup.find("span", {"class": maintenance_class})

    texts = []
    for elem in soup.find_all("a"):
        texts.append(elem.text.strip())

    if contact.user.is_superuser:
        text = "Show only visible products and categories" if expect_all_seeing else "Show all products and categories"
        assert text in texts
    else:
        assert "Show only visible products and categories" not in texts
        assert "Show all products and categories" not in texts
Example #3
0
def _get_front_soup(rf):
    view_func = IndexView.as_view()
    request = apply_request_middleware(rf.get("/"))
    response = view_func(request)
    response.render()
    content = (response.content)
    return BeautifulSoup(content)
def _get_front_soup(rf):
    view_func = IndexView.as_view()
    request = apply_request_middleware(rf.get("/"))
    response = view_func(request)
    response.render()
    content = (response.content)
    return BeautifulSoup(content)
Example #5
0
def do_request_and_asserts(rf, contact, maintenance=False, expect_all_seeing=False, expect_toolbar=False):
    request = apply_request_middleware(rf.get("/"), user=contact.user, customer=contact)
    response = IndexView.as_view()(request)
    response.render()
    soup = BeautifulSoup(response.content)
    if expect_toolbar:
        toolbar = soup.find("nav", {"class": "navbar-admin-tools"})
        assert toolbar

    if maintenance:
        maintenance_class = "maintenance-on" if maintenance else "maintenance-off"
        assert soup.find("span", {"class": maintenance_class})

    texts = []
    for elem in soup.find_all("a"):
        texts.append(elem.text.strip())

    if contact.user.is_superuser:
        text = "Show only visible products and categories" if expect_all_seeing else "Show all products and categories"
        assert text in texts
    else:
        assert "Show only visible products and categories" not in texts
        assert "Show all products and categories" not in texts
Example #6
0
def do_request_and_asserts(rf, contact, maintenance=False, expect_all_seeing=False, expect_toolbar=False):
    request = apply_request_middleware(rf.get("/"), user=contact.user, customer=contact)
    response = IndexView.as_view()(request)
    response.render()
    soup = BeautifulSoup(response.content)
    if expect_toolbar:
        toolbar = soup.find("nav", {"class": "navbar-admin-tools"})
        assert toolbar

    if expect_toolbar:
        assert request.shop.maintenance_mode == maintenance
        maintenance_class = "badge-warning" if maintenance else "badge-success"
        assert soup.find("span", {"class": maintenance_class})

    texts = []
    for elem in soup.find_all("a"):
        texts.append(elem.text.strip())

    if contact.user.is_superuser:
        text = "show only visible products and categories" if expect_all_seeing else "show all products and categories"
        assert_text_in_texts(texts, text, True)
    else:
        assert_text_in_texts(texts, "show only visible products and categories", False)
        assert_text_in_texts(texts, "show all products and categories", False)
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
def test_admin_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.admin.error_handlers.AdminPageErrorHandler"
            ],
            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("^index/", IndexView.as_view()),
                url("^dash/", DashboardView.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()

            # here the urlconfs will be the default handlers
            # because admin can't handle such requests
            # but the functions of the handlers are pointing to our factory view
            assert urlconf.handler404 != four_oh_four
            assert urlconf.handler500 != handler500
            assert "flesh wound" in force_text(
                urlconf.handler404(rf.get("/notfound/")).content)
            assert "The best error" in force_text(
                urlconf.handler500(rf.get("/aaargh/")).content)

            # Admin must handle only admin app errors
            error_handler = AdminPageErrorHandler()

            # simulate a view to check whether the handler can handle an error of a non-front view
            # Admin handler will check for the resolver_match and admin app only
            response = handler.get_response(rf.get("/aaargh/"))
            assert b"The best error" in response.content

            # can't handle non admin views neither media or static files
            from django.conf import settings
            for path in ("/aaargh/", "/index/",
                         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

            # simulate a view to check whether the handler can handle an error of an admin view
            request = rf.get("/dash/")
            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

            # check the error handlers return the correct status and text
            for status, content in [
                (500, "admin 500"),
                (400, "admin 400"),
                (403, "admin 403"),
                (404, "admin 404"),
            ]:
                response = error_handler.handle_error(request, status)
                assert response.status_code == status
                assert content in force_text(response.content)
Example #9
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
Example #10
0
def test_admin_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.admin.error_handlers.AdminPageErrorHandler"],
        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("^index/", IndexView.as_view()),
            url("^dash/", DashboardView.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()

            # here the urlconfs will be the default handlers
            # because admin can't handle such requests
            # but the functions of the handlers are pointing to our factory view
            assert urlconf.handler404 != four_oh_four
            assert urlconf.handler500 != handler500
            assert "flesh wound" in force_text(urlconf.handler404(rf.get("/notfound/")).content)
            assert "The best error" in force_text(urlconf.handler500(rf.get("/aaargh/")).content)

            # Admin must handle only admin app errors
            error_handler = AdminPageErrorHandler()

            # simulate a view to check whether the handler can handle an error of a non-front view
            # Admin handler will check for the resolver_match and admin app only
            response = handler.get_response(rf.get("/aaargh/"))
            assert b"The best error" in response.content

            # can't handle non admin views neither media or static files
            from django.conf import settings
            for path in ("/aaargh/", "/index/", 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

            # simulate a view to check whether the handler can handle an error of an admin view
            request = rf.get("/dash/")
            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

            # check the error handlers return the correct status and text
            for status, content in [
                (500, "admin 500"),
                (400, "admin 400"),
                (403, "admin 403"),
                (404, "admin 404"),
            ]:
                response = error_handler.handle_error(request, status)
                assert response.status_code == status
                assert content in force_text(response.content)