web/test/unittest_web.py
changeset 9147 01124cfd4b1f
parent 8728 75be9de9d68e
child 9601 e5a80bd337e8
equal deleted inserted replaced
9146:9b58a6406a64 9147:01124cfd4b1f
    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 
    18 
       
    19 from json import loads
       
    20 from os.path import join
       
    21 
       
    22 try:
       
    23     import requests
       
    24     assert [int(n) for n in requests.__version__.split('.', 2)][:2] >= [1, 2]
       
    25 except (ImportError, AssertionError):
       
    26     requests = None
       
    27 
    19 from logilab.common.testlib import TestCase, unittest_main
    28 from logilab.common.testlib import TestCase, unittest_main
       
    29 from cubicweb.devtools.httptest import CubicWebServerTC
    20 from cubicweb.devtools.fake import FakeRequest
    30 from cubicweb.devtools.fake import FakeRequest
    21 
    31 
    22 class AjaxReplaceUrlTC(TestCase):
    32 class AjaxReplaceUrlTC(TestCase):
    23 
    33 
    24     def test_ajax_replace_url_1(self):
    34     def test_ajax_replace_url_1(self):
    41             'function %s() { $("#foo").loadxhtml("http://testing.fr/cubicweb/ajax?%s",'
    51             'function %s() { $("#foo").loadxhtml("http://testing.fr/cubicweb/ajax?%s",'
    42             '{"pageid": "%s"},"get","replace"); }' %
    52             '{"pageid": "%s"},"get","replace"); }' %
    43             (cbname, qs, req.pageid),
    53             (cbname, qs, req.pageid),
    44             req.html_headers.post_inlined_scripts[0])
    54             req.html_headers.post_inlined_scripts[0])
    45 
    55 
       
    56 
       
    57 class FileUploadTC(CubicWebServerTC):
       
    58 
       
    59     def setUp(self):
       
    60         "Skip whole test class if a suitable requests module is not available"
       
    61         if requests is None:
       
    62             self.skipTest('Python ``requests`` module is not available')
       
    63         super(FileUploadTC, self).setUp()
       
    64 
       
    65     @property
       
    66     def _post_url(self):
       
    67         return self.request().build_url('ajax', fname='fileupload')
       
    68 
       
    69     def _fobject(self, fname):
       
    70         return open(join(self.datadir, fname), 'rb')
       
    71 
       
    72     def _fcontent(self, fname):
       
    73         return self._fobject(fname).read()
       
    74 
       
    75     def test_single_file_upload(self):
       
    76         files = {'file': ('schema.py', self._fobject('schema.py'))}
       
    77         webreq = requests.post(self._post_url, files=files)
       
    78         # check backward compat : a single uploaded file leads to a single
       
    79         # 2-uple in the request form
       
    80         expect = {'fname': u'fileupload',
       
    81                   'file': ['schema.py', self._fcontent('schema.py')]}
       
    82         self.assertEqual(webreq.status_code, 200)
       
    83         self.assertDictEqual(expect, loads(webreq.content))
       
    84 
       
    85     def test_multiple_file_upload(self):
       
    86         files = [('files', ('schema.py', self._fobject('schema.py'))),
       
    87                  ('files', ('views.py',  self._fobject('views.py')))]
       
    88         webreq = requests.post(self._post_url, files=files,)
       
    89         expect = {'fname': u'fileupload',
       
    90                   'files': [['schema.py', self._fcontent('schema.py')],
       
    91                             ['views.py', self._fcontent('views.py')]],}
       
    92         self.assertEqual(webreq.status_code, 200)
       
    93         self.assertDictEqual(expect, loads(webreq.content))
       
    94 
       
    95 
    46 if __name__ == '__main__':
    96 if __name__ == '__main__':
    47     unittest_main()
    97     unittest_main()