diff --git a/server/jsonrpc/jsontypes.cpp b/server/jsonrpc/jsontypes.cpp index 2ab58210..4b67ff35 100644 --- a/server/jsonrpc/jsontypes.cpp +++ b/server/jsonrpc/jsontypes.cpp @@ -1392,6 +1392,11 @@ QPair, RuleEngine::RuleError> JsonTypes::verifyActions(const Q { QList actions; QVariantList actionList = params.value("actions").toList(); + if (actionList.isEmpty()) { + qCWarning(dcJsonRpc) << "Rule has no actions. This rule will do nothing."; + return QPair, RuleEngine::RuleError>(actions, RuleEngine::RuleErrorInvalidRuleFormat); + } + qCDebug(dcJsonRpc) << "unpacking actions:" << actionList; foreach (const QVariant &actionVariant, actionList) { QVariantMap actionMap = actionVariant.toMap(); diff --git a/tests/auto/restrules/testrestrules.cpp b/tests/auto/restrules/testrestrules.cpp index 556cf5ab..a4df9e74 100644 --- a/tests/auto/restrules/testrestrules.cpp +++ b/tests/auto/restrules/testrestrules.cpp @@ -52,6 +52,8 @@ private slots: void addRemoveRules_data(); void addRemoveRules(); + void emptyRule(); + void editRules_data(); void editRules(); @@ -394,6 +396,36 @@ void TestRestRules::addRemoveRules() nam->deleteLater(); } +void TestRestRules::emptyRule() +{ + // create add params for rule + QVariantMap params; + params.insert("name", QString()); + params.insert("actions", QVariantList()); + + QNetworkAccessManager *nam = new QNetworkAccessManager(this); + QSignalSpy clientSpy(nam, SIGNAL(finished(QNetworkReply*))); + + // Get rules and verify there is no rule added + QNetworkRequest request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules"))); + request.setHeader(QNetworkRequest::ContentTypeHeader, "text/json"); + QNetworkReply *reply = nam->post(request, QJsonDocument::fromVariant(params).toJson(QJsonDocument::Compact)); + clientSpy.wait(); + QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver"); + int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + QCOMPARE(statusCode, 400); + QByteArray data = reply->readAll(); + reply->deleteLater(); + + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + QCOMPARE(error.error, QJsonParseError::NoError); + + QVERIFY2(jsonDoc.toVariant().toMap().contains("error"), "The error message is missing"); + QVERIFY2(jsonDoc.toVariant().toMap().value("error").toString() == "RuleErrorMissingParameter", "Wrong RuleError."); + nam->deleteLater(); +} + void TestRestRules::editRules_data() { // RuleAction diff --git a/tests/auto/rules/testrules.cpp b/tests/auto/rules/testrules.cpp index 9d4e6326..12d38cf2 100644 --- a/tests/auto/rules/testrules.cpp +++ b/tests/auto/rules/testrules.cpp @@ -49,6 +49,8 @@ private slots: void cleanup(); + void emptyRule(); + void addRemoveRules_data(); void addRemoveRules(); @@ -98,6 +100,15 @@ void TestRules::cleanup() { cleanupRules(); } +void TestRules::emptyRule() +{ + QVariantMap params; + params.insert("name", QString()); + params.insert("actions", QVariantList()); + QVariant response = injectAndWait("Rules.AddRule", params); + verifyRuleError(response, RuleEngine::RuleErrorMissingParameter); +} + void TestRules::verifyRuleExecuted(const ActionTypeId &actionTypeId) { // Verify rule got executed