예제 #1
0
    def test_from_json(self):
        plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
        encrypted_field1 = EncryptedDeviceField('test3', 'boolean', True)
        encrypted_field2 = EncryptedDeviceField('test4', 'string', 'something else')
        encrypted_fields = [encrypted_field1, encrypted_field2]

        encrypted_field_json1 = encrypted_field1.as_json()
        encrypted_field_json2 = encrypted_field2.as_json()
        encrypted_field_json1_with_value = dict(encrypted_field_json1)
        encrypted_field_json2_with_value = dict(encrypted_field_json2)
        encrypted_field_json1_with_value['value'] = True
        encrypted_field_json2_with_value['value'] = 'something_else'

        fields_json = [field.as_json() for field in plaintext_fields]
        fields_json.extend([encrypted_field_json1_with_value, encrypted_field_json2_with_value])

        device = Device.from_json({'name': 'test', 'fields': fields_json, 'type': 'something', 'description': 'desc'})

        expected_plaintext_fields_as_json = [field.as_json() for field in plaintext_fields]
        expected_encrypted_fields_as_json = [field.as_json() for field in encrypted_fields]

        self.assertEqual(device.name, 'test')
        self.assertEqual(device.type, 'something')
        self.assertEqual(device.description, 'desc')
        for field in device.plaintext_fields:
            self.assertIn(field.as_json(), expected_plaintext_fields_as_json)
        for field in device.encrypted_fields:
            self.assertIn(field.as_json(), expected_encrypted_fields_as_json)
예제 #2
0
    def __func():
        add_device_json = request.get_json()
        if device_db.session.query(Device).filter(
                Device.name == add_device_json['name']).first() is not None:
            current_app.logger.error('Could not create device {0}. '
                                     'Device already exists.'.format(
                                         add_device_json['name']))
            return {"error": "Device already exists."}, OBJECT_EXISTS_ERROR

        fields = {
            field['name']: field['value']
            for field in add_device_json['fields']
        }
        app = add_device_json['app_name']
        device_type = add_device_json['type']
        try:
            device_api = get_app_device_api(app, device_type)
            device_fields_api = device_api['fields']
            validate_device_fields(device_fields_api, fields, device_type, app)
        except UnknownApp:
            current_app.logger.error(
                'Cannot create device for app {0}, type {1}. '
                'App does not exist'.format(app, device_type))
            return {'error': 'Unknown app'}, INVALID_INPUT_ERROR
        except UnknownDevice:
            current_app.logger.error(
                'Cannot create device for app {0}, type {1}. '
                'Type does not exist'.format(app, device_type))
            return {'error': 'Unknown device type'}, INVALID_INPUT_ERROR
        except InvalidArgument as e:
            current_app.logger.error(
                'Cannot create device for app {0}, type {1}. '
                'Invalid input'.format(app, device_type,
                                       format_exception_message(e)))
            return {'error': 'Invalid device fields'}, INVALID_INPUT_ERROR
        else:
            fields = add_device_json['fields']
            add_configuration_keys_to_device_json(fields, device_fields_api)
            app = device_db.session.query(App).filter(App.name == app).first()
            if app is None:
                current_app.logger.error(
                    'SEVERE: App defined in api does not have corresponding entry in database. '
                    'Cannot add device')
                return {'error': 'Unknown app'}, INVALID_INPUT_ERROR
            device = Device.from_json(add_device_json)
            app.add_device(device)
            device_db.session.add(device)
            device_db.session.commit()
            device_json = get_device_json_with_app_name(device)
            # remove_configuration_keys_from_device_json(device_json)
            return device_json, OBJECT_CREATED
예제 #3
0
    def __func():
        data = request.get_json()
        filename = data['filename'] if 'filename' in data else core.config.paths.default_appdevice_export_path
        try:
            with open(filename, 'r') as devices_file:
                read_file = devices_file.read()
                read_file = read_file.replace('\n', '')
                apps = json.loads(read_file)
        except (OSError, IOError) as e:
            current_app.logger.error('Error importing devices from {0}: {1}'.format(filename, e))
            return {"error": "Error reading file."}, IO_ERROR
        for app in apps:
            for device in apps[app]:
                if device_db.session.query(Device).filter(Device.name == device['name']).first() is not None:
                    current_app.logger.error('Could not import device {0}. '
                                             'Device already exists.'.format(device['name']))
                    continue
                fields = {field['name']: field['value'] for field in device['fields']}
                device_type = device['type']
                try:
                    device_api = get_app_device_api(app, device_type)
                    device_fields_api = device_api['fields']
                    validate_device_fields(device_fields_api, fields, device_type, app)
                except UnknownDevice:
                    current_app.logger.error('Cannot import device for app {0}, type {1}. '
                                             'Type does not exist'.format(app, device_type))
                    continue
                except InvalidInput as e:
                    current_app.logger.error('Cannot import device for app {0}, type {1}. '
                                             'Invalid input'.format(app, device_type,
                                                                    format_exception_message(e)))
                    continue
                else:
                    fields = device['fields']
                    add_configuration_keys_to_device_json(fields, device_fields_api)
                    app = device_db.session.query(App).filter(App.name == app).first()
                    if app is None:
                        current_app.logger.error(
                            'SEVERE: App defined in api does not have corresponding entry in database. '
                            'Cannot import device')
                        continue
                    device_obj = Device.from_json(device)
                    app.add_device(device_obj)
                    device_db.session.add(device_obj)
                    device_db.session.commit()

        current_app.logger.debug('Imported devices from {0}'.format(filename))
        return {}, SUCCESS