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