web/test/unittest_idownloadable.py
branchstable
changeset 8600 d74addac92bb
child 8610 b1145ad53999
equal deleted inserted replaced
8599:4fabc81cd924 8600:d74addac92bb
       
     1 # -*- coding: utf-8 -*-
       
     2 # copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This file is part of CubicWeb.
       
     6 #
       
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     8 # terms of the GNU Lesser General Public License as published by the Free
       
     9 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    10 # any later version.
       
    11 #
       
    12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    15 # details.
       
    16 #
       
    17 # You should have received a copy of the GNU Lesser General Public License along
       
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    19 from __future__ import with_statement
       
    20 
       
    21 from functools import partial
       
    22 
       
    23 from logilab.common.testlib import unittest_main
       
    24 
       
    25 from cubicweb.devtools.testlib import CubicWebTC
       
    26 from cubicweb import view
       
    27 from cubicweb.predicates import is_instance
       
    28 
       
    29 
       
    30 class IDownloadableTC(CubicWebTC):
       
    31 
       
    32     def setUp(self):
       
    33         super(IDownloadableTC, self).setUp()
       
    34         class IDownloadableUser(view.EntityAdapter):
       
    35             __regid__ = 'IDownloadable'
       
    36             __select__ = is_instance('CWUser')
       
    37 
       
    38             def download_content_type(self):
       
    39                 """return MIME type of the downloadable content"""
       
    40                 return 'text/plain'
       
    41 
       
    42             def download_encoding(self):
       
    43                 """return encoding of the downloadable content"""
       
    44                 return 'ascii'
       
    45 
       
    46             def download_file_name(self):
       
    47                 """return file name of the downloadable content"""
       
    48                 return  self.entity.name() + '.txt'
       
    49 
       
    50             def download_data(self):
       
    51                 return 'Babar is not dead!'
       
    52         self.vreg.register(IDownloadableUser)
       
    53         self.addCleanup(partial(self.vreg.unregister, IDownloadableUser))
       
    54 
       
    55     def test_header_simple_case(self):
       
    56         req = self.request()
       
    57         req.form['vid'] = 'download'
       
    58         req.form['eid'] = str(req.user.eid)
       
    59         data = self.ctrl_publish(req,'view')
       
    60         get = req.headers_out.getRawHeaders
       
    61         self.assertEqual(['attachment;filename=admin.txt'],
       
    62                          get('content-disposition'))
       
    63         self.assertEqual(['text/plain;charset=ascii'],
       
    64                          get('content-type'))
       
    65         self.assertEqual('Babar is not dead!', data)
       
    66 
       
    67     def test_header_unicode_filename(self):
       
    68         req = self.request()
       
    69         self.create_user(req, login=u'cécilia', password='babar')
       
    70         self.commit()
       
    71         with self.login(u'cécilia', password='babar'):
       
    72             req = self.request()
       
    73             req.form['vid'] = 'download'
       
    74             req.form['eid'] = str(req.user.eid)
       
    75             self.ctrl_publish(req,'view')
       
    76             get = req.headers_out.getRawHeaders
       
    77             self.assertEqual(["attachment;filename=ccilia.txt;filename*=utf-8''c%C3%A9cilia.txt"],
       
    78                              get('content-disposition'))
       
    79 
       
    80     def test_header_unicode_long_filename(self):
       
    81         req = self.request()
       
    82         name = u'Bèrte_hô_grand_nôm_ça_va_totallement_déborder_de_la_limite_là'
       
    83         self.create_user(req, login=name, password='babar')
       
    84         self.commit()
       
    85         with self.login(name, password='babar'):
       
    86             req = self.request()
       
    87             req.form['vid'] = 'download'
       
    88             req.form['eid'] = str(req.user.eid)
       
    89             self.ctrl_publish(req,'view')
       
    90             get = req.headers_out.getRawHeaders
       
    91             self.assertEqual(["attachment;filename=Brte_h_grand_nm_a_va_totallement_dborder_de_la_limite_l.txt;filename*=utf-8''B%C3%A8rte_h%C3%B4_grand_n%C3%B4m_%C3%A7a_va_totallement_d%C3%A9border_de_la_limite_l%C3%A0.txt"],
       
    92                              get('content-disposition'))
       
    93 
       
    94 if __name__ == '__main__':
       
    95     unittest_main()