web/test/unittest_views_errorform.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    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/>.
       
    18 import re
       
    19 import sys
       
    20 
       
    21 from logilab.common.testlib import unittest_main
       
    22 
       
    23 from cubicweb import Forbidden
       
    24 from cubicweb.devtools.testlib import CubicWebTC
       
    25 from cubicweb.view import StartupView
       
    26 from cubicweb.web import Redirect
       
    27 
       
    28 
       
    29 class ErrorViewTC(CubicWebTC):
       
    30     def setUp(self):
       
    31         super(ErrorViewTC, self).setUp()
       
    32         self.vreg.config['submit-mail'] = "test@logilab.fr"
       
    33         self.vreg.config['print-traceback'] = "yes"
       
    34 
       
    35     def test_error_generation(self):
       
    36         """
       
    37         tests
       
    38         """
       
    39 
       
    40         class MyWrongView(StartupView):
       
    41             __regid__ = 'my-view'
       
    42             def call(self):
       
    43                 raise ValueError('This is wrong')
       
    44 
       
    45         with self.temporary_appobjects(MyWrongView):
       
    46             with self.admin_access.web_request() as req:
       
    47                 try:
       
    48                     self.view('my-view', req=req)
       
    49                 except Exception as e:
       
    50                     req.data['excinfo'] = sys.exc_info()
       
    51                     req.data['ex'] = e
       
    52                     html = self.view('error', req=req)
       
    53                     self.assertTrue(re.search(b'^<input name="__signature" type="hidden" '
       
    54                                               b'value="[0-9a-f]{32}" />$',
       
    55                                               html.source, re.M))
       
    56 
       
    57 
       
    58     def test_error_submit_nosig(self):
       
    59         """
       
    60         tests that the reportbug controller refuses submission if
       
    61         there is not content signature
       
    62         """
       
    63         with self.admin_access.web_request() as req:
       
    64             req.form = {'description': u'toto'}
       
    65             with self.assertRaises(Forbidden) as cm:
       
    66                 self.ctrl_publish(req, 'reportbug')
       
    67 
       
    68     def test_error_submit_wrongsig(self):
       
    69         """
       
    70         tests that the reportbug controller refuses submission if the
       
    71         content signature is invalid
       
    72         """
       
    73         with self.admin_access.web_request() as req:
       
    74             req.form = {'__signature': 'X',
       
    75                         'description': u'toto'}
       
    76             with self.assertRaises(Forbidden) as cm:
       
    77                 self.ctrl_publish(req, 'reportbug')
       
    78 
       
    79     def test_error_submit_ok(self):
       
    80         """
       
    81         tests that the reportbug controller accept the email submission if the
       
    82         content signature is valid
       
    83         """
       
    84         with self.admin_access.web_request() as req:
       
    85             sign = self.vreg.config.sign_text('toto')
       
    86             req.form = {'__signature': sign,
       
    87                         'description': u'toto'}
       
    88             with self.assertRaises(Redirect) as cm:
       
    89                 self.ctrl_publish(req, 'reportbug')
       
    90 
       
    91 if __name__ == '__main__':
       
    92     unittest_main()