test_ldap.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import pkg_resources
  17. import pytest
  18. import six
  19. try:
  20. import mock
  21. except ImportError:
  22. import unittest.mock as mock
  23. import six.moves.urllib.parse as urlparse
  24. from mediagoblin import mg_globals
  25. from mediagoblin.db.base import Session
  26. from mediagoblin.tests.tools import get_app
  27. from mediagoblin.tools import template
  28. pytest.importorskip("ldap")
  29. @pytest.fixture()
  30. def ldap_plugin_app(request):
  31. return get_app(
  32. request,
  33. mgoblin_config=pkg_resources.resource_filename(
  34. 'mediagoblin.tests.auth_configs',
  35. 'ldap_appconfig.ini'))
  36. def return_value():
  37. return u'chris', u'chris@example.com'
  38. def test_ldap_plugin(ldap_plugin_app):
  39. res = ldap_plugin_app.get('/auth/login/')
  40. assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/login/'
  41. res = ldap_plugin_app.get('/auth/register/')
  42. assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/register/'
  43. res = ldap_plugin_app.get('/auth/ldap/register/')
  44. assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/login/'
  45. template.clear_test_template_context()
  46. res = ldap_plugin_app.post(
  47. '/auth/ldap/login/', {})
  48. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html']
  49. form = context['login_form']
  50. assert form.username.errors == [u'This field is required.']
  51. assert form.password.errors == [u'This field is required.']
  52. @mock.patch('mediagoblin.plugins.ldap.tools.LDAP.login',
  53. mock.Mock(return_value=return_value()))
  54. def _test_authentication():
  55. template.clear_test_template_context()
  56. res = ldap_plugin_app.post(
  57. '/auth/ldap/login/',
  58. {'username': u'chris',
  59. 'password': u'toast'})
  60. context = template.TEMPLATE_TEST_CONTEXT[
  61. 'mediagoblin/auth/register.html']
  62. register_form = context['register_form']
  63. assert register_form.username.data == u'chris'
  64. assert register_form.email.data == u'chris@example.com'
  65. template.clear_test_template_context()
  66. res = ldap_plugin_app.post(
  67. '/auth/ldap/register/',
  68. {'username': u'chris',
  69. 'email': u'chris@example.com'})
  70. res.follow()
  71. assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
  72. assert 'mediagoblin/user_pages/user_nonactive.html' in \
  73. template.TEMPLATE_TEST_CONTEXT
  74. # Try to register with same email and username
  75. template.clear_test_template_context()
  76. res = ldap_plugin_app.post(
  77. '/auth/ldap/register/',
  78. {'username': u'chris',
  79. 'email': u'chris@example.com'})
  80. context = template.TEMPLATE_TEST_CONTEXT[
  81. 'mediagoblin/auth/register.html']
  82. register_form = context['register_form']
  83. assert register_form.email.errors == [
  84. u'Sorry, a user with that email address already exists.']
  85. assert register_form.username.errors == [
  86. u'Sorry, a user with that name already exists.']
  87. # Log out
  88. ldap_plugin_app.get('/auth/logout/')
  89. # Get user and detach from session
  90. test_user = mg_globals.database.User.query.filter_by(
  91. username=u'chris').first()
  92. Session.expunge(test_user)
  93. # Log back in
  94. template.clear_test_template_context()
  95. res = ldap_plugin_app.post(
  96. '/auth/ldap/login/',
  97. {'username': u'chris',
  98. 'password': u'toast'})
  99. res.follow()
  100. assert urlparse.urlsplit(res.location)[2] == '/'
  101. assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
  102. # Make sure user is in the session
  103. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
  104. session = context['request'].session
  105. assert session['user_id'] == six.text_type(test_user.id)
  106. _test_authentication()