def testb_history_room_temperature_1hour(self): req = GET( config.ui_host, config.ui_port, "/gateways/1284174504043136/devices/0xa335d00019f5234e/sensors/0/history" + "?range=hour") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(14, len(result["data"])) # again, but ask for min and max req = GET( config.ui_host, config.ui_port, "/gateways/1284174504043136/devices/0xa335d00019f5234e/sensors/0/history" + "?range=hour&aggregation=min,max") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(14, len(result["data"])) self.assertEqual(2, len(result["data"][0]["values"])) # again, but ask for min, max and avg req = GET( config.ui_host, config.ui_port, "/gateways/1284174504043136/devices/0xa335d00019f5234e/sensors/0/history" + "?range=hour&aggregation=min,max,avg") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(14, len(result["data"])) self.assertEqual(3, len(result["data"][0]["values"])) # the same request with explicit timestamps start = datetime.now() - timedelta(0, 60 * 60) end = datetime.now() req = GET( config.ui_host, config.ui_port, "/gateways/1284174504043136/devices/0xa335d00019f5234e/sensors/0/history" + "?range=%s,%s" % (start.strftime("%s"), end.strftime("%s"))) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(14, len(result["data"]))
def test2_cannot_update_myself_atall(self): req = GET(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/roles/current") req.authorize(self.session) response, content = req() self.assertEqual(303, response.status) req = GET(config.ui_host, config.ui_port, response.headers["Location"]) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(200, result["code"]) result = json.loads(content) req = PUT( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/roles/" + result["data"]["id"]) req.authorize(self.session) req.body(json.dumps({"access_level": "admin"})) response, content = req() self.assertEqual(403, response.status)
def test2_notice_with_custom_key(self): id = str(uuid.uuid4()) req = GET(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/messages") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) messages_count = len(json.loads(content)["data"]) msg = json.dumps({ "id": id, "message_type": "notice_request", "at": 1546363722909123, "severity": "info", "key": "some_message", "context": { "reason": "unknown", "attempt": 1, "valid": True } }) self.ws.send(msg) response = json.loads(self.ws.recv()) self.assertEqual("generic_response", response["message_type"]) self.assertEqual(id, response["id"]) assureNotClosed(self, self.ws) req = GET(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/messages") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) messages = json.loads(content)["data"] self.assertEqual(messages_count + 1, len(messages)) message = None for m in messages: if m["at"] == 1546363722909: message = m self.assertIsNotNone(message) self.assertEqual("info", message["severity"]) self.assertEqual("gateway.some_message", message["key"]) self.assertEqual("unknown", message["context"]["reason"]) self.assertEqual(1, message["context"]["attempt"]) self.assertEqual(True, message["context"]["valid"])
def test1_assign_unassign(self): # assign a prepared gateway req = POST(config.ui_host, config.ui_port, "/gateways") req.authorize(self.session) req.body( json.dumps({ "id": config.gateway_id, "name": "My Home", "timezone_id": "Europe/Prague" })) response, content = req() self.assertEqual(201, response.status) self.assertEqual("/gateways/" + config.gateway_id, response.getheader("Location")) # check the gateway's contents resultLink = response.getheader("Location") req = GET(config.ui_host, config.ui_port, resultLink) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual("My Home", result["data"]["name"]) self.assertEqual(config.gateway_id, result["data"]["id"]) self.assertEqual("Europe/Prague", result["data"]["timezone"]["id"]) # unassign the gateway req = DELETE(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id) req.authorize(self.session) response, content = req() self.assertEqual(204, response.status) # test the gateway is inaccessible req = GET(config.ui_host, config.ui_port, resultLink) req.authorize(self.session) response, content = req() self.assertEqual(403, response.status) result = json.loads(content) self.assertEqual(403, result["code"]) self.assertEqual("not enough permission to access the resource", result["message"])
def test6_list_non_existing_device(self): req = GET(config.ui_host, config.ui_port, "/gateways/1284174504043136/devices/0x4471959aad24618f/controls") req.authorize(self.session) response, content = req() self.assertEqual(404, response.status)
def test4_current_non_existing_sensor(self): req = GET(config.ui_host, config.ui_port, "/gateways/1284174504043136/devices/0xa371959aad24618e/controls/1000/current") req.authorize(self.session) response, content = req() self.assertEqual(404, response.status)
def getDevices(self, gwId): req = GET("localhost", globalConfig.uiPort, "/gateways/" + str(gwId) + "/devices") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) return json.loads(content)['data']
def assignGateway(self, gwId, name): req = POST("localhost", globalConfig.uiPort, "/gateways") req.authorize(self.session) req.body(json.dumps({ "id": gwId, "name": name, "timezone_id": "Europe/Prague" })) response, content = req() self.assertEqual(201, response.status) self.assertEqual("/gateways/" + gwId, response.getheader("Location")) # check the gateway's contents resultLink = response.getheader("Location") req = GET("localhost", globalConfig.uiPort, resultLink) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual("My Home", result["data"]["name"]) self.assertEqual(gwId, result["data"]["id"]) self.assertEqual("Europe/Prague", result["data"]["timezone"]["id"]) return resultLink
def currentDeviceValue(self, gwId, deviceId, moduleId): req = GET("localhost", globalConfig.uiPort, "/gateways/" + str(gwId) + "/devices/" + str(deviceId) + "/sensors/"+str(moduleId)+"/current") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) return json.loads(content)['data']
def test1_keys(self): req = GET(config.ui_host, config.ui_port, "/gateways/devices/properties/keys") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(5, len(result["data"])) self.assertEqual("ip-address", result["data"][0]["key"]) self.assertEqual("IP address", result["data"][0]["display_name"]) self.assertNotIn("read-only", result["data"][0]) self.assertNotIn("write-only", result["data"][0]) self.assertEqual("password", result["data"][1]["key"]) self.assertEqual("Password", result["data"][1]["display_name"]) self.assertTrue(result["data"][1]["write-only"]) self.assertEqual("firmware", result["data"][2]["key"]) self.assertEqual("Firmware", result["data"][2]["display_name"]) self.assertTrue(result["data"][2]["read-only"]) self.assertEqual("mac-address", result["data"][3]["key"]) self.assertEqual("MAC address", result["data"][3]["display_name"]) self.assertTrue(result["data"][3]["read-only"]) self.assertEqual("serial", result["data"][4]["key"]) self.assertEqual("Serial number", result["data"][4]["display_name"]) self.assertTrue(result["data"][4]["read-only"])
def test9_check_bitmap_with_group(self): req = GET(config.ui_host, config.ui_port, "/types/bitmap/MOD_CURRENT_BOILER_OT_OEM_FAULTS") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) bitmap = result["data"] self.assertEqual("OT OEM Faults", bitmap["name"]) self.assertTrue("groups" in bitmap) self.assertEqual(1, len(bitmap["groups"])) self.assertEqual("OEM specific", bitmap["groups"][0]["name"]) self.assertEqual(8, len(bitmap["groups"][0]["mapping"])) self.assertEqual(0, bitmap["groups"][0]["mapping"][0]) self.assertEqual(1, bitmap["groups"][0]["mapping"][1]) self.assertEqual(2, bitmap["groups"][0]["mapping"][2]) self.assertEqual(3, bitmap["groups"][0]["mapping"][3]) self.assertEqual(4, bitmap["groups"][0]["mapping"][4]) self.assertEqual(5, bitmap["groups"][0]["mapping"][5]) self.assertEqual(6, bitmap["groups"][0]["mapping"][6]) self.assertEqual(7, bitmap["groups"][0]["mapping"][7])
def test3_discover(self): req = POST(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/discovery") req.authorize(self.session) req.body(json.dumps({"time_limit": 1})) response, content = req() self.assertEqual(202, response.status) data = json.loads(content) self.assertEqual("success", data["status"]) scan_uri = response.getheader("Location") self.assertEqual("/gateways/" + config.gateway_id + "/discovery", scan_uri) for i in range(10): req = GET(config.ui_host, config.ui_port, scan_uri) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertTrue("success", result["status"]) if result["data"]["state"]["name"] == "finished": break self.assertTrue("started" in result["data"]) self.assertTrue("duration" in result["data"]) self.assertEqual(1, result["data"]["duration"]) time.sleep(1)
def assure_range(self, id, name, min, max, step): req = GET(config.ui_host, config.ui_port, "/types/" + id) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) type = result["data"] self.assertEqual(name, type["name"]) self.assertTrue("range" in type) if min is not None: self.assertTrue("min" in type["range"]) self.assertEqual(min, type["range"]["min"]) else: self.assertFalse("min" in type["range"]) if max is not None: self.assertTrue("max" in type["range"]) self.assertEqual(max, type["range"]["max"]) else: self.assertFalse("max" in type["range"]) if step is not None: self.assertTrue("step" in type["range"]) self.assertEqual(step, type["range"]["step"]) else: self.assertFalse("step" in type["range"])
def test2_current_locale_is_en(self): req = GET(config.ui_host, config.ui_port, "/profiles/current") req.authorize(self.session) response, _ = req() self.assertEqual(303, response.status) req = GET(config.ui_host, config.ui_port, response.headers["Location"]) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual("en", result["data"]["locale"])
def test4_list_sensors_of_device_unknown(self): req = GET( config.ui_host, config.ui_port, "/gateways/1284174504043136/devices/0xa371959aad24618e/sensors") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(9, len(result["data"])) self.assert_find_sensor(result["data"], "0", "temperature", "temperature", "0", False) self.assert_find_sensor(result["data"], "1", "humidity", "humidity", "1", True) self.assert_find_sensor(result["data"], "2", "noise", "noise", "2", False) self.assert_find_sensor(result["data"], "3", "temperature", "temperature", "3", False) self.assert_find_sensor(result["data"], "4", "humidity", "humidity", "4", False) self.assert_find_sensor(result["data"], "5", "noise", "noise", "5", False) self.assert_find_sensor(result["data"], "6", "temperature", "temperature", "6", False) self.assert_find_sensor(result["data"], "7", "humidity", "humidity", "7", False) self.assert_find_sensor(result["data"], "8", "noise", "noise", "8", False)
def test1_auth_returns_strict_transport_security(self): req = GET(config.ui_host, config.ui_port, "/auth") response, _ = req() self.assertEqual(200, response.status) self.assertIn("Strict-Transport-Security", response.headers) self.assertEqual("max-age=31536000; includeSubDomains", response.headers["Strict-Transport-Security"])
def test7_create_get_remove_password(self): req = GET( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/password") req.authorize(self.session) response, content = req() self.assertEqual(404, response.status) result = json.loads(content) self.assertEqual("error", result["status"]) req = POST( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/password") req.authorize(self.session) req.body(json.dumps({"value": "some-password"})) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) req = GET( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/password") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual("password", result["data"]["key"]) self.assertEqual("Password", result["data"]["display_name"]) self.assertIsNone(result["data"]["value"]) self.assertTrue(result["data"]["write-only"]) self.assertNotIn("read-only", result["data"]) req = DELETE( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/password") req.authorize(self.session) response, content = req() self.assertEqual(204, response.status)
def test6_create_get_remove_ip_address(self): req = GET( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/ip-address") req.authorize(self.session) response, content = req() self.assertEqual(404, response.status) result = json.loads(content) self.assertEqual("error", result["status"]) req = POST( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/ip-address") req.authorize(self.session) req.body(json.dumps({"value": "192.168.0.1"})) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) req = GET( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/ip-address") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual("ip-address", result["data"]["key"]) self.assertEqual("IP address", result["data"]["display_name"]) self.assertEqual("192.168.0.1", result["data"]["value"]) self.assertNotIn("read-only", result["data"]) self.assertNotIn("write-only", result["data"]) req = DELETE( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/ip-address") req.authorize(self.session) response, content = req() self.assertEqual(204, response.status)
def fetch_zone_by_id(self, id): req = GET(config.ui_host, config.ui_port, "/time/zones/" + id) response, content = req() self.assertEqual(200, response.status) data = json.loads(content) self.assertEqual("success", data["status"]) return data["data"]
def test1_list_all(self): req = GET(config.ui_host, config.ui_port, "/types") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(30, len(result["data"]))
def test2_exploit_get_uri(self): req = GET(config.ui_host, config.ui_port, "/gateways/%c0%ae%c0%ae") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(200, result["code"]) self.assertEqual(0, len(result["data"]))
def test5_get_nonexisting_key(self): req = GET( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/0xa335d00019f5234e/properties/sssss") req.authorize(self.session) response, content = req() self.assertEqual(400, response.status) result = json.loads(content) self.assertEqual("error", result["status"])
def test1_notice_valid(self): id = str(uuid.uuid4()) req = GET(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/messages") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) messages_count = len(json.loads(content)["data"]) msg = json.dumps({ "id": id, "message_type": "notice_request", "at": 1546363722908213, "severity": "error", "key": "pair_device_failed", "context": { "gateway_id": config.gateway_id, "device_id": "0xa701020311223344" } }) self.ws.send(msg) response = json.loads(self.ws.recv()) self.assertEqual("generic_response", response["message_type"]) self.assertEqual(id, response["id"]) assureNotClosed(self, self.ws) req = GET(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/messages") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) messages = json.loads(content)["data"] self.assertEqual(messages_count + 1, len(messages)) self.assertEqual(1546363722908, messages[0]["at"]) self.assertEqual("error", messages[0]["severity"]) self.assertEqual("gateway.pair_device_failed", messages[0]["key"]) self.assertEqual(config.gateway_id, messages[0]["context"]["gateway_id"]) self.assertEqual("0xa701020311223344", messages[0]["context"]["device_id"])
def test1_current(self): req = GET(config.ui_host, config.ui_port, "/profiles/current") req.authorize(self.session) response, content = req() self.assertEqual(303, response.status) req = GET(config.ui_host, config.ui_port, response.headers["Location"]) req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual("*****@*****.**", result["data"]["email"]) self.assertEqual("Joe", result["data"]["first_name"]) self.assertEqual("Doe", result["data"]["last_name"]) self.assertEqual("google", result["data"]["provider"])
def test2_inaccessible(self): req = GET(config.ui_host, config.ui_port, "/profiles/ab9996fa-5252-4df5-a3f6-1d7efde7a021") req.authorize(self.session) response, content = req() self.assertEqual(403, response.status) result = json.loads(content) self.assertEqual("error", result["status"]) self.assertEqual("not enough permission to access the resource", result["message"])
def test2_detail_of_non_existing(self): req = GET(config.ui_host, config.ui_port, "/types/12904232") req.authorize(self.session) response, content = req() self.assertEqual(404, response.status) result = json.loads(content) self.assertEqual("error", result["status"]) self.assertEqual("requested resource does not exist", result["message"])
def test7_detail_big_euid(self): req = GET( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/" + "1208925819614629174706175") req.authorize(self.session) response, content = req() self.assertEqual(400, response.status) result = json.loads(content) self.assertEqual("error", result["status"]) self.assertEqual("could not process input data", result["message"])
def test6_detail_invalid_euid(self): req = GET( config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices/" + "a1231r30430") req.authorize(self.session) response, content = req() self.assertEqual(400, response.status) result = json.loads(content) self.assertEqual("error", result["status"]) self.assertEqual("could not process input data", result["message"])
def test4_list_active(self): req = GET(config.ui_host, config.ui_port, "/gateways/" + config.gateway_id + "/devices?filter=active") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual(3, len(result["data"]))
def test3_detail_of_battery(self): req = GET(config.ui_host, config.ui_port, "/types/battery") req.authorize(self.session) response, content = req() self.assertEqual(200, response.status) result = json.loads(content) self.assertEqual("success", result["status"]) self.assertEqual("battery", result["data"]["name"]) self.assertEqual("%", result["data"]["unit"])