3 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
5 from helpers.common import IpsilonTestBase # pylint: disable=relative-import
6 from helpers.http import HttpSessions # pylint: disable=relative-import
10 from string import Template
12 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
13 'CONFDIR': '${TESTDIR}/etc',
14 'DATADIR': '${TESTDIR}/lib',
15 'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
16 'STATICDIR': '${ROOTDIR}',
17 'BINDIR': '${ROOTDIR}/ipsilon',
18 'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
21 idp_a = {'hostname': '${ADDRESS}:${PORT}',
22 'admin_user': '${TEST_USER}',
23 'system_user': '${TEST_USER}',
24 'instance': '${NAME}',
30 'server_debugging': 'True'}
33 sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
34 'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
35 'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
36 'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
39 sp_a = {'hostname': '${ADDRESS}:${PORT}',
40 'saml_idp_metadata': 'http://127.0.0.10:45080/idp1/saml2/metadata',
41 'saml_secure_setup': 'False',
43 'httpd_user': '${TEST_USER}'}
45 sp2_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
46 'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
47 'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
48 'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
50 sp2_a = {'hostname': '${ADDRESS}:${PORT}',
51 'saml_idp_url': 'http://127.0.0.10:45080/idp1',
52 'admin_user': '${TEST_USER}',
53 'admin_password': '${TESTDIR}/pw.txt',
54 'saml_sp_name': 'sp2',
55 'saml_secure_setup': 'False',
57 'httpd_user': '${TEST_USER}'}
60 def fixup_sp_httpd(httpdir):
63 Alias /sp ${HTTPDIR}/sp
65 <Directory ${HTTPDIR}/sp>
71 t = Template(location)
72 text = t.substitute({'HTTPDIR': httpdir})
73 with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
76 os.mkdir(httpdir + '/sp')
77 with open(httpdir + '/sp/index.html', 'w') as f:
81 class IpsilonTest(IpsilonTestBase):
84 super(IpsilonTest, self).__init__('test1', __file__)
86 def setup_servers(self, env=None):
87 print "Installing IDP server"
91 idp = self.generate_profile(idp_g, idp_a, name, addr, port)
92 conf = self.setup_idp_server(idp, name, addr, port, env)
94 print "Starting IDP's httpd server"
95 self.start_http_server(conf, env)
97 print "Installing first SP server"
101 sp = self.generate_profile(sp_g, sp_a, name, addr, port)
102 conf = self.setup_sp_server(sp, name, addr, port, env)
103 fixup_sp_httpd(os.path.dirname(conf))
105 print "Starting first SP's httpd server"
106 self.start_http_server(conf, env)
108 print "Installing second SP server"
112 sp = self.generate_profile(sp2_g, sp2_a, name, addr, port)
113 with open(os.path.dirname(sp) + '/pw.txt', 'a') as f:
115 conf = self.setup_sp_server(sp, name, addr, port, env)
116 os.remove(os.path.dirname(sp) + '/pw.txt')
117 fixup_sp_httpd(os.path.dirname(conf))
119 print "Starting second SP's httpd server"
120 self.start_http_server(conf, env)
123 if __name__ == '__main__':
128 user = pwd.getpwuid(os.getuid())[0]
130 sess = HttpSessions()
131 sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
132 sess.add_server(sp1name, 'http://127.0.0.11:45081')
133 sess.add_server(sp2name, 'http://127.0.0.11:45082')
135 print "test1: Authenticate to IDP ...",
137 sess.auth_to_idp(idpname)
138 except Exception, e: # pylint: disable=broad-except
139 print >> sys.stderr, " ERROR: %s" % repr(e)
143 print "test1: Add first SP Metadata to IDP ...",
145 sess.add_sp_metadata(idpname, sp1name)
146 except Exception, e: # pylint: disable=broad-except
147 print >> sys.stderr, " ERROR: %s" % repr(e)
151 print "test1: Access first SP Protected Area ...",
153 page = sess.fetch_page(idpname, 'http://127.0.0.11:45081/sp/')
154 page.expected_value('text()', 'WORKS!')
155 except ValueError, e:
156 print >> sys.stderr, " ERROR: %s" % repr(e)
160 print "test1: Access second SP Protected Area ...",
162 page = sess.fetch_page(idpname, 'http://127.0.0.11:45082/sp/')
163 page.expected_value('text()', 'WORKS!')
164 except ValueError, e:
165 print >> sys.stderr, " ERROR: %s" % repr(e)
169 print "test1: Try authentication failure ...",
170 newsess = HttpSessions()
171 newsess.add_server(idpname, 'http://127.0.0.10:45080', user, 'wrong')
173 newsess.auth_to_idp(idpname)
174 print >> sys.stderr, " ERROR: Authentication should have failed"
176 except Exception, e: # pylint: disable=broad-except