Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a POST that uses Flask-WTF validate_on_submit

I am stumped on testing a POST to add a category to the database where I've used Flask_WTF for validation and CSRF protection. For the CRUD operations pm my website. I've used Flask, Flask_WTF and Flask-SQLAlchemy. It is my first independent project, and I find myself a little at a lost on how to test the Flask-WTForm validate_on_submit function.

Here's are the models:

class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True, unique=True)
    name = db.Column(db.String(80), nullable=False)
    email = db.Column(db.String(250), unique=True)

class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True, unique=True)
    name = db.Column(db.String(250), nullable=False, unique=True)
    users_id = db.Column(db.Integer, db.ForeignKey('users.id'))

Here's the form:

class CategoryForm(Form):
    name = StringField(
        'Name', [validators.Length(min=4, max=250, message="name problem")])

And here's the controller:

@category.route('/category/add', methods=['GET', 'POST'])
@login_required
def addCategory():
    """ Add a new category.
        Returns: Redirect Home.
    """
    # Initiate the form.
    form = CategoryForm()
    # On POST of a valid form, add the new category.
    if form.validate_on_submit():
        category = Category(
            form.name.data, login_session['users_id'])
        db.session.add(category)
        db.session.commit()
        flash('New Category %s Successfully Created' % category.name)
        return redirect(url_for('category.showHome'))
    else:
        # Render the form to add the category.
        return render_template('newCategory.html', form=form)

How do I write a test for the if statement with the validate_on_submit function?

like image 495
Greg Quinlan Avatar asked Jun 01 '16 21:06

Greg Quinlan


2 Answers

You should have different configurations for your app, depending if you are local / in production / executing unit tests. One configuration you can set is

WTF_CSRF_ENABLED = False

See flask-wtforms documentation.

like image 80
Martin Thoma Avatar answered Oct 18 '22 21:10

Martin Thoma


Using py.test and a conftest.py recommended by Delightful testing with pytest and SQLAlchemy, here's a test that confirms the added category.

def test_add_category_post(app, session):
    """Does add category post a new category?"""
    TESTEMAIL = "[email protected]"
    TESTUSER = "Joe Test"
    user = Users.query.filter(Users.email==TESTEMAIL).first()
    category = Category(name="Added Category", users_id=user.id)
    form = CategoryForm(formdata=None, obj=category)
    with app.test_client() as c:
        with c.session_transaction() as sess:
            sess['email'] = TESTEMAIL
            sess['username'] = TESTUSER 
            sess['users_id'] = user.id
            response = c.post(
                '/category/add', data=form.data, follow_redirects=True)
    assert response.status_code == 200
    added_category = Category.query.filter(
        Category.name=="Added Category").first()
    assert added_category
    session.delete(added_category)
    session.commit()

Note that the new category is assigned to a variable and then used to create a form. The form's data is used in the post.

like image 34
Greg Quinlan Avatar answered Oct 18 '22 20:10

Greg Quinlan