github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/alert/util_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package alert
    21  
    22  import (
    23  	"fmt"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  )
    28  
    29  var _ = Describe("alter", func() {
    30  	const (
    31  		webhookURL = "https://oapi.dingtalk.com/robot/send?access_token=123456"
    32  	)
    33  	It("string to map", func() {
    34  		key := "url"
    35  		str := key + "=" + webhookURL
    36  		res := strToMap(str)
    37  		Expect(res).ShouldNot(BeNil())
    38  		Expect(res["url"]).Should(Equal(webhookURL))
    39  	})
    40  
    41  	It("get url webhook type", func() {
    42  		testCases := []struct {
    43  			url      string
    44  			expected webhookType
    45  		}{
    46  			{url: "", expected: unknownWebhookType},
    47  			{url: "https://test.com", expected: unknownWebhookType},
    48  			{url: webhookURL, expected: dingtalkWebhookType},
    49  			{url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=123456", expected: wechatWebhookType},
    50  			{url: "https://open.feishu.cn/open-apis/bot/v2/hook/123456", expected: feishuWebhookType},
    51  		}
    52  		for _, tc := range testCases {
    53  			webhookType := getWebhookType(tc.url)
    54  			Expect(webhookType).Should(Equal(tc.expected))
    55  		}
    56  	})
    57  
    58  	It("remove duplicated string from slice", func() {
    59  		slice := []string{"a", "b", "a", "c"}
    60  		res := removeDuplicateStr(slice)
    61  		Expect(res).ShouldNot(BeNil())
    62  		Expect(res).Should(Equal([]string{"a", "b", "c"}))
    63  	})
    64  
    65  	It("url validation", func() {
    66  		testCases := []struct {
    67  			url      string
    68  			expected bool
    69  		}{
    70  			{url: "", expected: false},
    71  			{url: "https://test.com", expected: true},
    72  			{url: "/foo/bar", expected: true},
    73  			{url: "\"https://test.com\"", expected: false},
    74  		}
    75  		for _, tc := range testCases {
    76  			By(fmt.Sprintf("url: %s, expected: %t", tc.url, tc.expected))
    77  			res, _ := urlIsValid(tc.url)
    78  			Expect(res).Should(Equal(tc.expected))
    79  		}
    80  	})
    81  
    82  	It("email validation", func() {
    83  		testCases := []struct {
    84  			email    string
    85  			expected bool
    86  		}{
    87  			{email: "", expected: false},
    88  			{email: "test.com", expected: false},
    89  			{email: "test@com", expected: true},
    90  		}
    91  		for _, tc := range testCases {
    92  			By(fmt.Sprintf("email: %s, expected: %t", tc.email, tc.expected))
    93  			res := validEmail(tc.email)
    94  			Expect(res).Should(Equal(tc.expected))
    95  		}
    96  	})
    97  
    98  })