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. 4
      {{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 python manage.py shell
By default, you will have access to ``app`` and ``db``. 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 nose
Flask-Testing Flask-Testing
factory-boy>=2.2.1 factory-boy>=2.2.1
Flask-WebTest>=0.0.6
# Management script # Management script
Flask-Script Flask-Script

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

@ -29,7 +29,7 @@
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li><a href="{{ url_for('public.register') }}">Create account</a></li> <li><a href="{{ url_for('public.register') }}">Create account</a></li>
</ul> </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() }} {{ form.hidden_tag() }}
<div class="form-group"> <div class="form-group">
{{ form.username(placeholder="Username", class_="form-control") }} {{ form.username(placeholder="Username", class_="form-control") }}

@ -4,7 +4,7 @@
<div class="container-narrow"> <div class="container-narrow">
<h1>Register</h1> <h1>Register</h1>
<br/> <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() }} {{ form.hidden_tag() }}
<div class="form-group"> <div class="form-group">
{{form.username.label}} {{form.username.label}}
@ -27,4 +27,4 @@
<p><em>Already registered?</em> Click <a href="/">here</a> to login.</p> <p><em>Already registered?</em> Click <a href="/">here</a> to login.</p>
</div> </div>
{% endblock %} {% endblock %}
{% endraw %} {% endraw %}

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

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