Fix file permissions and remove shebang's
[cascardo/ipsilon.git] / tests / fconf.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 from helpers.common import IpsilonTestBase  # pylint: disable=relative-import
22 from helpers.http import HttpSessions  # pylint: disable=relative-import
23 import ConfigParser
24 import os
25 import pwd
26 import sys
27 from string import Template
28
29
30 idpname = 'idp1'
31 idpaddr = '127.0.0.10'
32 idpport = '45080'
33 spname = 'sp1'
34 spaddr = '127.0.0.11'
35 spport = '45081'
36
37
38 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
39          'CONFDIR': '${TESTDIR}/etc',
40          'DATADIR': '${TESTDIR}/lib',
41          'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
42          'STATICDIR': '${ROOTDIR}',
43          'BINDIR': '${ROOTDIR}/ipsilon',
44          'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
45
46
47 idp_a = {'hostname': '${ADDRESS}:${PORT}',
48          'admin_user': '${TEST_USER}',
49          'system_user': '${TEST_USER}',
50          'instance': '${NAME}',
51          'secure': 'no',
52          'testauth': 'yes',
53          'pam': 'no',
54          'krb': 'no',
55          'ipa': 'no',
56          'server_debugging': 'True'}
57
58 idp_file_conf = """
59 [login_config]
60 global enabled = testauth
61 [provider_config]
62 global enabled = openid,saml2
63 openid endpoint url = ${IDPURI}/openid/
64 openid identity_url_template = ${IDPURI}/openid/id/%(username)s
65 saml2 idp key file = ${TESTDIR}/lib/${NAME}/saml2/idp.key
66 saml2 idp storage path = ${TESTDIR}/lib/${NAME}/saml2
67 saml2 idp metadata file = metadata.xml
68 saml2 idp certificate file = ${TESTDIR}/lib/${NAME}/saml2/idp.pem
69 [saml2_data]
70 811d0231-9362-46c9-a105-a01a64818904 id = http://${SPADDR}:${SPPORT}/saml2
71 811d0231-9362-46c9-a105-a01a64818904 type = SP
72 811d0231-9362-46c9-a105-a01a64818904 name = ${SPNAME}
73 811d0231-9362-46c9-a105-a01a64818904 metadata = ${SPMETA}
74 """
75
76 sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
77         'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
78         'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
79         'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
80
81
82 sp_a = {'hostname': '${ADDRESS}:${PORT}',
83         'saml_idp_metadata': '${TESTDIR}/lib/idp1/saml2/metadata.xml',
84         'saml_secure_setup': 'False',
85         'saml_auth': '/sp',
86         'httpd_user': '${TEST_USER}'}
87
88
89 def fixup_sp_httpd(httpdir):
90     location = """
91
92 Alias /sp ${HTTPDIR}/sp
93
94 <Directory ${HTTPDIR}/sp>
95     Require all granted
96 </Directory>
97 """
98     index = """WORKS!"""
99
100     t = Template(location)
101     text = t.substitute({'HTTPDIR': httpdir})
102     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
103         f.write(text)
104
105     os.mkdir(httpdir + '/sp')
106     with open(httpdir + '/sp/index.html', 'w') as f:
107         f.write(index)
108
109
110 def fixup_idp_conf(testdir):
111
112     with open(os.path.join(testdir, spname, 'saml2',
113                            '%s:%s' % (spaddr, spport), 'metadata.xml')) as f:
114         spmeta = f.read()
115     spmeta = spmeta.replace("\n", "")
116
117     idpuri = "http://%s:%s/%s" % (idpaddr, idpport, idpname)
118
119     t = Template(idp_file_conf)
120     text = t.substitute({'NAME': idpname, 'IDPURI': idpuri,
121                          'SPNAME': spname, 'SPADDR': spaddr, 'SPPORT': spport,
122                          'SPMETA': spmeta, 'TESTDIR': testdir})
123
124     adminconf = os.path.join(testdir, 'etc/admin.conf')
125     with open(adminconf, 'w+') as f:
126         f.write(text)
127
128     ipsilonconf = os.path.join(testdir, 'etc', idpname, 'ipsilon.conf')
129     newconf = ConfigParser.ConfigParser()
130     with open(ipsilonconf, 'r') as f:
131         newconf.readfp(f)
132     with open(ipsilonconf, 'w+') as f:
133         newconf.set('global', 'admin.config.db',
134                     '"configfile://%s"' % adminconf)
135         newconf.write(f)
136
137     os.remove(os.path.join(testdir, 'lib', idpname, 'adminconfig.sqlite'))
138
139
140 class IpsilonTest(IpsilonTestBase):
141
142     def __init__(self):
143         super(IpsilonTest, self).__init__('fconf', __file__)
144
145     def setup_servers(self, env=None):
146         print "Installing IDP server"
147         idp = self.generate_profile(idp_g, idp_a, idpname, idpaddr, idpport)
148         idpconf = self.setup_idp_server(idp, idpname, idpaddr, idpport, env)
149
150         print "Installing SP server"
151         sp = self.generate_profile(sp_g, sp_a, spname, spaddr, spport)
152         spconf = self.setup_sp_server(sp, spname, spaddr, spport, env)
153         fixup_sp_httpd(os.path.dirname(spconf))
154
155         fixup_idp_conf(self.testdir)
156
157         print "Starting IDP's httpd server"
158         self.start_http_server(idpconf, env)
159
160         print "Starting SP's httpd server"
161         self.start_http_server(spconf, env)
162
163
164 if __name__ == '__main__':
165
166     user = pwd.getpwuid(os.getuid())[0]
167
168     sess = HttpSessions()
169     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
170     sess.add_server(spname, 'http://127.0.0.11:45081')
171
172     print "test1: Access SP Protected Area ...",
173     try:
174         page = sess.fetch_page(idpname, 'http://127.0.0.11:45081/sp/')
175         page.expected_value('text()', 'WORKS!')
176     except ValueError, e:
177         print >> sys.stderr, " ERROR: %s" % repr(e)
178         sys.exit(1)
179     print " SUCCESS"