From 2f0d0aca1c6f92bd98d9389d331d36a17ecca573 Mon Sep 17 00:00:00 2001 From: miguelreiswildlife <108534663+miguelreiswildlife@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:20:15 -0300 Subject: [PATCH] Default to no modetation on messages (#19) * Do not moderate messages sent from server side * Fix tests * Fix tests * Fix tests * Allow moderation if set explicitly * Do not use version on go run --- api/sendmqtt.go | 4 ++++ api/sendmqtt_test.go | 32 +++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/api/sendmqtt.go b/api/sendmqtt.go index a114cd3..393e2b2 100644 --- a/api/sendmqtt.go +++ b/api/sendmqtt.go @@ -52,6 +52,10 @@ func SendMqttHandler(app *App) func(c echo.Context) error { return FailWith(400, err.Error(), c) } + if _, exists := msgPayload["should_moderate"]; !exists { + msgPayload["should_moderate"] = false + } + topic := c.ParamValues()[0] if err != nil { return FailWith(400, err.Error(), c) diff --git a/api/sendmqtt_test.go b/api/sendmqtt_test.go index b4464c4..e465bb5 100644 --- a/api/sendmqtt_test.go +++ b/api/sendmqtt_test.go @@ -12,6 +12,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "strings" "time" mqtt "github.com/eclipse/paho.mqtt.golang" @@ -30,11 +31,24 @@ var _ = Describe("Send to MQTT Handler", func() { testJSON := map[string]interface{}{ "message": "hello", } - response := `{"topic": "test", "retained": false, "payload": {"message":"hello"}}` + response := `{"topic": "test", "retained": false, "payload": {"message":"hello","should_moderate":false}}` status, body := PostJSON(a, "/sendmqtt/test", testJSON) Expect(status).To(Equal(http.StatusOK)) - Expect(body).To(Equal(response)) + Expect(strings.TrimSpace(body)).To(Equal(strings.TrimSpace(response))) + }) + + It("Should respond with 200 and not override should_moderate", func() { + a := GetDefaultTestApp() + testJSON := map[string]interface{}{ + "message": "hello", + "should_moderate": true, + } + response := `{"topic": "test", "retained": false, "payload": {"message":"hello","should_moderate":true}}` + status, body := PostJSON(a, "/sendmqtt/test", testJSON) + + Expect(status).To(Equal(http.StatusOK)) + Expect(strings.TrimSpace(body)).To(Equal(strings.TrimSpace(response))) }) It("Should respond with 200 for a valid message with hierarchical topic", func() { @@ -42,12 +56,12 @@ var _ = Describe("Send to MQTT Handler", func() { testJSON := map[string]interface{}{ "message": "hello", } - response := `{"topic": "test/topic", "retained": false, "payload": {"message":"hello"}}` + response := `{"topic": "test/topic", "retained": false, "payload": {"message":"hello","should_moderate":false}}` url := "/sendmqtt/test/topic" status, body := PostJSON(a, url, testJSON) Expect(status).To(Equal(http.StatusOK)) - Expect(body).To(Equal(response)) + Expect(strings.TrimSpace(body)).To(Equal(strings.TrimSpace(response))) }) It("Should respond with 400 if malformed JSON", func() { @@ -66,11 +80,11 @@ var _ = Describe("Send to MQTT Handler", func() { "message": "hello", } topic := uuid.NewV4().String() - expectedMsg := `{"message":"hello"}` + expectedPayload := `{"message":"hello","should_moderate":false}` response := fmt.Sprintf( `{"topic": "%s", "retained": true, "payload": %s}`, topic, - expectedMsg, + expectedPayload, ) url := fmt.Sprintf("/sendmqtt/%s?retained=true", topic) status, body := PostJSON(a, url, testJSON) @@ -89,7 +103,7 @@ var _ = Describe("Send to MQTT Handler", func() { Expect(msg).NotTo(BeNil()) Expect(msg.Retained()).To(BeTrue()) - Expect(string(msg.Payload())).To(Equal(expectedMsg)) + Expect(string(msg.Payload())).To(Equal(expectedPayload)) }) It("Should respond with 200 for a valid message with hierarchical topic", func() { @@ -97,12 +111,12 @@ var _ = Describe("Send to MQTT Handler", func() { testJSON := map[string]interface{}{ "message": "hello", } - response := `{"topic": "test/topic", "retained": true, "payload": {"message":"hello"}}` + response := `{"topic": "test/topic", "retained": true, "payload": {"message":"hello","should_moderate":false}}` url := "/sendmqtt/test/topic?retained=true" status, body := PostJSON(a, url, testJSON) Expect(status).To(Equal(http.StatusOK)) - Expect(body).To(Equal(response)) + Expect(strings.TrimSpace(body)).To(Equal(strings.TrimSpace(response))) }) It("Should respond with 400 if malformed JSON", func() {