Update Copyright header point to COPYING file
[cascardo/ipsilon.git] / tests / openid.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
4
5 from helpers.common import IpsilonTestBase  # pylint: disable=relative-import
6 from helpers.http import HttpSessions  # pylint: disable=relative-import
7 import os
8 import pwd
9 import sys
10 import inspect
11 from string import Template
12
13 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
14          'CONFDIR': '${TESTDIR}/etc',
15          'DATADIR': '${TESTDIR}/lib',
16          'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
17          'STATICDIR': '${ROOTDIR}',
18          'BINDIR': '${ROOTDIR}/ipsilon',
19          'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
20
21
22 idp_a = {'hostname': '${ADDRESS}:${PORT}',
23          'admin_user': '${TEST_USER}',
24          'system_user': '${TEST_USER}',
25          'instance': '${NAME}',
26          'secure': 'no',
27          'testauth': 'yes',
28          'openid': 'yes',
29          'openid_extensions': 'Attribute Exchange,Simple Registration,Teams',
30          'pam': 'no',
31          'gssapi': 'no',
32          'ipa': 'no',
33          'server_debugging': 'True'}
34
35
36 def fixup_sp_httpd(httpdir, testdir):
37     client_wsgi = """
38
39 WSGIScriptAlias / ${TESTDIR}/blobs/openid_app.py
40
41 <Directory ${TESTDIR}/blobs>
42     Require all granted
43 </Directory>
44 """
45     t = Template(client_wsgi)
46     text = t.substitute({'TESTDIR': testdir})
47     with open(httpdir + '/conf.d/ipsilon-openid-client.conf', 'a') as f:
48         f.write(text)
49
50
51 class IpsilonTest(IpsilonTestBase):
52
53     def __init__(self):
54         super(IpsilonTest, self).__init__('openid', __file__)
55
56     def setup_servers(self, env=None):
57         print "Installing IDP server"
58         name = 'idp1'
59         addr = '127.0.0.10'
60         port = '45080'
61         idp = self.generate_profile(idp_g, idp_a, name, addr, port)
62         conf = self.setup_idp_server(idp, name, addr, port, env)
63
64         print "Starting IDP's httpd server"
65         self.start_http_server(conf, env)
66
67         print "Installing first SP server"
68         name = 'sp1'
69         addr = '127.0.0.11'
70         port = '45081'
71         conf = self.setup_http(name, addr, port)
72         testdir = os.path.dirname(os.path.abspath(inspect.getfile(
73             inspect.currentframe())))
74         fixup_sp_httpd(os.path.dirname(conf), testdir)
75
76         print "Starting SP's httpd server"
77         self.start_http_server(conf, env)
78
79
80 if __name__ == '__main__':
81
82     idpname = 'idp1'
83     sp1name = 'sp1'
84     user = pwd.getpwuid(os.getuid())[0]
85
86     sess = HttpSessions()
87     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
88     sess.add_server(sp1name, 'http://127.0.0.11:45081')
89
90     print "openid: Authenticate to IDP ...",
91     try:
92         sess.auth_to_idp(idpname)
93     except Exception as e:  # pylint: disable=broad-except
94         print >> sys.stderr, " ERROR: %s" % repr(e)
95         sys.exit(1)
96     print " SUCCESS"
97
98     print "openid: Run OpenID Protocol ...",
99     try:
100         page = sess.fetch_page(idpname,
101                                'http://127.0.0.11:45081/?extensions=NO')
102         page.expected_value('text()', 'SUCCESS, WITHOUT EXTENSIONS')
103     except ValueError as e:
104         print >> sys.stderr, " ERROR: %s" % repr(e)
105         sys.exit(1)
106     print " SUCCESS"
107
108     print "openid: Run OpenID Protocol with extensions ...",
109     try:
110         page = sess.fetch_page(idpname,
111                                'http://127.0.0.11:45081/?extensions=YES')
112         page.expected_value('text()', 'SUCCESS, WITH EXTENSIONS')
113     except ValueError as e:
114         print >> sys.stderr, " ERROR: %s" % repr(e)
115         sys.exit(1)
116     print " SUCCESS"