Ejemplo n.º 1
0
    def test_delete_sensor(self):
        data = self.sensordata.copy()
        data['honeypot'] = 'conpot'

        # Create sensor and respective authkey.
        sensor = Sensor(**data)
        sensor.uuid = str(uuid1())
        self.clio.authkey.new(**sensor.new_auth_dict()).post()
        db.session.add(sensor)
        db.session.commit()

        # Objects before should equal 1.
        sensors_before = Sensor.query.count()
        keys_before = self.clio.authkey.count()

        # Logging in and making the DELETE request.
        self.login()
        delete_url = url_for('api.delete_sensor', uuid=sensor.uuid)
        self.client.delete(delete_url)

        # Sensors after should equal 0.
        sensors_after = Sensor.query.count()
        keys_after = self.clio.authkey.count()

        # Asserts that both sensor and authkey got deleted.
        self.assertEqual(sensors_before, sensors_after + 1)
        self.assertEqual(keys_before, keys_after + 1)
Ejemplo n.º 2
0
    def test_delete_sensor(self):
        data = self.sensordata.copy()
        data['honeypot'] = 'conpot'

        # Create sensor and respective authkey.
        sensor = Sensor(**data)
        sensor.uuid = str(uuid1())
        self.clio.authkey.new(**sensor.new_auth_dict()).post()
        db.session.add(sensor)
        db.session.commit()

        # Objects before should equal 1.
        sensors_before = Sensor.query.count()
        keys_before = self.clio.authkey.count()

        # Logging in and making the DELETE request.
        self.login()
        delete_url = url_for('api.delete_sensor', uuid=sensor.uuid)
        self.client.delete(delete_url)

        # Sensors after should equal 0.
        sensors_after = Sensor.query.count()
        keys_after = self.clio.authkey.count()

        # Asserts that both sensor and authkey got deleted.
        self.assertEqual(sensors_before, sensors_after + 1)
        self.assertEqual(keys_before, keys_after + 1)
Ejemplo n.º 3
0
Archivo: views.py Proyecto: raum01/mhn
def update_sensor(uuid):
    sensor = Sensor.query.filter_by(uuid=uuid).first_or_404()
    for field in request.json.keys():
        if field in Sensor.editable_fields():
            setattr(sensor, field, request.json[field])
        elif field in Sensor.fields():
            return error_response(errors.API_FIELD_NOT_EDITABLE.format(field), 400)
        else:
            return error_response(errors.API_FIELD_INVALID.format(field), 400)
    else:
        try:
            db.session.commit()
        except IntegrityError:
            return error_response(errors.API_SENSOR_EXISTS.format(request.json["name"]), 400)
        return jsonify(sensor.to_dict())
Ejemplo n.º 4
0
def update_sensor(uuid):
    sensor = Sensor.query.filter_by(uuid=uuid).first_or_404()
    for field in request.json.keys():
        if field in Sensor.editable_fields():
            setattr(sensor, field, request.json[field])
        elif field in Sensor.fields():
            return error_response(errors.API_FIELD_NOT_EDITABLE.format(field),
                                  400)
        else:
            return error_response(errors.API_FIELD_INVALID.format(field), 400)
    else:
        try:
            db.session.commit()
        except IntegrityError:
            return error_response(
                errors.API_SENSOR_EXISTS.format(request.json['name']), 400)
        return jsonify(sensor.to_dict())
Ejemplo n.º 5
0
def create_sensor():
    missing = Sensor.check_required(request.json)
    if missing:
        return error_response(errors.API_FIELDS_MISSING.format(missing), 400)
    else:
        sensor = Sensor(**request.json)
        sensor.uuid = str(uuid1())
        sensor.ip = request.remote_addr
        Clio().authkey.new(**sensor.new_auth_dict()).post()
        try:
            db.session.add(sensor)
            db.session.commit()
        except IntegrityError:
            return error_response(
                errors.API_SENSOR_EXISTS.format(request.json['name']), 400)
        else:
            return jsonify(sensor.to_dict())
Ejemplo n.º 6
0
Archivo: views.py Proyecto: raum01/mhn
def create_sensor():
    missing = Sensor.check_required(request.json)
    if missing:
        return error_response(errors.API_FIELDS_MISSING.format(missing), 400)
    else:
        sensor = Sensor(**request.json)
        sensor.uuid = str(uuid1())
        sensor.ip = request.remote_addr
        Clio().authkey.new(**sensor.new_auth_dict()).post()
        try:
            db.session.add(sensor)
            db.session.commit()
        except IntegrityError:
            return error_response(errors.API_SENSOR_EXISTS.format(request.json["name"]), 400)
        else:
            return jsonify(sensor.to_dict())