Skip to content

Commit a4fd831

Browse files
committed
Implement a test covering operation of the cat funcitonality file.
Make use of the seam that exists by virtue of the functionality files return output objects which are intrepreted by a wrapper to unit test the behaviour of cat. Specifically, we can arrange for a suitable environment plus arguments and then check the right output_objects are created. Allow doing this by exposing a variant of the cat main method which returns them rather than invoking the outer wrapper. Name this _main and call it from the original main. Note that this change is in effect a blueprint that can be used when adding coverage for other functionaity files.
1 parent 9180ec5 commit a4fd831

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

mig/shared/functionality/cat.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def main(client_id, user_arguments_dict, environ=None):
6161

6262
(configuration, logger, output_objects, op_name) = \
6363
initialize_main_variables(client_id)
64+
65+
return _main(configuration, logger, client_id=client_id, output_objects=output_objects, user_arguments_dict=user_arguments_dict)
66+
67+
def _main(configuration, logger, client_id=None, output_objects=[], user_arguments_dict=None, environ=None):
68+
if logger is None:
69+
logger = configuration.logger
70+
6471
client_dir = client_id_dir(client_id)
6572
defaults = signature()[1]
6673
status = returnvalues.OK

tests/support/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,16 @@ def temppath(relative_path, test_case, ensure_dir=False, skip_clean=False):
340340
"""Get absolute temp path for relative_path"""
341341
assert isinstance(test_case, MigTestCase)
342342
tmp_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
343+
return _temppath(tmp_path, test_case, ensure_dir=ensure_dir, skip_clean=skip_clean)
344+
345+
346+
def _temppath(tmp_path, test_case, ensure_dir=False, skip_clean=False):
343347
if ensure_dir:
344348
try:
345349
os.mkdir(tmp_path)
346350
except FileExistsError:
347351
raise AssertionError(
348-
"ABORT: use of unclean output path: %s" % relative_path)
352+
"ABORT: use of unclean output path: %s" % tmp_path)
349353
if not skip_clean:
350354
test_case._cleanup_paths.add(tmp_path)
351355
return tmp_path

tests/test_mig_cgibin_cat.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from __future__ import print_function
2+
import importlib
3+
import os
4+
import sys
5+
6+
from tests.support import MIG_BASE, MigTestCase, testmain, _temppath
7+
8+
from mig.shared.base import client_id_dir
9+
from mig.shared.functionality.cat import _main as main
10+
11+
12+
def create_wsgi_environ(configuration, wsgi_variables={}):
13+
environ = {}
14+
environ['wsgi.input'] = ()
15+
environ['MIG_CONF'] = configuration.config_file
16+
environ['HTTP_HOST'] = wsgi_variables.get('http_host', 'localhost')
17+
environ['PATH_INFO'] = wsgi_variables.get('path_info', '/')
18+
environ['REMOTE_ADDR'] = wsgi_variables.get('remote_addr', '127.0.0.1')
19+
environ['SCRIPT_URI'] = ''.join(('http://', environ['HTTP_HOST'], environ['PATH_INFO']))
20+
return environ
21+
22+
23+
class MigCgibinCat(MigTestCase):
24+
TEST_CLIENT_ID = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=test@example.com'
25+
26+
def _provide_configuration(self):
27+
return 'testconfig'
28+
29+
def before_each(self):
30+
user_home = self.configuration.user_home[:-1]
31+
client_dir = client_id_dir(self.TEST_CLIENT_ID)
32+
# create the test user home directory
33+
self.test_user_dir = _temppath(os.path.join(user_home, client_dir), self, ensure_dir=True)
34+
self.test_environ = create_wsgi_environ(self.configuration)
35+
36+
def test_returns_file_output_with_single_file_match(self):
37+
with open(os.path.join(self.test_user_dir, 'foobar.txt'), 'w'):
38+
pass
39+
payload = {
40+
'path': ['foobar.txt'],
41+
}
42+
43+
(output_objects, status) = main(self.configuration, self.logger, self.TEST_CLIENT_ID, user_arguments_dict=payload, environ=self.test_environ)
44+
self.assertEqual(len(output_objects), 1)
45+
output_obj = output_objects[0]
46+
self.assertEqual(output_obj['object_type'], 'file_output')
47+
48+
49+
if __name__ == '__main__':
50+
testmain()

0 commit comments

Comments
 (0)