Make it possible to use PluginLoader without store
[cascardo/ipsilon.git] / tests / attrs.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 from string import Template
11
12
13 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
14          'CONFDIR': '${TESTDIR}/etc',
15          'DATADIR': '${TESTDIR}/lib',
16          'CACHEDIR': '${TESTDIR}/cache',
17          'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
18          'STATICDIR': '${ROOTDIR}',
19          'BINDIR': '${ROOTDIR}/ipsilon',
20          'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
21
22
23 idp_a = {'hostname': '${ADDRESS}:${PORT}',
24          'admin_user': '${TEST_USER}',
25          'system_user': '${TEST_USER}',
26          'instance': '${NAME}',
27          'secure': 'no',
28          'testauth': 'yes',
29          'pam': 'no',
30          'gssapi': 'no',
31          'ipa': 'no',
32          'server_debugging': 'True'}
33
34
35 sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
36         'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
37         'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
38         'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
39
40
41 sp_a = {'hostname': '${ADDRESS}:${PORT}',
42         'saml_idp_metadata': 'http://127.0.0.10:45080/idp1/saml2/metadata',
43         'saml_secure_setup': 'False',
44         'saml_auth': '/sp',
45         'httpd_user': '${TEST_USER}'}
46
47
48 def fixup_sp_httpd(httpdir):
49     merge = """
50     MellonSetEnv "FULLNAME" "fullname"
51 </Location>"""
52     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'r') as f:
53         conf = f.read()
54     conf = conf.replace('</Location>', merge, 1)
55     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'w') as f:
56         f.write(conf)
57
58     location = """
59
60 Alias /sp ${HTTPDIR}/sp
61
62 <Directory ${HTTPDIR}/sp>
63     Require all granted
64     Options +Includes
65 </Directory>
66 """
67     t = Template(location)
68     text = t.substitute({'HTTPDIR': httpdir})
69     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
70         f.write(text)
71
72     index = """<!--#echo var="MELLON_FULLNAME" -->"""
73     os.mkdir(httpdir + '/sp')
74     with open(httpdir + '/sp/index.shtml', 'w') as f:
75         f.write(index)
76
77
78 class IpsilonTest(IpsilonTestBase):
79
80     def __init__(self):
81         super(IpsilonTest, self).__init__('attrs', __file__)
82
83     def setup_servers(self, env=None):
84         print "Installing IDP server"
85         name = 'idp1'
86         addr = '127.0.0.10'
87         port = '45080'
88         idp = self.generate_profile(idp_g, idp_a, name, addr, port)
89         conf = self.setup_idp_server(idp, name, addr, port, env)
90
91         print "Starting IDP's httpd server"
92         self.start_http_server(conf, env)
93
94         print "Installing SP server"
95         name = 'sp1'
96         addr = '127.0.0.11'
97         port = '45081'
98         sp = self.generate_profile(sp_g, sp_a, name, addr, port)
99         conf = self.setup_sp_server(sp, name, addr, port, env)
100         fixup_sp_httpd(os.path.dirname(conf))
101
102         print "Starting SP's httpd server"
103         self.start_http_server(conf, env)
104
105
106 if __name__ == '__main__':
107
108     idpname = 'idp1'
109     spname = 'sp1'
110     user = pwd.getpwuid(os.getuid())[0]
111
112     sess = HttpSessions()
113     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
114     sess.add_server(spname, 'http://127.0.0.11:45081')
115
116     print "attrs: Authenticate to IDP ...",
117     try:
118         sess.auth_to_idp(idpname)
119     except Exception, e:  # pylint: disable=broad-except
120         print >> sys.stderr, " ERROR: %s" % repr(e)
121         sys.exit(1)
122     print " SUCCESS"
123
124     print "attrs: Add SP Metadata to IDP ...",
125     try:
126         sess.add_sp_metadata(idpname, spname)
127     except Exception, e:  # pylint: disable=broad-except
128         print >> sys.stderr, " ERROR: %s" % repr(e)
129         sys.exit(1)
130     print " SUCCESS"
131
132     print "attrs: Access SP Protected Area Variables...",
133     try:
134         page = sess.fetch_page(idpname,
135                                'http://127.0.0.11:45081/sp/index.shtml')
136         page.expected_value('text()', 'Test User %s' % user)
137     except ValueError, e:
138         print >> sys.stderr, " ERROR: %s" % repr(e)
139         sys.exit(1)
140     print " SUCCESS"