github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/registryv1/tokens_test.go (about) 1 package registryv1 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 ibmcloud "github.com/IBM-Cloud/bluemix-go" 9 "github.com/IBM-Cloud/bluemix-go/client" 10 ibmcloudHttp "github.com/IBM-Cloud/bluemix-go/http" 11 "github.com/IBM-Cloud/bluemix-go/session" 12 13 "github.com/onsi/gomega/ghttp" 14 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 const ( 20 tokenID = `ae57b394-e39d-595f-ae5d-e46905285df6` 21 tokenDescription = "Test Token" 22 tokenList = `{ 23 "tokens": [ 24 { 25 "_id": "ae57b394-e39d-595f-ae5d-e46905285df6", 26 "owner": "abc", 27 "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDA0MjMwNTQsImp0aSI6ImFlNTdiMzk0LWUzOWQtNTk1Zi1hZTVkLWU0NjkwNTI4NWRmNiIsImlzcyI6InJlZ2lzdHJ5Lm5nLmJsdWVtaXgubmV0In0.mZBpsw-6RxuvV_Bv7WBk6I6YGJK9f70fEuH5a2cVr3g", 28 "secondary_owner": "Test Token" 29 } 30 ] 31 }` 32 token = `{ 33 "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDA0MjMwNTQsImp0aSI6ImFlNTdiMzk0LWUzOWQtNTk1Zi1hZTVkLWU0NjkwNTI4NWRmNiIsImlzcyI6InJlZ2lzdHJ5Lm5nLmJsdWVtaXgubmV0In0.mZBpsw-6RxuvV_Bv7WBk6I6YGJK9f70fEuH5a2cVr3g" 34 }` 35 tokenError = `{ 36 "code": "CRG0009E", 37 "message": "You are not authorized to access the specified account.", 38 "request-id": "72-1540337041.610-99712" 39 }` 40 ) 41 42 var _ = Describe("Tokens", func() { 43 var server *ghttp.Server 44 AfterEach(func() { 45 server.Close() 46 }) 47 Describe("GetTokens", func() { 48 Context("When get tokens is completed", func() { 49 BeforeEach(func() { 50 server = ghttp.NewServer() 51 server.AppendHandlers( 52 ghttp.CombineHandlers( 53 ghttp.VerifyRequest(http.MethodGet, "/api/v1/tokens"), 54 ghttp.RespondWith(http.StatusOK, tokenList), 55 ), 56 ) 57 }) 58 59 It("should return get tokens results", func() { 60 target := TokenTargetHeader{ 61 AccountID: "abc", 62 } 63 respptr, err := newTokens(server.URL()).GetTokens(target) 64 Expect(err).NotTo(HaveOccurred()) 65 Expect(respptr).NotTo(BeNil()) 66 resp := *respptr 67 Expect(resp.Tokens).To(HaveLen(1)) 68 Expect(resp.Tokens[0].ID).Should(Equal(tokenID)) 69 Expect(resp.Tokens[0].Owner).Should(Equal("abc")) 70 Expect(resp.Tokens[0].Token).Should(Equal("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDA0MjMwNTQsImp0aSI6ImFlNTdiMzk0LWUzOWQtNTk1Zi1hZTVkLWU0NjkwNTI4NWRmNiIsImlzcyI6InJlZ2lzdHJ5Lm5nLmJsdWVtaXgubmV0In0.mZBpsw-6RxuvV_Bv7WBk6I6YGJK9f70fEuH5a2cVr3g")) 71 Expect(resp.Tokens[0].Description).Should(Equal("Test Token")) 72 }) 73 }) 74 Context("When get token fails", func() { 75 BeforeEach(func() { 76 server = ghttp.NewServer() 77 server.SetAllowUnhandledRequests(true) 78 server.AppendHandlers( 79 ghttp.CombineHandlers( 80 ghttp.VerifyRequest(http.MethodGet, "/api/v1/tokens"), 81 ghttp.RespondWith(http.StatusInternalServerError, `Internal Error`), 82 ), 83 ) 84 }) 85 86 It("should return error when tokens are retrieved", func() { 87 target := TokenTargetHeader{ 88 AccountID: "abc", 89 } 90 respptr, err := newTokens(server.URL()).GetTokens(target) 91 Expect(err).To(HaveOccurred()) 92 Expect(respptr).Should(BeNil()) 93 }) 94 }) 95 }) 96 97 Describe("GetToken", func() { 98 Context("When get token is completed", func() { 99 BeforeEach(func() { 100 server = ghttp.NewServer() 101 server.AppendHandlers( 102 ghttp.CombineHandlers( 103 ghttp.VerifyRequest(http.MethodGet, fmt.Sprintf("/api/v1/tokens/%s", tokenID)), 104 ghttp.RespondWith(http.StatusOK, token), 105 ), 106 ) 107 }) 108 109 It("should return get token results", func() { 110 target := TokenTargetHeader{ 111 AccountID: "abc", 112 } 113 respptr, err := newTokens(server.URL()).GetToken(tokenID, target) 114 Expect(err).NotTo(HaveOccurred()) 115 Expect(respptr).NotTo(BeNil()) 116 resp := *respptr 117 Expect(resp.ID).Should(Equal(tokenID)) 118 Expect(resp.Token).Should(Equal("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDA0MjMwNTQsImp0aSI6ImFlNTdiMzk0LWUzOWQtNTk1Zi1hZTVkLWU0NjkwNTI4NWRmNiIsImlzcyI6InJlZ2lzdHJ5Lm5nLmJsdWVtaXgubmV0In0.mZBpsw-6RxuvV_Bv7WBk6I6YGJK9f70fEuH5a2cVr3g")) 119 }) 120 }) 121 Context("When get token fails", func() { 122 BeforeEach(func() { 123 server = ghttp.NewServer() 124 server.AppendHandlers( 125 ghttp.CombineHandlers( 126 ghttp.VerifyRequest(http.MethodGet, fmt.Sprintf("/api/v1/tokens/%s", tokenID)), 127 ghttp.RespondWith(http.StatusUnauthorized, tokenError), 128 ), 129 ) 130 }) 131 132 It("should return error when token is goten", func() { 133 target := TokenTargetHeader{ 134 AccountID: "abc", 135 } 136 resparr, err := newTokens(server.URL()).GetToken(tokenID, target) 137 Expect(err).To(HaveOccurred()) 138 Expect(resparr).Should(BeNil()) 139 }) 140 }) 141 }) 142 143 Describe("IssueToken", func() { 144 Context("When issue token is completed", func() { 145 BeforeEach(func() { 146 server = ghttp.NewServer() 147 server.AppendHandlers( 148 ghttp.CombineHandlers( 149 ghttp.VerifyRequest(http.MethodPost, "/api/v1/tokens"), 150 ghttp.RespondWith(http.StatusOK, token), 151 ), 152 ) 153 }) 154 155 It("should return issue token results", func() { 156 param := IssueTokenRequest{ 157 Description: "Test Token", 158 Permanent: false, 159 Write: false, 160 } 161 target := TokenTargetHeader{ 162 AccountID: "abc", 163 } 164 respptr, err := newTokens(server.URL()).IssueToken(param, target) 165 Expect(err).NotTo(HaveOccurred()) 166 Expect(respptr).NotTo(BeNil()) 167 resp := *respptr 168 Expect(resp.ID).Should(Equal(tokenID)) 169 Expect(resp.Token).Should(Equal("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDA0MjMwNTQsImp0aSI6ImFlNTdiMzk0LWUzOWQtNTk1Zi1hZTVkLWU0NjkwNTI4NWRmNiIsImlzcyI6InJlZ2lzdHJ5Lm5nLmJsdWVtaXgubmV0In0.mZBpsw-6RxuvV_Bv7WBk6I6YGJK9f70fEuH5a2cVr3g")) 170 }) 171 }) 172 Context("When issue token fails", func() { 173 BeforeEach(func() { 174 server = ghttp.NewServer() 175 server.AppendHandlers( 176 ghttp.CombineHandlers( 177 ghttp.VerifyRequest(http.MethodPost, "/api/v1/tokens"), 178 ghttp.RespondWith(http.StatusUnauthorized, tokenError), 179 ), 180 ) 181 }) 182 183 It("should return error when token is issued", func() { 184 param := IssueTokenRequest{ 185 Description: "Test Token", 186 Permanent: false, 187 Write: false, 188 } 189 target := TokenTargetHeader{ 190 AccountID: "abc", 191 } 192 respptr, err := newTokens(server.URL()).IssueToken(param, target) 193 Expect(err).To(HaveOccurred()) 194 Expect(respptr).Should(BeNil()) 195 }) 196 }) 197 }) 198 Describe("IssueToken", func() { 199 Context("When issue token is completed", func() { 200 BeforeEach(func() { 201 server = ghttp.NewServer() 202 server.AppendHandlers( 203 ghttp.CombineHandlers( 204 ghttp.VerifyRequest(http.MethodPost, "/api/v1/tokens"), 205 ghttp.RespondWith(http.StatusOK, token), 206 ), 207 ) 208 }) 209 210 It("should return issue token results", func() { 211 param := IssueTokenRequest{ 212 Description: "Test Token", 213 Permanent: false, 214 Write: false, 215 } 216 target := TokenTargetHeader{ 217 AccountID: "abc", 218 } 219 respptr, err := newTokens(server.URL()).IssueToken(param, target) 220 Expect(err).NotTo(HaveOccurred()) 221 Expect(respptr).NotTo(BeNil()) 222 resp := *respptr 223 Expect(resp.ID).Should(Equal(tokenID)) 224 Expect(resp.Token).Should(Equal("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDA0MjMwNTQsImp0aSI6ImFlNTdiMzk0LWUzOWQtNTk1Zi1hZTVkLWU0NjkwNTI4NWRmNiIsImlzcyI6InJlZ2lzdHJ5Lm5nLmJsdWVtaXgubmV0In0.mZBpsw-6RxuvV_Bv7WBk6I6YGJK9f70fEuH5a2cVr3g")) 225 }) 226 }) 227 Context("When issue token fails", func() { 228 BeforeEach(func() { 229 server = ghttp.NewServer() 230 server.AppendHandlers( 231 ghttp.CombineHandlers( 232 ghttp.VerifyRequest(http.MethodPost, "/api/v1/tokens"), 233 ghttp.RespondWith(http.StatusUnauthorized, tokenError), 234 ), 235 ) 236 }) 237 238 It("should return error when token is issued", func() { 239 param := IssueTokenRequest{ 240 Description: "Test Token", 241 Permanent: false, 242 Write: false, 243 } 244 target := TokenTargetHeader{ 245 AccountID: "abc", 246 } 247 resparr, err := newTokens(server.URL()).IssueToken(param, target) 248 Expect(err).To(HaveOccurred()) 249 Expect(resparr).Should(BeNil()) 250 }) 251 }) 252 }) 253 Describe("DeleteToken", func() { 254 Context("When delete token is completed", func() { 255 BeforeEach(func() { 256 server = ghttp.NewServer() 257 server.AppendHandlers( 258 ghttp.CombineHandlers( 259 ghttp.VerifyRequest(http.MethodDelete, fmt.Sprintf("/api/v1/tokens/%s", tokenID)), 260 ghttp.RespondWith(http.StatusOK, token), 261 ), 262 ) 263 }) 264 265 It("should delete token results", func() { 266 target := TokenTargetHeader{ 267 AccountID: "abc", 268 } 269 err := newTokens(server.URL()).DeleteToken(tokenID, target) 270 Expect(err).NotTo(HaveOccurred()) 271 }) 272 }) 273 Context("When dlete token fails", func() { 274 BeforeEach(func() { 275 server = ghttp.NewServer() 276 server.AppendHandlers( 277 ghttp.CombineHandlers( 278 ghttp.VerifyRequest(http.MethodDelete, fmt.Sprintf("/api/v1/tokens/%s", tokenID)), 279 ghttp.RespondWith(http.StatusUnauthorized, tokenError), 280 ), 281 ) 282 }) 283 284 It("should return error when token is deleted", func() { 285 target := TokenTargetHeader{ 286 AccountID: "abc", 287 } 288 err := newTokens(server.URL()).DeleteToken(tokenID, target) 289 Expect(err).To(HaveOccurred()) 290 }) 291 }) 292 }) 293 Describe("DeleteTokenByDescription", func() { 294 Context("When delete token by description is completed", func() { 295 BeforeEach(func() { 296 server = ghttp.NewServer() 297 server.AppendHandlers( 298 ghttp.CombineHandlers( 299 ghttp.VerifyRequest(http.MethodDelete, "/api/v1/tokens"), 300 ghttp.RespondWith(http.StatusOK, tokenDescription), 301 ), 302 ) 303 }) 304 305 It("should delete token by description results", func() { 306 target := TokenTargetHeader{ 307 AccountID: "abc", 308 } 309 err := newTokens(server.URL()).DeleteTokenByDescription(tokenDescription, target) 310 Expect(err).NotTo(HaveOccurred()) 311 }) 312 }) 313 Context("When delete token by description fails", func() { 314 BeforeEach(func() { 315 server = ghttp.NewServer() 316 server.AppendHandlers( 317 ghttp.CombineHandlers( 318 ghttp.VerifyRequest(http.MethodDelete, "/api/v1/tokens"), 319 ghttp.RespondWith(http.StatusUnauthorized, tokenError), 320 ), 321 ) 322 }) 323 324 It("should return error when token is deleted with description", func() { 325 target := TokenTargetHeader{ 326 AccountID: "abc", 327 } 328 err := newTokens(server.URL()).DeleteTokenByDescription(tokenID, target) 329 Expect(err).To(HaveOccurred()) 330 }) 331 }) 332 }) 333 334 }) 335 336 func newTokens(url string) Tokens { 337 338 sess, err := session.New() 339 if err != nil { 340 log.Fatal(err) 341 } 342 conf := sess.Config.Copy() 343 conf.HTTPClient = ibmcloudHttp.NewHTTPClient(conf) 344 conf.Endpoint = &url 345 346 client := client.Client{ 347 Config: conf, 348 ServiceName: ibmcloud.ContainerRegistryService, 349 } 350 return newTokenAPI(&client) 351 }