Add forms tests

master
Steven Loria 11 years ago
parent 1018859800
commit c156760f67
  1. 6
      {{cookiecutter.app_name}}/tests/conftest.py
  2. 62
      {{cookiecutter.app_name}}/tests/test_forms.py
  3. 1
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/settings.py
  4. 2
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/user/forms.py

@ -9,6 +9,7 @@ from {{ cookiecutter.app_name }}.settings import TestConfig
from {{cookiecutter.app_name}}.app import create_app
from {{cookiecutter.app_name}}.database import db as _db
from .factories import UserFactory
@pytest.yield_fixture(scope='function')
def app():
@ -34,3 +35,8 @@ def db(app):
yield _db
_db.drop_all()
@pytest.fixture
def user(db):
return UserFactory()

@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
import pytest
from {{cookiecutter.app_name}}.public.forms import LoginForm
from {{cookiecutter.app_name}}.user.forms import RegisterForm
from .factories import UserFactory
class TestRegisterForm:
def test_validate_user_already_registered(self, user):
# Enters username that is already registered
form = RegisterForm(username=user.username, email='foo@bar.com',
password='example', confirm='example')
assert form.validate() is False
assert 'Username already registered' in form.username.errors
def test_validate_email_already_registered(self, user):
# enters email that is already registered
form = RegisterForm(username='unique', email=user.email,
password='example', confirm='example')
assert form.validate() is False
assert 'Email already registered' in form.email.errors
def test_validate_success(self, db):
form = RegisterForm(username='newusername', email='new@test.test',
password='example', confirm='example')
assert form.validate() is True
class TestLoginForm:
def test_validate_success(self, user):
user.set_password('example')
user.save()
form = LoginForm(username=user.username, password='example')
assert form.validate() is True
assert form.user == user
def test_validate_unknown_username(self, db):
form = LoginForm(username='unknown', password='example')
assert form.validate() is False
assert 'Unknown username' in form.username.errors
assert form.user is None
def test_validate_invalid_password(self, user):
user.set_password('example')
user.save()
form = LoginForm(username=user.username, password='wrongpassword')
assert form.validate() is False
assert 'Invalid password' in form.password.errors
def test_validate_inactive_user(self, user):
user.active = False
user.set_password('example')
user.save()
# Correct username and password, but user is not activated
form = LoginForm(username=user.username, password='example')
assert form.validate() is False
assert 'User not activated' in form.username.errors

@ -39,3 +39,4 @@ class TestConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite://'
BCRYPT_LOG_ROUNDS = 1 # For faster tests
WTF_CSRF_ENABLED = False # Allows form testing

@ -30,6 +30,4 @@ class RegisterForm(Form):
if user:
self.email.errors.append("Email already registered")
return False
self.user = user
return True

Loading…
Cancel
Save