# HG changeset patch # User Daniele Nicolodi # Date 1313343088 -7200 # Node ID ad6e52b8d07c4b4ff22b547aa275cb4ba1ab270c # Parent d8f600938f31afef6e2acffbc3635fe4529a10e5 Cleanup tests. diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/__init__.py --- a/src/ltpdarepo/tests/__init__.py Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/__init__.py Sun Aug 14 19:31:28 2011 +0200 @@ -1,62 +0,0 @@ -import unittest -import doctest - -import zope.testbrowser.wsgi - - -class Browser(zope.testbrowser.wsgi.Browser): - def __init__(self, url='http://localhost/'): - - from ltpdarepo import app - import logging - handler = logging.StreamHandler() - handler.setLevel(logging.DEBUG) - app.logger.addHandler(handler) - - super(Browser, self).__init__(url, wsgi_app=app) - - -USERNAME = 'u1' -PASSWORD = 'u1' - - -def doctestSetUp(self): - from ltpdarepo.admin import wipe, install, useradd, grant, createdb - wipe() - install() - useradd('u1', 'u1') - grant('u1', '%', ['admin', ]) - createdb('db1') - - -def doctestTearDown(self): - from ltpdarepo.admin import wipe - wipe() - - -def suite(): - suite = unittest.TestSuite() - suite.addTest( - doctest.DocFileSuite( - 'manage-users.txt', - 'manage-databases.txt', - setUp=doctestSetUp, tearDown=doctestTearDown, - optionflags=doctest.ELLIPSIS | doctest.REPORT_ONLY_FIRST_FAILURE)) - - from ltpdarepo.tests import test_csrf - suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_csrf)) - - from ltpdarepo.tests import test_users - suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_users)) - - from ltpdarepo.tests import test_security - suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_security)) - - from ltpdarepo.tests import test_pagination - suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_pagination)) - - return suite - - -def main(): - unittest.TextTestRunner().run(suite()) diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/manage-databases.txt --- a/src/ltpdarepo/tests/manage-databases.txt Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/manage-databases.txt Sun Aug 14 19:31:28 2011 +0200 @@ -1,7 +1,7 @@ Test setup:: - >>> from ltpdarepo.tests import Browser - >>> from ltpdarepo.tests import USERNAME, PASSWORD + >>> from ltpdarepo.tests.utils import Browser + >>> USERNAME, PASSWORD = 'u1', 'u1' >>> browser = Browser() Login:: @@ -24,7 +24,7 @@ Create a new database:: - >>> browser.getLink('Create new database').click() + >>> browser.getLink('Create database').click() >>> browser.url 'http://localhost/manage/databases/+' @@ -59,7 +59,7 @@ Cannot create database with duplicate id:: >>> browser.open('/manage/databases/') - >>> browser.getLink('Create new database').click() + >>> browser.getLink('Create database').click() >>> browser.getControl(name='id').value = 'database1' >>> browser.getControl(name='submit').click() >>> browser.url diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/manage-users.txt --- a/src/ltpdarepo/tests/manage-users.txt Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/manage-users.txt Sun Aug 14 19:31:28 2011 +0200 @@ -1,7 +1,7 @@ Test setup:: - >>> from ltpdarepo.tests import Browser - >>> from ltpdarepo.tests import USERNAME, PASSWORD + >>> from ltpdarepo.tests.utils import Browser + >>> USERNAME, PASSWORD = 'u1', 'u1' >>> browser = Browser() Login:: diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_base.py --- a/src/ltpdarepo/tests/test_base.py Sun Aug 14 12:08:21 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -import unittest -import mechanize - -import ltpdarepo - -USERNAME = 'u1' -PASSWORD = 'u1' - -class TestFactory(unittest.TestCase): - - def setUp(self): - self.app = ltpdarepo.app.test_client() - - def tearDown(self): - pass - - def test_foo(self): - rv = self.app.get('/') - print rv.data - print rv.status_code - print rv.headers - print rv.mimetype - import pdb; pdb.set_trace() - - def test_login(self): - rv = self.app.get('/login') - - rv = self.app.get('/login') - -def test_suite(): - return unittest.defaultTestLoader.loadTestsFromName(__name__) - -if __name__ == '__main__': - unittest.main() diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_browser.py --- a/src/ltpdarepo/tests/test_browser.py Sun Aug 14 12:08:21 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -import unittest - -from ltpdarepo.tests import Browser -from ltpdarepo.tests import USERNAME, PASSWORD - - -class TestCase(unittest.TestCase): - - def setUp(self): - self.browser = Browser('http://localhost/') - - def test_login(self): - self.browser.open('/') - self.assertEqual(self.browser.url, 'http://localhost/login') - - self.browser.getForm().getControl(name='username').value = USERNAME - self.browser.getForm().getControl(name='password').value = PASSWORD - self.browser.getForm().submit() - - self.assertEqual(self.browser.url, 'http://localhost/') - - def test_login_redirect(self): - self.browser.open('/manage/databases/') - print self.browser.url - #self.assertEqual(self.browser.url, 'http://localhost/login') - - self.browser.getForm().getControl(name='username').value = USERNAME - self.browser.getForm().getControl(name='password').value = PASSWORD - self.browser.getForm().submit() - - self.assertEqual(self.browser.url, 'http://localhost/manage/databases/') - - def test_login_bad_redirect(self): - self.browser.open('/login?next=http://google.com') - print self.browser.url - self.browser.getForm().getControl(name='username').value = USERNAME - self.browser.getForm().getControl(name='password').value = PASSWORD - self.browser.getForm().submit() - print self.browser.url - - def test_foo(self): - self.browser.open('/login?next=http%3A%2F%2Fgoogle.com') - print self.browser.url - self.browser.getForm().getControl(name='username').value = USERNAME - self.browser.getForm().getControl(name='password').value = PASSWORD - self.browser.getForm().submit() - print self.browser.url - - -if __name__ == '__main__': - unittest.main() diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_csrf.py --- a/src/ltpdarepo/tests/test_csrf.py Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/test_csrf.py Sun Aug 14 19:31:28 2011 +0200 @@ -42,10 +42,3 @@ rv = self.app.post('/', data={'test': 2, 'csrf': rv.data}) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.data, '2') - - -def suite(): - suite = unittest.TestLoader() - -if __name__ == '__main__': - unittest.main() diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_doctests.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/tests/test_doctests.py Sun Aug 14 19:31:28 2011 +0200 @@ -0,0 +1,27 @@ +import doctest +import unittest + + +def doctestSetUp(self): + from ltpdarepo.admin import wipe, install, useradd, grant, createdb + wipe() + install() + useradd('u1', 'u1') + grant('u1', '%', ['admin', ]) + createdb('db1') + + +def doctestTearDown(self): + from ltpdarepo.admin import wipe + wipe() + + +def load_tests(loader, tests, pattern): + suite = unittest.TestSuite() + suite.addTest( + doctest.DocFileSuite( + 'manage-users.txt', + 'manage-databases.txt', + setUp=doctestSetUp, tearDown=doctestTearDown, + optionflags=doctest.ELLIPSIS | doctest.REPORT_ONLY_FIRST_FAILURE)) + return suite diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_login.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/tests/test_login.py Sun Aug 14 19:31:28 2011 +0200 @@ -0,0 +1,62 @@ +import unittest2 as unittest +from urllib import urlencode + +USERNAME, PASSWORD = 'u1', 'u1' + + +class Test(unittest.TestCase): + + @classmethod + def setUpClass(cls): + from ltpdarepo.admin import wipe, setup + wipe() + setup() + + def setUp(self): + from ltpdarepo import app + app.config.from_pyfile('config.py') + self.app = app.test_client() + + def test_login_required(self): + response = self.app.get('/') + self.assertEqual(response.status_code, 302) + self.assertEqual(response.location, 'http://localhost/login') + + response = self.app.get('/manage/databases/') + self.assertEqual(response.status_code, 302) + target = urlencode(dict(next='/manage/databases/')) + self.assertEqual(response.location, 'http://localhost/login?' + target) + + def test_login(self): + response = self.app.post('/login', + data=dict(username=USERNAME, password=PASSWORD)) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.location, 'http://localhost/') + + def test_login_redirect(self): + response = self.app.post('/login', + data=dict(username=USERNAME, password=PASSWORD), + query_string=dict(next='/manage/databases/')) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.location, 'http://localhost/manage/databases/') + + def test_login_bad_redirect(self): + response = self.app.post('/login', + data=dict(username=USERNAME, password=PASSWORD), + query_string=dict(next='http://example.net')) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.location, 'http://localhost/') + + # try the same with urlencoded argument + response = self.app.post('/login', + data=dict(username=USERNAME, password=PASSWORD), + query_string='next=http%3A%2F%2Fexample.net') + self.assertEqual(response.status_code, 302) + self.assertEqual(response.location, 'http://localhost/') + + # try the same without urlencoded argument + response = self.app.post('/login', + data=dict(username=USERNAME, password=PASSWORD), + query_string='next=http://example.net') + self.assertEqual(response.status_code, 302) + self.assertEqual(response.location, 'http://localhost/') diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_pagination.py --- a/src/ltpdarepo/tests/test_pagination.py Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/test_pagination.py Sun Aug 14 19:31:28 2011 +0200 @@ -77,7 +77,3 @@ l = list(p) self.assertEqual(len(l), 7) self.assertEqual(l, [1, -9, 11, 12, 13, 14, 15]) - - -if __name__ == '__main__': - unittest.main() diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_security.py --- a/src/ltpdarepo/tests/test_security.py Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/test_security.py Sun Aug 14 19:31:28 2011 +0200 @@ -34,8 +34,3 @@ response = client.get('/2') self.assertEqual(response.status_code, 302) - - -if __name__ == '__main__': - unittest.main() - diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/test_users.py --- a/src/ltpdarepo/tests/test_users.py Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/test_users.py Sun Aug 14 19:31:28 2011 +0200 @@ -85,7 +85,3 @@ conn = mysql.connect(host=self.app.config['HOSTNAME'], user=u2.username, passwd=u2.password, charset='utf8') - - -if __name__ == '__main__': - unittest.main() diff -r d8f600938f31 -r ad6e52b8d07c src/ltpdarepo/tests/utils.py --- a/src/ltpdarepo/tests/utils.py Sun Aug 14 12:08:21 2011 +0200 +++ b/src/ltpdarepo/tests/utils.py Sun Aug 14 19:31:28 2011 +0200 @@ -1,4 +1,15 @@ -import MySQLdb as mysql -from ..config import HOSTNAME, DATABASE, USERNAME, PASSWORD +import zope.testbrowser.wsgi +class Browser(zope.testbrowser.wsgi.Browser): + def __init__(self, url='http://localhost/'): + + from ltpdarepo import app + app.config.from_pyfile('config.py') + + import logging + handler = logging.StreamHandler() + handler.setLevel(logging.DEBUG) + app.logger.addHandler(handler) + + super(Browser, self).__init__(url, wsgi_app=app)