Skip to content
Snippets Groups Projects
Verified Commit a363d4a1 authored by sistason's avatar sistason
Browse files

started with unittests, but stuck on a mock issue

parent d8b17fcb
No related branches found
No related tags found
No related merge requests found
......@@ -135,3 +135,6 @@ class TestSession(UffdTestCase):
class TestSessionOL(TestSession):
use_openldap = True
class TestSessionOLUser(TestSessionOL):
use_userconnection = True
......@@ -25,6 +25,7 @@ def db_flush():
class UffdTestCase(unittest.TestCase):
use_openldap = False
use_userconnection = False
def setUp(self):
self.dir = tempfile.mkdtemp()
......@@ -43,6 +44,9 @@ class UffdTestCase(unittest.TestCase):
self.skipTest('OPENLDAP_TESTING not set')
config['LDAP_SERVICE_MOCK'] = False
config['LDAP_SERVICE_URL'] = 'ldap://localhost'
if self.use_userconnection:
config['LDAP_SERVICE_BIND_DN'] = None
else:
config['LDAP_SERVICE_BIND_DN'] = 'cn=uffd,ou=system,dc=example,dc=com'
config['LDAP_SERVICE_BIND_PASSWORD'] = 'uffd-ldap-password'
os.system("ldapdelete -c -D 'cn=uffd,ou=system,dc=example,dc=com' -w 'uffd-ldap-password' -H 'ldap://localhost' -f ldap_server_entries_cleanup.ldif > /dev/null 2>&1")
......
from flask import current_app, request, abort, session
import ldap3
from ldap3.core.exceptions import LDAPBindError, LDAPPasswordIsMandatoryError
from ldap3.core.exceptions import LDAPBindError, LDAPPasswordIsMandatoryError, LDAPInvalidDnError
from ldapalchemy import LDAPMapper, LDAPCommitError # pylint: disable=unused-import
from ldapalchemy.model import Query
......@@ -42,8 +42,9 @@ def test_user_bind(bind_dn, bind_pw):
return False
conn.search(conn.user, encode_filter(current_app.config["LDAP_USER_SEARCH_FILTER"]))
#conn.unbind()
return len(conn.entries) == 1
lazy_entries = conn.entries
conn.unbind()
return len(lazy_entries) == 1
def connect_and_bind_to_ldap(server, bind_dn, bind_pw):
......
......@@ -5,7 +5,7 @@ import functools
from flask import Blueprint, render_template, request, url_for, redirect, flash, current_app, session, abort
from uffd.user.models import User
from uffd.ldap import ldap, test_user_bind
from uffd.ldap import ldap, test_user_bind, LDAPInvalidDnError
from uffd.ratelimit import Ratelimit, host_ratelimit, format_delay
bp = Blueprint("session", __name__, template_folder='templates', url_prefix='/')
......@@ -16,7 +16,7 @@ def login_get_user(loginname, password):
dn = User(loginname=loginname).dn
# If we use a service connection, test user bind seperately
if current_app.config['LDAP_SERVICE_BIND_DN']:
if current_app.config['LDAP_SERVICE_BIND_DN'] or current_app.config.get('LDAP_SERVICE_MOCK', False):
if not test_user_bind(dn, password):
return None
# If we use a user connection, just create the connection normally
......@@ -25,9 +25,15 @@ def login_get_user(loginname, password):
session['user_dn'] = dn
session['user_pw'] = password
if not ldap.get_connection():
session.clear()
return None
return User.query.get(dn)
try:
user = User.query.get(dn)
if user:
return user
except LDAPInvalidDnError:
return None
@bp.route("/logout")
def logout():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment