forked from orbit-oss/flask
* Converts example/flaskr to have a setup.py Makes the flaskr app easier to run, ex. workflow: - pip install --editable . - export FLASK_APP=flaskr.flaskr - flask initdb - flask run Testing is also easier now: - python setup.py test * Fixed an import error in flaskr/tests - the statement `import flaskr` caused errors in python3 - `from . import flaskr` fixes the issue in 2.7.11 and 3.5.1 * Better project structure and updates the docs - Re-factors *flaskr*'s project structure a bit - Updates docs to make sense with the new structure - Adds a new step about installing Flask apps with setuptools - Switches first-person style writing to second-person (reads better IMO) - Adds segments in *testing.rst* for running tests with setuptools * Remove __init__.py from tests - py.test recommends not using __init__.py * Fix testing import errors
76 lines
2 KiB
Python
76 lines
2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Flaskr Tests
|
|
~~~~~~~~~~~~
|
|
|
|
Tests the Flaskr application.
|
|
|
|
:copyright: (c) 2015 by Armin Ronacher.
|
|
:license: BSD, see LICENSE for more details.
|
|
"""
|
|
|
|
import pytest
|
|
import os
|
|
import tempfile
|
|
|
|
from context import flaskr
|
|
|
|
@pytest.fixture
|
|
def client(request):
|
|
db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
|
|
flaskr.app.config['TESTING'] = True
|
|
client = flaskr.app.test_client()
|
|
with flaskr.app.app_context():
|
|
flaskr.init_db()
|
|
|
|
def teardown():
|
|
os.close(db_fd)
|
|
os.unlink(flaskr.app.config['DATABASE'])
|
|
request.addfinalizer(teardown)
|
|
|
|
return client
|
|
|
|
|
|
def login(client, username, password):
|
|
return client.post('/login', data=dict(
|
|
username=username,
|
|
password=password
|
|
), follow_redirects=True)
|
|
|
|
|
|
def logout(client):
|
|
return client.get('/logout', follow_redirects=True)
|
|
|
|
|
|
def test_empty_db(client):
|
|
"""Start with a blank database."""
|
|
rv = client.get('/')
|
|
assert b'No entries here so far' in rv.data
|
|
|
|
|
|
def test_login_logout(client):
|
|
"""Make sure login and logout works"""
|
|
rv = login(client, flaskr.app.config['USERNAME'],
|
|
flaskr.app.config['PASSWORD'])
|
|
assert b'You were logged in' in rv.data
|
|
rv = logout(client)
|
|
assert b'You were logged out' in rv.data
|
|
rv = login(client, flaskr.app.config['USERNAME'] + 'x',
|
|
flaskr.app.config['PASSWORD'])
|
|
assert b'Invalid username' in rv.data
|
|
rv = login(client, flaskr.app.config['USERNAME'],
|
|
flaskr.app.config['PASSWORD'] + 'x')
|
|
assert b'Invalid password' in rv.data
|
|
|
|
|
|
def test_messages(client):
|
|
"""Test that messages work"""
|
|
login(client, flaskr.app.config['USERNAME'],
|
|
flaskr.app.config['PASSWORD'])
|
|
rv = client.post('/add', data=dict(
|
|
title='<Hello>',
|
|
text='<strong>HTML</strong> allowed here'
|
|
), follow_redirects=True)
|
|
assert b'No entries here so far' not in rv.data
|
|
assert b'<Hello>' in rv.data
|
|
assert b'<strong>HTML</strong> allowed here' in rv.data
|