samedi 29 août 2015

How to unit test a form submission when multiple forms on a route?

I want to add unit tests to my flask app that tests form behavior on valid and invalid logins + signups. Currently, I have the signup form and a login form hosted on one page and route, and am using a hidden input field to identify which of the two forms is submitted / determine next actions.

My question is - how do I write a unit test that targets a specific form on a page? All the examples I've seen so far post data to a specific route, which is currently what I am doing. But that is failing because I need an added way to say "and we're submitting x form".

So is there a way to add "and we're submitting x form" in the post request?

login unit test:

from app import app
import unittest

class FlaskTestCase(unittest.TestCase):

  #ensure that login works with correct credentials
  def test_correct_login(self):
    tester = app.test_client(self)
    response = tester.post(
      '/login',
      data = dict(username="test@gmail.com", password="test"),
      follow_redirects=True
      )
    self.assertIn(b'you are logged in', response.data)

login route in views.py:

@app.route('/login', methods=['POST', 'GET'])
def login():
  login_form = LoginForm()
  signup_form = SignupForm()
  error_login = ''
  error_signup = ''

  #login form
  if 'login_form' in request.form and login_form.validate():
   # do login form stuff

  #signup form 
  if 'signup_form' in request.form and signup_form.validate():
   # do signup form stuff

  return render_template('login.html', login_form=login_form, signup_form=signup_form, error=error)

login.html:

<div class="login-form form-400">
      <h3>Log In To Your Account</h3>
      <input type="hidden" name="login_form">
      {% if error_login != '' %}
      <label class="error">
        {{ error_login }}
      </label>
      {% endif %}

      {% from "_formhelper.html" import render_field %}
      {{ login_form.hidden_tag() }}
      {{ render_field(login_form.email, placeholder="Your Email", class="form-item__full", type="email") }}
      {{ render_field(login_form.password, placeholder="Your Password", class="form-item__full") }}
      <input type="submit" value="Login" class="button button-blue">
    </form>
  </div>

  <p class="login-divider">or</p>

  <div class="signup-form form-400">
    <h3>Create a New Account</h3>
    <form action="" method="post">
      <input type="hidden" name="signup_form">
      {% if error_signup != '' %}
      <label class="error">
        {{ error_signup | safe}}
      </label>
      {% endif %}

      {% from "_formhelper.html" import render_field %}
      {{ signup_form.hidden_tag() }}
      {{ render_field(signup_form.username, placeholder="Pick a Username", class="form-item__full") }}
      {{ render_field(signup_form.email, placeholder="Your Email", class="form-item__full", type="email") }}
      {{ render_field(signup_form.password, placeholder="Create a Password", class="form-item__full") }}

      <input type="submit" value="Sign Up" class="button button-green">
    </form>
  </div>

Aucun commentaire:

Enregistrer un commentaire