Skip to content

Instantly share code, notes, and snippets.

@fweep
Last active December 20, 2015 13:39
Show Gist options
  • Save fweep/6140862 to your computer and use it in GitHub Desktop.
Save fweep/6140862 to your computer and use it in GitHub Desktop.
Pyramid, WebTest, SQLAlchemy functional test
import unittest
from myapp.tests.helper import create_user
SQLALCHEMY_URL = "postgresql://unireg@localhost/myapp"
def _init_testing_db():
from sqlalchemy import create_engine
from myapp.models import DBSession, Base
engine = create_engine(SQLALCHEMY_URL)
Base.metadata.create_all(engine)
DBSession.configure(bind=engine)
return DBSession
class LoginTests(unittest.TestCase):
def setUp(self):
from myapp import main
settings = {'sqlalchemy.url': SQLALCHEMY_URL}
app = main({}, **settings)
from webtest import TestApp
self.testapp = TestApp(app)
_init_testing_db()
def tearDown(self):
import transaction
transaction.abort()
del self.testapp
from myapp.models import DBSession
DBSession.remove()
def test_login_redirects_to_root_on_success(self):
create_user(login="testuser", password="testpass")
page = self.testapp.get('/', status=200)
form = page.forms['loginForm']
form['login'] = "testuser"
form['password'] = "testpass"
result = form.submit(status=302)
self.assertTrue("Set-Cookie" in result.headers)
self.assertEqual(result.headers['Set-Cookie'].split("=")[0], "auth_tkt")
self.assertEqual(result.location, "http://localhost/")
def test_login_does_not_set_cookie_on_failure(self):
# FIXME: tests don't appear to be transactional
page = self.testapp.get('/', status=200)
form = page.forms['loginForm']
form['login'] = "testuser"
form['password'] = "testpass"
result = form.submit(status=200)
self.assertFalse("Set-Cookie" in result.headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment