Changeset 7101


Ignore:
Timestamp:
13 Nov 2011, 06:36:31 (13 years ago)
Author:
Henrik Bettermann
Message:

Add test for file uploader in students.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_browser.py

    r7068 r7101  
    2525import shutil
    2626import tempfile
    27 import cStringIO
     27from StringIO import StringIO
    2828from datetime import datetime
    2929import grok
     
    357357        return
    358358
     359    def test_upload_file(self):
     360        # Managers can upload a file via the StudentClearanceManageFormPage
     361        # The image is stored even if form has errors
     362        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     363        self.browser.open(self.edit_clearance_student_path)
     364        # Create a pseudo image file and select it to be uploaded in form
     365        # as birth certificate
     366        pseudo_image = StringIO('I pretend to be a graphics file')
     367        ctrl = self.browser.getControl(name='form.birth_certificate')
     368        file_ctrl = ctrl.mech_control
     369        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
     370        self.browser.getControl("Save").click() # submit form
     371        # Form has errors ...
     372        self.assertTrue(
     373            'Required input is missing' in self.browser.contents)
     374        # ... but file has been uploaded and a correct <img> link included
     375        self.assertTrue(
     376            '<a target="image" href="birth_certificate.jpg">' in self.browser.contents)
     377        # There is a correct <img> link included
     378        self.assertTrue(
     379            '<a target="image" href="birth_certificate.jpg">'
     380            in self.browser.contents)
     381        # Browsing the link shows a real image
     382        self.browser.open('birth_certificate.jpg')
     383        self.assertEqual(
     384            self.browser.headers['content-type'], 'image/jpeg')
     385        self.assertEqual(len(self.browser.contents), 31)
     386        # Reuploading a file which is bigger than 150k will raise an error
     387        self.browser.open(self.edit_clearance_student_path)
     388        photo_content = 'A' * 1024 * 151  # A string of 21 KB size
     389        pseudo_image = StringIO(photo_content)
     390        ctrl = self.browser.getControl(name='form.birth_certificate')
     391        file_ctrl = ctrl.mech_control
     392        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
     393        self.browser.getControl("Save").click() # submit form
     394        self.assertTrue(
     395            'Uploaded image is too big' in self.browser.contents)
     396
    359397    def test_manage_course_lists(self):
    360398        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     
    468506        self.browser.open(datacenter_path)
    469507        self.browser.getLink('Upload CSV file').click()
    470         filecontents = cStringIO.StringIO(open('students.csv', 'rb').read())
     508        filecontents = StringIO(open('students.csv', 'rb').read())
    471509        filewidget = self.browser.getControl(name='uploadfile:file')
    472510        filewidget.add_file(filecontents, 'text/plain', 'students.csv')
     
    494532        self.browser.open(datacenter_path)
    495533        self.browser.getLink('Upload CSV file').click()
    496         filecontents = cStringIO.StringIO(open('studycourses.csv', 'rb').read())
     534        filecontents = StringIO(open('studycourses.csv', 'rb').read())
    497535        filewidget = self.browser.getControl(name='uploadfile:file')
    498536        filewidget.add_file(filecontents, 'text/plain', 'studycourses.csv')
Note: See TracChangeset for help on using the changeset viewer.