Exemplo n.º 1
0
def test_http_non_unique_key_return():
    # Test KeyExReturn.NonUniqueKey()
    dev_id = "ABC"
    data = "Device ID {} already exists; and could not be added.".format(
        dev_id)
    status_code = 400

    non_unique = KeyExReturn.NonUniqueKey(dev_id)
    assert non_unique.status_code() == status_code
    assert non_unique.message() == data
    assert non_unique() == (data, status_code)
Exemplo n.º 2
0
def register():
    registration_data = request.get_json()
    if KeyEx_helpers.check_valid_json(registration_data):
        # Having checked the validity we can safely access the JSON data by key name...
        identity = registration_data['identity']
        pub_key = registration_data['key']
        dev_type = registration_data['type']

        if server_active:
            # The store_data function will check the device doesn't already exist, and then create a record for it,
            # and store it's key & type...
            # it will return false if there's an error – and the error type will be in error...
            db_ret = database_functions.store_data(DATABASE_FILE, identity, pub_key, dev_type)

            if db_ret.success():
                if db_ret.type() == "Success":
                    # All is good – so return the broker details, and the server's public key...
                    key = KeyEx_helpers.keyfile(SERVER_PUBLIC_KEY_FILE)
                    return_data = {'broker': MQTT_BROKER, 'key': key}
                    rv = KeyExReturn.Success(json.dumps(return_data))
                    logging.info("Device {} added.".format(identity))
                    return rv()
                else:
                    # If we get here - something went wrong: as we'll've returned True in success
                    # but have the wrong return type...
                    # This is another one of the "can't happen" errors unless we've screwed up.
                    logging.error("Database return type invalid... {}".format(db_ret.message()))
                    return KeyExReturn.DatabaseError("Internal Error")()

            else:
                # We couldn't add the data to the database... So let's find out why...
                if db_ret.type() == 'NonUnique':
                    # Most likely is that we're trying to add a Device ID we already have...
                    rv = KeyExReturn.NonUniqueKey(identity)
                    logging.warning(rv.message())
                    return rv()

                elif db_ret.type() == 'NotConnected':
                    # We couldn't connect to the database...
                    # This is the worst case: so log as an error...
                    logging.error("Could not connect to the database... {}".format(db_ret.message()))
                    # Since this can't be caused by the user; there's nothing to report back beyond the generic message
                    return KeyExReturn.DatabaseConnectionError()()

                else:
                    # Something else went wrong with the database - so we'll return a generic database error
                    # This has a status value of 500...
                    # Together with any message from the database (this should never happen).

                    return KeyExReturn.DatabaseError(db_ret.message())()
        else:
            # if server is inactive...
            # Note: the odd syntax here is because we initialize and then call the KeyExReturn.Forbidden object
            logging.warning("Device registration from {}, attempted when server was inactive.".format(identity))
            return KeyExReturn.Forbidden()()
    else:
        # if invalid JSON...
        # We'll generate a list of the missing keys...
        missing = KeyEx_helpers.missing_json(registration_data)

        # ...and log that list as a warning.
        logging.warning("JSON data received had missing values: {}".format(missing))

        # We'll also create a dictionary we can return showing the missing values.
        processed_data = {}
        for d in missing:
            processed_data.update({d: ''})

        # Again we'll use the custom class - with the same syntax as the Forbidden case...
        return KeyExReturn.MissingJSON(processed_data)()