9030dc9517cf

web: add basic tests

This patch adds a new test module `test_web` to automate testing of web
requests. For now the tests are rather simple and only check for
expected status codes.

To set up the flask app within the tests, it has to be configured
properly. This is the reason why the app configuration part in `web.py`
has been moved into an own function - now it may also be used by the
test module.
[view raw] [browse files]
author Oben Sonne <obensonne@googlemail.com>
date Mon, 02 Jul 2012 22:32:48 +0200
parents 638009474a3b
children 8be7ac46f0ec
branches/tags (none)
files review/tests/test_web.py review/web.py

Changes

--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/review/tests/test_web.py	Mon Jul 02 22:32:48 2012 +0200
@@ -0,0 +1,38 @@
+import unittest
+
+from nose.tools import eq_
+
+import util
+
+from .. import web
+
+NON_EXISTENT_REV = 999999
+
+class WebTestCase(unittest.TestCase):
+    """Simple web tests. Currently only checks for correct status codes."""
+
+    def setUp(self):
+        util.setup_reviewed_sandbox()()
+        ui = util.get_ui()
+        repo = util.get_sandbox_repo()
+        web._configure_app(ui, repo)
+        web.app.config['TESTING'] = True
+        self.app = web.app.test_client()
+
+    def tearDown(self):
+        util.teardown_sandbox()
+
+    def test_index_no_rev(self):
+        rv = self.app.get('/')
+        eq_(rv.status_code, 200)
+
+    def test_index_unknown_rev(self):
+        rv = self.app.get('/%s/' % NON_EXISTENT_REV)
+        eq_(rv.status_code, 404)
+
+    def test_index_known_rev(self):
+        rv = self.app.get('/0/')
+        eq_(rv.status_code, 404)
+
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file
--- a/review/web.py	Mon Jan 23 15:01:27 2012 +0100
+++ b/review/web.py	Mon Jul 02 22:32:48 2012 +0200
@@ -231,23 +231,29 @@
     return _render('500.html'), 500
 
 
+def _configure_app(ui, repo, read_only=False, allow_anon=False):
+    """Configure the web app.
+
+    This happens in a distinct function to reuse these steps in tests.
+
+    """
+    app.read_only = read_only
+    app.debug = ui.debugflag
+    app.allow_anon = allow_anon
+    app.site_root = ''
+    if app.allow_anon:
+        ui.setconfig('ui', 'username', 'Anonymous <anonymous@example.com>')
+    app.ui = ui
+    app.repo = repo
+    app.title = os.path.basename(repo.root)
+    app.project_url = None
+
 def load_interface(ui, repo, read_only=False, allow_anon=False,
         open=False, address='127.0.0.1', port=8080):
     if open:
         import webbrowser
         webbrowser.open('http://localhost:%d/' % port)
 
-    app.read_only = read_only
-    app.debug = ui.debugflag
-    app.allow_anon = allow_anon
-    app.site_root = ''
-
-    if app.allow_anon:
-        ui.setconfig('ui', 'username', 'Anonymous <anonymous@example.com>')
-
-    app.ui = ui
-    app.repo = repo
-    app.title = os.path.basename(repo.root)
-    app.project_url = None
+    _configure_app(ui, repo, read_only, allow_anon)
 
     app.run(host=address, port=port)