cubicweb/server/test/unittest_ldapsource.py
author Philippe Pepiot <philippe.pepiot@logilab.fr>
Fri, 24 May 2019 16:29:14 +0200
changeset 12626 32ee89340e59
parent 12567 26744ad37953
parent 12618 3f125fdbcd70
child 12896 d18bd998852c
permissions -rw-r--r--
Merge 3.26
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11764
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5064
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
10911
cff2dbc33eff [server/test] document expected packages to run ldap tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10908
diff changeset
    18
"""cubicweb.server.sources.ldapfeed unit and functional tests
cff2dbc33eff [server/test] document expected packages to run ldap tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10908
diff changeset
    19
cff2dbc33eff [server/test] document expected packages to run ldap tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10908
diff changeset
    20
Those tests expect to have slapd, python-ldap3 and ldapscripts packages installed.
cff2dbc33eff [server/test] document expected packages to run ldap tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10908
diff changeset
    21
"""
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11764
diff changeset
    22
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    23
import os
8902
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    24
import sys
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    25
import shutil
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    26
import time
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    27
import subprocess
8681
48731a0d3df8 [ldap test] move the slapd database directory to a tempdir (closes #2583993)
David Douard <david.douard@logilab.fr>
parents: 8680
diff changeset
    28
import tempfile
10950
282880f81311 [test] Skip ldap source tests if slapd is not available
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10912
diff changeset
    29
import unittest
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
    30
from os.path import join
3855
dbd37c10f865 try to update ldap tests to make them work from the DMZ (eg using another ldap directory)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3587
diff changeset
    31
12153
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
    32
from cubicweb import AuthenticationError, ValidationError
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    33
from cubicweb.devtools.testlib import CubicWebTC
6986
18343456ee49 [ldap test] use get_available_port from cw.devtools.httptest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6985
diff changeset
    34
from cubicweb.devtools.httptest import get_available_port
360
600dd2fe8b40 backport unittest_ldapuser
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
600dd2fe8b40 backport unittest_ldapuser
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
    37
CONFIG_LDAPFEED = u'''
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
    38
user-base-dn=ou=People,dc=cubicweb,dc=test
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
    39
group-base-dn=ou=Group,dc=cubicweb,dc=test
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
    40
user-attrs-map=uid=login,mail=email,userPassword=upassword
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
    41
group-attrs-map=cn=name,memberUid=member
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
    42
'''
8901
661b6aaac240 [test/ldap source] better source naming, some cleanups (prepares #2528116)
Jérôme Roy
parents: 8885
diff changeset
    43
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
    44
URL = None
3855
dbd37c10f865 try to update ldap tests to make them work from the DMZ (eg using another ldap directory)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3587
diff changeset
    45
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
    46
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    47
def create_slapd_configuration(cls):
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    48
    global URL
8681
48731a0d3df8 [ldap test] move the slapd database directory to a tempdir (closes #2583993)
David Douard <david.douard@logilab.fr>
parents: 8680
diff changeset
    49
    slapddir = tempfile.mkdtemp('cw-unittest-ldap')
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    50
    config = cls.config
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    51
    slapdconf = join(config.apphome, "slapd.conf")
10614
57dfde80df11 [py3k] file → open
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
    52
    confin = open(join(config.apphome, "slapd.conf.in")).read()
57dfde80df11 [py3k] file → open
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
    53
    confstream = open(slapdconf, 'w')
8681
48731a0d3df8 [ldap test] move the slapd database directory to a tempdir (closes #2583993)
David Douard <david.douard@logilab.fr>
parents: 8680
diff changeset
    54
    confstream.write(confin % {'apphome': config.apphome, 'testdir': slapddir})
6985
dda970c8a315 [test] generate slapd.conf to be cwd independant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6888
diff changeset
    55
    confstream.close()
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
    56
    # fill ldap server with some data
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
    57
    ldiffile = join(config.apphome, "ldap_test.ldif")
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
    58
    config.info('Initing ldap database')
8902
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    59
    cmdline = ['/usr/sbin/slapadd', '-f', slapdconf, '-l', ldiffile, '-c']
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    60
    PIPE = subprocess.PIPE
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    61
    slapproc = subprocess.Popen(cmdline, stdout=PIPE, stderr=PIPE)
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    62
    stdout, stderr = slapproc.communicate()
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    63
    if slapproc.returncode:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9794
diff changeset
    64
        print('slapadd returned with status: %s'
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9794
diff changeset
    65
              % slapproc.returncode, file=sys.stderr)
8902
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    66
        sys.stdout.write(stdout)
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    67
        sys.stderr.write(stderr)
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    68
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
    69
    # ldapuri = 'ldapi://' + join(basedir, "ldapi").replace('/', '%2f')
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    70
    port = get_available_port(range(9000, 9100))
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    71
    host = 'localhost:%s' % port
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    72
    ldapuri = 'ldap://%s' % host
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
    73
    cmdline = ["/usr/sbin/slapd", "-f", slapdconf, "-h", ldapuri, "-d", "0"]
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
    74
    config.info('Starting slapd:', ' '.join(cmdline))
8902
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    75
    PIPE = subprocess.PIPE
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    76
    cls.slapd_process = subprocess.Popen(cmdline, stdout=PIPE, stderr=PIPE)
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    77
    time.sleep(0.2)
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    78
    if cls.slapd_process.poll() is None:
8901
661b6aaac240 [test/ldap source] better source naming, some cleanups (prepares #2528116)
Jérôme Roy
parents: 8885
diff changeset
    79
        config.info('slapd started with pid %s', cls.slapd_process.pid)
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    80
    else:
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    81
        raise EnvironmentError('Cannot start slapd with cmdline="%s" (from directory "%s")' %
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    82
                               (" ".join(cmdline), os.getcwd()))
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
    83
    URL = u'ldap://%s' % host
8681
48731a0d3df8 [ldap test] move the slapd database directory to a tempdir (closes #2583993)
David Douard <david.douard@logilab.fr>
parents: 8680
diff changeset
    84
    return slapddir
6887
72d7feeb071e [tests] make ldap source test run its own local (Open)LDAP server
David Douard <david.douard@logilab.fr>
parents: 6781
diff changeset
    85
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
    86
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    87
def terminate_slapd(cls):
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    88
    config = cls.config
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    89
    if cls.slapd_process and cls.slapd_process.returncode is None:
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
    90
        config.info('terminating slapd')
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    91
        if hasattr(cls.slapd_process, 'terminate'):
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    92
            cls.slapd_process.terminate()
6986
18343456ee49 [ldap test] use get_available_port from cw.devtools.httptest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6985
diff changeset
    93
        else:
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
    94
            import signal
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
    95
            os.kill(cls.slapd_process.pid, signal.SIGTERM)
8902
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    96
        stdout, stderr = cls.slapd_process.communicate()
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
    97
        if cls.slapd_process.returncode:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9794
diff changeset
    98
            print('slapd returned with status: %s'
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9794
diff changeset
    99
                  % cls.slapd_process.returncode, file=sys.stderr)
8902
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
   100
            sys.stdout.write(stdout)
9edfa3660bf4 [ldap] quieter tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8901
diff changeset
   101
            sys.stderr.write(stderr)
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   102
        config.info('DONE')
12222
52f395015ff9 [test] Use ignore_errors=True in shutil.rmtree instead of try/except/pass
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12153
diff changeset
   103
    shutil.rmtree(cls._tmpdir, ignore_errors=True)
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   104
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   105
12136
6069ee7d4824 [server/test] Extract a "ldapsource" function in unittest_ldapsource.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12132
diff changeset
   106
def ldapsource(cnx):
6069ee7d4824 [server/test] Extract a "ldapsource" function in unittest_ldapsource.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12132
diff changeset
   107
    return cnx.find('CWSource', type=u'ldapfeed').one()
6069ee7d4824 [server/test] Extract a "ldapsource" function in unittest_ldapsource.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12132
diff changeset
   108
6069ee7d4824 [server/test] Extract a "ldapsource" function in unittest_ldapsource.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12132
diff changeset
   109
12143
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   110
def update_source_config(source, options):
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   111
    config = source.dictconfig
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   112
    config.update(options)
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   113
    source.cw_set(config=u'\n'.join('%s=%s' % x for x in config.items()))
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   114
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   115
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   116
class LDAPFeedTestBase(CubicWebTC):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   117
    test_db_id = 'ldap-feed'
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   118
    loglevel = 'ERROR'
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   119
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   120
    @classmethod
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   121
    def setUpClass(cls):
11270
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   122
        super(LDAPFeedTestBase, cls).setUpClass()
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   123
        cls.init_slapd()
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   124
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   125
    @classmethod
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   126
    def tearDownClass(cls):
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   127
        cls.terminate_slapd()
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   128
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   129
    @classmethod
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   130
    def init_slapd(cls):
11073
d7e8912549cd [test] Skip LDAP tests if any of the underlying programs is not found
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   131
        for path in ('/usr/sbin/slapd',
d7e8912549cd [test] Skip LDAP tests if any of the underlying programs is not found
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   132
                     '/usr/sbin/slapadd',
d7e8912549cd [test] Skip LDAP tests if any of the underlying programs is not found
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   133
                     '/usr/bin/ldapmodify'):
d7e8912549cd [test] Skip LDAP tests if any of the underlying programs is not found
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   134
            if not os.path.exists(path):
d7e8912549cd [test] Skip LDAP tests if any of the underlying programs is not found
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   135
                raise unittest.SkipTest('%s not found' % path)
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   136
        from cubicweb.cwctl import init_cmdline_log_threshold
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   137
        init_cmdline_log_threshold(cls.config, cls.loglevel)
8681
48731a0d3df8 [ldap test] move the slapd database directory to a tempdir (closes #2583993)
David Douard <david.douard@logilab.fr>
parents: 8680
diff changeset
   138
        cls._tmpdir = create_slapd_configuration(cls)
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   139
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   140
    @classmethod
11270
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   141
    def terminate_slapd(cls):
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8430
diff changeset
   142
        terminate_slapd(cls)
8430
5bee87a14bb1 fix ldap removal handling in ldapfeed (closes #2376625 and #2385133)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8387
diff changeset
   143
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   144
    @classmethod
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   145
    def pre_setup_database(cls, cnx, config):
12523
4d68d20427de Skip tests for ldapsource with python >= 3.7
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12222
diff changeset
   146
        if sys.version_info[:2] >= (3, 7):
4d68d20427de Skip tests for ldapsource with python >= 3.7
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12222
diff changeset
   147
            raise unittest.SkipTest(
4d68d20427de Skip tests for ldapsource with python >= 3.7
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12222
diff changeset
   148
                'ldapfeed is not currently compatible with Python 3.7')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   149
        cnx.create_entity('CWSource', name=u'ldap', type=u'ldapfeed', parser=u'ldapfeed',
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   150
                          url=URL, config=CONFIG_LDAPFEED)
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   151
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   152
        cnx.commit()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   153
        return cls.pull(cnx)
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   154
12132
be2c14ea2736 [server/test] Turn "pull" method of LDAPFeedTestBase into a staticmethod
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12058
diff changeset
   155
    @staticmethod
be2c14ea2736 [server/test] Turn "pull" method of LDAPFeedTestBase into a staticmethod
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12058
diff changeset
   156
    def pull(cnx):
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   157
        lfsource = cnx.repo.source_by_uri('ldap')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   158
        stats = lfsource.pull_data(cnx, force=True, raise_on_error=True)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   159
        cnx.commit()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   160
        return stats
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   161
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   162
    def setup_database(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   163
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   164
            cnx.execute('DELETE Any E WHERE E cw_source S, S name "ldap"')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   165
            cnx.execute('SET S config %(conf)s, S url %(url)s '
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   166
                        'WHERE S is CWSource, S name "ldap"',
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   167
                        {"conf": CONFIG_LDAPFEED, 'url': URL})
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   168
            cnx.commit()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   169
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   170
            self.pull(cnx)
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   171
9142
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   172
    def add_ldap_entry(self, dn, mods):
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   173
        """
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   174
        add an LDAP entity
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   175
        """
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   176
        modcmd = ['dn: %s' % dn, 'changetype: add']
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10651
diff changeset
   177
        for key, values in mods.items():
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12523
diff changeset
   178
            if isinstance(values, str):
9142
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   179
                values = [values]
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   180
            for value in values:
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   181
                modcmd.append('%s: %s' % (key, value))
9142
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   182
        self._ldapmodify(modcmd)
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   183
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   184
    def delete_ldap_entry(self, dn):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   185
        """
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   186
        delete an LDAP entity
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   187
        """
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   188
        modcmd = ['dn: %s' % dn, 'changetype: delete']
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   189
        self._ldapmodify(modcmd)
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   190
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   191
    def update_ldap_entry(self, dn, mods):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   192
        """
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   193
        modify one or more attributes of an LDAP entity
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   194
        """
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   195
        modcmd = ['dn: %s' % dn, 'changetype: modify']
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10651
diff changeset
   196
        for (kind, key), values in mods.items():
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   197
            modcmd.append('%s: %s' % (kind, key))
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12523
diff changeset
   198
            if isinstance(values, str):
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   199
                values = [values]
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   200
            for value in values:
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   201
                modcmd.append('%s: %s' % (key, value))
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   202
            modcmd.append('-')
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   203
        self._ldapmodify(modcmd)
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   204
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   205
    def _ldapmodify(self, modcmd):
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   206
        uri = self.repo.source_by_uri('ldap').urls[0]
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   207
        updatecmd = ['ldapmodify', '-H', uri, '-v', '-x', '-D',
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   208
                     'cn=admin,dc=cubicweb,dc=test', '-w', 'cw']
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   209
        PIPE = subprocess.PIPE
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   210
        p = subprocess.Popen(updatecmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
10768
99689a5862ea [py3k] make ldapfeed tests pass
Julien Cristau <julien.cristau@logilab.fr>
parents: 10662
diff changeset
   211
        p.stdin.write('\n'.join(modcmd).encode('ascii'))
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   212
        p.stdin.close()
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   213
        if p.wait():
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   214
            raise RuntimeError("ldap update failed: %s" % ('\n'.join(p.stderr.readlines())))
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   215
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   216
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   217
class CheckWrongGroup(LDAPFeedTestBase):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   218
    """
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   219
    A testcase for situations where the default group for CWUser
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   220
    created from LDAP is wrongly configured.
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   221
    """
8679
cf4dacc80976 [ldapfeed] don't crash if one specify an unexisting group in the configuration. Closes #2538399
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8639
diff changeset
   222
cf4dacc80976 [ldapfeed] don't crash if one specify an unexisting group in the configuration. Closes #2538399
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8639
diff changeset
   223
    def test_wrong_group(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   224
        with self.admin_access.repo_cnx() as cnx:
12136
6069ee7d4824 [server/test] Extract a "ldapsource" function in unittest_ldapsource.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12132
diff changeset
   225
            source = ldapsource(cnx)
8679
cf4dacc80976 [ldapfeed] don't crash if one specify an unexisting group in the configuration. Closes #2538399
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8639
diff changeset
   226
            # inject a bogus group here, along with at least a valid one
12151
569dce882f60 [test] Fix option name in ldap wrong group test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12146
diff changeset
   227
            options = {'user-default-group': 'thisgroupdoesnotexists,users'}
12143
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   228
            update_source_config(source, options)
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   229
            cnx.commit()
8679
cf4dacc80976 [ldapfeed] don't crash if one specify an unexisting group in the configuration. Closes #2538399
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8639
diff changeset
   230
            # here we emitted an error log entry
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   231
            source.repo_source.pull_data(cnx, force=True, raise_on_error=True)
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   232
            cnx.commit()
8679
cf4dacc80976 [ldapfeed] don't crash if one specify an unexisting group in the configuration. Closes #2538399
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8639
diff changeset
   233
8901
661b6aaac240 [test/ldap source] better source naming, some cleanups (prepares #2528116)
Jérôme Roy
parents: 8885
diff changeset
   234
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   235
class LDAPFeedUserTC(LDAPFeedTestBase):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   236
    """
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   237
    A testcase for CWUser support in ldapfeed (basic tests and authentication).
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   238
    """
8684
6c7c2a02c9a0 [ldap test] fix bad merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8683
diff changeset
   239
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   240
    def assertMetadata(self, entity):
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   241
        self.assertTrue(entity.creation_date)
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   242
        self.assertTrue(entity.modification_date)
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   243
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   244
    def test_authenticate(self):
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   245
        source = self.repo.source_by_uri('ldap')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   246
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   247
            # ensure we won't be logged against
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   248
            self.assertRaises(AuthenticationError,
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   249
                              source.authenticate, cnx, 'toto', 'toto')
11262
25f9a76ddf50 [test] add a test to ensure we get an AuthenticationError with ldapfeed
David Douard <david.douard@logilab.fr>
parents: 10950
diff changeset
   250
            self.assertRaises(AuthenticationError,
25f9a76ddf50 [test] add a test to ensure we get an AuthenticationError with ldapfeed
David Douard <david.douard@logilab.fr>
parents: 10950
diff changeset
   251
                              source.authenticate, cnx, 'syt', 'toto')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   252
            self.assertTrue(source.authenticate(cnx, 'syt', 'syt'))
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   253
12618
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   254
    def test_ldapfeed_insert_collision(self):
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   255
        """
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   256
        when a user computed login from ldapfeed collides with a CWUser
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   257
        login the user MUST not be inserted, and message MUST be present
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   258
        at error level regarding the collision for troubleshooting purpose.
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   259
        We also check that in case the CWUser is skipped, the entity EmailAddress
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   260
        is not modified.
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   261
        If EmailAddress are not modified, CWGroup are not.
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   262
        """
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   263
        with self.admin_access.cnx() as cnx:
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   264
            user = cnx.find('CWUser', login=u'syt').one()
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   265
            user.cw_set(cw_source=cnx.find('CWSource', name=u'system').one())
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   266
            with cnx.security_enabled(write=False):
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   267
                user.cw_set(cwuri=u'http://testing.fr/cubicweb/{}'.format(user.eid))
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   268
                for mail in user.use_email:
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   269
                    mail.cw_set(address=mail.address[:-3] + u".net")
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   270
            cnx.commit()
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   271
            with self.assertLogs('cubicweb.appobject', level='ERROR') as cm:
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   272
                self.pull(cnx)
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   273
                self.assertEqual(
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   274
                    cm.output,
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   275
                    ['ERROR:cubicweb.appobject:not synchronizing user syt.'
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   276
                     ' User already exist in source system']
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   277
                )
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   278
            for mail in user.use_email:
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   279
                self.assertTrue(mail.address.endswith(".net"))
3f125fdbcd70 [ldapfeed] FIX: Unique Key violation when synchronizing with LDAPfeed
julien tayon <julien.tayon@logilab.fr>
parents: 12523
diff changeset
   280
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   281
    def test_base(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   282
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   283
            # check a known one
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   284
            rset = cnx.execute('CWUser X WHERE X login %(login)s', {'login': 'syt'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   285
            e = rset.get_entity(0, 0)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   286
            self.assertEqual(e.login, 'syt')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   287
            e.complete()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   288
            self.assertMetadata(e)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   289
            self.assertEqual(e.firstname, None)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   290
            self.assertEqual(e.surname, None)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   291
            self.assertIn('users', set(g.name for g in e.in_group))
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   292
            self.assertEqual(e.owned_by[0].login, 'syt')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   293
            self.assertEqual(e.created_by, ())
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   294
            addresses = [pe.address for pe in e.use_email]
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   295
            addresses.sort()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   296
            self.assertEqual(['sylvain.thenault@logilab.fr', 'syt@logilab.fr'],
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   297
                             addresses)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   298
            self.assertIn(e.primary_email[0].address, ['sylvain.thenault@logilab.fr',
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   299
                                                       'syt@logilab.fr'])
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   300
            # email content should be indexed on the user
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   301
            rset = cnx.execute('CWUser X WHERE X has_text "thenault"')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   302
            self.assertEqual(rset.rows, [[e.eid]])
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   303
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   304
    def test_copy_to_system_source(self):
8921
da46624a0880 [test/ldap] small improvement to ldapfeed unit tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8917
diff changeset
   305
        "make sure we can 'convert' an LDAP user into a system one"
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   306
        with self.admin_access.repo_cnx() as cnx:
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   307
            source = self.repo.source_by_uri('ldap')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   308
            eid = cnx.execute('CWUser X WHERE X login %(login)s', {'login': 'syt'})[0][0]
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   309
            cnx.execute('SET X cw_source S WHERE X eid %(x)s, S name "system"', {'x': eid})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   310
            cnx.commit()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   311
            rset = cnx.execute('CWUser X WHERE X login %(login)s', {'login': 'syt'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   312
            self.assertEqual(len(rset), 1)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   313
            e = rset.get_entity(0, 0)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   314
            self.assertEqual(e.eid, eid)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   315
            self.assertEqual(e.cw_source[0].name, 'system')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   316
            self.assertTrue(e.creation_date)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   317
            self.assertTrue(e.modification_date)
11755
96ced95e4002 [ldap] Stop using entities table in ldap source authentication and parser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11719
diff changeset
   318
            source.pull_data(cnx, raise_on_error=True)
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   319
            rset = cnx.execute('CWUser X WHERE X login %(login)s', {'login': 'syt'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   320
            self.assertEqual(len(rset), 1)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   321
            self.assertTrue(self.repo.system_source.authenticate(cnx, 'syt', password='syt'))
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   322
            # make sure the pull from ldap have not "reverted" user as a ldap-feed user
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   323
            # and that the password stored in the system source is not empty or so
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   324
            user = cnx.execute('CWUser U WHERE U login "syt"').get_entity(0, 0)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   325
            user.cw_clear_all_caches()
11764
6ab14a1afb65 [entity] Reimplement and deprecate entity.cw_metainformation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11762
diff changeset
   326
            self.assertEqual(user.cw_source[0].name, 'system')
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   327
            cu = cnx.system_sql("SELECT cw_upassword FROM cw_cwuser WHERE cw_login='syt';")
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   328
            pwd = cu.fetchall()[0][0]
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   329
            self.assertIsNotNone(pwd)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   330
            self.assertTrue(str(pwd))
8921
da46624a0880 [test/ldap] small improvement to ldapfeed unit tests
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8917
diff changeset
   331
12153
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   332
    def test_bad_config(self):
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   333
        with self.admin_access.cnx() as cnx:
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   334
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   335
            with self.assertRaises(ValidationError) as cm:
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   336
                cnx.create_entity(
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   337
                    'CWSource', name=u'erroneous', type=u'ldapfeed', parser=u'ldapfeed',
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   338
                    url=u'ldap.com', config=CONFIG_LDAPFEED)
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   339
            self.assertIn('badly formatted url',
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   340
                          str(cm.exception))
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   341
            cnx.rollback()
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   342
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   343
            with self.assertRaises(ValidationError) as cm:
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   344
                cnx.create_entity(
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   345
                    'CWSource', name=u'erroneous', type=u'ldapfeed', parser=u'ldapfeed',
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   346
                    url=u'http://ldap.com', config=CONFIG_LDAPFEED)
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   347
            self.assertIn('unsupported protocol',
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   348
                          str(cm.exception))
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   349
            cnx.rollback()
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   350
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   351
            with self.assertRaises(ValidationError) as cm:
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   352
                cnx.create_entity(
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   353
                    'CWSource', name=u'erroneous', type=u'ldapfeed', parser=u'ldapfeed',
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   354
                    url=u'ldap://host1\nldap://host2', config=CONFIG_LDAPFEED)
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   355
            self.assertIn('can only have one url',
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   356
                          str(cm.exception))
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   357
            cnx.rollback()
0ff0aff4413d [sources] Check source's url attribute value on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12151
diff changeset
   358
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8146
diff changeset
   359
11719
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   360
class LDAPGeneratePwdTC(LDAPFeedTestBase):
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   361
    """
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   362
    A testcase for password generation on CWUser when none is imported
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   363
    """
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   364
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   365
    def setup_database(self):
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   366
        with self.admin_access.repo_cnx() as cnx:
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   367
            lfsource = cnx.repo.source_by_uri('ldap')
11719
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   368
            del lfsource.user_attrs['userPassword']
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   369
        super(LDAPGeneratePwdTC, self).setup_database()
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   370
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   371
    def test_no_password(self):
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   372
        with self.admin_access.repo_cnx() as cnx:
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   373
            cu = cnx.system_sql("SELECT cw_upassword FROM cw_cwuser WHERE cw_login='syt';")
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   374
            pwd = cu.fetchall()[0][0]
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   375
            self.assertTrue(pwd)
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   376
36443c44b9a5 [ldapfeed] Fix crash when user's password has to be generated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11718
diff changeset
   377
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   378
class LDAPFeedUserDeletionTC(LDAPFeedTestBase):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   379
    """
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   380
    A testcase for situations where users are deleted from or
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   381
    unavailable in the LDAP database.
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   382
    """
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   383
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   384
    def test_a_filter_inactivate(self):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   385
        """ filtered out people should be deactivated, unable to authenticate """
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   386
        with self.admin_access.repo_cnx() as cnx:
12136
6069ee7d4824 [server/test] Extract a "ldapsource" function in unittest_ldapsource.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12132
diff changeset
   387
            source = ldapsource(cnx)
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   388
            # filter with adim's phone number
12143
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   389
            options = {'user-filter': '(%s=%s)' % ('telephonenumber', '109')}
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   390
            update_source_config(source, options)
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   391
            cnx.commit()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   392
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   393
            self.pull(cnx)
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   394
            repo_source = self.repo.source_by_uri('ldap')
12044
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   395
            self.assertRaises(AuthenticationError,
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   396
                              repo_source.authenticate, cnx, 'syt', 'syt')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   397
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   398
            self.assertEqual(cnx.execute('Any N WHERE U login "syt", '
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   399
                                         'U in_state S, S name N').rows[0][0],
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   400
                             'deactivated')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   401
            self.assertEqual(cnx.execute('Any N WHERE U login "adim", '
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   402
                                         'U in_state S, S name N').rows[0][0],
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   403
                             'activated')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   404
            # unfilter, syt should be activated again
12143
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   405
            source = ldapsource(cnx)
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   406
            options = {'user-filter': u''}
a446124bcf3c [server] Drop update_config method of source
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12136
diff changeset
   407
            update_source_config(source, options)
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   408
            cnx.commit()
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   409
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   410
            self.pull(cnx)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   411
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   412
            self.assertEqual(cnx.execute('Any N WHERE U login "syt", '
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   413
                                         'U in_state S, S name N').rows[0][0],
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   414
                             'activated')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   415
            self.assertEqual(cnx.execute('Any N WHERE U login "adim", '
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   416
                                         'U in_state S, S name N').rows[0][0],
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   417
                             'activated')
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   418
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   419
    def test_delete(self):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   420
        """ delete syt, pull, check deactivation, repull,
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   421
        read syt, pull, check activation
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   422
        """
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   423
        self.delete_ldap_entry('uid=syt,ou=People,dc=cubicweb,dc=test')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   424
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   425
            self.pull(cnx)
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   426
            source = self.repo.source_by_uri('ldap')
12044
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   427
            self.assertRaises(AuthenticationError,
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   428
                              source.authenticate, cnx, 'syt', 'syt')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   429
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   430
            self.assertEqual(cnx.execute('Any N WHERE U login "syt", '
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   431
                                         'U in_state S, S name N').rows[0][0],
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   432
                             'deactivated')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   433
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   434
            # check that it doesn't choke
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   435
            self.pull(cnx)
9142
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   436
        # reinsert syt
41fb930dc751 [test/ldap] fix ldap tests
David Douard <david.douard@logilab.fr>
parents: 8959
diff changeset
   437
        self.add_ldap_entry('uid=syt,ou=People,dc=cubicweb,dc=test',
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   438
                            {'objectClass': ['OpenLDAPperson', 'posixAccount', 'top',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   439
                                             'shadowAccount'],
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   440
                             'cn': 'Sylvain Thenault',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   441
                             'sn': 'Thenault',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   442
                             'gidNumber': '1004',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   443
                             'uid': 'syt',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   444
                             'homeDirectory': '/home/syt',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   445
                             'shadowFlag': '134538764',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   446
                             'uidNumber': '1004',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   447
                             'givenName': 'Sylvain',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   448
                             'telephoneNumber': '106',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   449
                             'displayName': 'sthenault',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   450
                             'gecos': 'Sylvain Thenault',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   451
                             'mail': ['sylvain.thenault@logilab.fr', 'syt@logilab.fr'],
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   452
                             'userPassword': 'syt',
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   453
                             })
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   454
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   455
            self.pull(cnx)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   456
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   457
            self.assertEqual(cnx.execute('Any N WHERE U login "syt", '
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   458
                                         'U in_state S, S name N').rows[0][0],
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   459
                             'activated')
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   460
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   461
    def test_reactivate_deleted(self):
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   462
        # test reactivating BY HAND the user isn't enough to
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   463
        # authenticate, as the native source refuse to authenticate
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   464
        # user from other sources
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12143
diff changeset
   465
        repo_source = self.repo.source_by_uri('ldap')
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   466
        self.delete_ldap_entry('uid=syt,ou=People,dc=cubicweb,dc=test')
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   467
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   468
            self.pull(cnx)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   469
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   470
            # reactivate user (which source is still ldap-feed)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   471
            user = cnx.execute('CWUser U WHERE U login "syt"').get_entity(0, 0)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   472
            user.cw_adapt_to('IWorkflowable').fire_transition('activate')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   473
            cnx.commit()
12044
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   474
            self.assertRaises(AuthenticationError,
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   475
                              repo_source.authenticate, cnx, 'syt', 'syt')
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   476
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   477
            # ok now let's try to make it a system user
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   478
            cnx.execute('SET X cw_source S WHERE X eid %(x)s, S name "system"', {'x': user.eid})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   479
            cnx.commit()
12044
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   480
            # and that we can now authenticate again
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   481
            self.assertRaises(AuthenticationError,
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   482
                              repo_source.authenticate, cnx, 'syt', 'toto')
70bb46dfa87b [repo] Drop repo.new_session method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12042
diff changeset
   483
            self.assertTrue(self.repo.authenticate_user(cnx, 'syt', password='syt'))
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   484
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   485
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   486
class LDAPFeedGroupTC(LDAPFeedTestBase):
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   487
    """
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   488
    A testcase for group support in ldapfeed.
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   489
    """
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   490
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   491
    def test_groups_exist(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   492
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   493
            rset = cnx.execute('CWGroup X WHERE X name "dir"')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   494
            self.assertEqual(len(rset), 1)
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   495
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   496
            rset = cnx.execute('CWGroup X WHERE X cw_source S, S name "ldap"')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   497
            self.assertEqual(len(rset), 2)
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   498
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   499
    def test_group_deleted(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   500
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   501
            rset = cnx.execute('CWGroup X WHERE X name "dir"')
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   502
            self.assertEqual(len(rset), 1)
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   503
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   504
    def test_in_group(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   505
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   506
            rset = cnx.execute('CWGroup X WHERE X name %(name)s', {'name': 'dir'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   507
            dirgroup = rset.get_entity(0, 0)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   508
            self.assertEqual(set(['syt', 'adim']),
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   509
                             set([u.login for u in dirgroup.reverse_in_group]))
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   510
            rset = cnx.execute('CWGroup X WHERE X name %(name)s', {'name': 'logilab'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   511
            logilabgroup = rset.get_entity(0, 0)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   512
            self.assertEqual(set(['adim']),
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   513
                             set([u.login for u in logilabgroup.reverse_in_group]))
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   514
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   515
    def test_group_member_added(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   516
        with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   517
            self.pull(cnx)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   518
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   519
            rset = cnx.execute('Any L WHERE U in_group G, G name %(name)s, U login L',
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   520
                               {'name': 'logilab'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   521
            self.assertEqual(len(rset), 1)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   522
            self.assertEqual(rset[0][0], 'adim')
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   523
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   524
        try:
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   525
            self.update_ldap_entry('cn=logilab,ou=Group,dc=cubicweb,dc=test',
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   526
                                   {('add', 'memberUid'): ['syt']})
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   527
            with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   528
                self.pull(cnx)
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   529
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   530
            with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   531
                rset = cnx.execute('Any L WHERE U in_group G, G name %(name)s, U login L',
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   532
                                   {'name': 'logilab'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   533
                self.assertEqual(len(rset), 2)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   534
                members = set([u[0] for u in rset])
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   535
                self.assertEqual(set(['adim', 'syt']), members)
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   536
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   537
        finally:
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   538
            # back to normal ldap setup
11270
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   539
            self.terminate_slapd()
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   540
            self.init_slapd()
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   541
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   542
    def test_group_member_deleted(self):
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   543
        with self.repo.internal_cnx() as cnx:
10905
adffe1308a8f [test/ldap] a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10768
diff changeset
   544
            self.pull(cnx)  # ensure we are sync'ed
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   545
        with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   546
            rset = cnx.execute('Any L WHERE U in_group G, G name %(name)s, U login L',
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   547
                               {'name': 'logilab'})
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   548
            self.assertEqual(len(rset), 1)
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   549
            self.assertEqual(rset[0][0], 'adim')
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   550
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   551
        try:
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   552
            self.update_ldap_entry('cn=logilab,ou=Group,dc=cubicweb,dc=test',
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   553
                                   {('delete', 'memberUid'): ['adim']})
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   554
            with self.repo.internal_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   555
                self.pull(cnx)
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   556
9793
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   557
            with self.admin_access.repo_cnx() as cnx:
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   558
                rset = cnx.execute('Any L WHERE U in_group G, G name %(name)s, U login L',
52647b05bda8 [tests/ldap] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9754
diff changeset
   559
                                   {'name': 'logilab'})
10908
f80428f94761 [server/test] display rset content on failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10905
diff changeset
   560
                self.assertEqual(len(rset), 0, rset.rows)
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   561
        finally:
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   562
            # back to normal ldap setup
11270
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   563
            self.terminate_slapd()
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
   564
            self.init_slapd()
8922
715b9eec6da9 [ldapfeed] Add support for LDAP groups (closes #2528116)
David Douard <david.douard@logilab.fr>
parents: 8921
diff changeset
   565
8904
030463e2b620 [ldap] Major test refactoring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8903
diff changeset
   566
360
600dd2fe8b40 backport unittest_ldapuser
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   567
if __name__ == '__main__':
10572
2d5f7780b568 [test] kill unused lgc.testlib imports
Julien Cristau <julien.cristau@logilab.fr>
parents: 9794
diff changeset
   568
    from logilab.common.testlib import unittest_main
360
600dd2fe8b40 backport unittest_ldapuser
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   569
    unittest_main()