title: How to test POST action on view with form in Flask
body: <p>I have a flask application that has a view function that looks like this:</p>
  
  <pre><code>@login_required
  @app.route('/suspect_tracker/new_list', methods=['GET', 'POST'])
  def new_list():
      form = ListForm()
      if form.validate_on_submit():
          if form.private:
              privacy_setting = 1
          else:
              privacy_setting = 0
          new_list = List(name=form.name.data, last_updated=datetime.utcnow(), user_id=g.user.id, private=privacy_setting)
          db.session.add(new_list)
          db.session.commit()
          flash('New list %s added' % new_list.name)
          return redirect('/suspect_tracker/' + form.name.data)
      else:
          flash(form.name.errors)
  
      return render_template('newlist.html', title="Make a new list!", form=form)
  </code></pre>
  
  <p>And I am attempting to write a test for it, with the test looking like this:</p>
  
  <pre><code>from config import basedir
  from app import app, db
  from app.models import User, List, Suspect, SuspectList
  from flask import g
  from flask_testing import TestCase
  
  class TestViews(TestCase):
      def create_app(self):
          app.config['TESTING'] = True
          app.config['WTF_CSRF_ENABLED'] = False
          app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
          self.client = app.test_client()
  
          return app
  
      def setUp(self):
          self.app = self.create_app()
          db.create_all()
  
          test_user = User(nickname="test")
  
          db.session.add(test_user)
          db.session.commit()
  
          g.user = User.query.filter_by(id=1).first()
  
      def tearDown(self):
          db.session.remove()
          db.drop_all()
  
      def test_new_list_view(self):
          self.client.get('/suspect_tracker/new_list/')
  
          form = {'name':'Test', 'private':False}
  
          g.user = User.query.filter_by(id=1).first()
  
          self.client.post('/suspect_tracker/new_list', data=form)
  
          assert List.query.filter_by(name="Test").first() != None
  </code></pre>
  
  <p><strike>After running the test, the assertion fails, and I have tested to see that after running <code>self.client.post</code>, <code>List</code> does not contain a new list, and is still empty. This leads me to believe that <code>form.validate_on_submit()</code> returned <code>false</code>, which leads me to believe that I am not passing the correct data in the <code>self.client.post()</code> function in <code>test_new_list_view()</code>. My question is, how do I correctly create a <code>ListForm()</code> in the testing function and then POST it with <code>self.client.post()</code> to test the <code>new_list()</code> function?</strike></p>
  
  <p>I've fixed the issue with getting the form data to properly be sent, so now the form validates but I have no idea how to set g.user to be the mock user so when I try running the test I get an "AttributeError: 'AnonymousUserMixin' object has no attribute 'id'" error.</p>