From 937b5c802ebcdf749f40e1e73e802d45184357e2 Mon Sep 17 00:00:00 2001 From: Adam Chalkley Date: Thu, 26 Mar 2020 04:57:51 -0500 Subject: [PATCH] Allow multiple valid webhook URL FQDNs Based on research/testing, it appears that both of these two FQDNs are interchangeable: - outlook.office.com - outlook.office365.com This commit makes the following changes to accept either FQDN as a valid webhook prefix: - update the isValidWebhookURL validation function to accept either one as a valid webhook prefix - update the TestTeamsClientSend table test to include outlook.office365.com variations of existing test cases specific to outlook.office.com refs dasrick/go-teams-notify#3 --- send.go | 8 +++++--- send_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/send.go b/send.go index 146c570..9f104ab 100644 --- a/send.go +++ b/send.go @@ -82,9 +82,11 @@ func isValidWebhookURL(webhookURL string) (bool, error) { return false, err } // only pass MS teams webhook URLs - hasPrefix := strings.HasPrefix(webhookURL, "https://outlook.office.com/webhook/") - if !hasPrefix { - err = errors.New("unvalid ms teams webhook url") + switch { + case strings.HasPrefix(webhookURL, "https://outlook.office.com/webhook/"): + case strings.HasPrefix(webhookURL, "https://outlook.office365.com/webhook/"): + default: + err = errors.New("invalid ms teams webhook url") return false, err } return true, nil diff --git a/send_test.go b/send_test.go index 370f4a6..c049b98 100644 --- a/send_test.go +++ b/send_test.go @@ -33,7 +33,7 @@ func TestTeamsClientSend(t *testing.T) { resError: nil, error: &url.Error{}, }, - // invalid webhookURL - missing pefix in (https://outlook.office.com...) URL + // invalid webhookURL - missing prefix in webhook URL { reqURL: "", reqMsg: emptyMessage, @@ -49,6 +49,14 @@ func TestTeamsClientSend(t *testing.T) { resError: errors.New("pling"), error: &url.Error{}, }, + // invalid httpClient.Do call + { + reqURL: "https://outlook.office365.com/webhook/xxx", + reqMsg: emptyMessage, + resStatus: 200, + resError: errors.New("pling"), + error: &url.Error{}, + }, // invalid response status code { reqURL: "https://outlook.office.com/webhook/xxx", @@ -57,6 +65,14 @@ func TestTeamsClientSend(t *testing.T) { resError: nil, error: errors.New(""), }, + // invalid response status code + { + reqURL: "https://outlook.office365.com/webhook/xxx", + reqMsg: emptyMessage, + resStatus: 400, + resError: nil, + error: errors.New(""), + }, // valid { reqURL: "https://outlook.office.com/webhook/xxx", @@ -65,6 +81,14 @@ func TestTeamsClientSend(t *testing.T) { resError: nil, error: nil, }, + // valid + { + reqURL: "https://outlook.office365.com/webhook/xxx", + reqMsg: emptyMessage, + resStatus: 200, + resError: nil, + error: nil, + }, } for _, test := range tests { client := NewTestClient(func(req *http.Request) (*http.Response, error) {