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

RC, RR, SQ, SC, ST, SC to use new send #696

Merged
merged 12 commits into from
Feb 21, 2024
22 changes: 7 additions & 15 deletions handlers/redrabbit/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -36,19 +35,13 @@
}

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, "")
password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if username == "" || password == "" {
return nil, fmt.Errorf("Missing username or password for RR channel")
return courier.ErrChannelConfig

Check warning on line 41 in handlers/redrabbit/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/redrabbit/handler.go#L41

Added line #L41 was not covered by tests
}

text := handlers.GetTextAndAttachments(msg)
status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)
form := url.Values{
"LoginName": []string{username},
"Password": []string{password},
Expand All @@ -73,16 +66,15 @@
msgURL.RawQuery = form.Encode()
req, err := http.NewRequest(http.MethodGet, msgURL.String(), nil)
if err != nil {
return nil, err
return err

Check warning on line 69 in handlers/redrabbit/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/redrabbit/handler.go#L69

Added line #L69 was not covered by tests
}

resp, _, 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
}

// all went well, set ourselves to wired
status.SetStatus(courier.MsgStatusWired)

return status, nil
return nil
}
172 changes: 101 additions & 71 deletions handlers/redrabbit/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,130 @@
package redrabbit

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"
)

// setSendURL takes care of setting the send_url to our test server host
func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel, m courier.MsgOut) {
sendURL = s.URL
}

var defaultSendTestCases = []OutgoingTestCase{
{Label: "Plain Send",
MsgText: "Simple Message", MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "Simple Message",
"SenderName": "2020",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"Simple Message"},
"SenderName": {"2020"},
}}},
},
{Label: "Unicode Send",
MsgText: "☺", MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "☺",
"SenderName": "2020",
"MsgTyp": "9",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"☺"},
"SenderName": {"2020"},
"MsgTyp": {"9"},
},
}},
},
{Label: "Longer Unicode Send",
MsgText: "This is a message more than seventy characters with some unicode ☺ in them",
MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "This is a message more than seventy characters with some unicode ☺ in them",
"SenderName": "2020",
"MsgTyp": "10",
MsgText: "This is a message more than seventy characters with some unicode ☺ in them",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"This is a message more than seventy characters with some unicode ☺ in them"},
"SenderName": {"2020"},
"MsgTyp": {"10"},
}}},
},
{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",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "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",
"SenderName": "2020",
"MsgTyp": "5",
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://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"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"},
"SenderName": {"2020"},
"MsgTyp": {"5"},
}}},
},
{Label: "Send Attachment",
MsgText: "My pic!", MsgURN: "tel:+250788383383", MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "My pic!\nhttps://foo.bar/image.jpg",
"SenderName": "2020",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"My pic!\nhttps://foo.bar/image.jpg"},
"SenderName": {"2020"},
}}},
},
{Label: "Error Sending",
MsgText: "Error Sending", MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "E",
MockResponseBody: "Error", MockResponseStatus: 403,
SendPrep: setSendURL},
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(403, nil, []byte(`Error`)),
},
},
ExpectedError: courier.ErrResponseStatus,
},
{Label: "Connection Error",
MsgText: "Error Sending", MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(500, nil, []byte(`Error`)),
},
},
ExpectedError: courier.ErrConnectionFailed,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
44 changes: 20 additions & 24 deletions handlers/rocketchat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
return nil
}

type Attachment struct {
type RCAttachment struct {
Type string `json:"type"`
URL string `json:"url"`
}
Expand All @@ -52,8 +52,8 @@
Username string `json:"username"`
FullName string `json:"full_name"`
} `json:"user" validate:"required"`
Text string `json:"text"`
Attachments []Attachment `json:"attachments"`
Text string `json:"text"`
Attachments []RCAttachment `json:"attachments"`
}

// receiveMessage is our HTTP handler function for incoming messages
Expand Down Expand Up @@ -99,54 +99,50 @@
var _ courier.AttachmentRequestBuilder = (*handler)(nil)

type mtPayload struct {
UserURN string `json:"user"`
BotUsername string `json:"bot"`
Text string `json:"text,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
UserURN string `json:"user"`
BotUsername string `json:"bot"`
Text string `json:"text,omitempty"`
Attachments []RCAttachment `json:"attachments,omitempty"`
}

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) {
baseURL := msg.Channel().StringConfigForKey(configBaseURL, "")
secret := msg.Channel().StringConfigForKey(configSecret, "")
botUsername := msg.Channel().StringConfigForKey(configBotUsername, "")

// the status that will be written for this message
status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)

if baseURL == "" || secret == "" || botUsername == "" {
return courier.ErrChannelConfig
}

Check warning on line 114 in handlers/rocketchat/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/rocketchat/handler.go#L113-L114

Added lines #L113 - L114 were not covered by tests
payload := &mtPayload{
UserURN: msg.URN().Path(),
BotUsername: botUsername,
Text: msg.Text(),
}
for _, attachment := range msg.Attachments() {
mimeType, url := handlers.SplitAttachment(attachment)
payload.Attachments = append(payload.Attachments, Attachment{mimeType, url})
payload.Attachments = append(payload.Attachments, RCAttachment{mimeType, url})
}

body := jsonx.MustMarshal(payload)

req, err := http.NewRequest(http.MethodPost, baseURL+"/message", bytes.NewReader(body))
if err != nil {
return status, err
return err

Check warning on line 129 in handlers/rocketchat/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/rocketchat/handler.go#L129

Added line #L129 was not covered by tests
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", secret))

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
}

msgID, err := jsonparser.GetString(respBody, "id")
if err == nil {
status.SetExternalID(msgID)
if err != nil {
return courier.ErrResponseUnexpected

Check warning on line 143 in handlers/rocketchat/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/rocketchat/handler.go#L143

Added line #L143 was not covered by tests
}
res.AddExternalID(msgID)

status.SetStatus(courier.MsgStatusSent)
return status, nil
return nil
}
Loading
Loading