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