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

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

Completed fixes to unit tests for move to 'unit' package.

Line 
1#!/usr/bin/env python
2"""Unit tests for WSGI Authentication redirect handler
3
4NERC DataGrid 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
31class TestAuthNMiddleware(object):
32    '''Test Application for the Authentication handler to protect'''
33    response = "Test Authentication redirect application"
34       
35    def __init__(self, app_conf, **local_conf):
36        pass
37   
38    def __call__(self, environ, start_response):
39       
40        if environ['PATH_INFO'] == '/test_401WithNotLoggedIn':
41            status = "401 Unauthorized"
42           
43        elif environ['PATH_INFO'] == '/test_401WithLoggedIn':
44            status = "401 Unauthorized"
45           
46        elif environ['PATH_INFO'] == '/test_200WithNotLoggedIn':
47            status = "200 OK"
48           
49        elif environ['PATH_INFO'] == '/test_200WithLoggedIn':
50            environ['REMOTE_USER'] = 'testuser'
51            status = "200 OK"
52        else:
53            status = "404 Not found"
54               
55        start_response(status,
56                       [('Content-length', 
57                         str(len(TestAuthNMiddleware.response))),
58                        ('Content-type', 'text/plain')])
59        return [TestAuthNMiddleware.response]
60
61
62class WSGIAuthNTestController(unittest.TestCase):
63
64    def __init__(self, *args, **kwargs):
65        here_dir = os.path.dirname(os.path.abspath(__file__))
66        wsgiapp = loadapp('config:test.ini', relative_to=here_dir)
67        self.app = paste.fixture.TestApp(wsgiapp)
68         
69        unittest.TestCase.__init__(self, *args, **kwargs)
70       
71
72    def test01Catch401WithNotLoggedIn(self):
73        response = self.app.get('/test_401WithNotLoggedIn')
74        self.assert_(response.status == 302)
75       
76        try:
77            redirectResponse = response.follow()
78        except paste.fixture.AppError, e:
79            self.failIf('404 Not found' not in str(e), 
80                        "Expecting 404 Not found")
81
82    def test02Skip200WithLoggedIn(self):
83        response = self.app.get('/test_200WithLoggedIn',
84                                extra_environ={'REMOTE_USER': 'testuser'},
85                                status=403)
86
87    def test03Catch401WithLoggedIn(self):
88        response = self.app.get('/test_401WithLoggedIn', 
89                                extra_environ={'REMOTE_USER': 'testuser'},
90                                status=403)
91       
92    def test04Catch200WithNotLoggedIn(self):
93        response = self.app.get('/test_200WithNotLoggedIn')
94        self.assert_(response.status == 302)
95       
96        try:
97            redirectResponse = response.follow()
98        except paste.fixture.AppError, e:
99            self.failIf('404 Not found' not in str(e), 
100                        "Expecting 404 Not found")
101   
102
103if __name__ == "__main__":
104    unittest.main()       
Note: See TracBrowser for help on using the repository browser.