How to use handler500 method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...25def errorful_view(request, *args, **kwargs):26 raise Exception("Error! Aaargh.") # The Castle of.27def notfound_view(request, *args, **kwargs):28 raise Http404("Error! Ops!")29def handler500(request, *args, **kwargs):30 return HttpResponse("Error! The best error.", status=500)31def four_oh_four(request, *args, **kwargs):32 return HttpResponse("Error! Just a flesh wound.", status=404)33def test_front_error_handlers(rf):34 """35 Test that `SHUUP_ERROR_PAGE_HANDLERS_SPEC` installs error handlers that are overwriting custom ones.36 """37 with override_settings(38 DEBUG=False,39 SHUUP_ERROR_PAGE_HANDLERS_SPEC=["shuup.front.error_handlers.FrontPageErrorHandler"],40 MIDDLEWARE_CLASSES=[], # For Django < 241 MIDDLEWARE=[],42 TEMPLATES=[ # Overriden to be sure about the contents of our 500.jinja43 {44 "BACKEND": "django_jinja.backend.Jinja2",45 "DIRS": [os.path.realpath(os.path.join(os.path.dirname(__file__), "templates"))],46 "OPTIONS": {47 "match_extension": ".jinja",48 "newstyle_gettext": True,49 },50 "NAME": "jinja2",51 }52 ],53 ):54 with replace_urls(55 [56 url("^aaargh/", errorful_view),57 url("^notfound/", notfound_view),58 url("^dash/", DashboardView.as_view()),59 url("^index/", IndexView.as_view()),60 ],61 {"handler404": four_oh_four, "handler500": handler500},62 ):63 resolver = get_resolver(None)64 urlconf = resolver.urlconf_module65 handler = BaseHandler()66 handler.load_middleware()67 # test without installing the handler68 assert urlconf.handler404 == four_oh_four69 assert urlconf.handler500 == handler50070 # Test 50071 response = handler.get_response(rf.get("/aaargh/"))72 assert response.status_code == 50073 assert b"The best error" in response.content74 # Test 40475 response = handler.get_response(rf.get("/another_castle/"))76 assert response.status_code == 40477 assert b"flesh wound" in response.content78 # inject our custom error handlers79 install_error_handlers()80 assert urlconf.handler404 != four_oh_four81 assert urlconf.handler500 != handler50082 assert "miss something? 404" in force_text(urlconf.handler404(rf.get("/notfound/")).content)83 assert "intergalactic testing 500" in force_text(urlconf.handler500(rf.get("/aaargh/")).content)84 # Front must handle all possible apps85 error_handler = FrontPageErrorHandler()86 response = handler.get_response(rf.get("/aaargh/"))87 assert "intergalactic testing 500" in force_text(response.content)88 # simulate a view to check whether the handler can handle an89 # error of a non-front view, a front view and a admin view90 for path in ("/aaargh/", "/index/", "/dash/"):91 request = rf.get(path)92 assert error_handler.can_handle_error(request, 500)93 assert error_handler.can_handle_error(request, 400)94 assert error_handler.can_handle_error(request, 403)95 assert error_handler.can_handle_error(request, 404)96 # check the error handlers return the correct status and text97 for status, content in [98 (500, "intergalactic testing 500"),99 (400, "about 400"),100 (403, "get out 403"),101 (404, "miss something? 404"),102 ]:103 response = error_handler.handle_error(request, status)104 assert response.status_code == status105 assert content in force_text(response.content)106 from django.conf import settings107 # front can't handle static and media paths108 for path in (settings.STATIC_URL + "mystaticfile", settings.MEDIA_URL + "mymediafile"):109 request = rf.get(path)110 assert error_handler.can_handle_error(request, 500) is False111 assert error_handler.can_handle_error(request, 400) is False112 assert error_handler.can_handle_error(request, 403) is False113 assert error_handler.can_handle_error(request, 404) is False114def test_admin_error_handlers(rf):115 """116 Test that SHUUP_ERROR_PAGE_HANDLERS_SPEC installs error handlers that are overwriting custom ones.117 """118 with override_settings(119 DEBUG=False,120 SHUUP_ERROR_PAGE_HANDLERS_SPEC=["shuup.admin.error_handlers.AdminPageErrorHandler"],121 MIDDLEWARE_CLASSES=[], # For Django 2122 MIDDLEWARE=[],123 TEMPLATES=[ # Overriden to be sure about the contents of our 500.jinja124 {125 "BACKEND": "django_jinja.backend.Jinja2",126 "DIRS": [os.path.realpath(os.path.join(os.path.dirname(__file__), "templates"))],127 "OPTIONS": {128 "match_extension": ".jinja",129 "newstyle_gettext": True,130 },131 "NAME": "jinja2",132 }133 ],134 ):135 with replace_urls(136 [137 url("^aaargh/", errorful_view),138 url("^index/", IndexView.as_view()),139 url("^dash/", DashboardView.as_view()),140 ],141 {"handler404": four_oh_four, "handler500": handler500},142 ):143 resolver = get_resolver(None)144 urlconf = resolver.urlconf_module145 handler = BaseHandler()146 handler.load_middleware()147 # test without installing the handler148 assert urlconf.handler404 == four_oh_four149 assert urlconf.handler500 == handler500150 # Test 500151 response = handler.get_response(rf.get("/aaargh/"))152 assert response.status_code == 500153 assert b"The best error" in response.content154 # Test 404155 response = handler.get_response(rf.get("/another_castle/"))156 assert response.status_code == 404157 assert b"flesh wound" in response.content158 # inject our custom error handlers159 install_error_handlers()160 # here the urlconfs will be the default handlers161 # because admin can't handle such requests162 # but the functions of the handlers are pointing to our factory view163 assert urlconf.handler404 != four_oh_four164 assert urlconf.handler500 != handler500165 assert "flesh wound" in force_text(urlconf.handler404(rf.get("/notfound/")).content)166 assert "The best error" in force_text(urlconf.handler500(rf.get("/aaargh/")).content)167 # Admin must handle only admin app errors168 error_handler = AdminPageErrorHandler()169 # simulate a view to check whether the handler can handle an error of a non-front view170 # Admin handler will check for the resolver_match and admin app only171 response = handler.get_response(rf.get("/aaargh/"))172 assert b"The best error" in response.content173 # can't handle non admin views neither media or static files174 from django.conf import settings175 for path in (176 "/aaargh/",177 "/index/",178 settings.STATIC_URL + "mystaticfile",179 settings.MEDIA_URL + "mymediafile",180 ):181 request = rf.get(path)182 assert error_handler.can_handle_error(request, 500) is False183 assert error_handler.can_handle_error(request, 400) is False184 assert error_handler.can_handle_error(request, 403) is False185 assert error_handler.can_handle_error(request, 404) is False186 # simulate a view to check whether the handler can handle an error of an admin view187 request = rf.get("/dash/")188 assert error_handler.can_handle_error(request, 500) is False189 assert error_handler.can_handle_error(request, 400) is False190 assert error_handler.can_handle_error(request, 403) is False191 assert error_handler.can_handle_error(request, 404) is False192 # check the error handlers return the correct status and text193 for status, content in [194 (500, "admin 500"),195 (400, "admin 400"),196 (403, "admin 403"),197 (404, "admin 404"),198 ]:199 response = error_handler.handle_error(request, status)200 assert response.status_code == status201 assert content in force_text(response.content)202def test_install_error_handlers(rf):203 # no error handler set204 with override_settings(DEBUG=False, SHUUP_ERROR_PAGE_HANDLERS_SPEC=[], MIDDLEWARE_CLASSES=[], MIDDLEWARE=[]):205 def intact_view(request, *args, **kwargs):206 return HttpResponse("OK")207 # set handlers in root urlconf208 with replace_urls(209 [210 url("^/", intact_view),211 ],212 {213 "handler400": intact_view,214 "handler403": intact_view,215 "handler404": intact_view,216 "handler500": intact_view,217 },218 ):219 # install error handlers - as soon as no spec was set,220 # the handlers must return the same as the default handlers221 install_error_handlers()222 resolver = get_resolver(None)223 urlconf = resolver.urlconf_module224 assert urlconf.handler500 != intact_view225 assert urlconf.handler400 != intact_view226 assert urlconf.handler403 != intact_view227 assert urlconf.handler404 != intact_view228 request = rf.get("/")229 assert urlconf.handler400(request).content == intact_view(request).content230 assert urlconf.handler403(request).content == intact_view(request).content231 assert urlconf.handler404(request).content == intact_view(request).content232 assert urlconf.handler500(request).content == intact_view(request).content233 # force clear again234 import shuup.core.error_handling as error_handling235 error_handling._URLCONF_ERROR_HANDLERS.clear()236 # NO handler set in root urlconf237 with replace_urls(238 [239 url("^aaargh/", errorful_view),240 url("^notfound/", notfound_view),241 ],242 {},243 ):244 # install error handlers - as soon as no spec was set,245 # neither handlers set in urlconf, must return blank http responses with errors246 install_error_handlers()247 resolver = get_resolver(None)248 urlconf = resolver.urlconf_module249 request = rf.get("/")250 assert urlconf.handler400(request).status_code == 400251 assert urlconf.handler403(request).status_code == 403252 assert urlconf.handler404(request).status_code == 404253 assert urlconf.handler500(request).status_code == 500254 handler = BaseHandler()255 handler.load_middleware()256 response = handler.get_response(rf.get("/aaargh/"))257 assert response.status_code == 500258 response = handler.get_response(rf.get("/notfound/"))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?