Report to user if an LDAP error occurs
[cascardo/ipsilon.git] / tests / ldapdown.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2015 Ipsilon project Contributors, for license see COPYING
4
5 # Test that we get a reasonable error back when the LDAP backend is down
6
7 from helpers.common import IpsilonTestBase  # pylint: disable=relative-import
8 from helpers.http import HttpSessions  # pylint: disable=relative-import
9 import os
10 import sys
11 from string import Template
12
13
14 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
15          'CONFDIR': '${TESTDIR}/etc',
16          'DATADIR': '${TESTDIR}/lib',
17          'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
18          'STATICDIR': '${ROOTDIR}',
19          'BINDIR': '${ROOTDIR}/ipsilon',
20          'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
21
22
23 idp_a = {'hostname': '${ADDRESS}:${PORT}',
24          'admin_user': 'tuser',
25          'system_user': '${TEST_USER}',
26          'instance': '${NAME}',
27          'secure': 'no',
28          'pam': 'no',
29          'gssapi': 'no',
30          'ipa': 'no',
31          'ldap': 'yes',
32          'ldap_server_url': 'ldap://${ADDRESS}:45389/',
33          'ldap_bind_dn_template':
34          'uid=%(username)s,ou=People,dc=example,dc=com',
35          'ldap_tls_level': 'NoTLS',
36          'server_debugging': 'True'}
37
38
39 sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
40         'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
41         'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
42         'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
43
44
45 sp_a = {'hostname': '${ADDRESS}:${PORT}',
46         'saml_idp_metadata': 'http://127.0.0.10:45080/idp1/saml2/metadata',
47         'saml_secure_setup': 'False',
48         'saml_auth': '/sp',
49         'httpd_user': '${TEST_USER}'}
50
51
52 def fixup_sp_httpd(httpdir):
53     merge = """
54     MellonSetEnv "GROUPS" "groups"
55     MellonMergeEnvVars On
56 </Location>"""
57     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'r') as f:
58         conf = f.read()
59     conf = conf.replace('</Location>', merge, 1)
60     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'w') as f:
61         f.write(conf)
62
63     location = """
64
65 Alias /sp ${HTTPDIR}/sp
66
67 <Directory ${HTTPDIR}/sp>
68     Require all granted
69     Options +Includes
70 </Directory>
71 """
72     t = Template(location)
73     text = t.substitute({'HTTPDIR': httpdir})
74     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
75         f.write(text)
76
77     index = """<!--#echo var="MELLON_GROUPS" -->"""
78     os.mkdir(httpdir + '/sp')
79     with open(httpdir + '/sp/index.shtml', 'w') as f:
80         f.write(index)
81
82
83 class IpsilonTest(IpsilonTestBase):
84
85     def __init__(self):
86         super(IpsilonTest, self).__init__('ldapdown', __file__)
87
88     def setup_servers(self, env=None):
89
90         print "Installing IDP's ldap server"
91         addr = '127.0.0.10'
92         port = '45389'
93         conf = self.setup_ldap(env)
94
95         print "Not starting IDP's ldap server"
96
97         print "Installing IDP server"
98         name = 'idp1'
99         addr = '127.0.0.10'
100         port = '45080'
101         idp = self.generate_profile(idp_g, idp_a, name, addr, port)
102         conf = self.setup_idp_server(idp, name, addr, port, env)
103
104         print "Starting IDP's httpd server"
105         self.start_http_server(conf, env)
106
107         print "Installing SP server"
108         name = 'sp1'
109         addr = '127.0.0.11'
110         port = '45081'
111         sp = self.generate_profile(sp_g, sp_a, name, addr, port)
112         conf = self.setup_sp_server(sp, name, addr, port, env)
113         fixup_sp_httpd(os.path.dirname(conf))
114
115         print "Starting SP's httpd server"
116         self.start_http_server(conf, env)
117
118
119 if __name__ == '__main__':
120
121     idpname = 'idp1'
122     spname = 'sp1'
123     user = 'tuser'
124
125     sess = HttpSessions()
126     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'tuser')
127     sess.add_server(spname, 'http://127.0.0.11:45081')
128
129     print "ldapdown: Authenticate to IDP with no LDAP backend...",
130     try:
131         sess.auth_to_idp(
132             idpname,
133             rule='//div[@class="alert alert-danger"]/p/text()',
134             expected="Internal system error"
135         )
136     except Exception, e:  # pylint: disable=broad-except
137         print >> sys.stderr, " ERROR: %s" % repr(e)
138         sys.exit(1)
139     print " SUCCESS"