source: mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/test/security_filter.py @ 8696

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/test/security_filter.py@8696
Revision 8696, 4.9 KB checked in by mnagni, 7 years ago (diff)

Adds a new optional parameter 'DJ_SECURITY_FILTER' to define which url should undergoes the middleware feature

  • Property svn:mime-type set to text/plain
Line 
1'''
2Created on 30 Nov 2012
3
4@author: mnagni
5'''
6import unittest
7
8from dj_security_middleware.middleware import security_url_filter,\
9    _security_filter
10
11
12class Test(unittest.TestCase):
13
14    def test_security_filter(self):       
15        self.assertFalse(_security_filter(None, None),
16                         "Error with None url and None filter")
17        self.assertFalse(_security_filter(None, []),
18                         "Error with None url and empty filter")   
19        self.assertFalse(_security_filter(' ', None),
20                         "Error with empty url and None filter")       
21        self.assertFalse(_security_filter(' ', []),
22                         "Error with empty url and empty filter")       
23        self.assertFalse(_security_filter(None, ['test']),
24                         "Error with None url")
25        self.assertFalse(_security_filter(' ', ['test']),
26                         "Error with empty url")
27        self.assertFalse(_security_filter('cidev1.jc.rl.ac.uk', ['test']),
28                         "Recognizes not existing string in filter")
29        self.assertTrue(_security_filter('cidev1.jc.rl.ac.uk',
30                                         ['test', 'cidev1.jc.rl.ac.uk']),
31                         "Cannot simple recognize string in filter")
32        self.assertTrue(_security_filter('cidev1.jc.rl.ac.uk',
33                                         ['test', 'cidev1']),
34                         "Cannot matches a simple regexpr")
35        self.assertFalse(_security_filter('cidev1.jc.rl.ac.uk',
36                                         ['test', 'cidev1\.jccc']),
37                         "Matches wrongly a regexpr")       
38        self.assertFalse(_security_filter('cide.jc.rl.ac.uk',
39                                         ['test', 'cidev1']),
40                         "Matches wrongly a regexpr")       
41        self.assertTrue(_security_filter('cidev1.jc.rl.ac.uk',
42                                         ['test', '\.*']),
43                         "Cannot simple recognize string in filter")       
44        self.assertFalse(_security_filter('cidev1.jc.rl.ac.uk',
45                                         ['test', 'jc\.rl']),
46                         "Cannot simple recognize string in filter")
47       
48
49    def test_security_url_filter(self):       
50        self.assertFalse(security_url_filter(None, None),
51                         "Error with None url and None filter")
52        self.assertFalse(security_url_filter(None, []),
53                         "Error with None url and empty filter")   
54        self.assertFalse(security_url_filter(' ', None),
55                         "Error with empty url and None filter")       
56        self.assertFalse(security_url_filter(' ', []),
57                         "Error with empty url and empty filter")       
58        self.assertFalse(security_url_filter(None, ['test']),
59                         "Error with None url")
60        self.assertFalse(security_url_filter(' ', ['test']),
61                         "Error with empty url")
62        self.assertFalse(security_url_filter('http://cidev1.jc.rl.ac.uk', ['']),
63                         "Recognizes not existing string in filter")
64        self.assertFalse(security_url_filter('http://cidev1.jc.rl.ac.uk',
65                                         ['test', '/my_ceda']),
66                         "Cannot simple recognize string in filter")
67        self.assertTrue(security_url_filter('http://cidev1.jc.rl.ac.uk/my_ceda',
68                                         ['test', '/my_ceda']),
69                         "Cannot matches a simple regexpr")
70        self.assertTrue(security_url_filter('http://cidev1.jc.rl.ac.uk/my_ceda/my_internal_page',
71                                         ['test', '/my_ceda']),
72                         "Cannot matches a simple regexpr")
73        self.assertFalse(security_url_filter('http://cidev1.jc.rl.ac.uk/my_ceda/my_internal_page',
74                                         ['test', 'my_ceda']),
75                         "Error, The filter items shall start with '/'")       
76        self.assertFalse(security_url_filter('http://cidev1.jc.rl.ac.uk/my_ceda',
77                                 ['test', '/my_ceda/my_internal_page']),
78                 "Error, Cannot filter inner paths")
79        self.assertTrue(security_url_filter('http://cidev1.jc.rl.ac.uk/my_ceda',
80                         ['test', '/.*']),
81         "Error, Cannot filter global regexpr")
82        self.assertTrue(security_url_filter('http://cidev1.jc.rl.ac.uk/my_ceda/1_test',
83                         ['test', '/my_ceda/[1-2]']),
84         "Error, Cannot filter global regexpr")               
85        self.assertFalse(security_url_filter('http://cidev1.jc.rl.ac.uk/my_ceda/3_test',
86                         ['test', '/my_ceda/[1-2]']),
87         "Error, Cannot filter global regexpr")       
88
89
90
91   
92   
93if __name__ == "__main__":
94    #import sys;sys.argv = ['', 'Test.test_security_filter']
95    unittest.main()
Note: See TracBrowser for help on using the repository browser.