From 275ab89e621b861e34d37d014f6bfc1fcf6f80fd Mon Sep 17 00:00:00 2001 From: Eric Duminil Date: Sun, 1 Dec 2024 22:12:00 +0100 Subject: [PATCH] Add MQTT tests with invalid topics --- test/backend-test/test-mqtt.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/backend-test/test-mqtt.js b/test/backend-test/test-mqtt.js index 537b1cbae..a10e0422a 100644 --- a/test/backend-test/test-mqtt.js +++ b/test/backend-test/test-mqtt.js @@ -80,11 +80,34 @@ describe("MqttMonitorType", { assert.strictEqual(heartbeat.status, UP); assert.strictEqual(heartbeat.msg, "Topic: a/b/c; Message: -> KEYWORD <-"); }); + + test("invalid topic", async () => { + await assert.rejects( + testMqtt("keyword will not be checked anyway", null, "message", "x/y/z", "a/b/c"), + new Error("Timeout, Message not received"), + ); + }); + + test("invalid wildcard topic (with #)", async () => { + await assert.rejects( + testMqtt("", null, "# should be last character", "#/c", "a/b/c"), + new Error("Timeout, Message not received"), + ); + }); + + test("invalid wildcard topic (with +)", async () => { + await assert.rejects( + testMqtt("", null, "message", "x/+/z", "a/b/c"), + new Error("Timeout, Message not received"), + ); + }); + test("valid keywords (type=keyword)", async () => { const heartbeat = await testMqtt("KEYWORD", "keyword", "-> KEYWORD <-"); assert.strictEqual(heartbeat.status, UP); assert.strictEqual(heartbeat.msg, "Topic: test; Message: -> KEYWORD <-"); }); + test("invalid keywords (type=default)", async () => { await assert.rejects( testMqtt("NOT_PRESENT", null, "-> KEYWORD <-"), @@ -98,12 +121,14 @@ describe("MqttMonitorType", { new Error("Message Mismatch - Topic: test; Message: -> KEYWORD <-"), ); }); + test("valid json-query", async () => { // works because the monitors' jsonPath is hard-coded to "firstProp" const heartbeat = await testMqtt("present", "json-query", "{\"firstProp\":\"present\"}"); assert.strictEqual(heartbeat.status, UP); assert.strictEqual(heartbeat.msg, "Message received, expected value is found"); }); + test("invalid (because query fails) json-query", async () => { // works because the monitors' jsonPath is hard-coded to "firstProp" await assert.rejects( @@ -111,6 +136,7 @@ describe("MqttMonitorType", { new Error("Message received but value is not equal to expected value, value was: [undefined]"), ); }); + test("invalid (because successMessage fails) json-query", async () => { // works because the monitors' jsonPath is hard-coded to "firstProp" await assert.rejects(