Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update D* handlers to use new send #703

Merged
merged 11 commits into from
Feb 28, 2024
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) 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

Check warning on line 152 in handlers/dart/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dart/handler.go#L152

Added line #L152 was not covered by tests
}

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 @@

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

Check warning on line 178 in handlers/dart/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dart/handler.go#L178

Added line #L178 was not covered by tests
}

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

Check warning on line 185 in handlers/dart/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dart/handler.go#L185

Added line #L185 was not covered by tests
} 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
Loading