pam: use a pam object method instead of pam module function
[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          'CACHEDIR': '${TESTDIR}/cache',
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          'pam': 'no',
29          'gssapi': 'no',
30          'ipa': 'no',
31          'server_debugging': 'True'}
32
33
34 sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
35         'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
36         'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
37         'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
38
39
40 sp_a = {'hostname': '${ADDRESS}:${PORT}',
41         'saml_idp_metadata': 'http://127.0.0.10:45080/idp1/saml2/metadata',
42         'saml_secure_setup': 'False',
43         'saml_auth': '/sp',
44         'httpd_user': '${TEST_USER}'}
45
46 sp2_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
47          'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
48          'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
49          'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
50
51 sp2_a = {'hostname': '${ADDRESS}:${PORT}',
52          'saml_idp_url': 'http://127.0.0.10:45080/idp1',
53          'admin_user': '${TEST_USER}',
54          'admin_password': '${TESTDIR}/pw.txt',
55          'saml_sp_name': 'sp2',
56          'saml_secure_setup': 'False',
57          'saml_auth': '/sp',
58          'httpd_user': '${TEST_USER}'}
59
60
61 def fixup_sp_httpd(httpdir):
62     location = """
63
64 Alias /sp ${HTTPDIR}/sp
65
66 <Directory ${HTTPDIR}/sp>
67     Require all granted
68 </Directory>
69 """
70     index = """WORKS!"""
71
72     t = Template(location)
73     text = t.substitute({'HTTPDIR': httpdir})
74     with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
75         f.write(text)
76
77     os.mkdir(httpdir + '/sp')
78     with open(httpdir + '/sp/index.html', 'w') as f:
79         f.write(index)
80
81
82 class IpsilonTest(IpsilonTestBase):
83
84     def __init__(self):
85         super(IpsilonTest, self).__init__('test1', __file__)
86
87     def setup_servers(self, env=None):
88         print "Installing IDP server"
89         name = 'idp1'
90         addr = '127.0.0.10'
91         port = '45080'
92         idp = self.generate_profile(idp_g, idp_a, name, addr, port)
93         conf = self.setup_idp_server(idp, name, addr, port, env)
94
95         print "Starting IDP's httpd server"
96         self.start_http_server(conf, env)
97
98         print "Installing first SP server"
99         name = 'sp1'
100         addr = '127.0.0.11'
101         port = '45081'
102         sp = self.generate_profile(sp_g, sp_a, name, addr, port)
103         conf = self.setup_sp_server(sp, name, addr, port, env)
104         fixup_sp_httpd(os.path.dirname(conf))
105
106         print "Starting first SP's httpd server"
107         self.start_http_server(conf, env)
108
109         print "Installing second SP server"
110         name = 'sp2'
111         addr = '127.0.0.11'
112         port = '45082'
113         sp = self.generate_profile(sp2_g, sp2_a, name, addr, port)
114         with open(os.path.dirname(sp) + '/pw.txt', 'a') as f:
115             f.write('ipsilon')
116         conf = self.setup_sp_server(sp, name, addr, port, env)
117         os.remove(os.path.dirname(sp) + '/pw.txt')
118         fixup_sp_httpd(os.path.dirname(conf))
119
120         print "Starting second SP's httpd server"
121         self.start_http_server(conf, env)
122
123
124 if __name__ == '__main__':
125
126     idpname = 'idp1'
127     sp1name = 'sp1'
128     sp2name = 'sp2'
129     user = pwd.getpwuid(os.getuid())[0]
130
131     sess = HttpSessions()
132     sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
133     sess.add_server(sp1name, 'http://127.0.0.11:45081')
134     sess.add_server(sp2name, 'http://127.0.0.11:45082')
135
136     print "test1: Authenticate to IDP ...",
137     try:
138         sess.auth_to_idp(idpname)
139     except Exception, e:  # pylint: disable=broad-except
140         print >> sys.stderr, " ERROR: %s" % repr(e)
141         sys.exit(1)
142     print " SUCCESS"
143
144     print "test1: Add first SP Metadata to IDP ...",
145     try:
146         sess.add_sp_metadata(idpname, sp1name)
147     except Exception, e:  # pylint: disable=broad-except
148         print >> sys.stderr, " ERROR: %s" % repr(e)
149         sys.exit(1)
150     print " SUCCESS"
151
152     print "test1: Access first SP Protected Area ...",
153     try:
154         page = sess.fetch_page(idpname, 'http://127.0.0.11:45081/sp/')
155         page.expected_value('text()', 'WORKS!')
156     except ValueError, e:
157         print >> sys.stderr, " ERROR: %s" % repr(e)
158         sys.exit(1)
159     print " SUCCESS"
160
161     print "test1: Access second SP Protected Area ...",
162     try:
163         page = sess.fetch_page(idpname, 'http://127.0.0.11:45082/sp/')
164         page.expected_value('text()', 'WORKS!')
165     except ValueError, e:
166         print >> sys.stderr, " ERROR: %s" % repr(e)
167         sys.exit(1)
168     print " SUCCESS"
169
170     print "test1: Try authentication failure ...",
171     newsess = HttpSessions()
172     newsess.add_server(idpname, 'http://127.0.0.10:45080', user, 'wrong')
173     try:
174         newsess.auth_to_idp(idpname)
175         print >> sys.stderr, " ERROR: Authentication should have failed"
176         sys.exit(1)
177     except Exception, e:  # pylint: disable=broad-except
178         print " SUCCESS"