Add WebTest and some tests

master
Steven Loria 11 years ago
parent 612dd55579
commit 64d40aa4be
  1. 8
      {{cookiecutter.repo_name}}/README.rst
  2. 1
      {{cookiecutter.repo_name}}/requirements/dev.txt
  3. 4
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/public/views.py
  4. 2
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/_layouts/nav.html
  5. 2
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/public/register.html
  6. 1
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/tests/factories.py
  7. 2
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/tests/test_models.py
  8. 108
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/tests/webtest_tests.py

@ -25,3 +25,11 @@ To open the interactive shell, run ::
python manage.py shell
By default, you will have access to ``app`` and ``db``.
Running Tests
-------------
To run all tests, run ::
python manage.py test

@ -5,6 +5,7 @@
nose
Flask-Testing
factory-boy>=2.2.1
Flask-WebTest>=0.0.6
# Management script
Flask-Script

@ -43,12 +43,10 @@ def logout():
def register():
form = RegisterForm(request.form, csrf_enabled=False)
if form.validate_on_submit():
new_user = User(username=form.username.data,
new_user = User.create(username=form.username.data,
email=form.email.data,
password=form.password.data,
active=True)
db.session.add(new_user)
db.session.commit()
flash("Thank you for registering. You can now log in.", 'success')
return redirect(url_for('public.home'))
else:

@ -29,7 +29,7 @@
<ul class="nav navbar-nav navbar-right">
<li><a href="{{ url_for('public.register') }}">Create account</a></li>
</ul>
<form method="POST" class="navbar-form form-inline navbar-right" action="" role="login">
<form id="loginForm" method="POST" class="navbar-form form-inline navbar-right" action="" role="login">
{{ form.hidden_tag() }}
<div class="form-group">
{{ form.username(placeholder="Username", class_="form-control") }}

@ -4,7 +4,7 @@
<div class="container-narrow">
<h1>Register</h1>
<br/>
<form class="form form-register" method="POST" action="" role="form">
<form id="registerForm" class="form form-register" method="POST" action="" role="form">
{{ form.hidden_tag() }}
<div class="form-group">
{{form.username.label}}

@ -13,3 +13,4 @@ class UserFactory(SQLAlchemyModelFactory):
username = Sequence(lambda n: "user{0}".format(n))
email = Sequence(lambda n: "user{0}@example.com".format(n))
password = PostGenerationMethodCall("set_password", 'example')
active = True

@ -16,7 +16,7 @@ class TestUser(DbTestCase):
assert_true(user.email)
assert_true(user.created_at)
assert_false(user.is_admin)
assert_false(user.active)
assert_true(user.active)
assert_true(user.check_password("myprecious"))
def test_check_password(self):

@ -0,0 +1,108 @@
# -*- coding: utf-8 -*-
'''Functional tests using WebTest.
See: http://webtest.readthedocs.org/
'''
from flask import url_for
from flask.ext.webtest import TestApp
from nose.tools import * # PEP8 asserts
from ..user.models import User
from .base import DbTestCase
from .factories import UserFactory
class TestLoggingIn(DbTestCase):
def setUp(self):
self.w = TestApp(self.app)
self.user = UserFactory(password="myprecious")
self.user.save()
def test_can_log_in(self):
# Goes to homepage
res = self.w.get("/")
# Fills out login form in navbar
form = res.forms['loginForm']
form['username'] = self.user.username
form['password'] = 'myprecious'
# Submits
res = form.submit().follow()
assert_equal(res.status_code, 200)
def test_sees_error_message_if_password_is_incorrect(self):
# Goes to homepage
res = self.w.get("/")
# Fills out login form, password incorrect
form = res.forms['loginForm']
form['username'] = self.user.username
form['password'] = 'wrong'
# Submits
res = form.submit()
# sees error
assert_in("Invalid password", res)
def test_sees_error_message_if_username_doesnt_exist(self):
# Goes to homepage
res = self.w.get("/")
# Fills out login form, password incorrect
form = res.forms['loginForm']
form['username'] = 'unknown'
form['password'] = 'myprecious'
# Submits
res = form.submit()
# sees error
assert_in("Unknown user", res)
class TestRegistering(DbTestCase):
def setUp(self):
self.w = TestApp(self.app)
def test_can_register(self):
# Goes to homepage
res = self.w.get("/")
# Clicks Create Account button
res = res.click("Create account")
# Fills out the form
form = res.forms["registerForm"]
form['username'] = 'foobar'
form['email'] = 'foo@bar.com'
form['password'] = 'secret'
form['confirm'] = 'secret'
# Submits
res = form.submit().maybe_follow()
assert_equal(res.status_code, 200)
# A new user was created
assert_equal(len(User.query.all()), 1)
def test_sees_error_message_if_passwords_dont_match(self):
# Goes to registration page
res = self.w.get(url_for("public.register"))
# Fills out form, but passwords don't match
form = res.forms["registerForm"]
form['username'] = 'foobar'
form['email'] = 'foo@bar.com'
form['password'] = 'secret'
form['confirm'] = 'secrets'
# Submits
res = form.submit()
# sees error message
assert_in("Passwords must match", res)
def test_sees_error_message_if_user_already_registered(self):
user = UserFactory(active=True) # A registered user
user.save()
# Goes to registration page
res = self.w.get(url_for("public.register"))
# Fills out form, but
form = res.forms["registerForm"]
form['username'] = user.username
form['email'] = 'foo@bar.com'
form['password'] = 'secret'
form['confirm'] = 'secret'
# Submits
res = form.submit()
# sees error
assert_in("Username already registered", res)
Loading…
Cancel
Save