How to use the webtest.forms.Upload function in WebTest

To help you get started, we’ve selected a few WebTest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github django-oscar / django-oscar / tests / functional / dashboard / test_range.py View on Github external
def test_upload_file_with_skus(self):
        range_products_page = self.get(self.url)
        form = range_products_page.form
        form['file_upload'] = Upload('new_skus.txt', b'456')
        form.submit().follow()
        all_products = self.range.all_products()
        self.assertEqual(len(all_products), 1)
        self.assertTrue(self.product3 in all_products)
        range_product_file_upload = RangeProductFileUpload.objects.get()
        self.assertEqual(range_product_file_upload.range, self.range)
        self.assertEqual(range_product_file_upload.num_new_skus, 1)
        self.assertEqual(range_product_file_upload.status, RangeProductFileUpload.PROCESSED)
        self.assertEqual(range_product_file_upload.size, 3)
github Pylons / webtest / tests / test_forms.py View on Github external
def test_post_upload_empty_files(self):
        app = webtest.TestApp(SingleUploadFileApp())
        resp = app.post(
            '/',
            upload_files=[('file', 'filename', b'')]
        )
        resp.mustcontain("<p>You selected 'filename'</p>",
                         "<p>with contents: ''</p>")
        resp = app.get('/')
        form = resp.form
        form['file-field'] = Upload('filename', b'', 'text/plain')
        resp = form.submit()
        resp.mustcontain("<p>You selected 'filename'</p>",
                         "<p>with contents: ''</p>")
github django-oscar / django-oscar / tests / functional / dashboard / test_range.py View on Github external
def test_same_skus_within_different_products_warning_file_upload(self):
        range_products_page = self.get(self.url)
        form = range_products_page.form
        form['file_upload'] = Upload('skus.txt', b'123123')
        response = form.submit().follow()
        messages = list(response.context['messages'])
        self.assertEqual(len(messages), 2)
        self.assertEqual(messages[1].level, WARNING)
        self.assertEqual(
            messages[1].message, 'There are more than one product with SKU 123123'
        )
github zopefoundation / zope.testbrowser / src / zope / testbrowser / browser.py View on Github external
def add_file(self, file, content_type, filename):
        if self.type != 'file':
            raise TypeError("Can't call add_file on %s controls"
                            % self.type)

        if hasattr(file, 'read'):
            contents = file.read()
        else:
            contents = file

        self._form[self.name] = webtest.forms.Upload(filename or '', contents,
                                                     content_type)
github stevearc / pypicloud / tests / test_security.py View on Github external
def test_api_pkg_versions_authed(self):
        """ /api/package// requires write perms """
        package = make_package(self.package.name, "1.5")
        params = {"content": webtest.forms.Upload(package.filename, b"datadatadata")}
        url = "/api/package/%s/%s" % (package.name, package.filename)
        response = self.app.post(url, params, headers=_auth("user2", "user2"))
        self.assertEqual(response.status_int, 200)
github stevearc / pypicloud / tests / test_security.py View on Github external
def test_api_pkg_versions_unauthed(self):
        """ /api/package// requires write perms """
        params = {"content": webtest.forms.Upload("filename.txt", b"datadatadata")}
        url = "/api/package/%s/%s/" % (self.package.name, self.package.filename)
        response = self.app.post(
            url, params, expect_errors=True, headers=_auth("user", "user")
        )
        self.assertEqual(response.status_int, 403)
github Pylons / webtest / webtest / app.py View on Github external
b'Content-Type: ' + fcontent, b'', value])

        for key, value in params:
            if isinstance(key, text_type):
                try:
                    key = key.encode('ascii')
                except:  # pragma: no cover
                    raise  # field name are always ascii
            if isinstance(value, forms.File):
                if value.value:
                    _append_file([key] + list(value.value))
                else:
                    # If no file was uploaded simulate an empty file with no
                    # name like real browsers do:
                    _append_file([key, b'', b''])
            elif isinstance(value, forms.Upload):
                file_info = [key, value.filename]
                if value.content is not None:
                    file_info.append(value.content)
                    if value.content_type is not None:
                        file_info.append(value.content_type)
                _append_file(file_info)
            else:
                if isinstance(value, int):
                    value = str(value).encode('utf8')
                elif isinstance(value, text_type):
                    value = value.encode('utf8')
                elif not isinstance(value, (bytes, str)):
                    raise ValueError((
                        'Value for field {0} is a {1} ({2}). '
                        'It must be str, bytes or an int'
                    ).format(key, type(value), value))
github devpi / devpi / server / test_devpi_server / test_views.py View on Github external
# without a slash was only meant for pushing a release. This test
    # checks that an upload is now working without a slash as well.
    from webtest.forms import Upload
    api = mapp.create_and_use()
    assert api.pypisubmit.endswith('/')
    url = api.pypisubmit.rstrip('/')
    name = "pkg"
    version = "1.0"
    basename = "%s-%s.tar.gz" % (name, version)
    content = b"a"
    mapp.set_versiondata(
        dict(name=name, version=version))
    assert mapp.get_release_paths('pkg') == []
    r = testapp.post(url,
        {":action": "file_upload", "name": name, "version": version,
         "content": Upload(basename, content)}, expect_errors=True)
    assert r.status_int == 200
    assert mapp.get_release_paths('pkg') == [
        '/%s/+f/ca9/78112ca1bbdca/%s' % (api.stagename, basename)]