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