Ignore:
Timestamp:
15 Mar 2013, 07:39:09 (12 years ago)
Author:
Henrik Bettermann
Message:

Let's forbid some mistakenly added characters like character 160.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/browser
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/pages.py

    r10027 r10029  
    7373from waeup.kofa.students.export import EXPORTER_NAMES as STUDENT_EXPORTERS
    7474
     75FORBIDDEN_CHARACTERS = (160,)
     76
    7577grok.context(IKofaObject)
    7678grok.templatedir('templates')
     
    10431045            logger = self.context.logger
    10441046
    1045             # XXX: temporary solution to prevent users from
    1046             # uploading non-ascii files.
    1047             #for element in filecontent:
    1048             #    try:
    1049             #        if ord(element) > 128:
    1050             #          self.flash(_(
    1051             #              "Only ASCII files are allowed. "
    1052             #              "Your file contains non-ASCII characters."))
    1053             #          logger.info('%s - non-ascii file uploaded: %s' %
    1054             #              (ob_class, target))
    1055             #          return
    1056             #    except TypeError:
    1057             #        self.flash(_(
    1058             #            "Only ASCII files are allowed. "
    1059             #            "Your file contains non-ASCII characters."))
    1060             #        logger.info('%s - non-ascii file uploaded: %s' %
    1061             #            (ob_class, target))
    1062             #        return
     1047            # Forbid certain characters in import files.
     1048            for element in filecontent:
     1049                try:
     1050                    if ord(element) in FORBIDDEN_CHARACTERS:
     1051                      self.flash(_(
     1052                          "Your file contains forbidden characters. "
     1053                          "Please replace."))
     1054                      logger.info('%s - invalid file uploaded: %s' %
     1055                          (ob_class, target))
     1056                      return
     1057                except TypeError:
     1058                    self.flash(_(
     1059                        "Your file contains forbidden characters. "
     1060                        "Please replace."))
     1061                    logger.info('%s - invalid file uploaded: %s' %
     1062                        (ob_class, target))
     1063                    return
    10631064
    10641065            open(target, 'wb').write(filecontent)
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/tests/test_browser.py

    r9843 r10029  
    3636
    3737SAMPLE_FILE = os.path.join(os.path.dirname(__file__), 'test_file.csv')
     38FORBIDDEN_FILE = os.path.join(os.path.dirname(__file__), 'forbidden_file.csv')
    3839
    3940class UniversitySetup(FunctionalTestCase):
     
    144145        return
    145146
     147    def test_forbidden_file_upload(self):
     148        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     149        self.browser.open(self.datacenter_path)
     150        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     151        self.assertEqual(self.browser.url, self.datacenter_path)
     152        self.browser.getLink("Upload data").click()
     153        file = open(FORBIDDEN_FILE)
     154        ctrl = self.browser.getControl(name='uploadfile:file')
     155        file_ctrl = ctrl.mech_control
     156        file_ctrl.add_file(file, filename='my_corrupted_file.csv')
     157        self.browser.getControl('Upload').click()
     158        self.assertTrue(
     159            'Your file contains forbidden characters. Please replace.'
     160            in self.browser.contents)
     161        logfile = os.path.join(
     162            self.app['datacenter'].storage, 'logs', 'datacenter.log')
     163        logcontent = open(logfile).read()
     164        self.assertTrue('zope.mgr - browser.pages.DatacenterUploadPage - '
     165            'invalid file uploaded:' in logcontent)
     166        return
    146167
    147168class DataCenterUIExportTests(UniversitySetup, FunctionalAsyncTestCase):
Note: See TracChangeset for help on using the changeset viewer.