Change references to authkrb plugin to authgssapi
[cascardo/ipsilon.git] / tests / openid.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 import inspect
27 from string import Template
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          'openid': 'yes',
45          'openid_extensions': 'Attribute Exchange,Simple Registration,Teams',
46          'pam': 'no',
47          'gssapi': 'no',
48          'ipa': 'no',
49          'server_debugging': 'True'}
50
51
52 def fixup_sp_httpd(httpdir, testdir):
53     client_wsgi = """
54
55 WSGIScriptAlias / ${TESTDIR}/blobs/openid_app.py
56
57 <Directory ${TESTDIR}/blobs>
58     Require all granted
59 </Directory>
60 """
61     t = Template(client_wsgi)
62     text = t.substitute({'TESTDIR': testdir})
63     with open(httpdir + '/conf.d/ipsilon-openid-client.conf', 'a') as f:
64         f.write(text)
65
66
67 class IpsilonTest(IpsilonTestBase):
68
69     def __init__(self):
70         super(IpsilonTest, self).__init__('openid', __file__)
71
72     def setup_servers(self, env=None):
73         print "Installing IDP server"
74         name = 'idp1'
75         addr = '127.0.0.10'
76         port = '45080'
77         idp = self.generate_profile(idp_g, idp_a, name, addr, port)
78         conf = self.setup_idp_server(idp, name, addr, port, env)
79
80         print "Starting IDP's httpd server"
81         self.start_http_server(conf, env)
82
83         print "Installing first SP server"
84         name = 'sp1'
85         addr = '127.0.0.11'
86         port = '45081'
87         conf = self.setup_http(name, addr, port)
88         testdir = os.path.dirname(os.path.abspath(inspect.getfile(
89             inspect.currentframe())))
90         fixup_sp_httpd(os.path.dirname(conf), testdir)
91
92         print "Starting SP's httpd server"
93         self.start_http_server(conf, env)
94
95
96 if __name__ == '__main__':
97
98     idpname = 'idp1'
99     sp1name = 'sp1'
100     user = pwd.getpwuid(os.getuid())[0]
101
102     sess = HttpSessions()
103     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
104     sess.add_server(sp1name, 'http://127.0.0.11:45081')
105
106     print "openid: Authenticate to IDP ...",
107     try:
108         sess.auth_to_idp(idpname)
109     except Exception as e:  # pylint: disable=broad-except
110         print >> sys.stderr, " ERROR: %s" % repr(e)
111         sys.exit(1)
112     print " SUCCESS"
113
114     print "openid: Run OpenID Protocol ...",
115     try:
116         page = sess.fetch_page(idpname,
117                                'http://127.0.0.11:45081/?extensions=NO')
118         page.expected_value('text()', 'SUCCESS, WITHOUT EXTENSIONS')
119     except ValueError as e:
120         print >> sys.stderr, " ERROR: %s" % repr(e)
121         sys.exit(1)
122     print " SUCCESS"
123
124     print "openid: Run OpenID Protocol with extensions ...",
125     try:
126         page = sess.fetch_page(idpname,
127                                'http://127.0.0.11:45081/?extensions=YES')
128         page.expected_value('text()', 'SUCCESS, WITH EXTENSIONS')
129     except ValueError as e:
130         print >> sys.stderr, " ERROR: %s" % repr(e)
131         sys.exit(1)
132     print " SUCCESS"