web/test/unittest_views_errorform.py
changeset 9809 29d52a785729
parent 8695 358d8bed9626
child 10785 e63fb79b32e0
equal deleted inserted replaced
9808:d121b74e043f 9809:29d52a785729
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 import re
       
    19 import sys
    18 
    20 
    19 from logilab.common.testlib import unittest_main
    21 from logilab.common.testlib import unittest_main
    20 from logilab.mtconverter import html_unescape
       
    21 
    22 
    22 from cubicweb import Forbidden, ValidationError
    23 from cubicweb import Forbidden
    23 from cubicweb.devtools.testlib import CubicWebTC
    24 from cubicweb.devtools.testlib import CubicWebTC
    24 from cubicweb.utils import json
    25 from cubicweb.view import StartupView
    25 from cubicweb.view import StartupView, TRANSITIONAL_DOCTYPE_NOEXT
       
    26 from cubicweb.web import Redirect
    26 from cubicweb.web import Redirect
    27 from cubicweb.web.htmlwidgets import TableWidget
       
    28 from cubicweb.web.views import vid_from_rset
       
    29 
    27 
    30 import re
       
    31 import hmac
       
    32 
    28 
    33 class ErrorViewTC(CubicWebTC):
    29 class ErrorViewTC(CubicWebTC):
    34     def setUp(self):
    30     def setUp(self):
    35         super(ErrorViewTC, self).setUp()
    31         super(ErrorViewTC, self).setUp()
    36         self.req = self.request()
       
    37         self.vreg.config['submit-mail'] = "test@logilab.fr"
    32         self.vreg.config['submit-mail'] = "test@logilab.fr"
    38         self.vreg.config['print-traceback'] = "yes"
    33         self.vreg.config['print-traceback'] = "yes"
    39 
    34 
    40     def test_error_generation(self):
    35     def test_error_generation(self):
    41         """
    36         """
    46             __regid__ = 'my-view'
    41             __regid__ = 'my-view'
    47             def call(self):
    42             def call(self):
    48                 raise ValueError('This is wrong')
    43                 raise ValueError('This is wrong')
    49 
    44 
    50         with self.temporary_appobjects(MyWrongView):
    45         with self.temporary_appobjects(MyWrongView):
    51             try:
    46             with self.admin_access.web_request() as req:
    52                 self.view('my-view')
    47                 try:
    53             except Exception as e:
    48                     self.view('my-view', req=req)
    54                 import sys
    49                 except Exception as e:
    55                 self.req.data['excinfo'] = sys.exc_info()
    50                     req.data['excinfo'] = sys.exc_info()
    56                 self.req.data['ex'] = e
    51                     req.data['ex'] = e
    57                 html = self.view('error', req=self.req)
    52                     html = self.view('error', req=req)
    58                 self.failUnless(re.search(r'^<input name="__signature" type="hidden" value="[0-9a-f]{32}" />$',
    53                     self.failUnless(re.search(r'^<input name="__signature" type="hidden" '
    59                                           html.source, re.M))
    54                                               'value="[0-9a-f]{32}" />$',
       
    55                                               html.source, re.M))
    60 
    56 
    61 
    57 
    62     def test_error_submit_nosig(self):
    58     def test_error_submit_nosig(self):
    63         """
    59         """
    64         tests that the reportbug controller refuses submission if
    60         tests that the reportbug controller refuses submission if
    65         there is not content signature
    61         there is not content signature
    66         """
    62         """
    67 
    63         with self.admin_access.web_request() as req:
    68         self.req.form = {'description': u'toto',
    64             req.form = {'description': u'toto'}
    69                          }
    65             with self.assertRaises(Forbidden) as cm:
    70         with self.assertRaises(Forbidden) as cm:
    66                 self.ctrl_publish(req, 'reportbug')
    71             self.ctrl_publish(self.req, 'reportbug')
       
    72 
    67 
    73     def test_error_submit_wrongsig(self):
    68     def test_error_submit_wrongsig(self):
    74         """
    69         """
    75         tests that the reportbug controller refuses submission if the
    70         tests that the reportbug controller refuses submission if the
    76         content signature is invalid
    71         content signature is invalid
    77         """
    72         """
    78 
    73         with self.admin_access.web_request() as req:
    79         self.req.form = {'__signature': 'X',
    74             req.form = {'__signature': 'X',
    80                          'description': u'toto',
    75                         'description': u'toto'}
    81                          }
    76             with self.assertRaises(Forbidden) as cm:
    82         with self.assertRaises(Forbidden) as cm:
    77                 self.ctrl_publish(req, 'reportbug')
    83             self.ctrl_publish(self.req, 'reportbug')
       
    84 
    78 
    85     def test_error_submit_ok(self):
    79     def test_error_submit_ok(self):
    86         """
    80         """
    87         tests that the reportbug controller accept the email submission if the
    81         tests that the reportbug controller accept the email submission if the
    88         content signature is valid
    82         content signature is valid
    89         """
    83         """
    90 
    84         with self.admin_access.web_request() as req:
    91         sign = self.vreg.config.sign_text('toto')
    85             sign = self.vreg.config.sign_text('toto')
    92         self.req.form = {'__signature': sign,
    86             req.form = {'__signature': sign,
    93                          'description': u'toto',
    87                         'description': u'toto'}
    94                          }
    88             with self.assertRaises(Redirect) as cm:
    95         with self.assertRaises(Redirect) as cm:
    89                 self.ctrl_publish(req, 'reportbug')
    96             self.ctrl_publish(self.req, 'reportbug')
       
    97 
    90 
    98 if __name__ == '__main__':
    91 if __name__ == '__main__':
    99     unittest_main()
    92     unittest_main()