github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/certificatemanager/certificate_test.go (about) 1 package certificatemanager 2 3 import ( 4 "log" 5 "net/http" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 "github.com/onsi/gomega/ghttp" 10 11 bluemix "github.com/IBM-Cloud/bluemix-go" 12 "github.com/IBM-Cloud/bluemix-go/client" 13 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 14 "github.com/IBM-Cloud/bluemix-go/models" 15 "github.com/IBM-Cloud/bluemix-go/session" 16 ) 17 18 var _ = Describe("Certificate_Manager", func() { 19 20 //CertID := "qwertyuiopasdfghjklmnbvcxzsdfghyhbn" 21 data := models.Data{ 22 Content: "asdfghjkl", 23 Privatekey: "", 24 IntermediateCertificate: "", 25 } 26 27 importdata := models.CertificateImportData{ 28 Name: "Test", 29 Description: "Test Certificate", 30 Data: data, 31 } 32 updatemetaData := models.CertificateMetadataUpdate{ 33 Name: "Test", 34 Description: "Test Certificate", 35 } 36 37 reimportData := models.CertificateReimportData{ 38 Content: "asdfghjk", 39 Privatekey: "qwertyuijhgfd", 40 IntermediateCertificate: "sdfghjmnbvcdf", 41 } 42 orderdata := models.CertificateOrderData{ 43 Name: "string", 44 Description: "string", 45 Domains: []string{"asdfg"}, 46 DomainValidationMethod: "dns-01", 47 DNSProviderInstanceCrn: "string", 48 Issuer: "Let's Encrypt", 49 Algorithm: "sha256WithRSAEncryption", 50 KeyAlgorithm: "rsaEncryption 2048 bit", 51 AutoRenewEnabled: false, 52 } 53 54 var server *ghttp.Server 55 AfterEach(func() { 56 server.Close() 57 }) 58 // import 59 Describe("Import Certificate", func() { 60 Context("Server returns response", func() { 61 BeforeEach(func() { 62 server = ghttp.NewServer() 63 server.AppendHandlers( 64 ghttp.CombineHandlers( 65 ghttp.VerifyRequest(http.MethodPost, "/api/v3/zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa/certificates/import"), 66 ghttp.RespondWith(http.StatusOK, `{ 67 "_id": "crn:v1:bluemix:public:cloudcerts:us-south:a/4448261269a14562b839e0a3019ed980:f352ce16-97c6-436c-a7b2-0f9bbe3fecf1:certificate:bd06f268c1b9ebf4d9aa403b556a132f", 68 "name": "aaa", 69 "issuer": "etcd ca", 70 "domains": [ 71 "ca" 72 ], 73 "begins_on": 1510768920000, 74 "expires_on": 1826128920000, 75 "imported": true, 76 "status": "valid", 77 "algorithm": "sha256WithRSAEncryption", 78 "key_algorithm": "rsaEncryption 2048 bit", 79 "description": "aaaa", 80 "has_previous": false 81 }`), 82 ), 83 ) 84 }) 85 It("Should return a response", func() { 86 resp, err := newCert(server.URL()).ImportCertificate("zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa", importdata) 87 Expect(resp).ShouldNot(BeNil()) 88 Expect(err).NotTo(HaveOccurred()) 89 }) 90 }) 91 92 Context("When unsuccessful", func() { 93 BeforeEach(func() { 94 server = ghttp.NewServer() 95 server.SetAllowUnhandledRequests(true) 96 server.AppendHandlers( 97 ghttp.CombineHandlers( 98 ghttp.VerifyRequest(http.MethodPost, "/api/v3/zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa/certificates/import"), 99 ghttp.RespondWith(http.StatusInternalServerError, `Failed to Import`), 100 ), 101 ) 102 }) 103 It("should return error ", func() { 104 _, err := newCert(server.URL()).ImportCertificate("zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa", importdata) 105 Expect(err).To(HaveOccurred()) 106 107 }) 108 109 }) 110 }) 111 112 // get cert data 113 Describe("Get certificate data", func() { 114 Context("Server returns response", func() { 115 BeforeEach(func() { 116 server = ghttp.NewServer() 117 server.AppendHandlers( 118 ghttp.CombineHandlers( 119 ghttp.VerifyRequest(http.MethodGet, "/api/v2/certificate/qwertyuiolkjhgfdsazxcvbnm"), 120 ghttp.RespondWith(http.StatusOK, `{ 121 "_id": "crn:v1:bluemix:public:cloudcerts:us-south:a/4448261269a14562b839e0a3019ed980:f352ce16-97c6-436c-a7b2-0f9bbe3fecf1:certificate:bd06f268c1b9ebf4d9aa403b556a132f", 122 "name": "aaa", 123 "issuer": "etcd ca", 124 "domains": [ 125 "ca" 126 ], 127 "begins_on": 1510768920000, 128 "expires_on": 1826128920000, 129 "imported": true, 130 "status": "valid", 131 "algorithm": "sha256WithRSAEncryption", 132 "key_algorithm": "rsaEncryption 2048 bit", 133 "description": "aaaa", 134 "has_previous": false, 135 "data": { 136 "content": "-----BEGIN CERTIFICATE-----\r\nMIIDsTCCApmgAwIBAgIUWzsBehxAkgLLYBUZEUpSjHkIaMowDQYJKoZIhvcNAQEL\r\nBQAwbzEMMAoGA1UEBhMDVVNBMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH\r\nEw1TYW4gRnJhbmNpc2NvMQ0wCwYDVQQKEwRldGNkMRYwFAYDVQQLEw1ldGNkIFNl\r\nY3VyaXR5MQswCQYDVQQDEwJjYTAeFw0xNzExMTUxODAyMDBaFw0yNzExMTMxODAy\r\nMDBaMG8xDDAKBgNVBAYTA1VTQTETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UE\r\nBxMNU2FuIEZyYW5jaXNjbzENMAsGA1UEChMEZXRjZDEWMBQGA1UECxMNZXRjZCBT\r\nZWN1cml0eTELMAkGA1UEAxMCY2EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\r\nAoIBAQCxjHVNtcCSCz1w9AiN7zAql0ZsPN6MNQWJ2j3iPCvmy9oi0wqSfYXTs+xw\r\nY4Q+j0dfA54+PcyIOSBQCZBeLLIwCaXN+gLkMxYEWCCVgWYUa6UY+NzPKRCfkbwG\r\noE2Ilv3R1FWIpMqDVE2rLmTb3YxSiw460Ruv4l16kodEzfs4BRcqrEiobBwaIMLd\r\n0rDJju7Q2TcioNji+HFoXV2aLN58LDgKO9AqszXxW88IKwUspfGBcsA4Zti/OHr+\r\nW+i/VxsxnQSJiAoKYbv9SkS8fUWw2hQ9SBBCKqE3jLzI71HzKgjS5TiQVZJaD6oK\r\ncw8FjexOELZd4r1+/p+nQdKqwnb5AgMBAAGjRTBDMA4GA1UdDwEB/wQEAwIBBjAS\r\nBgNVHRMBAf8ECDAGAQH/AgECMB0GA1UdDgQWBBRLfPxmhlZix1eTdBMAzMVlAnOV\r\ngTANBgkqhkiG9w0BAQsFAAOCAQEAeT2NfOt3WsBLUVcnyGMeVRQ0gXazxJXD/Z+3\r\n2RF3KClqBLuGmPUZVl0FU841J6hLlwNjS33mye7k2OHrjJcouElbV3Olxsgh/EV0\r\nJ7b7Wf4zWYHFNZz/VxwGHunsEZ+SCXUzU8OiMrEcHkOVzhtbC2veVPJzrESqd88z\r\nm1MseGW636VIcrg4fYRS9EebRPFvlwfymMd+bqLky9KsUbjNupYd/TlhpAudrIzA\r\nwO9ZUDb/0P44iOo+xURCoodxDTM0vvfZ8eJ6VZ/17HIf/a71kvk1oMqEhf060nmF\r\nIxnbK6iUqqhV8DLE1869vpFvgbDdOxP7BeabN5FXEnZFDTLDqg==\r\n-----END CERTIFICATE-----" 137 }, 138 "last_modified": 1575527437586, 139 "data_key_id": "data_key", 140 "has_previous": true 141 142 143 }`), 144 ), 145 ) 146 }) 147 It("Should return a response", func() { 148 resp, err := newCert(server.URL()).GetCertData("qwertyuiolkjhgfdsazxcvbnm") 149 Expect(resp).ShouldNot(BeNil()) 150 Expect(err).NotTo(HaveOccurred()) 151 }) 152 }) 153 154 Context("When unsuccessful", func() { 155 BeforeEach(func() { 156 server = ghttp.NewServer() 157 server.SetAllowUnhandledRequests(true) 158 server.AppendHandlers( 159 ghttp.CombineHandlers( 160 ghttp.VerifyRequest(http.MethodGet, "/api/v2/certificate/qwertyuiolkjhgfdsazxcvbnm"), 161 ghttp.RespondWith(http.StatusInternalServerError, `Failed to Get certificate data`), 162 ), 163 ) 164 }) 165 It("should return error ", func() { 166 _, err := newCert(server.URL()).GetCertData("qwertyuiolkjhgfdsazxcvbnm") 167 Expect(err).To(HaveOccurred()) 168 169 }) 170 171 }) 172 }) 173 174 // get meta data 175 Describe("Get certificate meta data", func() { 176 Context("Server returns response", func() { 177 BeforeEach(func() { 178 server = ghttp.NewServer() 179 server.AppendHandlers( 180 ghttp.CombineHandlers( 181 ghttp.VerifyRequest(http.MethodGet, "/api/v1/certificate/qwertyuiolkjhgfdsazxcvbnm/metadata"), 182 ghttp.RespondWith(http.StatusOK, `{ 183 "_id": "crn:v1:bluemix:public:cloudcerts:us-south:a/4448261269a14562b839e0a3019ed980:f352ce16-97c6-436c-a7b2-0f9bbe3fecf1:certificate:bd06f268c1b9ebf4d9aa403b556a132f", 184 "name": "aaa", 185 "issuer": "etcd ca", 186 "domains": [ 187 "ca" 188 ], 189 "begins_on": 1510768920000, 190 "expires_on": 1826128920000, 191 "imported": true, 192 "status": "valid", 193 "algorithm": "sha256WithRSAEncryption", 194 "key_algorithm": "rsaEncryption 2048 bit", 195 "description": "aaaa", 196 "has_previous": false 197 }`), 198 ), 199 ) 200 }) 201 It("Should return a response", func() { 202 resp, err := newCert(server.URL()).GetMetaData("qwertyuiolkjhgfdsazxcvbnm") 203 Expect(resp).ShouldNot(BeNil()) 204 Expect(err).NotTo(HaveOccurred()) 205 }) 206 }) 207 208 Context("When unsuccessful", func() { 209 BeforeEach(func() { 210 server = ghttp.NewServer() 211 server.SetAllowUnhandledRequests(true) 212 server.AppendHandlers( 213 ghttp.CombineHandlers( 214 ghttp.VerifyRequest(http.MethodGet, "/api/v1/certificate/qwertyuiolkjhgfdsazxcvbnm/metadata"), 215 ghttp.RespondWith(http.StatusInternalServerError, `Failed to Get certificate meta data`), 216 ), 217 ) 218 }) 219 It("should return error ", func() { 220 _, err := newCert(server.URL()).GetMetaData("qwertyuiolkjhgfdsazxcvbnm") 221 Expect(err).To(HaveOccurred()) 222 223 }) 224 225 }) 226 }) 227 228 // Delete Certificate 229 Describe("Delete certificate", func() { 230 Context("Server returns response", func() { 231 BeforeEach(func() { 232 server = ghttp.NewServer() 233 server.AppendHandlers( 234 ghttp.CombineHandlers( 235 ghttp.VerifyRequest(http.MethodDelete, "/api/v2/certificate/qwertyuiolkjhgfdsazxcvbnm"), 236 ghttp.RespondWith(http.StatusOK, nil), 237 ), 238 ) 239 }) 240 It("Should return a response", func() { 241 err := newCert(server.URL()).DeleteCertificate("qwertyuiolkjhgfdsazxcvbnm") 242 Expect(err).NotTo(HaveOccurred()) 243 }) 244 }) 245 246 Context("When unsuccessful", func() { 247 BeforeEach(func() { 248 server = ghttp.NewServer() 249 server.SetAllowUnhandledRequests(true) 250 server.AppendHandlers( 251 ghttp.CombineHandlers( 252 ghttp.VerifyRequest(http.MethodDelete, "/api/v2/certificate/qwertyuiolkjhgfdsazxcvbnm"), 253 ghttp.RespondWith(http.StatusInternalServerError, `Failed to Delete certificate data`), 254 ), 255 ) 256 }) 257 It("should return error ", func() { 258 err := newCert(server.URL()).DeleteCertificate("qwertyuiolkjhgfdsazxcvbnm") 259 Expect(err).To(HaveOccurred()) 260 261 }) 262 263 }) 264 }) 265 266 // Update Certificate Meta Data 267 Describe("Update certificate", func() { 268 Context("Server returns response", func() { 269 BeforeEach(func() { 270 server = ghttp.NewServer() 271 server.AppendHandlers( 272 ghttp.CombineHandlers( 273 ghttp.VerifyRequest(http.MethodPost, "/api/v3/certificate/qwertyuiolkjhgfdsazxcvbnm"), 274 ghttp.RespondWith(http.StatusOK, nil), 275 ), 276 ) 277 }) 278 It("Should return a response", func() { 279 err := newCert(server.URL()).UpdateCertificateMetaData("qwertyuiolkjhgfdsazxcvbnm", updatemetaData) 280 Expect(err).NotTo(HaveOccurred()) 281 }) 282 }) 283 284 Context("When unsuccessful", func() { 285 BeforeEach(func() { 286 server = ghttp.NewServer() 287 server.SetAllowUnhandledRequests(true) 288 server.AppendHandlers( 289 ghttp.CombineHandlers( 290 ghttp.VerifyRequest(http.MethodPost, "/api/v3/certificate/qwertyuiolkjhgfdsazxcvbnm"), 291 ghttp.RespondWith(http.StatusInternalServerError, `Failed to Update certificate metadata`), 292 ), 293 ) 294 }) 295 It("should return error ", func() { 296 err := newCert(server.URL()).UpdateCertificateMetaData("qwertyuiolkjhgfdsazxcvbnm", updatemetaData) 297 Expect(err).To(HaveOccurred()) 298 299 }) 300 301 }) 302 }) 303 304 // Reimport Certificate 305 Describe("ReImport Certificate", func() { 306 Context("Server returns response", func() { 307 BeforeEach(func() { 308 server = ghttp.NewServer() 309 server.AppendHandlers( 310 ghttp.CombineHandlers( 311 ghttp.VerifyRequest(http.MethodPut, "/api/v1/certificate/zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa"), 312 ghttp.RespondWith(http.StatusOK, `{ 313 "_id": "string", 314 "name": "string", 315 "issuer": "string", 316 "domains": [ 317 "string" 318 ], 319 "begins_on": 0, 320 "expires_on": 0, 321 "imported": true, 322 "status": "active", 323 "algorithm": "string", 324 "key_algorithm": "string", 325 "description": "string", 326 "has_previous": true, 327 "issuance_info": { 328 "ordered_on": 0, 329 "status": "pending", 330 "code": "string", 331 "additional_info": "string", 332 "auto": true 333 }, 334 "order_policy": { 335 "auto_renew_enabled": false 336 }, 337 "downloaded": true 338 }`), 339 ), 340 ) 341 }) 342 It("Should return a response", func() { 343 resp, err := newCert(server.URL()).ReimportCertificate("zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa", reimportData) 344 Expect(resp).ShouldNot(BeNil()) 345 Expect(err).NotTo(HaveOccurred()) 346 }) 347 }) 348 349 Context("When unsuccessful", func() { 350 BeforeEach(func() { 351 server = ghttp.NewServer() 352 server.SetAllowUnhandledRequests(true) 353 server.AppendHandlers( 354 ghttp.CombineHandlers( 355 ghttp.VerifyRequest(http.MethodPut, "/api/v1/certificate/zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa"), 356 ghttp.RespondWith(http.StatusInternalServerError, `Failed to ReImport`), 357 ), 358 ) 359 }) 360 It("should return error ", func() { 361 _, err := newCert(server.URL()).ReimportCertificate("zxcvbnmmkjhgfdsaqwertyuiokjhgfdsa", reimportData) 362 Expect(err).To(HaveOccurred()) 363 364 }) 365 366 }) 367 }) 368 369 // Order Certificate 370 Describe("Order Certificate", func() { 371 Context("Server returns response", func() { 372 BeforeEach(func() { 373 server = ghttp.NewServer() 374 server.AppendHandlers( 375 ghttp.CombineHandlers( 376 ghttp.VerifyRequest(http.MethodPost, "/api/v1/asdfghjmnbvcdfgh/certificates/order"), 377 ghttp.RespondWith(http.StatusOK, `{ 378 "_id": "string", 379 "name": "string", 380 "issuer": "string", 381 "domains": [ 382 "string" 383 ], 384 "begins_on": 0, 385 "expires_on": 0, 386 "imported": true, 387 "status": "expired", 388 "algorithm": "string", 389 "key_algorithm": "string", 390 "description": "string", 391 "has_previous": true, 392 "issuance_info": { 393 "ordered_on": 0, 394 "status": "pending", 395 "code": "string", 396 "additional_info": "string", 397 "auto": true 398 }, 399 "order_policy": { 400 "auto_renew_enabled": false 401 } 402 }`), 403 ), 404 ) 405 }) 406 It("Should return a response", func() { 407 resp, err := newCert(server.URL()).OrderCertificate("asdfghjmnbvcdfgh", orderdata) 408 Expect(resp).ShouldNot(BeNil()) 409 Expect(err).NotTo(HaveOccurred()) 410 }) 411 }) 412 413 Context("When unsuccessful", func() { 414 BeforeEach(func() { 415 server = ghttp.NewServer() 416 server.SetAllowUnhandledRequests(true) 417 server.AppendHandlers( 418 ghttp.CombineHandlers( 419 ghttp.VerifyRequest(http.MethodPost, "/api/v1/asdfghjmnbvcdfgh/certificates/order"), 420 ghttp.RespondWith(http.StatusInternalServerError, `Failed to Order`), 421 ), 422 ) 423 }) 424 It("should return error ", func() { 425 _, err := newCert(server.URL()).OrderCertificate("asdfghjmnbvcdfgh", orderdata) 426 Expect(err).To(HaveOccurred()) 427 428 }) 429 430 }) 431 }) 432 433 }) 434 435 func newCert(url string) Certificate { 436 437 sess, err := session.New() 438 if err != nil { 439 log.Fatal(err) 440 } 441 conf := sess.Config.Copy() 442 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 443 conf.Endpoint = &url 444 445 client := client.Client{ 446 Config: conf, 447 ServiceName: bluemix.CertificateManager, 448 } 449 450 return newCertificateAPI(&client) 451 }