Create cache directory for storing images for SP Portal
[cascardo/ipsilon.git] / tests / fconf.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
4
5 from helpers.common import IpsilonTestBase  # pylint: disable=relative-import
6 from helpers.http import HttpSessions  # pylint: disable=relative-import
7 import ConfigParser
8 import os
9 import pwd
10 import sys
11 from string import Template
12 import uuid
13
14
15 idpname = 'idp1'
16 idpaddr = '127.0.0.10'
17 idpport = '45080'
18 spname = 'sp1'
19 spaddr = '127.0.0.11'
20 spport = '45081'
21
22
23 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
24          'CONFDIR': '${TESTDIR}/etc',
25          'DATADIR': '${TESTDIR}/lib',
26          'CACHEDIR': '${TESTDIR}/cache',
27          'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
28          'STATICDIR': '${ROOTDIR}',
29          'BINDIR': '${ROOTDIR}/ipsilon',
30          'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
31
32
33 idp_a = {'hostname': '${ADDRESS}:${PORT}',
34          'admin_user': '${TEST_USER}',
35          'system_user': '${TEST_USER}',
36          'instance': '${NAME}',
37          'secure': 'no',
38          'testauth': 'yes',
39          'pam': 'no',
40          'gssapi': 'no',
41          'ipa': 'no',
42          'server_debugging': 'True'}
43
44 idp_file_conf = """
45 [login_config]
46 global enabled = testauth
47 [provider_config]
48 global enabled = openid,saml2
49 openid endpoint url = ${IDPURI}/openid/
50 openid identity_url_template = ${IDPURI}/openid/id/%(username)s
51 saml2 idp key file = ${TESTDIR}/lib/${NAME}/saml2/idp.key
52 saml2 idp storage path = ${TESTDIR}/lib/${NAME}/saml2
53 saml2 idp metadata file = metadata.xml
54 saml2 idp certificate file = ${TESTDIR}/lib/${NAME}/saml2/idp.pem
55 saml2 idp nameid salt = ${IDPSALT}
56 [saml2_data]
57 811d0231-9362-46c9-a105-a01a64818904 id = http://${SPADDR}:${SPPORT}/saml2
58 811d0231-9362-46c9-a105-a01a64818904 type = SP
59 811d0231-9362-46c9-a105-a01a64818904 name = ${SPNAME}
60 811d0231-9362-46c9-a105-a01a64818904 metadata = ${SPMETA}
61 """
62
63 sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
64         'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
65         'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
66         'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
67
68
69 sp_a = {'hostname': '${ADDRESS}:${PORT}',
70         'saml_idp_metadata': '${TESTDIR}/lib/idp1/saml2/metadata.xml',
71         'saml_secure_setup': 'False',
72         'saml_auth': '/sp',
73         'httpd_user': '${TEST_USER}'}
74
75
76 def fixup_sp_httpd(httpdir):
77     location = """
78
79 Alias /sp ${HTTPDIR}/sp
80
81 <Directory ${HTTPDIR}/sp>
82     Require all granted
83 </Directory>
84 """
85     index = """WORKS!"""
86
87     t = Template(location)
88     text = t.substitute({'HTTPDIR': httpdir})
89     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
90         f.write(text)
91
92     os.mkdir(httpdir + '/sp')
93     with open(httpdir + '/sp/index.html', 'w') as f:
94         f.write(index)
95
96
97 def fixup_idp_conf(testdir):
98
99     with open(os.path.join(testdir, spname, 'saml2',
100                            '%s:%s' % (spaddr, spport), 'metadata.xml')) as f:
101         spmeta = f.read()
102     spmeta = spmeta.replace("\n", "")
103
104     idpuri = "http://%s:%s/%s" % (idpaddr, idpport, idpname)
105
106     idpsalt = uuid.uuid4().hex
107     t = Template(idp_file_conf)
108     text = t.substitute({'NAME': idpname, 'IDPURI': idpuri,
109                          'SPNAME': spname, 'SPADDR': spaddr, 'SPPORT': spport,
110                          'SPMETA': spmeta, 'TESTDIR': testdir,
111                          'IDPSALT': idpsalt})
112
113     adminconf = os.path.join(testdir, 'etc/admin.conf')
114     with open(adminconf, 'w+') as f:
115         f.write(text)
116
117     ipsilonconf = os.path.join(testdir, 'etc', idpname, 'ipsilon.conf')
118     newconf = ConfigParser.ConfigParser()
119     with open(ipsilonconf, 'r') as f:
120         newconf.readfp(f)
121     with open(ipsilonconf, 'w+') as f:
122         newconf.set('global', 'admin.config.db',
123                     '"configfile://%s"' % adminconf)
124         newconf.write(f)
125
126     os.remove(os.path.join(testdir, 'lib', idpname, 'adminconfig.sqlite'))
127
128
129 class IpsilonTest(IpsilonTestBase):
130
131     def __init__(self):
132         super(IpsilonTest, self).__init__('fconf', __file__)
133
134     def setup_servers(self, env=None):
135         print "Installing IDP server"
136         idp = self.generate_profile(idp_g, idp_a, idpname, idpaddr, idpport)
137         idpconf = self.setup_idp_server(idp, idpname, idpaddr, idpport, env)
138
139         print "Installing SP server"
140         sp = self.generate_profile(sp_g, sp_a, spname, spaddr, spport)
141         spconf = self.setup_sp_server(sp, spname, spaddr, spport, env)
142         fixup_sp_httpd(os.path.dirname(spconf))
143
144         fixup_idp_conf(self.testdir)
145
146         print "Starting IDP's httpd server"
147         self.start_http_server(idpconf, env)
148
149         print "Starting SP's httpd server"
150         self.start_http_server(spconf, env)
151
152
153 if __name__ == '__main__':
154
155     user = pwd.getpwuid(os.getuid())[0]
156
157     sess = HttpSessions()
158     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
159     sess.add_server(spname, 'http://127.0.0.11:45081')
160
161     print "test1: Access SP Protected Area ...",
162     try:
163         page = sess.fetch_page(idpname, 'http://127.0.0.11:45081/sp/')
164         page.expected_value('text()', 'WORKS!')
165     except ValueError, e:
166         print >> sys.stderr, " ERROR: %s" % repr(e)
167         sys.exit(1)
168     print " SUCCESS"