Beispiel #1
0
 def size_constrained(self):
     max_size = 13
     self.file_field = FileField(allow_multiple=True,
                                 max_size_bytes=max_size,
                                 label='Attached files')
     self.validation_error_message = 'files should be smaller than 13.0bytes'
     self.valid_file = temp_file_with('c' * max_size,
                                      name='valid_size.html')
     self.invalid_file = temp_file_with('c' * (max_size + 1),
                                        name='invalid_size.html')
Beispiel #2
0
def control_file():
    control_file = temp_file_with('''
Source: python-reahl
Section: reahl
Priority: optional
Maintainer: Reahl Software Services <*****@*****.**>
Build-Depends: debhelper (>= 7), python-support (>= 1.0), python (>= 2.6), python (<< 3.0)
Standards-Version: 3.8.3

Package: python-reahl-component
Architecture: all
Depends: python-setuptools, ${python:Depends}, ${reahl:Depends}
Provides: ${python:Provides}
Description: Reahl-component.
Reahl-component - long description.

Package: python-reahl-stubble
Architecture: all
Depends: ${python:Depends}, ${reahl:Depends}
Provides: ${python:Provides}
Description: Stub tools for use in unit testing
 one line of description
 another line of description

''')
    return control_file
Beispiel #3
0
def test_simple_file_input_exceptions(web_fixture):
    """Usually, when a DomainException happens during a form submit Inputs save the input they received so that
       such input can be pre-populated on the screen rendered by a subsequent GET for a user to correct
       possible mistakes and resubmit.

       In the case of a SimpleFileInput however, the UploadedFile objects that serve as user input
       to a SimpleFileInput are discarded when a DomainException happens.  This is because it is not
       possible to prepopulate its value using HTML or JS as this would be a security risk.
    """

    file_to_upload = temp_file_with('some content')
    failing_constraint = FailingConstraint('I am breaking')

    class DomainObject(object):
        def __init__(self):
            self.file = None

        @exposed
        def fields(self, fields):
            fields.file = FileField(allow_multiple=False,
                                    label='Attached files')
            # FailingConstraint is declared in module level scope for it to be pickleable
            fields.file.add_validation_constraint(failing_constraint)

        @exposed
        def events(self, events):
            events.upload = Event(label='Upload')

    domain_object = DomainObject()

    class FileUploadForm(Form):
        def __init__(self, view):
            super(FileUploadForm, self).__init__(view, 'test')
            file_input = self.add_child(
                SimpleFileInput(self, domain_object.fields.file))
            if file_input.validation_error:
                self.add_child(self.create_error_label(file_input))
            self.define_event_handler(domain_object.events.upload)
            self.add_child(ButtonInput(self, domain_object.events.upload))

    wsgi_app = web_fixture.new_wsgi_app(child_factory=FileUploadForm.factory(),
                                        enable_js=False)
    web_fixture.reahl_server.set_app(wsgi_app)

    browser = web_fixture.driver_browser
    browser.open('/')

    browser.type(XPath.input_of_type('file'), file_to_upload.name)
    browser.click(XPath.button_labelled('Upload'))
    assert browser.is_element_present('//label[text()="I am breaking"]')

    # Message is cleared on second attempt
    failing_constraint.fail = False
    browser.type(XPath.input_of_type('file'), file_to_upload.name)
    browser.click(XPath.button_labelled('Upload'))
    assert not browser.is_element_present('//label[text()="I am breaking"]')
Beispiel #4
0
def test_i18n(web_fixture, file_input_fixture):
    """All messages have translations."""
    fixture = file_input_fixture

    wsgi_app = web_fixture.new_wsgi_app(
        child_factory=file_input_fixture.FileUploadForm.factory(),
        enable_js=True)
    web_fixture.reahl_server.set_app(wsgi_app)
    browser = web_fixture.driver_browser
    browser.open('/af/')

    file1 = temp_file_with('', name='file1.txt')
    file2 = temp_file_with('', name='file2.txt')
    browser.wait_for(fixture.message_displayed_is, 'Geen lêers gekies')
    browser.wait_for_element_present(XPath.input_labelled('Kies lêer(s)'))

    browser.type(XPath.input_labelled('Kies lêer(s)'), file1.name)
    browser.type(XPath.input_labelled('Kies lêer(s)'), file2.name)
    browser.wait_for(fixture.message_displayed_is, '2 gekose lêers')
Beispiel #5
0
def test_file_input_basics(web_fixture, file_input_fixture):
    """A FileInput is a FileInputButton combined with a area where the chosen file name is displayed."""
    fixture = file_input_fixture

    wsgi_app = web_fixture.new_wsgi_app(
        child_factory=file_input_fixture.FileUploadForm.factory(),
        enable_js=True)
    web_fixture.reahl_server.set_app(wsgi_app)
    browser = web_fixture.driver_browser
    browser.open('/')

    file1 = temp_file_with('', name='file1.txt')
    file2 = temp_file_with('', name='file2.txt')
    browser.wait_for(fixture.message_displayed_is, 'No files chosen')
    browser.type(XPath.input_labelled('Choose file(s)'), file1.name)
    browser.wait_for(fixture.message_displayed_is,
                     os.path.basename(file1.name))

    browser.type(XPath.input_labelled('Choose file(s)'), file2.name)
    browser.wait_for(fixture.message_displayed_is, '2 files chosen')
Beispiel #6
0
def test_fileupload(web_fixture, fileupload_scenario):

    fixture = fileupload_scenario
    fixture.start_example_app()
    web_fixture.driver_browser.open('/')

    file1 = temp_file_with(b'some content in a file', 'file1.txt', mode='w+b')
    file2 = temp_file_with(b'different content', 'file2.txt', mode='w+b')
    file3 = temp_file_with(b'even more content', 'file3.txt', mode='w+b')

    # Upload a file
    web_fixture.driver_browser.type('//input[@type="file"]', file1.name)
    assert fixture.uploaded_file_is_listed(file1.name)

    # Upload a file
    web_fixture.driver_browser.type('//input[@type="file"]', file2.name)
    assert fixture.uploaded_file_is_listed(file2.name)

    # Upload a file
    web_fixture.driver_browser.type('//input[@type="file"]', file3.name)
    assert fixture.uploaded_file_is_listed(file3.name)

    # Delete file2 from uploaded files
    web_fixture.driver_browser.click(
        XPath.button_labelled('Remove', filename=os.path.basename(file2.name)))
    assert not fixture.uploaded_file_is_listed(file2.name)

    # Submit the form
    web_fixture.driver_browser.type(XPath.input_labelled('Email address'),
                                    '*****@*****.**')
    web_fixture.driver_browser.type(XPath.input_labelled('Comment'),
                                    'some comment text')
    web_fixture.driver_browser.click(XPath.button_labelled('Submit'))

    attached_file1 = Session.query(AttachedFile).filter_by(
        filename=os.path.basename(file1.name)).one()
    attached_file3 = Session.query(AttachedFile).filter_by(
        filename=os.path.basename(file3.name)).one()
    assert Session.query(AttachedFile).count() == 2
    assert attached_file1.contents == b'some content in a file'
    assert attached_file3.contents == b'even more content'
Beispiel #7
0
def file_upload_button(fixture):
    """A FileInputButton lets you upload files using the browser's file choosing mechanism."""

    fixture.reahl_server.set_app(fixture.new_wsgi_app(enable_js=True))
    browser = fixture.driver_browser
    browser.open('/')

    file_to_upload = temp_file_with('some content')

    browser.type(XPath.input_labelled('Choose file(s)'), file_to_upload.name)

    vassert(len(fixture.domain_object.files) == 0)
    browser.click(XPath.button_labelled('Submit'))
    vassert(len(fixture.domain_object.files) == 1)
Beispiel #8
0
def test_reading_and_writing_substvar_files():
    raw_file = temp_file_with('')
    substvars = SubstvarsFile(raw_file.name)
    substvars.extend( [('python:Version', 'python stuff'), ('reahl:Depends', 'lots of depends')] )

    substvars.write()

    substvars = SubstvarsFile(raw_file.name)
    assert len(substvars) == 0
    substvars.read()

    assert len(substvars) == 2
    assert list(substvars.items())[0] == ('python:Version', 'python stuff')
    assert list(substvars.items())[1] == ('reahl:Depends', 'lots of depends')
Beispiel #9
0
def changelog_file():
    changlog_file = temp_file_with('''
python-reahl (2.0.0a1) unstable; urgency=low

  * Towards version 2.0.

 -- Iwan Vosloo <*****@*****.**>  Tue, 08 Feb 2011 12:03:44 +0000

python-reahl (0.8.0) unstable; urgency=low

  * Initial Release.

 -- Iwan Vosloo <*****@*****.**>  Wed, 22 Dec 2010 05:44:11 +0000
''')
    yield changlog_file
Beispiel #10
0
    def reading_and_writing(self, fixture):
        raw_file = temp_file_with('')
        substvars = SubstvarsFile(raw_file.name)
        substvars.extend([('python:Version', 'python stuff'),
                          ('reahl:Depends', 'lots of depends')])

        substvars.write()

        substvars = SubstvarsFile(raw_file.name)
        vassert(len(substvars) == 0)
        substvars.read()

        vassert(len(substvars) == 2)
        vassert(
            list(substvars.items())[0] == ('python:Version', 'python stuff'))
        vassert(
            list(substvars.items())[1] == ('reahl:Depends', 'lots of depends'))
Beispiel #11
0
def test_file_upload_button(web_fixture, file_input_button_fixture):
    """A FileInputButton lets you upload files using the browser's file choosing mechanism."""

    fixture = file_input_button_fixture

    wsgi_app = web_fixture.new_wsgi_app(child_factory=file_input_button_fixture.FileUploadForm.factory(), enable_js=True)
    web_fixture.reahl_server.set_app(wsgi_app)
    browser = web_fixture.driver_browser
    browser.open('/')

    file_to_upload = temp_file_with('some content')

    browser.type(XPath.input_labelled('Choose file(s)'), file_to_upload.name)

    assert len(fixture.domain_object.files) == 0 
    browser.click(XPath.button_labelled('Submit'))
    assert len(fixture.domain_object.files) == 1 
Beispiel #12
0
def test_simple_file_input(web_fixture):
    """A SimpleFileInput is a Widget with which a user can choose one or more files.
       The SimpleFileInput transforms the chosen files to UploadedFile objects, and passes these
       to its associated FileField upon a Form submit."""

    expected_content = b'some content'
    file_to_upload = temp_file_with(expected_content, mode='w+b')

    class DomainObject(object):
        def __init__(self):
            self.file = None

        @exposed
        def fields(self, fields):
            fields.file = FileField(allow_multiple=False,
                                    label='Attached files')

        @exposed
        def events(self, events):
            events.upload = Event(label='Upload')

    domain_object = DomainObject()

    class FileUploadForm(Form):
        def __init__(self, view):
            super(FileUploadForm, self).__init__(view, 'test')
            self.add_child(SimpleFileInput(self, domain_object.fields.file))
            self.define_event_handler(domain_object.events.upload)
            self.add_child(ButtonInput(self, domain_object.events.upload))

    wsgi_app = web_fixture.new_wsgi_app(child_factory=FileUploadForm.factory(),
                                        enable_js=False)
    web_fixture.reahl_server.set_app(wsgi_app)

    browser = web_fixture.driver_browser
    browser.open('/')

    browser.type(XPath.input_of_type('file'), file_to_upload.name)
    browser.click(XPath.button_labelled('Upload'))
    assert isinstance(domain_object.file, UploadedFile)
    assert domain_object.file.filename == os.path.basename(file_to_upload.name)
    with domain_object.file.open() as opened_file:
        read_contents = opened_file.read()
    assert read_contents == expected_content
Beispiel #13
0
 def type_constrained(self):
     self.file_field = FileField(allow_multiple=True, accept=['text/*'])
     self.validation_error_message = 'files should be of type text/*'
     self.valid_file = temp_file_with('contents', name='valid.html')
     self.invalid_file = temp_file_with('contents', name='invalid.gif')
Beispiel #14
0
 def new_file_to_upload2(self):
     return temp_file_with(self.file_to_upload2_content,
                           name=self.file_to_upload2_name,
                           mode='w+b')
Beispiel #15
0
 def new_file(self):
     return temp_file_with(self.file_contents)
Beispiel #16
0
def test_file_download_details(web_fixture):
    """FileDownloadStub (the GET response for a StaticFileResource) works correctly in
      different scenarios of partial GETting too."""

    file_content = b'some content'
    server_file = temp_file_with(file_content, 'afile.css', mode='w+b')

    @stubclass(FileDownload)
    class FileDownloadStub(FileDownload):
        chunk_size = 1

    response = FileDownloadStub(
        FileOnDisk(server_file.name, '/path/for/the/file'))

    # Case: The whole content is sent, in chunk_size bits
    read = [i for i in response.app_iter]
    expected = [bytes((i, )) for i in file_content]
    assert read == expected

    # Case: Headers are set correctly
    assert response.content_type == 'text/css'
    assert not response.content_encoding
    assert response.content_length == len(file_content)

    mtime = datetime.datetime.fromtimestamp(
        int(os.path.getmtime(server_file.name)))
    assert response.last_modified.replace(tzinfo=None) == mtime
    tag_mtime, tag_size, tag_hash = response.etag.split('-')
    mtime = str(os.path.getmtime(server_file.name))
    assert tag_mtime == mtime
    assert tag_size == str(len(file_content))
    assert tag_hash == str(abs(hash(server_file.name)))

    # Case: conditional response is supported
    assert response.conditional_response

    # Case: partial response is supported - different cases:
    #      - normal case
    actual = [i for i in response.app_iter.app_iter_range(3, 7)]
    expected = [bytes((i, )) for i in file_content[3:8]]
    assert actual == expected

    #      - no end specified
    actual = [i for i in response.app_iter.app_iter_range(3)]
    expected = [bytes((i, )) for i in file_content[3:]]
    assert actual == expected

    #      - no start specified
    actual = [i for i in response.app_iter.app_iter_range(end=7)]
    expected = [bytes((i, )) for i in file_content[:8]]
    assert actual == expected

    #      - where the last chunk read would stretch past end
    response.chunk_size = 2
    actual = b''.join([i for i in response.app_iter.app_iter_range(end=6)])
    expected = file_content[:7]
    assert actual == expected

    #      - where start > end
    response.chunk_size = 1
    actual = [i for i in response.app_iter.app_iter_range(start=7, end=3)]
    expected = [b'']
    assert actual == expected

    #      - where start < 0
    actual = [i for i in response.app_iter.app_iter_range(start=-10, end=7)]
    expected = [bytes((i, )) for i in file_content[:8]]
    assert actual == expected

    #      - where end > length of file
    actual = [i for i in response.app_iter.app_iter_range(start=3, end=2000)]
    expected = [bytes((i, )) for i in file_content[3:]]
    assert actual == expected

    #      - where start > length of file
    actual = [i for i in response.app_iter.app_iter_range(start=700)]
    expected = [b'']
    assert actual == expected