From 5bd2c6860410ae2cfa222922c0617b6963e21e67 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Tue, 14 Jan 2025 17:22:06 +0200 Subject: [PATCH] Add more tests for coverage --- handlers/clickmobile/handler_test.go | 17 ++++++++++ handlers/clicksend/handler_test.go | 46 ++++++++++++++++++++++++++ handlers/firebase/handler_test.go | 48 ++++++++++++++++++++++++++++ handlers/rocketchat/handler_test.go | 14 ++++++++ handlers/slack/handler_test.go | 41 ++++++++++++++++++++++++ handlers/start/handler_test.go | 18 +++++++++++ handlers/telegram/handler_test.go | 14 ++++++++ handlers/telesom/handler_test.go | 17 ++++++++++ handlers/thinq/handler_test.go | 16 ++++++++++ handlers/vk/handler_test.go | 16 ++++++++++ 10 files changed, 247 insertions(+) diff --git a/handlers/clickmobile/handler_test.go b/handlers/clickmobile/handler_test.go index ae3345c2b..9e29bb15f 100644 --- a/handlers/clickmobile/handler_test.go +++ b/handlers/clickmobile/handler_test.go @@ -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) { diff --git a/handlers/clicksend/handler_test.go b/handlers/clicksend/handler_test.go index a72c44085..fe54f26eb 100644 --- a/handlers/clicksend/handler_test.go +++ b/handlers/clicksend/handler_test.go @@ -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", @@ -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) { diff --git a/handlers/firebase/handler_test.go b/handlers/firebase/handler_test.go index bad9b7cee..f1c5615bf 100644 --- a/handlers/firebase/handler_test.go +++ b/handlers/firebase/handler_test.go @@ -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) { diff --git a/handlers/rocketchat/handler_test.go b/handlers/rocketchat/handler_test.go index bd7966d18..18d34ff00 100644 --- a/handlers/rocketchat/handler_test.go +++ b/handlers/rocketchat/handler_test.go @@ -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) { diff --git a/handlers/slack/handler_test.go b/handlers/slack/handler_test.go index cd8b470f3..90f29f716 100644 --- a/handlers/slack/handler_test.go +++ b/handlers/slack/handler_test.go @@ -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{ @@ -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) { diff --git a/handlers/start/handler_test.go b/handlers/start/handler_test.go index 1e977ba28..e60b0e9a8 100644 --- a/handlers/start/handler_test.go +++ b/handlers/start/handler_test.go @@ -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(`Accepted`)), + }, + }, + ExpectedRequests: []ExpectedRequest{{ + Headers: map[string]string{ + "Content-Type": "application/xml; charset=utf8", + "Authorization": "Basic VXNlcm5hbWU6UGFzc3dvcmQ=", + }, + Body: `+250788383383Simple Message ☺`, + }}, + ExpectedError: courier.ErrResponseUnexpectedUnlogged, + }, } func TestOutgoing(t *testing.T) { diff --git a/handlers/telegram/handler_test.go b/handlers/telegram/handler_test.go index 9022bb1bf..1a8b9157f 100644 --- a/handlers/telegram/handler_test.go +++ b/handlers/telegram/handler_test.go @@ -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!", diff --git a/handlers/telesom/handler_test.go b/handlers/telesom/handler_test.go index f28552f9f..65c5cff17 100644 --- a/handlers/telesom/handler_test.go +++ b/handlers/telesom/handler_test.go @@ -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" @@ -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(`Missing`)), + }, + }, + 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: Missing")}, + ExpectedError: courier.ErrResponseUnexpectedUnlogged, + }, } func TestOutgoing(t *testing.T) { diff --git a/handlers/thinq/handler_test.go b/handlers/thinq/handler_test.go index f21ba8207..dd1aeb131 100644 --- a/handlers/thinq/handler_test.go +++ b/handlers/thinq/handler_test.go @@ -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) { diff --git a/handlers/vk/handler_test.go b/handlers/vk/handler_test.go index df3e91369..d47b32d70 100644 --- a/handlers/vk/handler_test.go +++ b/handlers/vk/handler_test.go @@ -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) {