def test_APIDocs(self): def _cb(result): expectedResult = "<title>API Information for v1</title>" self.assertSubstring(expectedResult, result) return rm.testItem(self.api.getService().apiInfo_v1, "/v1/apiInfo",{} ).addBoth(_cb)
def test_blankServiceClass(self): def _cb(result): [error] = self.flushLoggedErrors() self.assertIsInstance(error.value, AttributeError) return rm.testItem( self.api.getService().api_v1_weather_GET,"/v1/weather", {"postcode": "9999", "unixTimestamp": "1"}).addBoth(_cb)
def test_authRequired(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "fail", "data": "Authentication required."} """)) self.assertEqual(expectedResult, result) return rm.testItem( self.api.getService().api_v1_authtest_GET, "/v1/authtest", {} ).addBoth(_cb)
def test_rootAPI(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "success", "data": {"message": "a", "setBy": "a", "setWhen": 0}} """)) self.assertEqual(expectedResult, result) return rm.testItem(self.api.getService().api_vROOT_motd_GET, "/motd", {}).addBoth(_cb)
def test_requiredParam(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "fail", "data": "Missing request parameters: \'postcode\'"} """)) self.assertEqual(expectedResult, result) return rm.testItem( self.api.getService().api_v1_weather_GET, "/v1/weather", {"unixTimestamp": "1"}).addBoth(_cb)
def test_authFailureByDefault(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "fail", "data": "Not a real authenticator."} """)) self.assertEqual(expectedResult, result) return rm.testItem(self.api.getService().api_v1_authtest_GET, "/v1/authtest", {}, headers={"Authorization": ["Basic QZxaZGRpbjpvcGVuIHNlc2FtZQ=="]} ).addBoth(_cb)
def test_returnList(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "success", "data": [{"message": "NO MOTD SET", "setBy": "nobody", "setWhen": 0}]} """)) self.assertEqual(expectedResult, result) return rm.testItem(self.api.getService().api_v2_motd_GET, "/v2/motd", {}).addBoth(_cb)
def test_usesServiceClass(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "success", "data": { "windSpeed": 20, "temperature": 30, "isRaining": false}} """)) self.assertEqual(expectedResult, result) return rm.testItem( self.api.getService().api_v1_weather_GET, "/v1/weather", {"postcode": "9999", "unixTimestamp": "1"}).addBoth(_cb)
def test_unsupportedAuthType(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "fail", "data": "Unsupported Authorization type."} """)) self.assertEqual(expectedResult, result) return rm.testItem(self.api.getService().api_v1_authtest_GET, "/v1/authtest", {}, headers={"Authorization": ["digest dfgdf"]} ).addBoth(_cb)
def test_malformedAuthHeader(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "fail", "data": "Malformed Authentication header."} """)) self.assertEqual(expectedResult, result) return rm.testItem(self.api.getService().api_v1_authtest_GET, "/v1/authtest", {}, headers={"Authorization": ["notvalidlol"]} ).addBoth(_cb)
def test_nonSpecifiedParam(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "fail", "data": "Unexpected request parameters: \'muffin\'"} """)) self.assertEqual(expectedResult, result) return rm.testItem( self.api.getService().api_v1_weather_GET, "/v1/weather", {"postcode": "9999", "unixTimestamp": "1", "muffin": "yes plz"} ).addBoth(_cb)
def test_authSuccess(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "success", "data": {"canonicalUsername": "******"}} """)) self.assertEqual(expectedResult, result) return rm.testItem(self.api.getService().api_v1_authtest_GET, "/v1/authtest", {}, headers={"Authorization": ["Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="]} ).addBoth(_cb)
def test_incorrectParamReturn(self): def _cb(result): [error] = self.flushLoggedErrors() self.assertIsInstance(error.value, haddock.api.BadResponseParams) params = { "message": "hi", "username": "******" } return rm.testItem( self.api.getService().api_v2_motd_POST, "/v2/motd/POST", params, method="POST", useBody=True).addBoth(_cb)
def test_jsonBody(self): def _cb(result): expectedResult = json.dumps(json.loads(""" {"status": "success", "data": {"status": "OK"}} """)) self.assertEqual(expectedResult, result) params = { "message": "hi", "username": "******" } return rm.testItem( self.api.getService().api_v1_motd_POST, "/v1/motd/POST", params, method="POST", useBody=True).addBoth(_cb)