source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/integration/authz/securityservicesapp.py @ 5017

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/integration/authz/securityservicesapp.py@5017
Revision 5017, 3.6 KB checked in by pjkersha, 11 years ago (diff)

Rearranged authz integration tests continued: renamed serverapp.py and services.ini to make it clear these set-up the security services stack.

Line 
1#!/usr/bin/env python
2"""NDG Security test harness for authorisation middleware
3
4NERC DataGrid Project
5
6"""
7__author__ = "P J Kershaw"
8__date__ = "20/11/08"
9__copyright__ = "(C) 2009 Science and Technology Facilities Council"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id$"
12import os
13from os.path import dirname, abspath, join
14
15class AuthZTestMiddleware(object):
16    method = {
17"/": 'default',
18"/test_401": "test_401",
19"/test_403": "test_403",
20"/test_securedURI": "test_securedURI"
21    }
22
23    def __init__(self, app, globalConfig, **localConfig):
24        self.app = app
25           
26    def __call__(self, environ, start_response):
27       
28        methodName = self.method.get(environ['PATH_INFO'], '').rstrip()
29        if methodName:
30            action = getattr(self, methodName)
31            return action(environ, start_response)
32        elif self.app is not None:
33            return self.app(environ, start_response)
34        else:
35            start_response('404 Not Found', [('Content-type', 'text/plain')])
36            return "Authorisation integration tests: invalid URI"
37           
38    def default(self, environ, start_response):
39        start_response('200 OK', [('Content-type', 'text/plain')])
40        return "Authorisation integration tests"
41
42    def test_401(self, environ, start_response):
43        if 'REMOTE_USER' in environ:
44            response = """<html>
45    <head/>
46    <body>
47        <p>Authenticated!</p>
48        <p><a href="/logout">logout</a></p>
49    </body>
50</html>"""
51            start_response('200 OK', 
52                           [('Content-type', 'text/html'),
53                            ('Content-length', str(len(response)))])
54        else:
55            response = "Trigger OpenID Relying Party..."
56            start_response('401 Unauthorized', 
57                           [('Content-type', 'text/plain'),
58                            ('Content-length', str(len(response)))])
59        return response
60
61    def test_403(self, environ, start_response):
62        if 'REMOTE_USER' in environ:
63            response = """<html>
64    <head/>
65    <body>
66        <p>Authorised!</p>
67        <p><a href="/logout">logout</a></p>
68    </body>
69</html>"""
70            start_response('200 OK', 
71                           [('Content-type', 'text/html'),
72                            ('Content-length', str(len(response)))])
73        else:
74            response = "Trigger AuthZ..."
75            start_response('403 Forbidden', 
76                           [('Content-type', 'text/plain'),
77                            ('Content-length', str(len(response)))])
78        return response
79
80    def test_securedURI(self, environ, start_response):
81        response = "Access allowed"
82        start_response('200 OK', 
83                       [('Content-type', 'text/plain'),
84                        ('Content-length', str(len(response)))])
85        return response
86   
87def app_factory(globalConfig, **localConfig):
88    return AuthZTestMiddleware(None, globalConfig, **localConfig)
89
90def filter_app_factory(app, globalConfig, **localConfig):
91    return AuthZTestMiddleware(app, globalConfig, **localConfig)
92   
93# To start run
94# $ paster serve services.ini or run this file as a script
95# $ ./serverapp.py [port #]
96if __name__ == '__main__':
97    import sys
98    import logging
99    logging.basicConfig(level=logging.DEBUG)
100
101    if len(sys.argv) > 1:
102        port = int(sys.argv[1])
103    else:
104        port = 80443
105       
106    cfgFilePath = os.path.join(dirname(abspath(__file__)), 'services.ini')
107       
108    from paste.httpserver import serve
109    from paste.deploy import loadapp
110   
111    app = loadapp('config:%s' % cfgFilePath)
112    serve(app, host='0.0.0.0', port=port)
Note: See TracBrowser for help on using the repository browser.