github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv1/webhooks_test.go (about) 1 package containerv1 2 3 import ( 4 "log" 5 "net/http" 6 7 bluemix "github.com/IBM-Cloud/bluemix-go" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 10 "github.com/IBM-Cloud/bluemix-go/session" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("Webhooks", func() { 18 var server *ghttp.Server 19 Describe("Add", func() { 20 Context("When adding a webhook is successful", func() { 21 BeforeEach(func() { 22 server = ghttp.NewServer() 23 server.AppendHandlers( 24 ghttp.CombineHandlers( 25 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/webhooks"), 26 ghttp.RespondWith(http.StatusCreated, `{}`), 27 ), 28 ) 29 }) 30 31 It("should return webhook added to cluster", func() { 32 target := ClusterTargetHeader{ 33 OrgID: "abc", 34 SpaceID: "def", 35 AccountID: "ghi", 36 } 37 params := WebHook{ 38 Level: "Warning", Type: "slack", URL: "http://slack.com/frwf-grev", 39 } 40 err := newWebhook(server.URL()).Add("test", params, target) 41 Expect(err).NotTo(HaveOccurred()) 42 }) 43 }) 44 Context("When adding webhook is unsuccessful", func() { 45 BeforeEach(func() { 46 server = ghttp.NewServer() 47 server.SetAllowUnhandledRequests(true) 48 server.AppendHandlers( 49 ghttp.CombineHandlers( 50 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/webhooks"), 51 ghttp.RespondWith(http.StatusInternalServerError, `Failed to add webhook to cluster`), 52 ), 53 ) 54 }) 55 56 It("should return error during add webhook to cluster", func() { 57 target := ClusterTargetHeader{ 58 OrgID: "abc", 59 SpaceID: "def", 60 AccountID: "ghi", 61 } 62 params := WebHook{ 63 Level: "Warning", Type: "slack", URL: "http://slack.com/frwf-grev", 64 } 65 err := newWebhook(server.URL()).Add("test", params, target) 66 Expect(err).To(HaveOccurred()) 67 }) 68 }) 69 }) 70 //List 71 Describe("List", func() { 72 Context("When retrieving available webhooks is successful", func() { 73 BeforeEach(func() { 74 server = ghttp.NewServer() 75 server.AppendHandlers( 76 ghttp.CombineHandlers( 77 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test/webhooks"), 78 ghttp.RespondWith(http.StatusOK, ` 79 [{"Level": "Warning", "Type": "slack", "URL": "http://slack.com/frwf-grev"}] 80 `), 81 ), 82 ) 83 }) 84 85 It("should return available webhooks ", func() { 86 target := ClusterTargetHeader{ 87 OrgID: "abc", 88 SpaceID: "def", 89 AccountID: "ghi", 90 } 91 webhooks, err := newWebhook(server.URL()).List("test", target) 92 Expect(err).NotTo(HaveOccurred()) 93 Expect(webhooks).ShouldNot(BeNil()) 94 for _, wObj := range webhooks { 95 Expect(wObj).ShouldNot(BeNil()) 96 Expect(wObj.Type).Should(Equal("slack")) 97 } 98 }) 99 }) 100 Context("When retrieving available webhooks is unsuccessful", func() { 101 BeforeEach(func() { 102 server = ghttp.NewServer() 103 server.SetAllowUnhandledRequests(true) 104 server.AppendHandlers( 105 ghttp.CombineHandlers( 106 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test/webhooks"), 107 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve webhooks`), 108 ), 109 ) 110 }) 111 112 It("should return error during retrieveing webhooks", func() { 113 target := ClusterTargetHeader{ 114 OrgID: "abc", 115 SpaceID: "def", 116 AccountID: "ghi", 117 } 118 webhook, err := newWebhook(server.URL()).List("test", target) 119 Expect(err).To(HaveOccurred()) 120 Expect(webhook).Should(BeNil()) 121 }) 122 }) 123 }) 124 125 }) 126 127 func newWebhook(url string) Webhooks { 128 129 sess, err := session.New() 130 if err != nil { 131 log.Fatal(err) 132 } 133 conf := sess.Config.Copy() 134 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 135 conf.Endpoint = &url 136 137 client := client.Client{ 138 Config: conf, 139 ServiceName: bluemix.MccpService, 140 } 141 return newWebhookAPI(&client) 142 }