class ActionsResource: def __init__(self): self.action_service = ActionService() self.configuration_store = ConfigurationStore() def on_get(self, request, response): Logger.info(LOCATION, INCOMING_REQUEST + METHOD_GET + ' ' + ACTIONS_ENDPOINT) response.media = self.action_service.get_actions() def on_post(self, request, response): configuration = self.configuration_store.get() if configuration.get_device_status() is not DeviceStatus.ACTIVE: error = 'Not allowed to trigger actions when device is not activated.' Logger.error(LOCATION, error) raise falcon.HTTPForbidden(description=error) Logger.info(LOCATION, INCOMING_REQUEST + METHOD_POST + ' ' + ACTIONS_ENDPOINT) data = request.media if ACTION_ID not in data.keys(): Logger.error( LOCATION, 'Missing parameter `' + ACTION_ID + '` for ' + METHOD_POST + ' ' + ACTIONS_ENDPOINT) raise falcon.HTTPBadRequest action_id = data[ACTION_ID] value = data[VALUE_KEY] if VALUE_KEY in data.keys() else None success = self.action_service.trigger(action_id, value) if success: response.media = {'message': 'Action triggered'} else: raise falcon.HTTPServiceUnavailable
def test_create_trigger_body(generate_uuid, get_device_id): as_obj = ActionService() uuid = 42 generate_uuid.return_value = uuid device_id = '43' get_device_id.return_value = device_id action_id = '44' alt_id = '45' value = '46' trigger_body = { ACTION_ID: action_id, DEVICE_ID: device_id, QUEUE_ID: uuid, ALTERNATIVE_ID: alt_id, VALUE: value } assert as_obj._create_trigger_body(action_id, value, alt_id) == trigger_body
def __init__(self): self.action_service = ActionService() self.configuration_store = ConfigurationStore()