984bceed6ab5e7b94f0547589bd6fa4da658bc2a
[cascardo/ipsilon.git] / tests / pgdb.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 os
24 import pwd
25 import sys
26 from string import Template
27
28
29 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
30          'CONFDIR': '${TESTDIR}/etc',
31          'DATADIR': '${TESTDIR}/lib',
32          'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
33          'STATICDIR': '${ROOTDIR}',
34          'BINDIR': '${ROOTDIR}/ipsilon',
35          'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
36
37
38 idp_a = {'hostname': '${ADDRESS}:${PORT}',
39          'users_dburi': 'postgresql://@127.0.0.10:45432/users',
40          'database_url': 'postgresql://@127.0.0.10:45432/%(dbname)s',
41          'session_type': 'sql',
42          'session_dburi': 'postgresql://@127.0.0.10:45432/sessions',
43          'admin_user': '${TEST_USER}',
44          'system_user': '${TEST_USER}',
45          'instance': '${NAME}',
46          'secure': 'no',
47          'testauth': 'yes',
48          'pam': 'no',
49          'gssapi': 'no',
50          'ipa': 'no',
51          'server_debugging': 'True'}
52
53
54 sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
55         'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
56         'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
57         'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
58
59
60 sp_a = {'hostname': '${ADDRESS}:${PORT}',
61         'saml_idp_metadata': 'http://127.0.0.10:45080/idp1/saml2/metadata',
62         'saml_secure_setup': 'False',
63         'saml_auth': '/sp',
64         'httpd_user': '${TEST_USER}'}
65
66
67 def fixup_sp_httpd(httpdir):
68     location = """
69
70 Alias /sp ${HTTPDIR}/sp
71
72 <Directory ${HTTPDIR}/sp>
73     Require all granted
74 </Directory>
75 """
76     index = """WORKS!"""
77
78     t = Template(location)
79     text = t.substitute({'HTTPDIR': httpdir})
80     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
81         f.write(text)
82
83     os.mkdir(httpdir + '/sp')
84     with open(httpdir + '/sp/index.html', 'w') as f:
85         f.write(index)
86
87
88 class IpsilonTest(IpsilonTestBase):
89
90     def __init__(self):
91         super(IpsilonTest, self).__init__('pgdb', __file__)
92
93     def setup_servers(self, env=None):
94
95         print "Installing IDP's database server"
96         datadir = os.path.join(self.testdir, 'pgdata')
97         rundir = self.testdir
98         log = os.path.join(self.testdir, 'log/pgdb.log')
99         addr = '127.0.0.10'
100         port = '45432'
101         self.setup_pgdb(datadir, env)
102
103         print "Starting IDP's database server"
104         self.start_pgdb_server(datadir, rundir, log, addr, port, env)
105
106         print "Installing IDP server"
107         name = 'idp1'
108         addr = '127.0.0.10'
109         port = '45080'
110         idp = self.generate_profile(idp_g, idp_a, name, addr, port)
111         conf = self.setup_idp_server(idp, name, addr, port, env)
112
113         print "Starting IDP's httpd server"
114         self.start_http_server(conf, env)
115
116         print "Installing SP server"
117         name = 'sp1'
118         addr = '127.0.0.11'
119         port = '45081'
120         sp = self.generate_profile(sp_g, sp_a, name, addr, port)
121         conf = self.setup_sp_server(sp, name, addr, port, env)
122         fixup_sp_httpd(os.path.dirname(conf))
123
124         print "Starting SP's httpd server"
125         self.start_http_server(conf, env)
126
127
128 if __name__ == '__main__':
129
130     idpname = 'idp1'
131     spname = 'sp1'
132     user = pwd.getpwuid(os.getuid())[0]
133
134     sess = HttpSessions()
135     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
136     sess.add_server(spname, 'http://127.0.0.11:45081')
137
138     print "test1: Authenticate to IDP ...",
139     sys.stdout.flush()
140     try:
141         print 'Stress-testing the database connections...',
142         sys.stdout.flush()
143         for i in xrange(50):
144             sess.auth_to_idp(idpname)
145             sess.logout_from_idp(idpname)
146         sess.auth_to_idp(idpname)
147     except Exception, e:  # pylint: disable=broad-except
148         print >> sys.stderr, " ERROR: %s" % repr(e)
149         sys.exit(1)
150     print " SUCCESS"
151
152     print "test1: Add SP Metadata to IDP ...",
153     try:
154         sess.add_sp_metadata(idpname, spname)
155     except Exception, e:  # pylint: disable=broad-except
156         print >> sys.stderr, " ERROR: %s" % repr(e)
157         sys.exit(1)
158     print " SUCCESS"
159
160     print "test1: Access SP Protected Area ...",
161     try:
162         page = sess.fetch_page(idpname, 'http://127.0.0.11:45081/sp/')
163         page.expected_value('text()', 'WORKS!')
164     except ValueError, e:
165         print >> sys.stderr, " ERROR: %s" % repr(e)
166         sys.exit(1)
167     print " SUCCESS"