source: ceda_http_fileserver/trunk/ceda_http_fileserver/ceda/server/wsgi/fileserver/test/test_fileserver.py @ 7022

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ceda_http_fileserver/trunk/ceda_http_fileserver/ceda/server/wsgi/fileserver/test/test_fileserver.py@7022
Revision 7022, 6.0 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 9: Data Browser Replacement:

  • Tested correct retrieval for sub-dir trees
  • Tested for forbidden access and file not found

Next: add regular expression filtering to exposing unwanted files over HTTP interface

  • Property svn:keywords set to Id
Line 
1"""CEDA (Centre for Environmental Data Archival) HTTP file server application
2unit test module
3
4CEDA HTTP File Server WSGI Application
5"""
6__author__ = "P J Kershaw"
7__date__ = "11/06/10"
8__copyright__ = "(C) 2010 Science and Technology Facilities Council"
9__license__ = """http://www.apache.org/licenses/LICENSE-2.0"""
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = '$Id$'
12import logging
13logging.basicConfig(level=logging.DEBUG)
14
15import unittest
16from os import path
17import os
18import httplib
19import urllib
20
21import paste.fixture
22from paste.deploy import loadapp
23
24from ceda.server.wsgi.fileserver.app import FileServerApp
25
26
27class FileServerAppTestCase(unittest.TestCase):
28    THIS_DIR = path.abspath(path.dirname(__file__))
29    HTDOCS_DIRNAME = 'htdocs'
30    HTDOCS_DIR = path.join(THIS_DIR, HTDOCS_DIRNAME)
31    HTDOCS_SUBDIR1 = 'sub.dir-1'
32    HTDOCS_SUBDIR2 = path.join('sub.dir-1', '.sub-dir2')
33    HTDOCS_SUBDIR3 = path.join('sub.dir-1', 'SubDir 3')
34   
35    PNG_REL_FILEPATH = path.join(HTDOCS_SUBDIR1,
36                                 'a png with uppercase suffix.PNG')
37    PNG_REL_URIPATH = '/' + urllib.pathname2url(PNG_REL_FILEPATH)
38   
39    JPEG_REL_FILEPATH = 'my test jpeg.jpg'
40    JPEG_REL_URIPATH = '/' + urllib.pathname2url(JPEG_REL_FILEPATH)
41   
42    PLAIN_TEXT_REL_FILEPATH = 'plain-text.file.txt'
43    PLAIN_TEXT_REL_URIPATH = '/' + urllib.pathname2url(PLAIN_TEXT_REL_FILEPATH)
44   
45    HTML_REL_FILEPATH = 'my file.html'
46    HTML_REL_URIPATH = '/' + urllib.pathname2url(HTML_REL_FILEPATH)
47   
48    PDF_FILENAME = 'a test PDF.pdf'
49    PDF_REL_FILEPATH = path.join(HTDOCS_SUBDIR3, PDF_FILENAME)
50    PDF_REL_URIPATH = '/' + urllib.pathname2url(PDF_REL_FILEPATH)
51   
52    def __init__(self, *args, **kwargs):
53        self.fileServerApp = FileServerApp(self.__class__.HTDOCS_DIR)
54        self.app = paste.fixture.TestApp(self.fileServerApp)
55         
56        unittest.TestCase.__init__(self, *args, **kwargs)
57
58    def test01Assert(self):
59        response = self.app.get('/', status=200)
60        print(response.body)
61        self.assert_(response.body)
62        self.assert_(self.__class__.HTML_REL_FILEPATH in response.body)
63
64    def test02CheckBlkSizeValidation(self):
65        def _set(x):
66            self.fileServerApp.readBlkSize = x
67           
68        self.assertRaises(ValueError, _set, 'my block size') 
69        self.assertRaises(ValueError, _set, -10)
70       
71    def test03CheckHttpMethodValidation(self):
72        def _set():
73            self.fileServerApp.httpMethodMap = None
74           
75        self.assertRaises(TypeError, _set)
76       
77    def test04TrapHttpMethodNotAllowed(self):
78        self.fileServerApp.httpMethodMap = {}
79        response = self.app.get('/', status=httplib.METHOD_NOT_ALLOWED)
80
81    def test05GetJPEG(self):
82        # Test jpeg is returned with correct content type
83        response = self.app.get(self.__class__.JPEG_REL_URIPATH, 
84                                status=httplib.OK)
85        print(response.headers)
86        print(response.body)
87        self.assert_(response.header_dict['content-type'] == 'image/jpeg')
88
89    def test06GetPngWithUppercaseSuffix(self):
90        # Test PNG is returned with correct content type
91        response = self.app.get(self.__class__.PNG_REL_URIPATH, 
92                                status=httplib.OK)
93        print(response.headers)
94        print(response.body)
95        self.assert_(response.header_dict['content-type'] == 'image/png')
96
97    def test07WriteBlocks(self):
98        # Test writing output from server in blocks
99        self.fileServerApp.writeBlkSize = 8
100        localFilePath = path.join(self.__class__.HTDOCS_DIR, 
101                                  self.__class__.PNG_REL_FILEPATH)
102        fileContent = open(localFilePath, 'rb').read()
103        response = self.app.get(self.__class__.PNG_REL_URIPATH, 
104                                status=httplib.OK)
105        print(response.headers)
106        print(response.body)
107        self.assert_(response.header_dict['content-type'] == 'image/png')
108        self.assert_(response.body == fileContent)
109
110    def test08FileNotFound(self):
111        response = self.app.get('/abc', status=httplib.NOT_FOUND)
112        print(response.headers)
113        print(response.body)
114
115    def test09ListSubDir(self):
116        # list content of a sub-directory
117        subDirUriPath = '/' + urllib.pathname2url(self.__class__.HTDOCS_SUBDIR3)
118        response = self.app.get(subDirUriPath, status=httplib.OK)
119        print(response.body)
120        self.assert_(self.__class__.PDF_FILENAME in response.body)
121
122    def test10PathFilter(self):
123        # Test filter which filters out path matching a given regular expression
124        pass
125     
126    def test11AccessForbidden(self):
127       
128        jpegFilePath = path.join(self.__class__.HTDOCS_DIR,
129                                 self.__class__.JPEG_REL_FILEPATH)
130        try:
131            # Get file permissions for the file
132            mode = os.stat(jpegFilePath).st_mode
133           
134            # Make file inaccessible
135            os.chmod(jpegFilePath, 0000)
136
137            response = self.app.get(self.__class__.JPEG_REL_URIPATH, 
138                                    status=httplib.FORBIDDEN)
139            print(response.headers)
140            print(response.body)
141           
142        finally:
143            # Restore the original file permissions
144            os.chmod(jpegFilePath, mode)
145   
146    def test12WithAltMountPoint(self):
147        # Test with alternative mount point to the default '/'
148        self.fileServerApp.mountPoint = '/file-server'
149               
150       
151class FileServerAppTestCaseBase(unittest.TestCase): 
152    """Base class for common Paste Deploy related set-up"""
153    THIS_DIR = path.abspath(path.dirname(__file__))
154    INI_FILENAME = 'fileserver.ini'
155    INI_FILEPATH = path.join(THIS_DIR, INI_FILENAME)
156   
157    def __init__(self, *args, **kwargs):
158        wsgiapp = loadapp('config:' + self.__class__.INI_FILEPATH)
159        self.app = paste.fixture.TestApp(wsgiapp)
160               
161        unittest.TestCase.__init__(self, *args, **kwargs) 
162
163
164if __name__ == "__main__":
165    unittest.main()
166
Note: See TracBrowser for help on using the repository browser.