How to use the webtest.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_product.py View on Github external
self.assertEqual(self.product.attr.float, 1.2)
        self.assertTrue(self.product.attr.boolean)
        self.assertEqual(self.product.attr.richtext, 'longread')
        self.assertEqual(self.product.attr.date, datetime.date(2016, 10, 12))
        self.assertEqual(self.product.attr.file.name, posixpath.join(self.image_folder, 'file1.txt'))
        self.assertEqual(self.product.attr.image.name, posixpath.join(self.image_folder, 'image1.png'))

        page = self.get(self.url)
        product_form = page.form
        product_form['attr_text'] = 'test2'
        product_form['attr_integer'] = '2'
        product_form['attr_float'] = '5.2'
        product_form['attr_boolean'] = ''
        product_form['attr_richtext'] = 'article'
        product_form['attr_date'] = '2016-10-10'
        product_form['attr_file'] = Upload('file2.txt', b"test", 'text/plain')
        product_form['attr_image'] = Upload('image2.png', self.generate_test_image('image2.png'), 'image/png')
        product_form.submit()

        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.attr.text, 'test2')
        self.assertEqual(self.product.attr.integer, 2)
        self.assertEqual(self.product.attr.float, 5.2)
        self.assertFalse(self.product.attr.boolean)
        self.assertEqual(self.product.attr.richtext, 'article')
        self.assertEqual(self.product.attr.date, datetime.date(2016, 10, 10))
        self.assertEqual(self.product.attr.file.name, posixpath.join(self.image_folder, 'file2.txt'))
        self.assertEqual(self.product.attr.image.name, posixpath.join(self.image_folder, 'image2.png'))
github naspeh / mailur / tests / test_web.py View on Github external
'name': 'A',
            'time': some,
            'title': 'A@t.com',
        },
        'b@t.com': {
            'addr': 'b@t.com',
            'hash': 'd2ed191b17f1b9abdfd509d877a765db',
            'name': 'b',
            'time': some,
            'title': 'b@t.com',
        }
    }

    web.post('/editor', {
        'draft_id': draft_id,
        'files': Upload('test.rst', b'txt', 'text/x-rst')
    }, status=200)
    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '3']
    assert [i['uid'] for i in msgs()] == ['3', '5']
    m = latest(parsed=1)
    assert local.data_msgids.get() == {
        '<101@mlr>': ['3'],
        m['meta']['msgid']: ['5']
    }
    assert m['flags'] == '\\Seen \\Draft test #personal'
    assert m['meta']['files'] == [
        {
            'filename': 'test.rst',
            'path': '2.1',
            'size': 3,
            'url': '/raw/3/2.1/test.rst',
        }
github django-oscar / django-oscar / tests / functional / dashboard / test_product.py View on Github external
def test_product_update_attribute_values(self):
        page = self.get(self.url)
        product_form = page.form
        # Send string field values due to an error
        # in the Webtest during multipart form encode.
        product_form['attr_text'] = 'test1'
        product_form['attr_integer'] = '1'
        product_form['attr_float'] = '1.2'
        product_form['attr_boolean'] = 'yes'
        product_form['attr_richtext'] = 'longread'
        product_form['attr_date'] = '2016-10-12'
        product_form['attr_file'] = Upload('file1.txt', b"test", 'text/plain')
        product_form['attr_image'] = Upload('image1.png', self.generate_test_image('image1.png'), 'image/png')
        product_form.submit()

        # Reloading model instance to re-initiate ProductAttributeContainer
        # with new attributes.
        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.attr.text, 'test1')
        self.assertEqual(self.product.attr.integer, 1)
        self.assertEqual(self.product.attr.float, 1.2)
        self.assertTrue(self.product.attr.boolean)
        self.assertEqual(self.product.attr.richtext, 'longread')
        self.assertEqual(self.product.attr.date, datetime.date(2016, 10, 12))
        self.assertEqual(self.product.attr.file.name, posixpath.join(self.image_folder, 'file1.txt'))
        self.assertEqual(self.product.attr.image.name, posixpath.join(self.image_folder, 'image1.png'))

        page = self.get(self.url)
        product_form = page.form
github RealGeeks / django-modelclone / tests / test_admin.py View on Github external
def test_clone_should_override_file_from_original_object_on_submit_if_new_file_was_chosen(self):
        response = self.app.get(self.multimedia_url, user='admin')
        response.form['image'] = Upload('tests/files/img-2.jpg')
        response.form['document'] = Upload('tests/files/file-2.txt')
        response.form.submit()

        multimedia = Multimedia.objects.latest('id')

        assert 'images/img-2.jpg' == str(multimedia.image)
        assert 'documents/file-2.txt' == str(multimedia.document)
github RealGeeks / django-modelclone / tests / test_admin.py View on Github external
def test_clone_should_override_file_from_original_object_on_submit_if_new_file_was_chosen(self):
        response = self.app.get(self.multimedia_url, user='admin')
        response.form['image'] = Upload('tests/files/img-2.jpg')
        response.form['document'] = Upload('tests/files/file-2.txt')
        response.form.submit()

        multimedia = Multimedia.objects.latest('id')

        assert 'images/img-2.jpg' == str(multimedia.image)
        assert 'documents/file-2.txt' == str(multimedia.document)
github django-oscar / django-oscar / tests / functional / dashboard / test_product.py View on Github external
def test_product_images_upload(self):
        page = self.get(self.url)
        product_form = page.form
        product_form['images-0-original'] = Upload('image1.png', self.generate_test_image('image1.png'), 'image/png')
        product_form['images-1-original'] = Upload('image2.png', self.generate_test_image('image2.png'), 'image/png')
        product_form.submit(name='action', value='continue').follow()
        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.images.count(), 2)
        page = self.get(self.url)
        product_form = page.form
        product_form['images-2-original'] = Upload('image3.png', self.generate_test_image('image3.png'), 'image/png')
        product_form.submit()
        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.images.count(), 3)
        images = self.product.images.all()
        self.assertEqual(images[0].original.name, os.path.join(self.image_folder, 'image1.png'))
        self.assertEqual(images[0].display_order, 0)
        self.assertEqual(images[1].original.name, os.path.join(self.image_folder, 'image2.png'))
        self.assertEqual(images[1].display_order, 1)
        self.assertEqual(images[2].original.name, os.path.join(self.image_folder, 'image3.png'))
        self.assertEqual(images[2].display_order, 2)
github django-functest / django-functest / django_functest / files.py View on Github external
from __future__ import absolute_import, print_function, unicode_literals

from webtest import Upload as WebTestUpload


class Upload(WebTestUpload):
    pass
github marshmallow-code / webargs / src / webargs / testing.py View on Github external
def test_parse_files(self, testapp):
        res = testapp.post(
            "/echo_file", {"myfile": webtest.Upload("README.rst", b"data")}
        )
        assert res.json == {"myfile": "data"}
github aptise / peter_sslers / peter_sslers / tests.py View on Github external
"/.well-known/admin/server-certificate/upload.json", status=200
        )
        chain_filepath = self._filepath_testfile("lets-encrypt-x1-cross-signed.pem.txt")
        _SelfSigned_id = "2"
        form = {}
        form["certificate_file"] = Upload(
            self._filepath_testfile(
                TEST_FILES["ServerCertificates"]["SelfSigned"][_SelfSigned_id]["cert"]
            )
        )
        form["chain_file"] = Upload(
            self._filepath_testfile(
                TEST_FILES["ServerCertificates"]["SelfSigned"][_SelfSigned_id]["cert"]
            )
        )
        form["private_key_file_pem"] = Upload(
            self._filepath_testfile(
                TEST_FILES["ServerCertificates"]["SelfSigned"][_SelfSigned_id]["pkey"]
            )
        )
        res2 = self.testapp.post(
            "/.well-known/admin/server-certificate/upload.json", form
        )
        assert res2.status_code == 200
        res2_json = json.loads(res2.body)
        assert res2_json["result"] == "success"
        assert res2_json["certificate"]["created"] is True
        certificate_id = res2_json["certificate"]["id"]
        res3 = self.testapp.get(
            "/.well-known/admin/server-certificate/%s" % certificate_id, status=200
        )
github Pylons / webtest / tests / test_app.py View on Github external
def test_encode_multipart_content_type(self):
        data = self.app.encode_multipart(
            [], [('file', 'data.txt', six.b('data'),
                 'text/x-custom-mime-type')])
        self.assertIn(to_bytes('Content-Type: text/x-custom-mime-type'),
                      data[-1])

        data = self.app.encode_multipart(
            [('file', webtest.Upload('data.txt', six.b('data'),
                                     'text/x-custom-mime-type'))], [])
        self.assertIn(to_bytes('Content-Type: text/x-custom-mime-type'),
                      data[-1])