test_authentication.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for the :mod:`repoze.who`-powered authentication sub-system.
  4. As pboard grows and the authentication method changes, only these tests
  5. should be updated.
  6. """
  7. from __future__ import unicode_literals
  8. from nose.tools import eq_, ok_
  9. from pboard.tests import TestController
  10. class TestAuthentication(TestController):
  11. """
  12. Tests for the default authentication setup.
  13. If your application changes how the authentication layer is configured
  14. those tests should be updated accordingly
  15. """
  16. application_under_test = 'main'
  17. def test_forced_login(self):
  18. """Anonymous users are forced to login
  19. Test that anonymous users are automatically redirected to the login
  20. form when authorization is denied. Next, upon successful login they
  21. should be redirected to the initially requested page.
  22. """
  23. # Requesting a protected area
  24. resp = self.app.get('/secc/', status=302)
  25. ok_( resp.location.startswith('http://localhost/login'))
  26. # Getting the login form:
  27. resp = resp.follow(status=200)
  28. form = resp.form
  29. # Submitting the login form:
  30. form['login'] = 'manager'
  31. form['password'] = 'managepass'
  32. post_login = form.submit(status=302)
  33. # Being redirected to the initially requested page:
  34. ok_(post_login.location.startswith('http://localhost/post_login'))
  35. initial_page = post_login.follow(status=302)
  36. ok_('authtkt' in initial_page.request.cookies,
  37. "Session cookie wasn't defined: %s" % initial_page.request.cookies)
  38. ok_(initial_page.location.startswith('http://localhost/secc/'),
  39. initial_page.location)
  40. def test_voluntary_login(self):
  41. """Voluntary logins must work correctly"""
  42. # Going to the login form voluntarily:
  43. resp = self.app.get('/login', status=200)
  44. form = resp.form
  45. # Submitting the login form:
  46. form['login'] = 'manager'
  47. form['password'] = 'managepass'
  48. post_login = form.submit(status=302)
  49. # Being redirected to the home page:
  50. ok_(post_login.location.startswith('http://localhost/post_login'))
  51. home_page = post_login.follow(status=302)
  52. ok_('authtkt' in home_page.request.cookies,
  53. 'Session cookie was not defined: %s' % home_page.request.cookies)
  54. eq_(home_page.location, 'http://localhost/')
  55. def test_logout(self):
  56. """Logouts must work correctly"""
  57. # Logging in voluntarily the quick way:
  58. resp = self.app.get('/login_handler?login=manager&password=managepass',
  59. status=302)
  60. resp = resp.follow(status=302)
  61. ok_('authtkt' in resp.request.cookies,
  62. 'Session cookie was not defined: %s' % resp.request.cookies)
  63. # Logging out:
  64. resp = self.app.get('/logout_handler', status=302)
  65. ok_(resp.location.startswith('http://localhost/post_logout'))
  66. # Finally, redirected to the home page:
  67. home_page = resp.follow(status=302)
  68. authtkt = home_page.request.cookies.get('authtkt')
  69. ok_(not authtkt or authtkt == 'INVALID',
  70. 'Session cookie was not deleted: %s' % home_page.request.cookies)
  71. eq_(home_page.location, 'http://localhost/')