Last active
December 20, 2015 13:39
-
-
Save fweep/6140862 to your computer and use it in GitHub Desktop.
Pyramid, WebTest, SQLAlchemy functional test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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