Ignore:
Timestamp:
18 Jan 2018, 07:34:42 (7 years ago)
Author:
Henrik Bettermann
Message:

Do not allow uploading data with trailing whitespaces.

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

Legend:

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

    r14648 r14939  
    13451345            invalid_line = check_csv_charset(filecontent.splitlines())
    13461346            if invalid_line:
    1347                 self.flash(_(
    1348                     "Your file contains forbidden characters or "
    1349                     "has invalid CSV format. "
    1350                     "First problematic line detected: line %s. "
    1351                     "Please replace." % invalid_line), type='danger')
     1347                if invalid_line == -1:
     1348                    self.flash(_(
     1349                        "The data in your file contain trailing whitespaces."
     1350                        "Please replace."), type='danger')
     1351                else:
     1352                    self.flash(_(
     1353                        "Your file contains forbidden characters or "
     1354                        "has invalid CSV format. "
     1355                        "First problematic line detected: line %s. "
     1356                        "Please replace." % invalid_line), type='danger')
    13521357                logger.info('%s - invalid file uploaded: %s' %
    13531358                            (ob_class, target))
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/tests/test_browser.py

    r14511 r14939  
    4040SAMPLE_FILE = os.path.join(os.path.dirname(__file__), 'test_file.csv')
    4141FORBIDDEN_FILE = os.path.join(os.path.dirname(__file__), 'forbidden_file.csv')
     42FORBIDDEN_FILE_2 = os.path.join(
     43    os.path.dirname(__file__), 'forbidden_file_2.csv')
    4244
    4345class UniversitySetup(FunctionalTestCase):
     
    193195        self.assertTrue(
    194196            'Your file contains forbidden characters or'
     197            in self.browser.contents)
     198        logfile = os.path.join(
     199            self.app['datacenter'].storage, 'logs', 'datacenter.log')
     200        logcontent = open(logfile).read()
     201        self.assertTrue('zope.mgr - browser.pages.DatacenterUploadPage - '
     202            'invalid file uploaded:' in logcontent)
     203        return
     204
     205    def test_forbidden_file_upload_2(self):
     206        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     207        self.browser.open(self.datacenter_path)
     208        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     209        self.assertEqual(self.browser.url, self.datacenter_path)
     210        self.browser.getLink("Upload data").click()
     211        file = open(FORBIDDEN_FILE_2)
     212        ctrl = self.browser.getControl(name='uploadfile:file')
     213        file_ctrl = ctrl.mech_control
     214        file_ctrl.add_file(file, filename='my_corrupted_file.csv')
     215        self.browser.getControl('Upload').click()
     216        self.assertTrue(
     217            'The data in your file contain trailing whitespaces.'
    195218            in self.browser.contents)
    196219        logfile = os.path.join(
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py

    r13537 r14939  
    756756
    757757def check_csv_charset(iterable):
    758     """Check contents of `iterable` regarding valid CSV encoding.
     758    """Check contents of `iterable` regarding valid CSV encoding and
     759    trailing whitespaces in data.
    759760
    760761    `iterable` is expected to be an iterable on _rows_ (not
     
    764765
    765766    Returns line num of first illegal char or ``None``. Line nums
    766     start counting with 1 (not zero).
     767    start counting with 1 (not zero). Returns -1 if data contain
     768    trailing whitespaces.
    767769    """
    768770    linenum = 1
     
    771773        for row in reader:
    772774            linenum += 1
     775            for value in row.values():
     776                if value.endswith(' '):
     777                    return -1
    773778    except UnicodeDecodeError:
    774779        return linenum
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py

    r12433 r14939  
    537537        self.assertEqual(helpers.check_csv_charset(csv), 2)
    538538
     539
     540    def test_invalid_data3(self):
     541        csv = (
     542            "code,title,title_prefix\n"
     543            "FAC1,Faculty 1,faculty\n"
     544            "FAC2,Faculty 2 ,institute\n"
     545            "FAC3,Fäcülty 3,school\n"
     546            ).splitlines()
     547        self.assertEqual(helpers.check_csv_charset(csv), -1)
    539548
    540549class MemInfoTestCase(unittest.TestCase):
Note: See TracChangeset for help on using the changeset viewer.