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