0a0abcf49b641235f67920d0a04814de84ca14a3
[cascardo/ipsilon.git] / tests / test1.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 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'}
19
20
21 idp_a = {'hostname': '${ADDRESS}:${PORT}',
22          'admin_user': '${TEST_USER}',
23          'system_user': '${TEST_USER}',
24          'instance': '${NAME}',
25          'secure': 'no',
26          'testauth': 'yes',
27          'pam': 'no',
28          'gssapi': 'no',
29          'ipa': 'no',
30          'server_debugging': 'True'}
31
32
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'}
37
38
39 sp_a = {'hostname': '${ADDRESS}:${PORT}',
40         'saml_idp_metadata': 'http://127.0.0.10:45080/idp1/saml2/metadata',
41         'saml_secure_setup': 'False',
42         'saml_auth': '/sp',
43         'httpd_user': '${TEST_USER}'}
44
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'}
49
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',
56          'saml_auth': '/sp',
57          'httpd_user': '${TEST_USER}'}
58
59
60 def fixup_sp_httpd(httpdir):
61     location = """
62
63 Alias /sp ${HTTPDIR}/sp
64
65 <Directory ${HTTPDIR}/sp>
66     Require all granted
67 </Directory>
68 """
69     index = """WORKS!"""
70
71     t = Template(location)
72     text = t.substitute({'HTTPDIR': httpdir})
73     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
74         f.write(text)
75
76     os.mkdir(httpdir + '/sp')
77     with open(httpdir + '/sp/index.html', 'w') as f:
78         f.write(index)
79
80
81 class IpsilonTest(IpsilonTestBase):
82
83     def __init__(self):
84         super(IpsilonTest, self).__init__('test1', __file__)
85
86     def setup_servers(self, env=None):
87         print "Installing IDP server"
88         name = 'idp1'
89         addr = '127.0.0.10'
90         port = '45080'
91         idp = self.generate_profile(idp_g, idp_a, name, addr, port)
92         conf = self.setup_idp_server(idp, name, addr, port, env)
93
94         print "Starting IDP's httpd server"
95         self.start_http_server(conf, env)
96
97         print "Installing first SP server"
98         name = 'sp1'
99         addr = '127.0.0.11'
100         port = '45081'
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))
104
105         print "Starting first SP's httpd server"
106         self.start_http_server(conf, env)
107
108         print "Installing second SP server"
109         name = 'sp2'
110         addr = '127.0.0.11'
111         port = '45082'
112         sp = self.generate_profile(sp2_g, sp2_a, name, addr, port)
113         with open(os.path.dirname(sp) + '/pw.txt', 'a') as f:
114             f.write('ipsilon')
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))
118
119         print "Starting second SP's httpd server"
120         self.start_http_server(conf, env)
121
122
123 if __name__ == '__main__':
124
125     idpname = 'idp1'
126     sp1name = 'sp1'
127     sp2name = 'sp2'
128     user = pwd.getpwuid(os.getuid())[0]
129
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')
134
135     print "test1: Authenticate to IDP ...",
136     try:
137         sess.auth_to_idp(idpname)
138     except Exception, e:  # pylint: disable=broad-except
139         print >> sys.stderr, " ERROR: %s" % repr(e)
140         sys.exit(1)
141     print " SUCCESS"
142
143     print "test1: Add first SP Metadata to IDP ...",
144     try:
145         sess.add_sp_metadata(idpname, sp1name)
146     except Exception, e:  # pylint: disable=broad-except
147         print >> sys.stderr, " ERROR: %s" % repr(e)
148         sys.exit(1)
149     print " SUCCESS"
150
151     print "test1: Access first SP Protected Area ...",
152     try:
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)
157         sys.exit(1)
158     print " SUCCESS"
159
160     print "test1: Access second SP Protected Area ...",
161     try:
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)
166         sys.exit(1)
167     print " SUCCESS"
168
169     print "test1: Try authentication failure ...",
170     newsess = HttpSessions()
171     newsess.add_server(idpname, 'http://127.0.0.10:45080', user, 'wrong')
172     try:
173         newsess.auth_to_idp(idpname)
174         print >> sys.stderr, " ERROR: Authentication should have failed"
175         sys.exit(1)
176     except Exception, e:  # pylint: disable=broad-except
177         print " SUCCESS"