test_root.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. """
  3. Functional test suite for the root controller.
  4. This is an example of how functional tests can be written for controllers.
  5. As opposed to a unit-test, which test a small unit of functionality,
  6. functional tests exercise the whole application and its WSGI stack.
  7. Please read http://pythonpaste.org/webtest/ for more information.
  8. """
  9. from nose.tools import ok_
  10. from pod.tests import TestController
  11. class TestRootController(TestController):
  12. """Tests for the method in the root controller."""
  13. def test_index(self):
  14. """The front page is working properly"""
  15. response = self.app.get('/')
  16. msg = 'TurboGears 2 is rapid web application development toolkit '\
  17. 'designed to make your life easier.'
  18. # You can look for specific strings:
  19. ok_(msg in response)
  20. # You can also access a BeautifulSoup'ed response in your tests
  21. # (First run $ easy_install BeautifulSoup
  22. # and then uncomment the next two lines)
  23. # links = response.html.findAll('a')
  24. # print links
  25. # ok_(links, "Mummy, there are no links here!")
  26. def test_environ(self):
  27. """Displaying the wsgi environ works"""
  28. response = self.app.get('/environ.html')
  29. ok_('The keys in the environment are: ' in response)
  30. def test_data(self):
  31. """The data display demo works with HTML"""
  32. response = self.app.get('/data.html?a=1&b=2')
  33. expected1 = """<td>a</td>\n <td>1</td>"""
  34. expected2 = """<td>b</td>\n <td>2</td>"""
  35. body = '\n'.join(response.text.splitlines())
  36. ok_(expected1 in body, response)
  37. ok_(expected2 in body, response)
  38. def test_data_json(self):
  39. """The data display demo works with JSON"""
  40. resp = self.app.get('/data.json?a=1&b=2')
  41. ok_(dict(page='data', params={'a':'1', 'b':'2'}) == resp.json, resp.json)
  42. def test_secc_with_manager(self):
  43. """The manager can access the secure controller"""
  44. # Note how authentication is forged:
  45. environ = {'REMOTE_USER': 'manager'}
  46. resp = self.app.get('/secc', extra_environ=environ, status=200)
  47. ok_('Secure Controller here' in resp.text, resp.text)
  48. def test_secc_with_editor(self):
  49. """The editor cannot access the secure controller"""
  50. environ = {'REMOTE_USER': 'editor'}
  51. self.app.get('/secc', extra_environ=environ, status=403)
  52. # It's enough to know that authorization was denied with a 403 status
  53. def test_secc_with_anonymous(self):
  54. """Anonymous users must not access the secure controller"""
  55. self.app.get('/secc', status=401)
  56. # It's enough to know that authorization was denied with a 401 status