source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/wsgi/authn/test_authn.py @ 5015

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/wsgi/authn/test_authn.py@5015
Revision 5015, 3.3 KB checked in by pjkersha, 11 years ago (diff)

Authentication WSGI middleware unit tests:

  • tests middleware to redirect to authentication URI if the user is not logged in or a 401 status is set upstream
  • uses paste.fixture to test WSGI app.
Line 
1#!/usr/bin/env python
2"""Unit tests for WSGI Authentication redirect handler
3
4NERC Data Grid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "23/02/06"
8__copyright__ = "(C) 2009 Science and Technology Facilities Council"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = '$Id: test_sessionmanagerclient.py 4437 2008-11-18 12:34:25Z pjkersha $'
12import logging
13
14
15import unittest
16import os
17import sys
18import getpass
19import re
20import base64
21import urllib2
22
23from os.path import expandvars as xpdVars
24from os.path import join as jnPath
25mkPath = lambda file: jnPath(os.environ['NDGSEC_COMBINED_SRVS_UNITTEST_DIR'], 
26                             file)
27
28import paste.fixture
29from paste.deploy import loadapp
30
31from ndg.security.server.wsgi.authn import AuthNRedirectHandlerMiddleware
32
33
34class TestAuthNMiddleware(object):
35    '''Test Application for the Authentication handler to protect'''
36    response = "Test Authentication redirect application"
37       
38    def __init__(self, app_conf, **local_conf):
39        pass
40   
41    def __call__(self, environ, start_response):
42       
43        if environ['PATH_INFO'] == '/test_401WithNotLoggedIn':
44            status = "401 Unauthorized"
45           
46        elif environ['PATH_INFO'] == '/test_401WithLoggedIn':
47            status = "401 Unauthorized"
48            environ['REMOTE_USER'] = 'testuser'           
49           
50        elif environ['PATH_INFO'] == '/test_200WithNotLoggedIn':
51            status = "200 OK"
52           
53        elif environ['PATH_INFO'] == '/test_200WithLoggedIn':
54            environ['REMOTE_USER'] = 'testuser'
55            status = "200 OK"
56        else:
57            status = "404 Not found"
58               
59        start_response(status,
60                       [('Content-length', 
61                         str(len(TestAuthNMiddleware.response))),
62                        ('Content-type', 'text/plain')])
63        return [TestAuthNMiddleware.response]
64
65
66class WSGIAuthNTestController(unittest.TestCase):
67
68    def __init__(self, *args, **kwargs):
69        here_dir = os.path.dirname(os.path.abspath(__file__))
70        wsgiapp = loadapp('config:test.ini', relative_to=here_dir)
71        self.app = paste.fixture.TestApp(wsgiapp)
72         
73        unittest.TestCase.__init__(self, *args, **kwargs)
74       
75
76    def test01Catch401WithNotLoggedIn(self):
77        response = self.app.get('/test_401WithNotLoggedIn')
78        try:
79            redirectResponse = response.follow()
80        except paste.fixture.AppError, e:
81            self.failIf('404 Not found' not in str(e), 
82                        "Expecting 404 Not found")
83
84    def test02Skip200WithLoggedIn(self):
85        response = self.app.get('/test_200WithLoggedIn')
86        self.assert_(TestAuthNMiddleware.response in response)
87
88    def test03Catch401WithLoggedIn(self):
89        try:
90            response = self.app.get('/test_401WithLoggedIn')
91           
92        except paste.fixture.AppError, e:
93            self.failIf('403 Forbidden' not in str(e),
94                        "Expecting 403 Forbidden status")
95       
96    def test04Catch200WithNotLoggedIn(self):
97        response = self.app.get('/test_200WithNotLoggedIn')
98        self.assert_(TestAuthNMiddleware.response in response)
99   
100
101if __name__ == "__main__":
102    unittest.main()       
Note: See TracBrowser for help on using the repository browser.