Skip to content

Commit

Permalink
Add more tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 14, 2025
1 parent 1c4826a commit 5bd2c68
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 0 deletions.
17 changes: 17 additions & 0 deletions handlers/clickmobile/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ var outgoingCases = []OutgoingTestCase{
},
},
},
{
Label: "Response unexpected",
MsgText: "Simple Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://example.com/send": {
httpx.NewMockResponse(200, nil, []byte(`{"code":"001","desc":"Database SQL Error"}`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{"Content-Type": "application/json"},
Body: `{"app_id":"001-app","org_id":"001-org","user_id":"Username","timestamp":"20180411182430","auth_key":"3e1347ddb444d13aa23d11e097602be0","operation":"send","reference":"10","message_type":"1","src_address":"2020","dst_address":"+250788383383","message":"Simple Message"}`,
},
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
46 changes: 46 additions & 0 deletions handlers/clicksend/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ const successResponse = `{
]
}`

const successResponseMissingMessageID = `{
"http_code": 200,
"response_code": "SUCCESS",
"response_msg": "Here are your data.",
"data": {
"total_price": 0.28,
"total_count": 2,
"queued_count": 2,
"messages": [
{
"direction": "out",
"date": 1436871253,
"to": "+61411111111",
"body": "Jelly liquorice marshmallow candy carrot cake 4Eyffjs1vL.",
"from": "sendmobile",
"schedule": 1436874701,
"message_id": "",
"message_parts": 1,
"message_price": 0.07,
"custom_string": "this is a test",
"user_id": 1,
"subaccount_id": 1,
"country": "AU",
"carrier": "Telstra",
"status": "SUCCESS"
}
]
}`

const failureResponse = `{
"http_code": 200,
"response_code": "SUCCESS",
Expand Down Expand Up @@ -188,6 +217,23 @@ var outgoingCases = []OutgoingTestCase{
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
{
Label: "Failure Response",
MsgText: "Error Sending",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://rest.clicksend.com/v3/sms/send": {
httpx.NewMockResponse(200, nil, []byte(successResponseMissingMessageID)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{"Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="},
Body: `{"messages":[{"to":"+250788383383","from":"2020","body":"Error Sending","source":"courier"}]}`,
},
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
48 changes: 48 additions & 0 deletions handlers/firebase/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,54 @@ var sendTestCases = []OutgoingTestCase{
}},
ExpectedError: courier.ErrConnectionFailed,
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "fcm:250788123123",
MsgURNAuth: "auth1",
MockResponses: map[string][]*httpx.MockResponse{
"https://fcm.googleapis.com/v1/projects/foo-project-id/messages:send": {
httpx.NewMockResponse(200, nil, []byte(`{"missing_name":"projects/foo-project-id/messages/123456-a"}`)),
},
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "fcm:250788123123",
MsgURNAuth: "auth1",
MockResponses: map[string][]*httpx.MockResponse{
"https://fcm.googleapis.com/v1/projects/foo-project-id/messages:send": {
httpx.NewMockResponse(200, nil, []byte(`{"name":"projects/not-our-project-id/messages/123456-a"}`)),
},
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "fcm:250788123123",
MsgURNAuth: "auth1",
MockResponses: map[string][]*httpx.MockResponse{
"https://fcm.googleapis.com/v1/projects/foo-project-id/messages:send": {
httpx.NewMockResponse(200, nil, []byte(`{"name":"projects/foo-project-id/messages/"}`)),
},
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func setupBackend(mb *test.MockBackend) {
Expand Down
14 changes: 14 additions & 0 deletions handlers/rocketchat/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ var sendTestCases = []OutgoingTestCase{
}},
ExpectedError: courier.ErrConnectionFailed,
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "rocketchat:direct:john.doe#john.doe",
MockResponses: map[string][]*httpx.MockResponse{
"https://my.rocket.chat/api/apps/public/684202ed-1461-4983-9ea7-fde74b15026c/message": {
httpx.NewMockResponse(201, nil, []byte(`{"missing":"0"}`)),
},
},
ExpectedRequests: []ExpectedRequest{{
Body: `{"user":"direct:john.doe","bot":"rocket.cat","text":"Simple Message"}`,
}},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
41 changes: 41 additions & 0 deletions handlers/slack/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,28 @@ var defaultSendTestCases = []OutgoingTestCase{
ExpectedError: courier.ErrFailedWithReason("", "invalid_auth"),
ExpectedLogErrors: []*clogs.LogError{clogs.NewLogError("", "", "invalid_auth")},
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "slack:U0123ABCDEF",
MockResponses: map[string][]*httpx.MockResponse{
"*/chat.postMessage": {
httpx.NewMockResponse(200, nil, []byte(`{"channel":"U0123ABCDEF"}`)),
},
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "slack:U0123ABCDEF",
MockResponses: map[string][]*httpx.MockResponse{
"*/chat.postMessage": {
httpx.NewMockResponse(200, nil, []byte(`{"ok":false,"channel":"U0123ABCDEF"}`)),
},
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

var fileSendTestCases = []OutgoingTestCase{
Expand All @@ -236,6 +258,25 @@ var fileSendTestCases = []OutgoingTestCase{
{BodyContains: "image.png"},
},
},
{
Label: "Unexpected Response",
MsgText: "",
MsgURN: "slack:U0123ABCDEF",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.png"},
MockResponses: map[string][]*httpx.MockResponse{
"*/image.png": {
httpx.NewMockResponse(200, nil, []byte(`filetype... ...file bytes... ...end`)),
},
"*/files.upload": {
httpx.NewMockResponse(200, nil, []byte(`{"ok":false,"file":{"id":"F1L3SL4CK1D"}}`)),
},
},
ExpectedRequests: []ExpectedRequest{
{},
{BodyContains: "image.png"},
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestIncoming(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions handlers/start/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,24 @@ var defaultSendTestCases = []OutgoingTestCase{
}},
ExpectedError: courier.ErrResponseStatus,
},
{
Label: "Response Unexpected",
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://bulk.startmobile.ua/clients.php": {
httpx.NewMockResponse(200, nil, []byte(`<status date='Wed, 25 May 2016 17:29:56 +0300'><id></id><state>Accepted</state></status>`)),
},
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{
"Content-Type": "application/xml; charset=utf8",
"Authorization": "Basic VXNlcm5hbWU6UGFzc3dvcmQ=",
},
Body: `<message><service id="single" source="2020" validity="+12 hours"></service><to>+250788383383</to><body content-type="plain/text" encoding="plain">Simple Message ☺</body></message>`,
}},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions handlers/telegram/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,20 @@ var outgoingCases = []OutgoingTestCase{
},
ExpectedExtIDs: []string{"133"},
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "telegram:12345",
MockResponses: map[string][]*httpx.MockResponse{
"*/botauth_token/sendMessage": {
httpx.NewMockResponse(200, nil, []byte(`{ "ok": true, "result": { "message_id": 0 } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"text": {"Simple Message"}, "chat_id": {"12345"}, "parse_mode": []string{"Markdown"}, "reply_markup": {`{"remove_keyboard":true}`}}},
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
{
Label: "Unknown attachment type",
MsgText: "My foo!",
Expand Down
17 changes: 17 additions & 0 deletions handlers/telesom/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/nyaruka/courier"
. "github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/test"
"github.com/nyaruka/courier/utils/clogs"
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/urns"
Expand Down Expand Up @@ -158,6 +159,22 @@ var defaultSendTestCases = []OutgoingTestCase{
}},
ExpectedError: courier.ErrConnectionFailed,
},
{
Label: "Response Unexpected",
MsgText: "Simple Message",
MsgURN: "tel:+252788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://telesom.com/sendsms_other*": {
httpx.NewMockResponse(200, nil, []byte(`<return>Missing</return>`)),
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"msg": {"Simple Message"}, "to": {"0788383383"}, "from": {"2020"}, "key": {"D69BB824F88F20482B94ECF3822EBD84"}},
Headers: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
}},
ExpectedLogErrors: []*clogs.LogError{clogs.NewLogError("", "", "Received invalid response content: <return>Missing</return>")},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions handlers/thinq/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ var sendTestCases = []OutgoingTestCase{
}},
ExpectedError: courier.ErrConnectionFailed,
},
{
Label: "Reponse Unexpected",
MsgText: "Simple Message ☺",
MsgURN: "tel:+12067791234",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.thinq.com/account/1234/product/origination/sms/send": {
httpx.NewMockResponse(200, nil, []byte(`{ "missing": "1002" }`)),
},
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Basic dXNlcjE6c2VzYW1l"},
Body: `{"from_did":"2065551212","to_did":"2067791234","message":"Simple Message ☺"}`,
}},
ExpectedLogErrors: []*clogs.LogError{courier.ErrorResponseValueMissing("guid")},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions handlers/vk/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,22 @@ var outgoingCases = []OutgoingTestCase{
},
ExpectedError: courier.ErrConnectionFailed,
},
{
Label: "Response unexpected",
MsgText: "Simple message",
MsgURN: "vk:123456789",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.vk.com/method/messages.send.json?*": {
httpx.NewMockResponse(200, nil, []byte(`{"missing": 1}`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Params: url.Values{"access_token": {"token123xyz"}, "attachment": {""}, "message": {"Simple message"}, "random_id": {"10"}, "user_id": {"123456789"}, "v": {"5.103"}},
},
},
ExpectedError: courier.ErrResponseUnexpectedUnlogged,
},
}

func TestOutgoing(t *testing.T) {
Expand Down

0 comments on commit 5bd2c68

Please sign in to comment.