def testBadEchoWithPosArg(self): "Echo takes one argument (message), with the positional argument it should fail" verb ='POST' url = self.urlbase + 'echo/stuff' input_data={'message': 'unit test'} output={'code':400, 'type':'text/json'} methodTest(verb, url, input_data, output=output)
def testPing(self): verb ='GET' url = self.urlbase + 'ping' output={'code':200, 'type':'text/json', 'data':'"ping"'} expireTime =3600 methodTest(verb, url, output=output, expireTime=expireTime)
def testInnerPingPass(self): verb ='GET' url = self.urlbase + 'foo/ping' output={'code':200, 'data':'"ping"'} expireTime =3600 methodTest(verb, url, output=output, expireTime=expireTime)
def testSupportedFormat(self): rf = RESTFormatter(config=self.config.Webtools) url = self.urlbase +'list1/' for type in rf.supporttypes.keys(): # test accepted type should return 200 error methodTest('GET', url, accept=type, output={'code':200})
def testEncodedInput(self): textType = 'text/plain' url = self.urlbase + 'list3?a=a%&b=b' data = json.dumps({'a': 'a%', 'b': 'b'}) methodTest('GET', url, accept=textType, output={ 'code': 200, 'data': data }) request_input = {'a': '%', 'b': 'b'} #methodTest encoded input with urlencode url = self.urlbase + 'list3' data = json.dumps({'a': '%', 'b': 'b'}) methodTest('GET', url, accept=textType, request_input=request_input, output={ 'code': 200, 'data': data })
def testNoArgMethods(self): """ list1 takes no arguments, it should raise an error if called with one. Require json output. """ return_type = 'application/json' url = self.urlbase + 'list1?int=a' expected_data = """{"exception": 400, "message": "Invalid input", "type": "HTTPError"}""" methodTest('GET', url, accept=return_type, output={'code':400, 'data':expected_data})
def testNoArgMethods(self): """ list1 takes no arguments, it should raise an error if called with one. Require json output. """ return_type = 'application/json' url = self.urlbase + 'list1?int=a' expected_data = """{"exception": 400, "message": "Invalid input: Arguments added where none allowed", "type": "HTTPError"}""" methodTest('GET', url, accept=return_type, output={'code':400, 'data':expected_data})
def testGoodEcho(self): verb ='POST' url = self.urlbase + 'echo' input_data={'message': 'unit test'} output={'code':200, 'type':'text/json', 'data':'{"message": "unit test"}'} methodTest(verb, url, input_data, output=output)
def testBadVerbEcho(self): "echo is only available to GET and POST, so should raise a 501" url = self.urlbase + 'echo' input_data = {'data': 'unit test'} output = {'code':501, 'type':'text/json'} for verb in ['DELETE']: methodTest(verb, url, input_data, output=output)
def testInnerPingError(self): verb ='GET' url = self.urlbase + 'foo/123/ping' output={'code':400} methodTest(verb, url, output=output) url = self.urlbase + 'foo/ping/123' methodTest(verb, url, output=output)
def testBadVerbEcho(self): "echo is only available to GET and POST, so should raise a 501" url = self.urlbase + 'echo' input={'data': 'unit test'} output={'code':501, 'type':'text/json'} for verb in ['DELETE']: methodTest(verb, url, input, output=output)
def testBadMethodEcho(self): """ The echo method isn't supported by GET, so should raise a 405 """ verb ='GET' url = self.urlbase + 'echo' input={'data': 'unit test'} output={'code':405, 'type':'text/json'} methodTest(verb, url, input, output=output)
def testBadMethodEcho(self): """ The echo method isn't supported by GET, so should raise a 405 """ verb ='GET' url = self.urlbase + 'echo' input_data = {'data': 'unit test'} output = {'code':405, 'type':'text/json'} methodTest(verb, url, input_data, output=output)
def testGenerator(self): rf = RESTFormatter(config=self.config.Webtools) url = self.urlbase +'gen' # gen method from DummyRESTModel will return this generator gen = ({'idx':i} for i in range(10)) # the WMCore should convert it into list regardless of accept type data = rf.json(gen) methodTest('GET', url, accept='application/json', output={'code':200, 'data':data}) methodTest('GET', url, accept='*/*', output={'code':200, 'data':data})
def testReturnFormat(self): return_type = 'application/json' url = self.urlbase +'list3?a=a%&b=b' methodTest('GET', url, accept=return_type, output={'code':200, 'data':'{"a": "a%", "b": "b"}'}) url = self.urlbase + 'list?input_int=a&input_str=a' expected_data = '''{"exception": 400, "message": "Invalid input: Input data failed validation.", "type": "HTTPError"}''' methodTest('GET', url, accept=return_type, output={'code':400, 'data':expected_data})
def testReturnFormat(self): return_type = 'application/json' url = self.urlbase +'list3?a=a%&b=b' methodTest('GET', url, accept=return_type, output={'code':200, 'data':'{"a": "a%", "b": "b"}'}) url = self.urlbase + 'list?input_int=a&input_str=a' expected_data = '''{"exception": 400, "message": "Invalid input", "type": "HTTPError"}''' methodTest('GET', url, accept=return_type, output={'code':400, 'data':expected_data})
def testAuthentication(self): verb ='PUT' url = self.urlbase + 'list1' urllib_data = urllib.urlopen(url) self.assertEquals(urllib_data.getcode(), 403) # pass proper role output={'code':200} methodTest(verb, url, output=output, secure=True, secureParam={'key': secureKey, 'role': DUMMY_ROLE, 'group': DUMMY_GROUP, 'site': DUMMY_SITE})
def testEncodedInput(self): type = 'text/plain' url = self.urlbase + 'list3?a=a%&b=b' methodTest('GET', url, accept=type, output={'code':200, 'data':"{'a': 'a%', 'b': 'b'}"}) request_input={'a':'%', 'b':'b'} #methodTest encoded input with urlencode url = self.urlbase +'list3' methodTest('GET', url, accept=type, request_input=request_input, output={'code':200, 'data':"{'a': '%', 'b': 'b'}"})
def testAuthentication(self): verb ='PUT' url = self.urlbase + 'list1' urllib_data = urllib.request.urlopen(url) self.assertEqual(urllib_data.getcode(), 403) # pass proper role output={'code':200} methodTest(verb, url, output=output, secure=True, secureParam={'key': secureKey, 'role': DUMMY_ROLE, 'group': DUMMY_GROUP, 'site': DUMMY_SITE})
def testSpecific404Exception(self): """ Method will raise an HTTPError and return 404 """ url = self.urlbase + 'specific_404_exception' response, expires = methodTest('GET', url, output={'code':404}) assert json.loads(response)['message'] == "I threw a 404", 'got: %s' % json.loads(response)['message']
def testList(self): verb ='GET' url = self.urlbase + 'list/' request_input = {'input_int':123, 'input_str':'abc'} output={'code':200, 'type':'text/json'} result = json.loads(methodTest(verb, url, request_input=request_input, output=output)[0]) for i in result.keys(): self.assertEqual(result[i], request_input[i], '%s does not match response' % i)
def testNotSerialisableException(self): """ Method will raise an EncodeError and return 500 """ url = self.urlbase + 'not_serialisable' response, expires = methodTest('GET', url, output={'code':500}) assert json.loads(response)['message'] == "Server Error", 'got: %s' % json.loads(response)['message'] assert json.loads(response)['type'] == "TypeError", 'got: %s' % json.loads(response)['type']
def testGeneratorMethod(self): # test not accepted type should return 406 error url = self.urlbase + 'gen' output={'code':200} data, _ = methodTest('GET', url, accept='text/json', output=output) data = json.loads(data) self.assertEqual(type(data), list) self.assertEqual(type(data[0]), dict)
def testGenericException(self): """ Method will raise an AssertionError and return 500 """ url = self.urlbase + 'generic_exception' response, expires = methodTest('GET', url, output={'code':500}) assert json.loads(response)['message'] == "Server Error", 'got: %s' % json.loads(response)['message'] assert json.loads(response)['type'] == "AssertionError", 'got: %s' % json.loads(response)['type']
def testGeneratorMethod(self): # test not accepted type should return 406 error url = self.urlbase + 'gen' output={'code':200} data, _ = methodTest('GET', url, accept='text/json', output=output) data = json.loads(data) self.assertEquals(type(data), list) self.assertEquals(type(data[0]), dict)
def testSpecific400Exception(self): """ Method will raise an HTTPError and return 400 """ url = self.urlbase + 'specific_400_exception' response, expires = methodTest('GET', url, output={'code': 400}) assert json.loads( response)['message'] == "I threw a 400", 'got: %s' % json.loads( response)['message']
def testNotSerialisableException(self): """ Method will raise an EncodeError and return 500 """ url = self.urlbase + 'not_serialisable' response, expires = methodTest('GET', url, output={'code': 500}) assert json.loads( response)['message'] == "Server Error", 'got: %s' % json.loads( response)['message'] assert json.loads(response)[ 'type'] == "TypeError", 'got: %s' % json.loads(response)['type']
def testGenericException(self): """ Method will raise an AssertionError and return 500 """ url = self.urlbase + 'generic_exception' response, expires = methodTest('GET', url, output={'code': 500}) assert json.loads( response)['message'] == "Server Error", 'got: %s' % json.loads( response)['message'] assert json.loads( response )['type'] == "AssertionError", 'got: %s' % json.loads(response)['type']
def testBadPing(self): verb = 'GET' url = self.urlbase + 'wrong' output = {'code': 404} methodTest(verb, url, output=output) url = self.urlbase + 'echo' output = {'code': 405} methodTest(verb, url, output=output) url = self.urlbase + 'ping/wrong' output = {'code': 400} methodTest(verb, url, output=output)
def testBadPing(self): verb ='GET' url = self.urlbase + 'wrong' output={'code':404} methodTest(verb, url, output=output) url = self.urlbase + 'echo' output={'code':405} methodTest(verb, url, output=output) url = self.urlbase + 'ping/wrong' output={'code':400} methodTest(verb, url, output=output)
def testOuterFooPass(self): verb ='GET' url = self.urlbase + 'foo' output={'code':200, 'data':'"foo"'} expireTime =3600 methodTest(verb, url, output=output, expireTime=expireTime) url = self.urlbase + 'foo/test' output={'code':200, 'data':'"foo test"'} methodTest(verb, url, output=output, expireTime=expireTime) url = self.urlbase + 'foo' request_input = {'message': 'test'} output={'code':200, 'data':'"foo test"'} methodTest(verb, url, request_input=request_input, output=output, expireTime=expireTime)
def testUnsupportedFormat(self): # test not accepted type should return 406 error url = self.urlbase + 'ping' methodTest('GET', url, accept='text/das', output={'code':406})
def testOuterFooError(self): verb ='GET' url = self.urlbase + 'foo/123/567' output={'code':400} methodTest(verb, url, output=output)