changeset 5903:a384ac02f8ac

tests: add basic test for existing user update from LDAP
author Konstantin Veretennicov <kveretennicov@gmail.com>
date Sun, 01 May 2016 23:29:09 +0200
parents 7d23f2491ce8
children a620c2c49cfe
files kallithea/tests/other/test_auth_ldap.py
diffstat 1 files changed, 64 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/tests/other/test_auth_ldap.py	Sun May 01 23:29:09 2016 +0200
@@ -0,0 +1,64 @@
+from kallithea.lib.auth_modules import auth_ldap, authenticate
+from kallithea.model.db import Setting, User
+from kallithea.model.meta import Session
+import uuid
+import pytest
+
+
+@pytest.fixture
+def arrange_ldap_auth(set_test_settings):
+    set_test_settings(
+        ('auth_plugins', 'kallithea.lib.auth_modules.auth_ldap', 'list'),
+        ('auth_ldap_enabled', True, 'bool'),
+        ('auth_ldap_attr_firstname', 'test_ldap_firstname'),
+        ('auth_ldap_attr_lastname', 'test_ldap_lastname'),
+        ('auth_ldap_attr_email', 'test_ldap_email'))
+
+
+class _AuthLdapMock():
+
+    def __init__(self, **kwargs):
+        pass
+
+    def authenticate_ldap(self, username, password):
+        return 'spam dn', dict(test_ldap_firstname=['spam ldap first name'],
+                               test_ldap_lastname=['spam ldap last name'],
+                               test_ldap_email=['spam ldap email'])
+
+
+def test_update_user_attributes_from_ldap(monkeypatch, create_test_user,
+                                          arrange_ldap_auth):
+    """Authenticate user with mocked LDAP, verify attributes are updated.
+    """
+
+    # Arrange test user.
+    uniqifier = uuid.uuid4()
+    username = 'test-user-{}'.format(uniqifier)
+    assert User.get_by_username(username) is None
+    user_input = dict(username='test-user-{}'.format(uniqifier),
+                      password='spam password',
+                      email='spam-email-{}'.format(uniqifier),
+                      firstname='spam first name',
+                      lastname='spam last name',
+                      active=True,
+                      admin=False)
+    user = create_test_user(user_input)
+
+    # Arrange LDAP auth.
+    monkeypatch.setattr(auth_ldap, 'AuthLdap', _AuthLdapMock)
+
+    # Authenticate with LDAP.
+    user_data = authenticate(username, 'password')
+
+    # Verify that authenication succeeded and retrieved correct attributes
+    # from LDAP.
+    assert user_data is not None
+    assert user_data.get('firstname') == 'spam ldap first name'
+    assert user_data.get('lastname') == 'spam ldap last name'
+    assert user_data.get('email') == 'spam ldap email'
+
+    # Verify that authentication overwrote user attributes with the ones
+    # retrieved from LDAP.
+    assert user.firstname == 'spam ldap first name'
+    assert user.lastname == 'spam ldap last name'
+    assert user.email == 'spam ldap email'