Al Ex Tsm
Al Ex Tsm

Reputation: 2102

Can't unit-test image upload with python

I have a function that receives data from a form via POST. I have some textfields and a file field in order to upload an image(.jpg).

The function expects to get the image file from request.files['file']. Being "file" the name of the image form field.

I don't know how to run my function as a test and pass it the request.files[] argument like I have done with the other field values(name, password, mail, phone...)

This is the function I wanna test (app.py):

@app.route('/registering', methods=['GET', 'POST'])
def registering():
    if request.method == 'POST':
        userCheck = request.form['username']
        userCheck2 = request.form['email']
        userCheck3 = request.form['password']
        userCheck4 = request.form['passwordCheck']
        userCheck5 = request.form['phone']

        file = request.files['file']
        if file and allowed_file(file.filename):

            newUser = users(userCheck, userCheck2, userCheck5, userCheck3)
            db.session.add(newUser)
            db.session.commit()
            userName = users.query.filter_by(
                    userName=userCheck, userPass=userCheck3).first()

            session['logged_in'] = True
            session['user_id'] = userName.id

            filename = str(userName.id)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename + ".jpg"))

            flash('"Registered Successfully"')
            return redirect(url_for('friendList'))

    else:
        return render_template('register.html')

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

Here is my test (app_tests.py):

import os
import app
import unittest
import tempfile

class AppTestCase(unittest.TestCase):

    def setUp(self):
        self.db_fd, app.app.config['DATABASE'] = tempfile.mkstemp()
        app.app.config['TESTING'] = True
        self.app = app.app.test_client()

    def tearDown(self):
        os.close(self.db_fd)
        os.unlink(app.app.config['DATABASE'])


    def registering(self, username, email, password, passwordCheck, phone, file):
    return self.app.post('/registering', data=dict(
        username=username, 
        email=email, 
        password=password, 
        passwordCheck=passwordCheck, 
        phone=phone,
        file=file
    ), follow_redirects=True)

    def test_registering(self):
        #successfully registered

        rv = self.registering('TestUser', '[email protected]', 'passwordTest', 'passwordTest', '900102030')
        assert 'Registered Successfully' in rv.data
        #existing username/user
        rv = self.registering('Hulda', '[email protected]', 'passwordHulda', 'passwordHulda', '900102030')
        assert 'Username Taken' in rv.data
         #no username
        rv = self.registering('', '[email protected]', 'passwordSantana', 'passwordSantana', '900102030')
        assert 'Username required' in rv.data
        #no email
        rv = self.registering('Santana', '', 'passwordSantana', 'passwordSantana', '900102030')
        assert 'Email required' in rv.data
        #no password
        rv = self.registering('Santana', '[email protected]', '', 'passwordSantana', '900102030')
        assert 'Password required' in rv.data
        #no password confirmation
        rv = self.registering('Santana', '[email protected]', 'passwordSantana', '', '900102030')
        assert 'Confirm password' in rv.data
        #no password match
        rv = self.registering('Santana', '[email protected]', 'passwordSantana', 'passwordSsssssntana', '900102030')
        assert 'Retype passwords' in rv.data
        #no phone
        rv = self.registering('Santana', '[email protected]', 'passwordSantana', 'passwordSantana', '')
        assert 'Phone required' in rv.data

Upvotes: 2

Views: 3535

Answers (2)

Adam Hughes
Adam Hughes

Reputation: 16319

FWIW - in testing a flask app in pytest, I did the following:

@pytest.fixture
def example_image():
    filename = 'data/example_image.jpeg'
    fileobj = open(fileobj, 'rb')
    return FileStorage(stream=fileobj, filename="example_image.jpeg", content_type="image")

Note file must be 'rb' type

def test_image_upload(client, example_image):
data = {
    'model_id': 2,
    'image': example_image,
}
headers = {'content_type': 'multipart/form-data'}
response = client.post(ROUTE, data=data, headers=headers)
assert response.status_code == 200

This tests a code path that looks for the following

request.files.get('image')

Upvotes: 0

Al Ex Tsm
Al Ex Tsm

Reputation: 2102

I found the solution. Here you go in case somebody has the same test needs:

def registering(self, username, email, password, passwordCheck, phone):
    with open('static/test.jpg') as test:
        imgStringIO = StringIO(test.read())

    return self.app.post('/registering',
        content_type='multipart/form-data',
        data=dict(
            {'file': (imgStringIO, 'test.jpg')},
            username=username,
            email=email,
            password=password,
            passwordCheck=passwordCheck,
            phone=phone
        ), follow_redirects=True
    )


def test_03_registering(self):
    rv = self.registering('TestUser', '[email protected]', 'passwordTest', 'passwordTest', '900102030')
    assert 'Registered Successfully' in rv.data

Upvotes: 2

Related Questions