def test_invalidProjectId(self): # None is not a valid project id badId = None try: d = device.Device(badId, _DEVICE_ID) self.assertTrue(False) except ValueError as e: self.assertEqual("projectId must be an int", str(e)) # No non-int project ids badId = "50" try: d = device.Device(badId, _DEVICE_ID) self.assertTrue(False) except ValueError as e: self.assertEqual("projectId must be an int", str(e)) # No negative project ids badId = -1 try: d = device.Device(badId, _DEVICE_ID) self.assertTrue(False) except ValueError as e: self.assertEqual("projectId must be greater than 0", str(e)) # 0 is not a valid project id badId = 0 try: d = device.Device(badId, _DEVICE_ID) self.assertTrue(False) except ValueError as e: self.assertEqual("projectId must be greater than 0", str(e))
def test_invalidDeviceId(self): # None is not a valid device id badId = None try: d = device.Device(_PROJECT_ID, badId) self.assertTrue(False) except ValueError as e: self.assertEqual("deviceId must be a string", str(e)) # Must be a str badId = 1 try: d = device.Device(_PROJECT_ID, badId) self.assertTrue(False) except ValueError as e: self.assertEqual("deviceId must be a string", str(e)) # Cannot be 0-length badId = "" try: d = device.Device(_PROJECT_ID, badId) self.assertTrue(False) except ValueError as e: self.assertEqual("deviceId must be more than 0 characters", str(e))
def registerDevice(self, projectId, deviceId=None, deviceName=None): """Wraps API call `POST /devices` Registers the device in project `projectId` with the iobeam backend. `deviceId` and `deviceName` are optional, if provided they will used in registration, otherwise they will be generated by the backend. Params: projectId - Project ID to register device in deviceId - Desired device ID; if None, will be generated. deviceName - Desired device name; if None, will be generated. Returns: A Device object corresponding to the parameters (explicit and generated); None if there is an error/failure. """ if not self.token: raise request.UnauthorizedError.noTokenSet() endpoint = self.makeEndpoint("devices") r = self.requester().post(endpoint).token(self.token) reqBody = {"project_id": projectId} if deviceId or deviceName: if deviceId: reqBody["device_id"] = deviceId if deviceName: reqBody["device_name"] = deviceName r.setBody(reqBody) r.execute() ret = None if r.getResponseCode() == 201: resp = r.getResponse() ret = device.Device(projectId, resp["device_id"], deviceName=resp["device_name"]) elif r.getResponseCode() == 403: raise request.UnauthorizedError("Invalid credentials.") elif r.getApiErrorCode() == request.ERROR_CODE_DUPLICATE_DEVICE_ID: raise DuplicateIdError() else: self.raiseUnknownCodeError(r) return ret
def test_validConstructorNoName(self): d = device.Device(_PROJECT_ID, _DEVICE_ID) self.assertEqual(_PROJECT_ID, d.projectId) self.assertEqual(_DEVICE_ID, d.deviceId) self.assertTrue(d.deviceName is None)
def test_validConstructor(self): d = device.Device(_PROJECT_ID, _DEVICE_ID, deviceName=_DEVICE_NAME) self.assertEqual(_PROJECT_ID, d.projectId) self.assertEqual(_DEVICE_ID, d.deviceId) self.assertEqual(_DEVICE_NAME, d.deviceName)