Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jan 11, 2024
1 parent b2b15b3 commit 7375941
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
17 changes: 11 additions & 6 deletions handlers/tembachat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/nyaruka/gocommon/urns"
)

const (
var (
defaultSendURL = "http://chatserver:8070/send"
)

Expand All @@ -35,19 +35,22 @@ func (h *handler) Initialize(s courier.Server) error {
}

type receivePayload struct {
Type string `json:"type"`
Type string `json:"type" validate:"required"`
Message struct {
Identifier string `json:"identifier"`
Text string `json:"text"`
Identifier string `json:"identifier" validate:"required"`
Text string `json:"text" validate:"required"`
} `json:"message"`
}

// receiveMessage is our HTTP handler function for incoming messages
func (h *handler) receiveMessage(ctx context.Context, c courier.Channel, w http.ResponseWriter, r *http.Request, payload *receivePayload, clog *courier.ChannelLog) ([]courier.Event, error) {
if payload.Type == "message" {
urn, _ := urns.NewWebChatURN(payload.Message.Identifier)
msg := h.Backend().NewIncomingMsg(c, urn, payload.Message.Text, "", clog)
urn, err := urns.NewWebChatURN(payload.Message.Identifier)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, c, w, r, err)
}

msg := h.Backend().NewIncomingMsg(c, urn, payload.Message.Text, "", clog)
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
}
return nil, handlers.WriteAndLogRequestIgnored(ctx, h, c, w, r, "")

Check warning on line 56 in handlers/tembachat/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/tembachat/handler.go#L56

Added line #L56 was not covered by tests
Expand All @@ -56,6 +59,7 @@ func (h *handler) receiveMessage(ctx context.Context, c courier.Channel, w http.
type sendPayload struct {
Identifier string `json:"identifier"`
Text string `json:"text"`
Origin string `json:"origin"`
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
Expand All @@ -64,6 +68,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch
payload := &sendPayload{
Identifier: msg.URN().Path(),
Text: msg.Text(),
Origin: string(msg.Origin()),
}
req, _ := http.NewRequest("POST", sendURL, bytes.NewReader(jsonx.MustMarshal(payload)))

Expand Down
77 changes: 77 additions & 0 deletions handlers/tembachat/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package tembachat

import (
"net/http/httptest"
"testing"

"github.com/nyaruka/courier"
. "github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/test"
)

var testChannels = []courier.Channel{
test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "TWC", "", "", nil),
}

var handleTestCases = []IncomingTestCase{
{
Label: "Receive Valid Message",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"type": "message", "message": {"identifier": "65vbbDAQCdPdEWlEhDGy4utO", "text": "Join"}}`,
ExpectedRespStatus: 200,
ExpectedBodyContains: "Accepted",
ExpectedMsgText: Sp("Join"),
ExpectedURN: "webchat:65vbbDAQCdPdEWlEhDGy4utO",
},
{
Label: "Invalid URN",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"type": "message", "message": {"identifier": "xxxxx", "text": "Join"}}`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "invalid webchat id: xxxxx",
},
{
Label: "Missing fields",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"foo": "message"}`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "Field validation for 'Type' failed on the 'required' tag",
},
}

func TestIncoming(t *testing.T) {
RunIncomingTestCases(t, testChannels, newHandler(), handleTestCases)
}

func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel, m courier.MsgOut) {
defaultSendURL = s.URL
}

var defaultSendTestCases = []OutgoingTestCase{
{
Label: "Plain Send",
MsgText: "Simple message ☺",
MsgURN: "webchat:65vbbDAQCdPdEWlEhDGy4utO",
MockResponseBody: `{"status": "queued"}`,
MockResponseStatus: 200,
ExpectedRequestBody: `{"identifier":"65vbbDAQCdPdEWlEhDGy4utO","text":"Simple message ☺","origin":"flow"}`,
ExpectedMsgStatus: "W",
SendPrep: setSendURL,
},
{
Label: "Error Sending",
MsgText: "Error message",
MsgURN: "webchat:65vbbDAQCdPdEWlEhDGy4utO",
MockResponseBody: `{"error": "boom"}`,
MockResponseStatus: 400,
ExpectedRequestBody: `{"identifier":"65vbbDAQCdPdEWlEhDGy4utO","text":"Error message","origin":"flow"}`,
ExpectedMsgStatus: "E",
SendPrep: setSendURL,
},
}

func TestOutgoing(t *testing.T) {
var defaultChannel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "TWC", "", "", nil)

RunOutgoingTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil, nil)
}

0 comments on commit 7375941

Please sign in to comment.