Skip to content

Commit

Permalink
Merge pull request #703 from nyaruka/d-sends
Browse files Browse the repository at this point in the history
Update D* handlers to use new send
  • Loading branch information
rowanseymour authored Feb 28, 2024
2 parents 7a79316 + f3401ba commit fa81698
Show file tree
Hide file tree
Showing 8 changed files with 465 additions and 391 deletions.
30 changes: 10 additions & 20 deletions handlers/dart/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,12 @@ func (h *handler) WriteMsgSuccessResponse(ctx context.Context, w http.ResponseWr
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
// TODO convert functionality from legacy method below
return nil
}

func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
if username == "" {
return nil, fmt.Errorf("no username set for %s channel", msg.Channel().ChannelType())
}

password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if password == "" {
return nil, fmt.Errorf("no password set for %s channel", msg.Channel().ChannelType())
if username == "" || password == "" {
return courier.ErrChannelConfig
}

status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)
parts := handlers.SplitMsgByChannel(msg.Channel(), handlers.GetTextAndAttachments(msg), h.maxLength)
for i, part := range parts {
form := url.Values{
Expand All @@ -185,24 +175,24 @@ func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *cour

req, err := http.NewRequest(http.MethodGet, partSendURL.String(), nil)
if err != nil {
return nil, err
return err
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, respBody, err := h.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 != 2 {
return status, nil
if err != nil || resp.StatusCode/100 == 5 {
return courier.ErrConnectionFailed
} else if resp.StatusCode/100 != 2 {
return courier.ErrResponseStatus
}

responseCode := stringsx.Truncate(string(respBody), 3)
if responseCode != "000" {
clog.Error(courier.ErrorExternal(responseCode, errorCodes[responseCode]))
return status, nil
return courier.ErrFailedWithReason(responseCode, errorCodes[responseCode])
}

status.SetStatus(courier.MsgStatusWired)

}
return status, nil

return nil
}
106 changes: 54 additions & 52 deletions handlers/dart/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package dart

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

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

var daTestChannels = []courier.Channel{
Expand Down Expand Up @@ -94,88 +94,90 @@ func BenchmarkHandler(b *testing.B) {
RunChannelBenchmarks(b, daTestChannels, NewHandler("DA", "DartMedia", sendURL, maxMsgLength), daTestCases)
}

// setSendURL takes care of setting the sendURL to call
func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel, m courier.MsgOut) {
daHandler := h.(*handler)
daHandler.sendURL = s.URL
}

var defaultSendTestCases = []OutgoingTestCase{
{
Label: "Plain Send",
MsgText: "Simple Message",
MsgURN: "tel:+250788383383",
MockResponseBody: "000",
MockResponseStatus: 200,
Label: "Plain Send",
MsgText: "Simple Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://202.43.169.11/APIhttpU/receive2waysms.php*": {
httpx.NewMockResponse(200, nil, []byte(`000`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Simple Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "W",
SendPrep: setSendURL,
},
{
Label: "Long Send",
MsgText: "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work",
MsgURN: "tel:+250788383383",
MockResponseBody: "000",
MockResponseStatus: 200,
Label: "Long Send",
MsgText: "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://202.43.169.11/APIhttpU/receive2waysms.php*": {
httpx.NewMockResponse(200, nil, []byte(`000`)),
httpx.NewMockResponse(200, nil, []byte(`000`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say,"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
{Params: url.Values{"message": {"I need to keep adding more things to make it work"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10.2"}}},
},
ExpectedMsgStatus: "W",
SendPrep: setSendURL,
},
{
Label: "Send Attachment",
MsgText: "My pic!",
MsgURN: "tel:+250788383383",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponseBody: "000",
MockResponseStatus: 200,
Label: "Send Attachment",
MsgText: "My pic!",
MsgURN: "tel:+250788383383",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponses: map[string][]*httpx.MockResponse{
"http://202.43.169.11/APIhttpU/receive2waysms.php*": {
httpx.NewMockResponse(200, nil, []byte(`000`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"My pic!\nhttps://foo.bar/image.jpg"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "W",
SendPrep: setSendURL,
},
{
Label: "Error Sending",
MsgText: "Error Message",
MsgURN: "tel:+250788383383",
MockResponseBody: `Error`,
MockResponseStatus: 400,
Label: "Error Sending",
MsgText: "Error Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://202.43.169.11/APIhttpU/receive2waysms.php*": {
httpx.NewMockResponse(400, nil, []byte(`Error`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Error Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "E",
SendPrep: setSendURL,
ExpectedError: courier.ErrResponseStatus,
},
{
Label: "Authentication Error",
MsgText: "Simple Message",
MsgURN: "tel:+250788383383",
MockResponseBody: "001",
MockResponseStatus: 200,
Label: "Authentication Error",
MsgText: "Simple Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://202.43.169.11/APIhttpU/receive2waysms.php*": {
httpx.NewMockResponse(200, nil, []byte(`001`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Simple Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "E",
ExpectedLogErrors: []*courier.ChannelError{courier.ErrorExternal("001", "Authentication error.")},
SendPrep: setSendURL,
ExpectedError: courier.ErrFailedWithReason("001", "Authentication error."),
},
{
Label: "Account Expired",
MsgText: "Simple Message",
MsgURN: "tel:+250788383383",
MockResponseBody: "101",
MockResponseStatus: 200,
Label: "Account Expired",
MsgText: "Simple Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://202.43.169.11/APIhttpU/receive2waysms.php*": {
httpx.NewMockResponse(200, nil, []byte(`101`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Simple Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "E",
ExpectedLogErrors: []*courier.ChannelError{courier.ErrorExternal("101", "Account expired or invalid parameters.")},
SendPrep: setSendURL,
ExpectedError: courier.ErrFailedWithReason("101", "Account expired or invalid parameters."),
},
}

Expand Down
Loading

0 comments on commit fa81698

Please sign in to comment.