Skip to content

Commit

Permalink
Default to no modetation on messages (#19)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
miguelreiswildlife authored Apr 25, 2024
1 parent cffb9ae commit 2f0d0ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions api/sendmqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 23 additions & 9 deletions api/sendmqtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
Expand All @@ -30,24 +31,37 @@ 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() {
a := GetDefaultTestApp()
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() {
Expand All @@ -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)
Expand All @@ -89,20 +103,20 @@ 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() {
a := GetDefaultTestApp()
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() {
Expand Down

0 comments on commit 2f0d0ac

Please sign in to comment.