Skip to content

Instantly share code, notes, and snippets.

@octaflop
Created November 10, 2012 01:44
Show Gist options
  • Select an option

  • Save octaflop/4049442 to your computer and use it in GitHub Desktop.

Select an option

Save octaflop/4049442 to your computer and use it in GitHub Desktop.
flask integration testing with postgresql and sqlalchemy
import joyhikeserve as j
from joyhikeserve.db.testdatabase import init_db, drop_db
import unittest
import fixtures
from BeautifulSoup import BeautifulSoup as bs
appurl = "/api/%s"
header = { "content-type": "application/json" }
class JoyHikeDBCase(unittest.TestCase):
test_user1 = {
'username': 'wxillixxaxxmm3',
'email': 'wixlm3@soxxxuthpark.com',
'password': 'pxmwerxxxs3'
}
def create_app(self):
j.app.config['TESTING'] = True
from joyhikeserve.db.testdatabase import dburi as testdburi
from joyhikeserve.db.testdatabase import engine as testengine
self.dburi = testdburi
self.engine = testengine
j.app.config['DATABASE'] = self.dburi
j.app.config['SQLALCHEMY_DATABASE_URI'] = self.dburi
self.app = j.app.test_client()
def setUp(self):
self.create_app()
drop_db() # clear anything else in the db
init_db()
fixtures.install(self.engine, *fixtures.all_data)
from joyhikeserve.db.testdatabase import db_session as testsession
self.session = testsession
def tearDown(self):
drop_db()
self.session.remove()
def cheat_csrf(self, url):
rv = self.app.get(url)
soup = bs(rv.data)
csrf = soup.findAll("input")[0] # Assuming first input to be csrf
csrf_val = csrf['value']
return csrf_val
def register(self, username, password, email):
url = "/register"
csrf_val = self.cheat_csrf(url)
return self.app.post('/register', data=dict(
username=username,
password=password,
passwordc=password,
email=email,
csrf_token=csrf_val
), follow_redirects=True)
def login(self, username, password):
url = "/login"
csrf_val = self.cheat_csrf(url)
return self.app.post('/login', data=dict(
username=username,
password=password,
csrf_token=csrf_val
), follow_redirects=True)
def logout(self):
return self.app.get('/logout', follow_redirects=True)
# TEST CLASSES
def test_register_login_logout_success(self):
rv = self.register(**self.test_user1)
assert "Welcome %s!" % self.test_user1['username'] in rv.data
rv = self.login(self.test_user1['username'],
self.test_user1['password'])
assert "Welcome %s!" % self.test_user1['username'] in rv.data
rv = self.logout()
assert "Logged out" in rv.data
def test_list_disallowed(self):
rv = self.app.get(appurl % "routes", headers=header)
#print rv.data
#veritas = '{\n "objects": [], \n "total_pages": 0, \n "page": 1\n}'
veritas = "401"
assert veritas in rv.data
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment