def test_admin(rf, admin_user): shop = get_default_shop() configuration.set(shop, "setup_wizard_complete", True) # just visit to make sure everything is ok request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 200 categories = [CategoryFactory().pk, CategoryFactory().pk, CategoryFactory().pk] manager.save_categories(shop, categories) request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 200
def test_dashboard_redirect(rf, admin_user, settings): settings.SHUUP_SETUP_WIZARD_PANE_SPEC = [] shop = get_default_shop() shop.maintenance_mode = True shop.save() request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:home") shop.maintenance_mode = False shop.save() request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 200
def get_urls(): urls = [] urls.extend(get_module_urls()) urls.extend([ admin_url(r'^$', DashboardView.as_view(), name='dashboard'), admin_url(r'^search/$', SearchView.as_view(), name='search'), admin_url(r'^select/$', MultiselectAjaxView.as_view(), name='select'), admin_url(r'^menu/$', MenuView.as_view(), name='menu'), admin_url(r'^login/$', login, kwargs={"template_name": "shuup/admin/auth/login.jinja"}, name='login', require_authentication=False), admin_url(r'^logout/$', auth_views.logout, kwargs={"template_name": "shuup/admin/auth/logout.jinja"}, name='logout', require_authentication=False), admin_url(r'^set-language/$', csrf_exempt(set_language), name="set-language"), ]) for u in urls: # pragma: no cover if not isinstance(u, AdminRegexURLPattern): warnings.warn("Admin URL %r is not an AdminRegexURLPattern" % u) # Add Django javascript catalog url urls.append(url(r'^i18n.js$', javascript_catalog_all, name='js-catalog')) return tuple(urls)
def get_urls(): urls = [] urls.extend(get_module_urls()) urls.extend([ admin_url(r'^$', DashboardView.as_view(), name='dashboard', permissions=()), admin_url(r'^home/$', HomeView.as_view(), name='home', permissions=()), admin_url(r'^wizard/$', WizardView.as_view(), name='wizard', permissions=()), admin_url(r'^tour/$', TourView.as_view(), name='tour', permissions=()), admin_url(r'^search/$', SearchView.as_view(), name='search', permissions=()), admin_url(r'^select/$', MultiselectAjaxView.as_view(), name='select', permissions=()), admin_url(r'^edit/$', EditObjectView.as_view(), name='edit', permissions=()), admin_url(r'^menu/$', MenuView.as_view(), name='menu', permissions=()), admin_url(r'^toggle-menu/$', MenuToggleView.as_view(), name='menu_toggle', permissions=()), admin_url( r'^login/$', login, kwargs={"template_name": "shuup/admin/auth/login.jinja"}, name='login', require_authentication=False, permissions=() ), admin_url( r'^logout/$', auth_views.logout, kwargs={"template_name": "shuup/admin/auth/logout.jinja"}, name='logout', require_authentication=False, permissions=() ), admin_url( r'^recover-password/(?P<uidb64>.+)/(?P<token>.+)/$', ResetPasswordView, name='recover_password', require_authentication=False, permissions=() ), admin_url( r'^request-password/$', RequestPasswordView, name='request_password', require_authentication=False, permissions=() ), admin_url( r'^set-language/$', csrf_exempt(set_language), name="set-language", permissions=() ), ]) for u in urls: # pragma: no cover if not isinstance(u, AdminRegexURLPattern): warnings.warn("Admin URL %r is not an AdminRegexURLPattern" % u) # Add Django javascript catalog url urls.append(url(r'^i18n.js$', javascript_catalog_all, name='js-catalog')) return tuple(urls)
def test_telemetry_is_sent_on_login(rf, admin_user): shop = get_default_shop() with patch.object(requests, "post", return_value=MockResponse("test")) as requestor: with override_settings(SHUUP_TELEMETRY_ENABLED=True): _backdate_installation_key(days=0) # instance was created today request = apply_request_middleware(rf.get("/"), user=admin_user) view_func = DashboardView.as_view() response = view_func(request) sent = get_last_submission_time() response = view_func(request) assert get_last_submission_time() == sent response = view_func(request) assert get_last_submission_time() == sent assert len(requestor.mock_calls) == 1
def get_urls(): urls = [] urls.extend(get_module_urls()) urls.extend([ admin_url(r'^$', DashboardView.as_view(), name='dashboard'), admin_url(r'^home/$', HomeView.as_view(), name='home'), admin_url(r'^wizard/$', WizardView.as_view(), name='wizard'), admin_url(r'^tour/$', TourView.as_view(), name='tour'), admin_url(r'^search/$', SearchView.as_view(), name='search'), admin_url(r'^select/$', MultiselectAjaxView.as_view(), name='select'), admin_url(r'^menu/$', MenuView.as_view(), name='menu'), admin_url( r'^login/$', login, kwargs={"template_name": "shuup/admin/auth/login.jinja"}, name='login', require_authentication=False ), admin_url( r'^logout/$', auth_views.logout, kwargs={"template_name": "shuup/admin/auth/logout.jinja"}, name='logout', require_authentication=False ), admin_url( r'^set-language/$', csrf_exempt(set_language), name="set-language" ), ]) for u in urls: # pragma: no cover if not isinstance(u, AdminRegexURLPattern): warnings.warn("Admin URL %r is not an AdminRegexURLPattern" % u) # Add Django javascript catalog url urls.append(url(r'^i18n.js$', javascript_catalog_all, name='js-catalog')) return tuple(urls)
def get_urls(): urls = [] urls.extend(get_module_urls()) urls.extend( [ admin_url(r"^$", DashboardView.as_view(), name="dashboard"), admin_url(r"^home/$", HomeView.as_view(), name="home"), admin_url(r"^wizard/$", WizardView.as_view(), name="wizard"), admin_url(r"^tour/$", TourView.as_view(), name="tour"), admin_url(r"^search/$", SearchView.as_view(), name="search"), admin_url(r"^select/$", MultiselectAjaxView.as_view(), name="select"), admin_url(r"^menu/$", MenuView.as_view(), name="menu"), admin_url( r"^login/$", login, kwargs={"template_name": "shuup/admin/auth/login.jinja"}, name="login", require_authentication=False, ), admin_url( r"^logout/$", auth_views.logout, kwargs={"template_name": "shuup/admin/auth/logout.jinja"}, name="logout", require_authentication=False, ), admin_url(r"^set-language/$", csrf_exempt(set_language), name="set-language"), ] ) for u in urls: # pragma: no cover if not isinstance(u, AdminRegexURLPattern): warnings.warn("Admin URL %r is not an AdminRegexURLPattern" % u) # Add Django javascript catalog url urls.append(url(r"^i18n.js$", javascript_catalog_all, name="js-catalog")) return tuple(urls)
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)
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
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)
def get_urls(): urls = [] urls.extend(get_module_urls()) urls.extend([ admin_url(r"^$", DashboardView.as_view(), name="dashboard", permissions=()), admin_url(r"^home/$", HomeView.as_view(), name="home", permissions=()), admin_url(r"^wizard/$", WizardView.as_view(), name="wizard", permissions=()), admin_url(r"^tour/$", TourView.as_view(), name="tour", permissions=()), admin_url(r"^search/$", SearchView.as_view(), name="search", permissions=()), admin_url(r"^select/$", MultiselectAjaxView.as_view(), name="select", permissions=()), admin_url(r"^edit/$", EditObjectView.as_view(), name="edit", permissions=()), admin_url(r"^menu/$", MenuView.as_view(), name="menu", permissions=()), admin_url(r"^toggle-menu/$", MenuToggleView.as_view(), name="menu_toggle", permissions=()), admin_url( r"^stop-impersonating-staff/$", stop_impersonating_staff, name="stop-impersonating-staff", permissions=(), ), admin_url( r"^login/$", login, kwargs={"template_name": "shuup/admin/auth/login.jinja"}, name="login", require_authentication=False, permissions=(), ), admin_url(r"^logout/$", LogoutView, name="logout", require_authentication=False, permissions=()), admin_url( r"^recover-password/(?P<uidb64>.+)/(?P<token>.+)/$", ResetPasswordView, name="recover_password", require_authentication=False, permissions=(), ), admin_url( r"^request-password/$", RequestPasswordView, name="request_password", require_authentication=False, permissions=(), ), admin_url(r"^set-language/$", csrf_exempt(set_language), name="set-language", permissions=()), ]) for u in urls: # pragma: no cover if not isinstance(u, AdminRegexURLPattern): warnings.warn( "Warning! Admin URL %r is not an `AdminRegexURLPattern`." % u) # Add Django javascript catalog url urls.append(url(r"^i18n.js$", javascript_catalog_all, name="js-catalog")) return tuple(urls)