def test_expect_http_error(self, browser): class GetRecordById(BrowserView): def __call__(self): raise BadRequest('Missing "id" parameter.') with register_view(GetRecordById, 'get-record-by-id'): with browser.expect_http_error(): browser.open(view='get-record-by-id')
def test_no_referer(self, browser): with register_view(RedirectView, 'redirect_view'): browser.login().visit(self.file, view="notification_form") browser.fill({"to_list:list": 'user.nr1', "comment": "blabla"}).submit() self.assertEquals( 'http://nohost/plone/folder/file/notification_form', browser.url)
def test_exceptions_are_passed_to_test(self, browser): class FailingView(BrowserView): def __call__(self): raise ValueError('The value is wrong.') with register_view(FailingView, 'failing-view'): with self.assertRaises(ValueError) as cm: browser.open(view='failing-view') self.assertEquals(str(cm.exception), 'The value is wrong.')
def test_redirect(self, browser): with register_view(RedirectView, 'redirect_view'): browser.login().visit(self.folder, view="redirect_view") browser.fill({"to_list:list": 'user.nr1', "comment": "blabla"}).submit() self.assertEquals('success', browser.contents) self.assertEquals('http://nohost/plone/folder/redirect_view', browser.url)
def test_no_exceptions_logged_when_errors_expected(self, browser): class FailingView(BrowserView): def __call__(self): raise ValueError('The value is wrong.') with register_view(FailingView, 'failing-view'): stderr = StringIO() with capture_streams(stderr=stderr): with browser.expect_http_error(): browser.open(view='failing-view') self.assertEquals( '', stderr.getvalue(), 'No errors should be logged when using expect_http_error')
def test_support_exception_bubbling(self, browser): class FailingView(BrowserView): def __call__(self): raise ValueError('The value is wrong.') with register_view(FailingView, 'failing-view'): # Excpect reguler HTTP error by default since the value error # is rendered by Zope as a 500. with browser.expect_http_error(code=500): browser.open(view='failing-view') # When exception bubbling is enabled we should be able to catch # the value error from the rendered view. browser.exception_bubbling = True with self.assertRaises(ValueError) as cm: browser.open(view='failing-view') self.assertEquals('The value is wrong.', str(cm.exception))
def test_send_authenticator(self, browser): class ProtectedView(BrowserView): def __call__(self): if not self.context.restrictedTraverse('@@authenticator').verify(): raise BadRequest('No way sir') else: return 'YAY' with register_view(ProtectedView, 'protected-view'): browser.login() # Fails when no authenticator token is provided: with browser.expect_http_error(code=400): browser.open(view='protected-view') # But we can tell the browser to send the authenticator token: browser.open(view='protected-view', send_authenticator=True) self.assertEquals('YAY', browser.contents)
def test_raises_500_as_server_error(self, browser): class ViewWithError(BrowserView): def __call__(self): raise ValueError('The value is wrong.') with register_view(ViewWithError, 'view-with-error'): with capture_streams(stderr=StringIO()): with self.assertRaises(HTTPServerError) as cm: browser.open(view='view-with-error') self.assertEquals(500, cm.exception.status_code) self.assertEquals('Internal Server Error', cm.exception.status_reason) with capture_streams(stderr=StringIO()): with self.assertRaises(HTTPServerError): browser.reload() browser.raise_http_errors = False with capture_streams(stderr=StringIO()): browser.reload()
def test_exceptions_are_printed_to_stderr(self, browser): class FailingView(BrowserView): def __call__(self): raise ValueError('The value is wrong.') with register_view(FailingView, 'failing-view'): stderr = StringIO() with capture_streams(stderr=stderr): with self.assertRaises(HTTPError): browser.open(view='failing-view') output = stderr.getvalue().strip() # The output starts with a random error_log id => strip it. self.assertTrue(output, 'No output in stderr') output = output.split(' ', 1)[1] self.assertEquals( '{}/failing-view\n'.format(self.portal.absolute_url()) + 'Traceback (innermost last):\n' ' Module ZPublisher.Publish, line 138, in publish\n' ' Module ZPublisher.mapply, line 77, in mapply\n' ' Module ZPublisher.Publish, line 48, in call_object\n' ' Module ftw.testbrowser.tests.test_log, line 18, in __call__\n' 'ValueError: The value is wrong.', output)