github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/iam/iamv1/api_keys_test.go (about)

     1  package iamv1
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	"github.com/IBM-Cloud/bluemix-go"
     8  
     9  	"github.com/IBM-Cloud/bluemix-go/client"
    10  	"github.com/IBM-Cloud/bluemix-go/models"
    11  	"github.com/IBM-Cloud/bluemix-go/session"
    12  	"github.com/onsi/gomega/ghttp"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("ApiKeys Repository", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	Describe("List() method", func() {
    25  		Context("When there is one api key", func() {
    26  			BeforeEach(func() {
    27  				server = ghttp.NewServer()
    28  				server.AppendHandlers(
    29  					ghttp.CombineHandlers(
    30  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc"),
    31  						ghttp.RespondWith(http.StatusOK, `
    32  						{
    33  							"currentPage": 1,
    34  							"pageSize": 1,
    35  							"items": [{
    36  								"metadata": {
    37  								"uuid": "ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
    38  								"crn": "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
    39  								"version": "1-892688a64fb68a35ed2aea31b62cbfef",
    40  								"createdAt": "2017-02-19T12:55+0000",
    41  								"modifiedAt": "2017-02-19T12:55+0000"
    42  								},
    43  								"entity": {
    44  								"boundTo": "crn:v:staging:public:iam:::IBMid:user:270004WA4U",
    45  								"name": "test1",
    46  								"description": "my first test api key",
    47  								"format": "APIKEY"
    48  								}
    49  							}]
    50  						}`),
    51  					),
    52  				)
    53  			})
    54  
    55  			It("should return all api keys", func() {
    56  				keys, err := newTestAPIKeyRepo(server.URL()).List("abc")
    57  				Expect(err).ShouldNot(HaveOccurred())
    58  				Expect(keys).Should(HaveLen(1))
    59  
    60  				key := keys[0]
    61  				Expect(key.UUID).Should(Equal("ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
    62  				Expect(key.Crn).Should(Equal("crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
    63  				Expect(key.Version).Should(Equal("1-892688a64fb68a35ed2aea31b62cbfef"))
    64  				Expect(key.CreatedAt).Should(Equal("2017-02-19T12:55+0000"))
    65  				Expect(key.ModifiedAt).Should(Equal("2017-02-19T12:55+0000"))
    66  				Expect(key.Name).Should(Equal("test1"))
    67  				Expect(key.BoundTo).Should(Equal("crn:v:staging:public:iam:::IBMid:user:270004WA4U"))
    68  				Expect(key.Description).Should(Equal("my first test api key"))
    69  				Expect(key.Format).Should(Equal("APIKEY"))
    70  			})
    71  		})
    72  
    73  		Context("When there are api keys across multiple pages", func() {
    74  			BeforeEach(func() {
    75  				server = ghttp.NewServer()
    76  				server.AppendHandlers(
    77  					ghttp.CombineHandlers(
    78  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc"),
    79  						ghttp.RespondWith(http.StatusOK, `
    80  						{
    81  							"currentPage": 1,
    82  							"pageSize": 1,
    83  							"nextPageToken": "abc",
    84  							"items": [{
    85  								"metadata": {
    86  								"uuid": "ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
    87  								"crn": "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
    88  								"version": "1-892688a64fb68a35ed2aea31b62cbfef",
    89  								"createdAt": "2017-02-19T12:55+0000",
    90  								"modifiedAt": "2017-02-19T12:55+0000"
    91  								},
    92  								"entity": {
    93  								"boundTo": "crn:v:staging:public:iam:::IBMid:user:270004WA4U",
    94  								"name": "test1",
    95  								"description": "my first test api key",
    96  								"format": "APIKEY"
    97  								}
    98  							}]
    99  						}`),
   100  					),
   101  					ghttp.CombineHandlers(
   102  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc&pagetoken=abc"),
   103  						ghttp.RespondWith(http.StatusOK, `
   104  						{
   105  							"currentPage": 2,
   106  							"pageSize": 1,
   107  							"prevPageToken": "def",
   108  							"items": [{
   109  								"metadata": {
   110  								"uuid": "ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   111  								"crn": "crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   112  								"version": "2-892688a64fb68a35ed2aea31b62cbfef",
   113  								"createdAt": "2017-02-20T12:55+0000",
   114  								"modifiedAt": "2017-02-21T12:55+0000"
   115  								},
   116  								"entity": {
   117  								"boundTo": "crn:v2:staging:public:iam:::IBMid:user:270004WA4U",
   118  								"name": "test2",
   119  								"description": "my second test api key",
   120  								"format": "APIKEY"
   121  								}
   122  							}]
   123  						}`),
   124  					),
   125  				)
   126  			})
   127  
   128  			It("should return all api keys", func() {
   129  				keys, err := newTestAPIKeyRepo(server.URL()).List("abc")
   130  				Expect(err).ShouldNot(HaveOccurred())
   131  				Expect(keys).Should(HaveLen(2))
   132  
   133  				key1 := keys[0]
   134  				Expect(key1.UUID).Should(Equal("ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
   135  				Expect(key1.Crn).Should(Equal("crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
   136  				Expect(key1.Version).Should(Equal("1-892688a64fb68a35ed2aea31b62cbfef"))
   137  				Expect(key1.CreatedAt).Should(Equal("2017-02-19T12:55+0000"))
   138  				Expect(key1.ModifiedAt).Should(Equal("2017-02-19T12:55+0000"))
   139  				Expect(key1.Name).Should(Equal("test1"))
   140  				Expect(key1.BoundTo).Should(Equal("crn:v:staging:public:iam:::IBMid:user:270004WA4U"))
   141  				Expect(key1.Description).Should(Equal("my first test api key"))
   142  				Expect(key1.Format).Should(Equal("APIKEY"))
   143  
   144  				key2 := keys[1]
   145  				Expect(key2.UUID).Should(Equal("ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
   146  				Expect(key2.Crn).Should(Equal("crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
   147  				Expect(key2.Version).Should(Equal("2-892688a64fb68a35ed2aea31b62cbfef"))
   148  				Expect(key2.CreatedAt).Should(Equal("2017-02-20T12:55+0000"))
   149  				Expect(key2.ModifiedAt).Should(Equal("2017-02-21T12:55+0000"))
   150  				Expect(key2.Name).Should(Equal("test2"))
   151  				Expect(key2.BoundTo).Should(Equal("crn:v2:staging:public:iam:::IBMid:user:270004WA4U"))
   152  				Expect(key2.Description).Should(Equal("my second test api key"))
   153  				Expect(key2.Format).Should(Equal("APIKEY"))
   154  			})
   155  		})
   156  
   157  		Context("When there is no api key", func() {
   158  			BeforeEach(func() {
   159  				server = ghttp.NewServer()
   160  				server.AppendHandlers(
   161  					ghttp.CombineHandlers(
   162  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc"),
   163  						ghttp.RespondWith(http.StatusNotFound, `
   164  						{
   165  							"context": {
   166  								"requestId": "1",
   167  								"requestType": "2",
   168  								"userAgent": "bluemix-cli",
   169  								"clientIp": "3",
   170  								"instanceId": "4",
   171  								"threadId": "5",
   172  								"host": "localhost",
   173  								"startTime": "12345",
   174  								"endTime": "67890",
   175  								"elapsedTime": "123",
   176  								"locale": "en-us"
   177  							},
   178  							"errorCode": "404",
   179  							"errorMessage": "cannot find api keys",
   180  							"errorDetails": "API keys bound to abc cannot be found."
   181  						}`),
   182  					),
   183  				)
   184  			})
   185  
   186  			It("should return error", func() {
   187  				keys, err := newTestAPIKeyRepo(server.URL()).List("abc")
   188  				Expect(err).ShouldNot(HaveOccurred())
   189  				Expect(keys).Should(BeEmpty())
   190  			})
   191  		})
   192  
   193  		Context("When there is error", func() {
   194  			BeforeEach(func() {
   195  				server = ghttp.NewServer()
   196  				server.AppendHandlers(
   197  					ghttp.CombineHandlers(
   198  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc"),
   199  						ghttp.RespondWith(http.StatusBadRequest, `
   200  						{
   201  							"context": {
   202  								"requestId": "1",
   203  								"requestType": "2",
   204  								"userAgent": "bluemix-cli",
   205  								"clientIp": "3",
   206  								"instanceId": "4",
   207  								"threadId": "5",
   208  								"host": "localhost",
   209  								"startTime": "12345",
   210  								"endTime": "67890",
   211  								"elapsedTime": "123",
   212  								"locale": "en-us"
   213  							},
   214  							"errorCode": "400",
   215  							"errorMessage": "input error",
   216  							"errorDetails": "boundTo is missing"
   217  						}`),
   218  					),
   219  				)
   220  			})
   221  
   222  			It("should return error", func() {
   223  				keys, err := newTestAPIKeyRepo(server.URL()).List("abc")
   224  				Expect(err).Should(HaveOccurred())
   225  				Expect(keys).Should(BeEmpty())
   226  			})
   227  		})
   228  	})
   229  
   230  	Describe("FindByName", func() {
   231  		Context("When there is match", func() {
   232  			BeforeEach(func() {
   233  				server = ghttp.NewServer()
   234  				server.AppendHandlers(
   235  					ghttp.CombineHandlers(
   236  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc"),
   237  						ghttp.RespondWith(http.StatusOK, `
   238  						{
   239  							"currentPage": 1,
   240  							"pageSize": 1,
   241  							"nextPageToken": "abc",
   242  							"items": [{
   243  								"metadata": {
   244  								"uuid": "ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   245  								"crn": "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   246  								"version": "1-892688a64fb68a35ed2aea31b62cbfef",
   247  								"createdAt": "2017-02-19T12:55+0000",
   248  								"modifiedAt": "2017-02-19T12:55+0000"
   249  								},
   250  								"entity": {
   251  								"boundTo": "crn:v:staging:public:iam:::IBMid:user:270004WA4U",
   252  								"name": "test0",
   253  								"description": "my first test api key",
   254  								"format": "APIKEY"
   255  								}
   256  							},{
   257  								"metadata": {
   258  								"uuid": "ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   259  								"crn": "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   260  								"version": "1-892688a64fb68a35ed2aea31b62cbfef",
   261  								"createdAt": "2017-02-19T12:55+0000",
   262  								"modifiedAt": "2017-02-19T12:55+0000"
   263  								},
   264  								"entity": {
   265  								"boundTo": "crn:v:staging:public:iam:::IBMid:user:270004WA4U",
   266  								"name": "test1",
   267  								"description": "my first test api key",
   268  								"format": "APIKEY"
   269  								}
   270  							}]
   271  						}`),
   272  					),
   273  					ghttp.CombineHandlers(
   274  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc&pagetoken=abc"),
   275  						ghttp.RespondWith(http.StatusOK, `
   276  						{
   277  							"currentPage": 2,
   278  							"pageSize": 1,
   279  							"prevPageToken": "def",
   280  							"items": [{
   281  								"metadata": {
   282  								"uuid": "ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   283  								"crn": "crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   284  								"version": "2-892688a64fb68a35ed2aea31b62cbfef",
   285  								"createdAt": "2017-02-20T12:55+0000",
   286  								"modifiedAt": "2017-02-21T12:55+0000"
   287  								},
   288  								"entity": {
   289  								"boundTo": "crn:v2:staging:public:iam:::IBMid:user:270004WA4U",
   290  								"name": "test2",
   291  								"description": "my second test api key",
   292  								"format": "APIKEY"
   293  								}
   294  							},{
   295  								"metadata": {
   296  								"uuid": "ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   297  								"crn": "crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   298  								"version": "2-892688a64fb68a35ed2aea31b62cbfef",
   299  								"createdAt": "2017-02-20T12:55+0000",
   300  								"modifiedAt": "2017-02-21T12:55+0000"
   301  								},
   302  								"entity": {
   303  								"boundTo": "crn:v2:staging:public:iam:::IBMid:user:270004WA4U",
   304  								"name": "test1",
   305  								"description": "my second test api key",
   306  								"format": "APIKEY"
   307  								}
   308  							}]
   309  						},`),
   310  					),
   311  				)
   312  			})
   313  
   314  			It("should return all match", func() {
   315  				keys, err := newTestAPIKeyRepo(server.URL()).FindByName("test1", "abc")
   316  				Expect(err).ShouldNot(HaveOccurred())
   317  				Expect(keys).Should(HaveLen(2))
   318  
   319  				key1 := keys[0]
   320  				Expect(key1.UUID).Should(Equal("ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
   321  				key2 := keys[1]
   322  				Expect(key2.UUID).Should(Equal("ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
   323  			})
   324  		})
   325  
   326  		Context("When there is no match", func() {
   327  			BeforeEach(func() {
   328  				server = ghttp.NewServer()
   329  				server.AppendHandlers(
   330  					ghttp.CombineHandlers(
   331  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc"),
   332  						ghttp.RespondWith(http.StatusOK, `
   333  						{
   334  							"currentPage": 1,
   335  							"pageSize": 1,
   336  							"pagetoken": "abc",
   337  							"items": [{
   338  								"metadata": {
   339  								"uuid": "ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   340  								"crn": "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   341  								"version": "1-892688a64fb68a35ed2aea31b62cbfef",
   342  								"createdAt": "2017-02-19T12:55+0000",
   343  								"modifiedAt": "2017-02-19T12:55+0000"
   344  								},
   345  								"entity": {
   346  								"boundTo": "crn:v:staging:public:iam:::IBMid:user:270004WA4U",
   347  								"name": "test0",
   348  								"description": "my first test api key",
   349  								"format": "APIKEY"
   350  								}
   351  							},{
   352  								"metadata": {
   353  								"uuid": "ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   354  								"crn": "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   355  								"version": "1-892688a64fb68a35ed2aea31b62cbfef",
   356  								"createdAt": "2017-02-19T12:55+0000",
   357  								"modifiedAt": "2017-02-19T12:55+0000"
   358  								},
   359  								"entity": {
   360  								"boundTo": "crn:v:staging:public:iam:::IBMid:user:270004WA4U",
   361  								"name": "test1",
   362  								"description": "my first test api key",
   363  								"format": "APIKEY"
   364  								}
   365  							}]
   366  						}`),
   367  					),
   368  					ghttp.CombineHandlers(
   369  						ghttp.VerifyRequest(http.MethodGet, "/apikeys", "boundTo=abc&pagetoken=abc"),
   370  						ghttp.RespondWith(http.StatusOK, `
   371  						{
   372  							"currentPage": 2,
   373  							"pageSize": 1,
   374  							"prevPageToken": "def",
   375  							"items": [{
   376  								"metadata": {
   377  								"uuid": "ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   378  								"crn": "crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   379  								"version": "2-892688a64fb68a35ed2aea31b62cbfef",
   380  								"createdAt": "2017-02-20T12:55+0000",
   381  								"modifiedAt": "2017-02-21T12:55+0000"
   382  								},
   383  								"entity": {
   384  								"boundTo": "crn:v2:staging:public:iam:::IBMid:user:270004WA4U",
   385  								"name": "test2",
   386  								"description": "my second test api key",
   387  								"format": "APIKEY"
   388  								}
   389  							},{
   390  								"metadata": {
   391  								"uuid": "ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   392  								"crn": "crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   393  								"version": "2-892688a64fb68a35ed2aea31b62cbfef",
   394  								"createdAt": "2017-02-20T12:55+0000",
   395  								"modifiedAt": "2017-02-21T12:55+0000"
   396  								},
   397  								"entity": {
   398  								"boundTo": "crn:v2:staging:public:iam:::IBMid:user:270004WA4U",
   399  								"name": "test1",
   400  								"description": "my second test api key",
   401  								"format": "APIKEY"
   402  								}
   403  							}]
   404  						},`),
   405  					),
   406  				)
   407  			})
   408  
   409  			It("should return no result", func() {
   410  				keys, err := newTestAPIKeyRepo(server.URL()).FindByName("test", "abc")
   411  				Expect(err).ShouldNot(HaveOccurred())
   412  				Expect(keys).Should(BeEmpty())
   413  			})
   414  		})
   415  	})
   416  
   417  	Describe("Create", func() {
   418  		Context("When creation is successful", func() {
   419  			BeforeEach(func() {
   420  				server = ghttp.NewServer()
   421  				server.AppendHandlers(
   422  					ghttp.CombineHandlers(
   423  						ghttp.VerifyRequest(http.MethodPost, "/apikeys"),
   424  						ghttp.VerifyBody([]byte(`{"name":"test1","description":"my first test api key","boundTo":"self"}`)),
   425  						ghttp.RespondWith(http.StatusCreated, `
   426  						{
   427  							"context": {
   428  								"requestId": "1",
   429  								"requestType": "2",
   430  								"userAgent": "bluemix-cli",
   431  								"clientIp": "3",
   432  								"instanceId": "4",
   433  								"threadId": "5",
   434  								"host": "localhost",
   435  								"startTime": "12345",
   436  								"endTime": "67890",
   437  								"elapsedTime": "123",
   438  								"locale": "en-us"
   439  							},
   440  							"metadata": {
   441  							"uuid": "ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   442  							"crn": "crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   443  							"version": "2-892688a64fb68a35ed2aea31b62cbfef",
   444  							"createdAt": "2017-02-20T12:55+0000",
   445  							"modifiedAt": "2017-02-21T12:55+0000"
   446  							},
   447  							"entity": {
   448  							"boundTo": "crn:v2:staging:public:iam:::IBMid:user:270004WA4U",
   449  							"name": "test1",
   450  							"description": "my first test api key",
   451  							"format": "APIKEY"
   452  							}
   453  						}`),
   454  					),
   455  				)
   456  			})
   457  
   458  			It("should return key created", func() {
   459  				key, err := newTestAPIKeyRepo(server.URL()).Create(models.APIKey{
   460  					Name:        "test1",
   461  					Description: "my first test api key",
   462  					BoundTo:     "self",
   463  				})
   464  				Expect(err).ShouldNot(HaveOccurred())
   465  				Expect(key).ShouldNot(BeNil())
   466  				Expect(key.Name).Should(Equal("test1"))
   467  				Expect(key.Description).Should(Equal("my first test api key"))
   468  				Expect(key.BoundTo).Should(Equal("crn:v2:staging:public:iam:::IBMid:user:270004WA4U"))
   469  				Expect(key.Format).Should(Equal("APIKEY"))
   470  			})
   471  		})
   472  
   473  		Context("When creation fails", func() {
   474  			BeforeEach(func() {
   475  				server = ghttp.NewServer()
   476  				server.AppendHandlers(
   477  					ghttp.CombineHandlers(
   478  						ghttp.VerifyRequest(http.MethodPost, "/apikeys"),
   479  						ghttp.VerifyBody([]byte(`{"name":"test1","description":"my first test api key"}`)),
   480  						ghttp.RespondWith(http.StatusBadRequest, `
   481  						{
   482  							"context": {
   483  								"requestId": "1",
   484  								"requestType": "2",
   485  								"userAgent": "bluemix-cli",
   486  								"clientIp": "3",
   487  								"instanceId": "4",
   488  								"threadId": "5",
   489  								"host": "localhost",
   490  								"startTime": "12345",
   491  								"endTime": "67890",
   492  								"elapsedTime": "123",
   493  								"locale": "en-us"
   494  							},
   495  							"errorCode": "400",
   496  							"errorMessage": "input error",
   497  							"errorDetails": "boundTo is missing"
   498  							}
   499  						}`),
   500  					),
   501  				)
   502  			})
   503  
   504  			It("should return error", func() {
   505  				key, err := newTestAPIKeyRepo(server.URL()).Create(models.APIKey{
   506  					Name:        "test1",
   507  					Description: "my first test api key",
   508  				})
   509  				Expect(err).Should(HaveOccurred())
   510  				Expect(key).Should(BeNil())
   511  			})
   512  		})
   513  	})
   514  
   515  	Describe("Delete", func() {
   516  		Context("When deletion is successful", func() {
   517  			BeforeEach(func() {
   518  				server = ghttp.NewServer()
   519  				server.AppendHandlers(
   520  					ghttp.CombineHandlers(
   521  						ghttp.VerifyRequest(http.MethodDelete, "/apikeys/abc"),
   522  						ghttp.RespondWith(http.StatusOK, `
   523  						{
   524  							"context": {
   525  								"requestId": "1",
   526  								"requestType": "2",
   527  								"userAgent": "bluemix-cli",
   528  								"clientIp": "3",
   529  								"instanceId": "4",
   530  								"threadId": "5",
   531  								"host": "localhost",
   532  								"startTime": "12345",
   533  								"endTime": "67890",
   534  								"elapsedTime": "123",
   535  								"locale": "en-us"
   536  							}
   537  						}`),
   538  					),
   539  				)
   540  			})
   541  
   542  			It("should succeed", func() {
   543  				err := newTestAPIKeyRepo(server.URL()).Delete("abc")
   544  				Expect(err).ShouldNot(HaveOccurred())
   545  			})
   546  		})
   547  
   548  		Context("When deletion fails", func() {
   549  			BeforeEach(func() {
   550  				server = ghttp.NewServer()
   551  				server.AppendHandlers(
   552  					ghttp.CombineHandlers(
   553  						ghttp.VerifyRequest(http.MethodDelete, "/apikeys/abc"),
   554  						ghttp.RespondWith(http.StatusNotFound, `
   555  						{
   556  							"context": {
   557  								"requestId": "1",
   558  								"requestType": "2",
   559  								"userAgent": "bluemix-cli",
   560  								"clientIp": "3",
   561  								"instanceId": "4",
   562  								"threadId": "5",
   563  								"host": "localhost",
   564  								"startTime": "12345",
   565  								"endTime": "67890",
   566  								"elapsedTime": "123",
   567  								"locale": "en-us"
   568  							},
   569  							"errorCode": "400",
   570  							"errorMessage": "Cannot find API Key",
   571  							"errorDetails": "UUID 'abc' is not found." 
   572  						}`),
   573  					),
   574  				)
   575  			})
   576  
   577  			It("should succeed", func() {
   578  				err := newTestAPIKeyRepo(server.URL()).Delete("abc")
   579  				Expect(err).Should(HaveOccurred())
   580  			})
   581  		})
   582  	})
   583  
   584  	Describe("Update", func() {
   585  		Context("When update is succeesful", func() {
   586  			BeforeEach(func() {
   587  				server = ghttp.NewServer()
   588  				server.AppendHandlers(
   589  					ghttp.CombineHandlers(
   590  						ghttp.VerifyRequest(http.MethodPut, "/apikeys/abc"),
   591  						ghttp.VerifyHeaderKV("If-Match", "1-892688a64fb68a35ed2aea31b62cbfef"),
   592  						ghttp.VerifyBody([]byte(`{"name":"test2","description":"my second test api key"}`)),
   593  						ghttp.RespondWith(http.StatusOK, `
   594  						{
   595  							"context": {
   596  								"requestId": "1",
   597  								"requestType": "2",
   598  								"userAgent": "bluemix-cli",
   599  								"clientIp": "3",
   600  								"instanceId": "4",
   601  								"threadId": "5",
   602  								"host": "localhost",
   603  								"startTime": "12345",
   604  								"endTime": "67890",
   605  								"elapsedTime": "123",
   606  								"locale": "en-us"
   607  							},
   608  							"metadata": {
   609  							"uuid": "ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   610  							"crn": "crn:v2:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a",
   611  							"version": "2-892688a64fb68a35ed2aea31b62cbfef",
   612  							"createdAt": "2017-02-20T12:55+0000",
   613  							"modifiedAt": "2017-02-21T12:55+0000"
   614  							},
   615  							"entity": {
   616  							"boundTo": "crn:v2:staging:public:iam:::IBMid:user:270004WA4U",
   617  							"name": "test2",
   618  							"description": "my second test api key",
   619  							"format": "APIKEY"
   620  							}
   621  							}
   622  						}`),
   623  					),
   624  				)
   625  			})
   626  
   627  			It("should return success", func() {
   628  				key, err := newTestAPIKeyRepo(server.URL()).Update("abc", "1-892688a64fb68a35ed2aea31b62cbfef", models.APIKey{
   629  					Name:        "test2",
   630  					Description: "my second test api key",
   631  				})
   632  				Expect(err).ShouldNot(HaveOccurred())
   633  				Expect(key).ShouldNot(BeNil())
   634  				Expect(key.Name).Should(Equal("test2"))
   635  				Expect(key.Description).Should(Equal("my second test api key"))
   636  				Expect(key.UUID).Should(Equal("ApiKey-92fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"))
   637  			})
   638  		})
   639  
   640  		Context("When update fails", func() {
   641  			BeforeEach(func() {
   642  				server = ghttp.NewServer()
   643  				server.AppendHandlers(
   644  					ghttp.CombineHandlers(
   645  						ghttp.VerifyRequest(http.MethodPut, "/apikeys/abc"),
   646  						ghttp.VerifyHeaderKV("If-Match", "1-892688a64fb68a35ed2aea31b62cbfef"),
   647  						ghttp.VerifyBody([]byte(`{"name":"test2","description":"my second test api key"}`)),
   648  						ghttp.RespondWith(http.StatusNotFound, `
   649  						{
   650  							"context": {
   651  								"requestId": "1",
   652  								"requestType": "2",
   653  								"userAgent": "bluemix-cli",
   654  								"clientIp": "3",
   655  								"instanceId": "4",
   656  								"threadId": "5",
   657  								"host": "localhost",
   658  								"startTime": "12345",
   659  								"endTime": "67890",
   660  								"elapsedTime": "123",
   661  								"locale": "en-us"
   662  							},
   663  							"errorCode": "404",
   664  							"errorMessage": "Cannot find API Key",
   665  							"errorDetails": "UUID 'abc' is not found." 
   666  							}
   667  						}`),
   668  					),
   669  				)
   670  			})
   671  
   672  			It("should return error", func() {
   673  				key, err := newTestAPIKeyRepo(server.URL()).Update("abc", "1-892688a64fb68a35ed2aea31b62cbfef", models.APIKey{
   674  					Name:        "test2",
   675  					Description: "my second test api key",
   676  				})
   677  				Expect(err).Should(HaveOccurred())
   678  				Expect(key).Should(BeNil())
   679  			})
   680  		})
   681  	})
   682  })
   683  
   684  func newTestAPIKeyRepo(url string) APIKeyRepository {
   685  	sess, err := session.New()
   686  	if err != nil {
   687  		log.Fatal(err)
   688  	}
   689  	conf := sess.Config.Copy()
   690  	conf.Endpoint = &url
   691  	client := client.Client{
   692  		Config:      conf,
   693  		ServiceName: bluemix.IAMService,
   694  	}
   695  	return NewAPIKeyRepository(&client)
   696  }