diff options
author | Bryan Bishop <kanzure@gmail.com> | 2014-08-28 12:11:35 -0500 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2014-08-28 12:11:35 -0500 |
commit | 271f5e0991ca31c95cc5ce4b0970e1a50237cf6f (patch) | |
tree | 4837bdecdfa0ac25f69f5400ba8e452dbba5177c | |
parent | 1795c4e7e702cd2f71cdc05f7509a6e7c3aa251e (diff) | |
download | paperbot-271f5e09.tar.gz paperbot-271f5e09.zip |
tests for ezproxy stuff
-rw-r--r-- | dev-requirements.txt | 2 | ||||
-rw-r--r-- | paperbot/ezproxy.py | 35 | ||||
-rw-r--r-- | tests/basetestcase.py | 4 | ||||
-rw-r--r-- | tests/test_ezproxy.py | 62 |
4 files changed, 88 insertions, 15 deletions
diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 0000000..956902a --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1,2 @@ +pytest>=2.6.1 +mock>=1.0.1 diff --git a/paperbot/ezproxy.py b/paperbot/ezproxy.py index d5d348c..d259d94 100644 --- a/paperbot/ezproxy.py +++ b/paperbot/ezproxy.py @@ -19,8 +19,18 @@ EZPROXY_DIR = os.environ.get( ) ) -# Default ezproxy config is empty because none of the files have been loaded yet. -EZPROXY_CONFIG = {} +def load_json_file(path): + """ + Load and parse json representing an ezproxy endpoint. + """ + # open up the file to read ezproxy config + with open(realpath, "r") as configfile: + config = configfile.read() + + # parse config as json + ezconfig = json.loads(config) + + return ezconfig def load_ezproxy_config(ezproxy_dir=EZPROXY_DIR): """ @@ -28,12 +38,11 @@ def load_ezproxy_config(ezproxy_dir=EZPROXY_DIR): as the ezproxy url template, username, password, and possibly other details. """ + ezproxy_config = {} + if not os.path.exists(ezproxy_dir): log.debug("Not loading ezproxy configs because EZPROXY_DIR doesn't exist: {}".format(ezproxy_dir)) - return {} - - # blank the existing ezproxy configs - EZPROXY_CONFIG = {} + return ezproxy_config # look at the directory to see config files filenames = os.listdir(ezproxy_dir) @@ -57,12 +66,8 @@ def load_ezproxy_config(ezproxy_dir=EZPROXY_DIR): # get abspath to this json file realpath = os.path.abspath(os.path.join(ezproxy_dir, filename)) - # open up the file to read ezproxy config - with open(realpath, "r") as configfile: - config = configfile.read() - - # parse config as json - ezconfig = json.loads(config) + # open up the file and read ezproxy config + ezconfig = load_json_file(realpath) # dump in some extra data why not ezconfig.update({ @@ -71,9 +76,9 @@ def load_ezproxy_config(ezproxy_dir=EZPROXY_DIR): }) # store this config for later - EZPROXY_CONFIG[name] = ezconfig + ezproxy_config[name] = ezconfig - return EZPROXY_CONFIG + return ezproxy_config # default action is dothings -load_ezproxy_config() +EZPROXY_CONFIG = load_ezproxy_config() diff --git a/tests/basetestcase.py b/tests/basetestcase.py new file mode 100644 index 0000000..dfe3f1f --- /dev/null +++ b/tests/basetestcase.py @@ -0,0 +1,4 @@ +import unittest + +class BaseTestCase(unittest.TestCase): + pass diff --git a/tests/test_ezproxy.py b/tests/test_ezproxy.py new file mode 100644 index 0000000..a3d1cdc --- /dev/null +++ b/tests/test_ezproxy.py @@ -0,0 +1,62 @@ +""" +Test some of the ezproxy infrastructure stuff. +""" + +import mock + +from basetestcase import BaseTestCase + +from paperbot.ezproxy import ( + load_ezproxy_config, +) + +class EzproxyTestCases(BaseTestCase): + def test_load_ezproxy_config_path_does_not_exist(self): + with mock.patch("os.path.exists", return_value=False) as ospathexists: + load_ezproxy_config() + self.assertTrue(ospathexists.called) + + @mock.patch("os.path.exists", return_value=True) + @mock.patch("os.listdir", return_value=["test.json"]) + @mock.patch("paperbot.ezproxy.load_json_file", return_value={}) + def test_load_ezproxy_config_calls_file_reader(self, load_json_file, oslistdir, ospathexists): + load_ezproxy_config() + self.assertTrue(load_json_file.called) + + @mock.patch("os.path.exists", return_value=True) + @mock.patch("paperbot.ezproxy.load_json_file", return_value={}) + def test_load_ezproxy_config_bails_on_no_json_filename(self, load_json_file, ospathexists): + with mock.patch("os.listdir", return_value=["something.xml"]): + output = load_ezproxy_config() + + # should still have returned a dictionary + self.assertTrue(isinstance(output, dict)) + + # should not have attempted to load xml file + self.assertFalse(load_json_file.called) + + @mock.patch("os.path.exists", return_value=True) + @mock.patch("os.listdir", return_value=["test.json"]) + def test_load_ezproxy_config_loads_dict(self, oslistdir, ospathexists): + testjson = { + "url": "http://httpbin.org/", + "data": { + "User": "jj", + "pw": "91", + }, + } + + with mock.patch("paperbot.ezproxy.load_json_file", return_value=testjson): + output = load_ezproxy_config() + + # should still be a dictionary result + self.assertTrue(isinstance(output, dict)) + + # but the dict should not be empty + self.assertNotEqual(output, {}) + + # this is based on listdir returning "test.json" + self.assertTrue("test" in output.keys()) + + # loaded config should be same as test config + self.assertEqual(output["test"], testjson) |