From b1e7f1a96a67e4df3d70d02eab641745d6e4048c Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Fri, 17 Jun 2022 11:54:36 +0200
Subject: [PATCH 2/2] ldap: display server error on technical info backoffice
 page (#65491)

---
 .../authentic2/manager/ldap_details.html          |  7 +++++--
 src/authentic2/manager/views.py                   | 15 ++++++++-------
 tests/test_ldap.py                                |  3 ++-
 3 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/src/authentic2/manager/templates/authentic2/manager/ldap_details.html b/src/authentic2/manager/templates/authentic2/manager/ldap_details.html
index 97cd75358..470115fda 100644
--- a/src/authentic2/manager/templates/authentic2/manager/ldap_details.html
+++ b/src/authentic2/manager/templates/authentic2/manager/ldap_details.html
@@ -1,12 +1,15 @@
 {% load i18n %}
 <h4>{% trans "Realm:" %} {{ ldap.realm }}</h4>
 <div class="a2-manager-ldap-{{ ldap.realm }}">
-{% if not error %}
+{% if not ldap.error %}
 <h5>{% blocktrans %}Base ldapsearch command:{% endblocktrans %}</h5>
  <pre>ldapsearch -v -H {{ ldap.ldap_uri }} -D "{{ ldap.binddn }}" -w "{{ ldap.bindpw }}" -b "{{ ldap.basedn }}"{% if ldap.user_filter or ldap.sync_ldap_users_filter %} "{% firstof ldap.sync_ldap_users_filter ldap.user_filter %}"{% endif %}</pre>
 {% else %}
 <div class="error">
-{% blocktrans %}Error while attempting to connect to LDAP server, base ldapsearch command won't be displayed.{% endblocktrans %}
+<p>{% blocktrans %}Error while attempting to connect to LDAP server, base ldapsearch command won't be displayed.{% endblocktrans %}</p>
+{% if ldap.errmsg %}
+<p>{% blocktrans %}Server error: {{ errmsg }}{% endblocktrans %}</p>
+{% endif %}
 </div>
 {% endif %}
 <h5>{% trans "Configuration:" %}</h5>
diff --git a/src/authentic2/manager/views.py b/src/authentic2/manager/views.py
index 103bad555..ac4cbbf95 100644
--- a/src/authentic2/manager/views.py
+++ b/src/authentic2/manager/views.py
@@ -675,20 +675,21 @@ class TechnicalInformationView(TitleMixin, MediaMixin, TemplateView):
         backend = ldap_backend.LDAPBackend
         kwargs['ldap_list'] = []
         for block in backend.get_config():
-            config = block.copy()
-            conn, dummy = backend.get_connection(config)
+            ldap_info = block.copy()
+            conn, errmsg = backend.get_connection(ldap_info)
             if not conn:
-                kwargs['error'] = True
+                ldap_info['error'] = True
+                ldap_info['errmsg'] = errmsg
             else:
                 # retrieve ldap uri, not directly visible in configuration block
-                config['ldap_uri'] = conn.get_option(ldap.OPT_URI)
+                ldap_info['ldap_uri'] = conn.get_option(ldap.OPT_URI)
                 # user filters need to be formatted to ldapsearch syntax
-                config['user_filter'] = force_text(block.get('user_filter'), '').replace('%s', '*')
-                config['sync_ldap_users_filter'] = (
+                ldap_info['user_filter'] = force_text(block.get('user_filter'), '').replace('%s', '*')
+                ldap_info['sync_ldap_users_filter'] = (
                     force_text(block.get('sync_ldap_users_filter'), '').replace('%s', '*').replace('%s', '*')
                 )
 
-            kwargs['ldap_list'].append(config)
+            kwargs['ldap_list'].append(ldap_info)
         return super().get_context_data(**kwargs)
 
 
diff --git a/tests/test_ldap.py b/tests/test_ldap.py
index 6d7c0c1de..71aa007e4 100644
--- a/tests/test_ldap.py
+++ b/tests/test_ldap.py
@@ -2289,12 +2289,13 @@ def test_technical_info_ldap(app, admin, superuser, slapd, settings, monkeypatch
         assert opt in ldap_config_text
 
     # mock a buggy connection
-    monkeypatch.setattr(ldap_backend.LDAPBackend, 'get_connection', lambda x: (None, None))
+    monkeypatch.setattr(ldap_backend.LDAPBackend, 'get_connection', lambda x: (None, 'some buggy connection error message'))
     resp = app.get(reverse('a2-manager-tech-info'))
     ldap_config_text = resp.pyquery('div#a2-manager-tech-info-ldap-list').text()
 
     assert 'Base ldapsearch command' not in ldap_config_text
     assert 'Error while attempting to connect to LDAP server' in ldap_config_text
+    assert 'some buggy connection error message' in ldap_config_text
 
 
 class TestLookup:
-- 
2.36.1

