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

Remove deprecated test fields #709

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 45 additions & 38 deletions handlers/africastalking/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package africastalking

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

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

const (
Expand Down Expand Up @@ -109,79 +109,86 @@ func TestIncoming(t *testing.T) {

var outgoingCases = []OutgoingTestCase{
{
Label: "Plain Send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MockResponseBody: `{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`,
MockResponseStatus: 200,
Label: "Plain Send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{"apikey": "KEY"},
Form: url.Values{"message": {"Simple Message ☺"}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}},
},
},
ExpectedExtIDs: []string{"1002"},
SendPrep: setSendURL,
},
{
Label: "Send Attachment",
MsgText: "My pic!",
MsgURN: "tel:+250788383383",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponseBody: `{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`,
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{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"message": {"My pic!\nhttps://foo.bar/image.jpg"}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}}},
},
ExpectedExtIDs: []string{"1002"},
SendPrep: setSendURL,
},
{
Label: "Explicit failed status",
MsgText: "Hi",
MsgURN: "tel:+250788383383",
MockResponseBody: `{ "SMSMessageData": {"Recipients": [{"status": "Failed" }] } }`,
MockResponseStatus: 200,
Label: "Explicit failed status",
MsgText: "Hi",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Failed" }] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"message": {`Hi`}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}}},
},
ExpectedError: courier.ErrResponseUnexpected,
SendPrep: setSendURL,
},
{
Label: "Missing status value",
MsgText: "Error Message",
MsgURN: "tel:+250788383383",
MockResponseBody: `{ "error": "failed" }`,
MockResponseStatus: 401,
Label: "Missing status value",
MsgText: "Error Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(401, nil, []byte(`{ "error": "failed" }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"message": {`Error Message`}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}}},
},
ExpectedError: courier.ErrResponseStatus,
SendPrep: setSendURL,
},
}

var sharedOutgoingCases = []OutgoingTestCase{
{
Label: "Shared Send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MockResponseBody: `{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`,
MockResponseStatus: 200,
ExpectedHeaders: map[string]string{"apikey": "KEY"},
Label: "Shared Send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"message": {"Simple Message ☺"}, "username": {"Username"}, "to": {"+250788383383"}}},
{
Headers: map[string]string{"apikey": "KEY"},
Form: url.Values{"message": {"Simple Message ☺"}, "username": {"Username"}, "to": {"+250788383383"}}},
},
ExpectedExtIDs: []string{"1002"},
SendPrep: setSendURL,
},
}

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

func TestOutgoing(t *testing.T) {
defaultChannel := test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "AT", "2020", "US",
map[string]any{
Expand Down
66 changes: 33 additions & 33 deletions handlers/arabiacell/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package arabiacell

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

const (
Expand Down Expand Up @@ -47,12 +47,11 @@ var outgoingCases = []OutgoingTestCase{
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponseBody: `<response>
<code>204</code>
<text>MT is successfully sent</text>
<message_id>external1</message_id>
</response>`,
MockResponseStatus: 200,
MockResponses: map[string][]*httpx.MockResponse{
"https://acsdp.arabiacell.net": {
httpx.NewMockResponse(200, nil, []byte(`<response><code>204</code><text>MT is successfully sent</text><message_id>external1</message_id></response>`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Form: url.Values{
Expand All @@ -67,41 +66,42 @@ var outgoingCases = []OutgoingTestCase{
},
},
ExpectedExtIDs: []string{"external1"},
SendPrep: setSendURL,
},
{
Label: "Invalid XML",
MsgText: "Invalid XML",
MsgURN: "tel:+250788383383",
MockResponseBody: `not xml`,
MockResponseStatus: 200,
ExpectedError: courier.ErrResponseUnparseable,
SendPrep: setSendURL,
Label: "Invalid XML",
MsgText: "Invalid XML",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://acsdp.arabiacell.net": {
httpx.NewMockResponse(200, nil, []byte(`not xml`)),
},
},
ExpectedError: courier.ErrResponseUnparseable,
},
{
Label: "Error Response",
MsgText: "Error Response",
MsgURN: "tel:+250788383383",
MockResponseBody: `<response><code>501</code><text>failure</text><message_id></message_id></response>`,
MockResponseStatus: 200,
ExpectedError: courier.ErrResponseUnexpected,
SendPrep: setSendURL,
Label: "Error Response",
MsgText: "Error Response",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://acsdp.arabiacell.net": {
httpx.NewMockResponse(200, nil, []byte(`<response><code>501</code><text>failure</text><message_id></message_id></response>`)),
},
},
ExpectedError: courier.ErrResponseUnexpected,
},
{
Label: "Error Sending",
MsgText: "Error Message",
MsgURN: "tel:+250788383383",
MockResponseBody: `Bad Gateway`,
MockResponseStatus: 501,
ExpectedError: courier.ErrConnectionFailed,
SendPrep: setSendURL,
Label: "Error Sending",
MsgText: "Error Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://acsdp.arabiacell.net": {
httpx.NewMockResponse(501, nil, []byte(`Bad Gateway`)),
},
},
ExpectedError: courier.ErrConnectionFailed,
},
}

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

func TestOutgoing(t *testing.T) {
ch := test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "AC", "2020", "US",
map[string]any{
Expand Down
79 changes: 42 additions & 37 deletions handlers/bandwidth/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bandwidth

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

"github.com/nyaruka/courier"
Expand Down Expand Up @@ -245,11 +244,14 @@ func TestIncoming(t *testing.T) {

var outgoingCases = []OutgoingTestCase{
{
Label: "Plain send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+12067791234",
MockResponseBody: `{"id": "55555"}`,
MockResponseStatus: 200,
Label: "Plain send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+12067791234",
MockResponses: map[string][]*httpx.MockResponse{
"https://messaging.bandwidth.com/api/v2/users/accound-id/messages": {
httpx.NewMockResponse(200, nil, []byte(`{"id": "55555"}`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{
Expand All @@ -261,15 +263,17 @@ var outgoingCases = []OutgoingTestCase{
},
},
ExpectedExtIDs: []string{"55555"},
SendPrep: setSendURL,
},
{
Label: "Attachment",
MsgText: "My pic!",
MsgURN: "tel:+12067791234",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponseBody: `{"id": "55555"}`,
MockResponseStatus: 200,
Label: "Attachment",
MsgText: "My pic!",
MsgURN: "tel:+12067791234",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponses: map[string][]*httpx.MockResponse{
"https://messaging.bandwidth.com/api/v2/users/accound-id/messages": {
httpx.NewMockResponse(200, nil, []byte(`{"id": "55555"}`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{
Expand All @@ -281,15 +285,17 @@ var outgoingCases = []OutgoingTestCase{
},
},
ExpectedExtIDs: []string{"55555"},
SendPrep: setSendURL,
},
{
Label: "Send Attachment no text",
MsgText: "",
MsgURN: "tel:+12067791234",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponseBody: `{"id": "55555"}`,
MockResponseStatus: 200,
Label: "Send Attachment no text",
MsgText: "",
MsgURN: "tel:+12067791234",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponses: map[string][]*httpx.MockResponse{
"https://messaging.bandwidth.com/api/v2/users/accound-id/messages": {
httpx.NewMockResponse(200, nil, []byte(`{"id": "55555"}`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{
Expand All @@ -301,14 +307,16 @@ var outgoingCases = []OutgoingTestCase{
},
},
ExpectedExtIDs: []string{"55555"},
SendPrep: setSendURL,
},
{
Label: "No External ID",
MsgText: "No External ID",
MsgURN: "tel:+12067791234",
MockResponseBody: `{}`,
MockResponseStatus: 200,
Label: "No External ID",
MsgText: "No External ID",
MsgURN: "tel:+12067791234",
MockResponses: map[string][]*httpx.MockResponse{
"https://messaging.bandwidth.com/api/v2/users/accound-id/messages": {
httpx.NewMockResponse(200, nil, []byte(`{}`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{
Expand All @@ -319,14 +327,16 @@ var outgoingCases = []OutgoingTestCase{
Body: `{"applicationId":"application-id","to":["+12067791234"],"from":"2020","text":"No External ID"}`,
},
},
SendPrep: setSendURL,
},
{
Label: "Error sending",
MsgText: "Error Message",
MsgURN: "tel:+12067791234",
MockResponseBody: `{ "type": "request-validation", "description": "Your request could not be accepted" }`,
MockResponseStatus: 401,
Label: "Error sending",
MsgText: "Error Message",
MsgURN: "tel:+12067791234",
MockResponses: map[string][]*httpx.MockResponse{
"https://messaging.bandwidth.com/api/v2/users/accound-id/messages": {
httpx.NewMockResponse(401, nil, []byte(`{ "type": "request-validation", "description": "Your request could not be accepted" }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{
Expand All @@ -338,14 +348,9 @@ var outgoingCases = []OutgoingTestCase{
},
},
ExpectedError: courier.ErrFailedWithReason("request-validation", "Your request could not be accepted"),
SendPrep: setSendURL,
},
}

func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel, m courier.MsgOut) {
sendURL = s.URL + "?%s"
}

func TestOutgoing(t *testing.T) {
ch := test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "BW", "2020", "US",
map[string]any{courier.ConfigUsername: "user1", courier.ConfigPassword: "pass1", configAccountID: "accound-id", configApplicationID: "application-id"},
Expand Down
Loading
Loading