def setUp(self): load_crypto('./keys') self.api = WebController(key='./keys') self.api.opener = OpenerProxy() self.func = WebControllerFunction(self.api, 'method_name')
class WebControllerFunctionTestCase(unittest.TestCase): def setUp(self): load_crypto('./keys') self.api = WebController(key='./keys') self.api.opener = OpenerProxy() self.func = WebControllerFunction(self.api, 'method_name') def test_trivial(self): """ Trivial test Verifies: * function is created """ self.assert_(self.func) def test_make_url(self): """ url for master api is constructed Verifies: * url matches format """ url = self.func.make_url() self.assert_('http://localhost:18801/method_name') def test_call(self): """ api call without args Verifies: * no exception thrown * response is as expected """ response = self.func() self.assertEqual(1, response) def test_call_with_args(self): """ API call with args and kwargs Verifies: * no exception thrown * response is as expected """ args = [1,2,3] kwargs = dict(a=1, b=2, c=3) response = self.func(*args, **kwargs) self.assertEqual(1, response) def test_call_404(self): """ API call for function that does not exist Verifies: * ControllerException is raised """ error = urllib2.HTTPError(None, 404, None, None, None) self.api.opener.raise_=error self.assertRaises(ControllerException, self.func) def test_call_401(self): """ API call for function that requires authorization Verifies: * authentication is triggered """ error = urllib2.HTTPError(None, 401, None, None, None) self.api.opener.raise_=error self.func._authenticate = CallProxy(self.func._authenticate, False) self.func() self.func._authenticate.assertCalled(self) def test_call_500(self): """ API call for function that has a serverside error Verifies: * ControllerRemoteException is raised * response incluse exception and stacktrace """ exception = {'exception':'FakeException','traceback':'fake stack trace'} error = urllib2.HTTPError(None, 500, simplejson.dumps(exception), None, None) self.api.opener.raise_=error try: self.func() self.fail('function should have raised ControllerRemoteException') except ControllerRemoteException, e: self.assert_(e.error, 'exception name is not set') self.assert_(e.traceback, 'traceback is not set ')