github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/iam/iamv1/service_ids_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/session"
    11  	"github.com/onsi/gomega/ghttp"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("ServiceIdRepository", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	Describe("List()", func() {
    24  		Context("When there is no service ID", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/serviceids", "boundTo=abc"),
    30  						ghttp.RespondWith(http.StatusOK, `{
    31  							"currentPage": 0,
    32  							"pageSize": 0,
    33  							"items":[]}`),
    34  					),
    35  				)
    36  			})
    37  			It("should return zero service ID", func() {
    38  				ids, err := newTestServiceIDRepo(server.URL()).List("abc")
    39  
    40  				Expect(err).ShouldNot(HaveOccurred())
    41  				Expect(ids).Should(BeEmpty())
    42  			})
    43  		})
    44  
    45  		Context("When there is only one service ID", func() {
    46  			BeforeEach(func() {
    47  				server = ghttp.NewServer()
    48  				server.AppendHandlers(
    49  					ghttp.CombineHandlers(
    50  						ghttp.VerifyRequest(http.MethodGet, "/serviceids", "boundTo=abc"),
    51  						ghttp.RespondWith(http.StatusOK, `{
    52  							"pageSize": 20,
    53  							"items":[{
    54  								"metadata": {
    55  									"iam_id": "iam-ServiceId-06379974-f0e3-4cda-804a-4b6bedb2505b",
    56  									"uuid": "ServiceId-06379974-f0e3-4cda-804a-4b6bedb2505b",
    57  									"crn": "crn:v1:staging:public:iam-identity::a/2bff70b5d2cc4b400814eca0bb730daa::serviceid:ServiceId-06379974-f0e3-4cda-804a-4b6bedb2505b",
    58  									"version": "4-1fd99b9b1f1bd6d013823fe77f1b81b2",
    59  									"createdAt": "2017-10-25T07:29+0000",
    60  									"modifiedAt": "2017-10-25T07:32+0000"
    61  								},
    62  								"entity": {
    63  									"boundTo": "crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::",
    64  									"name": "cli-test",
    65  									"description": "service id to cli-test service",
    66  									"uniqueInstanceCrns": []
    67  								}
    68  							}]}`),
    69  					),
    70  				)
    71  			})
    72  			It("should return one service ID", func() {
    73  				ids, err := newTestServiceIDRepo(server.URL()).List("abc")
    74  
    75  				Expect(err).ShouldNot(HaveOccurred())
    76  				Expect(ids).Should(HaveLen(1))
    77  				id := ids[0]
    78  				Expect(id.Name).Should(Equal("cli-test"))
    79  				Expect(id.Description).Should(Equal("service id to cli-test service"))
    80  				Expect(id.BoundTo).Should(Equal("crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::"))
    81  			})
    82  		})
    83  
    84  		Context("When there is pagination", func() {
    85  			BeforeEach(func() {
    86  				server = ghttp.NewServer()
    87  				server.AppendHandlers(
    88  					ghttp.CombineHandlers(
    89  						ghttp.VerifyRequest(http.MethodGet, "/serviceids", "boundTo=abc"),
    90  						ghttp.RespondWith(http.StatusOK, `{
    91  							"pageSize": 2,
    92  							"nextPageToken": "eyJkaXIiOiJGIiwiYWNjIjoiMmJmZjcwYjVkMmNjNGI0MDA4MTRlY2EwYmI3MzBkYWEiLCJwZ1MiOjUsInNvcnQiOiIiLCJvZmYiOjV9",
    93  							"items":[{
    94  								"metadata": {
    95  									"iam_id": "iam-ServiceId-06379974-f0e3-4cda-804a-4b6bedb2505b",
    96  									"uuid": "ServiceId-06379974-f0e3-4cda-804a-4b6bedb2505b",
    97  									"crn": "crn:v1:staging:public:iam-identity::a/2bff70b5d2cc4b400814eca0bb730daa::serviceid:ServiceId-06379974-f0e3-4cda-804a-4b6bedb2505b",
    98  									"version": "4-1fd99b9b1f1bd6d013823fe77f1b81b2",
    99  									"createdAt": "2017-10-25T07:29+0000",
   100  									"modifiedAt": "2017-10-25T07:32+0000"
   101  								},
   102  								"entity": {
   103  									"boundTo": "crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::",
   104  									"name": "cli-test",
   105  									"description": "service id to cli-test service",
   106  									"uniqueInstanceCrns": []
   107  								}
   108  							},
   109  							{
   110  								"metadata": {
   111  									"iam_id": "iam-ServiceId-5238472e-62fa-437b-9aa7-727f3f846e00",
   112  									"uuid": "ServiceId-5238472e-62fa-437b-9aa7-727f3f846e00",
   113  									"crn": "crn:v1:staging:public:iam-identity::a/2bff70b5d2cc4b400814eca0bb730daa::serviceid:ServiceId-5238472e-62fa-437b-9aa7-727f3f846e00",
   114  									"version": "1-448d54be8b259443ff6169c7a2f83f96",
   115  									"createdAt": "2017-10-25T07:31+0000",
   116  									"modifiedAt": "2017-10-25T07:31+0000"
   117  								},
   118  								"entity": {
   119  									"boundTo": "crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::",
   120  									"name": "cli-test-dup",
   121  									"uniqueInstanceCrns": []
   122  								}
   123  							}]}`),
   124  					),
   125  					ghttp.CombineHandlers(
   126  						ghttp.VerifyRequest(http.MethodGet, "/serviceids", "boundTo=abc&pagetoken=eyJkaXIiOiJGIiwiYWNjIjoiMmJmZjcwYjVkMmNjNGI0MDA4MTRlY2EwYmI3MzBkYWEiLCJwZ1MiOjUsInNvcnQiOiIiLCJvZmYiOjV9"),
   127  						ghttp.RespondWith(http.StatusOK, `{
   128  							"pageSize": 2,
   129  							"items":[{
   130  								"metadata": {
   131  									"iam_id": "iam-ServiceId-5238472e-62fa-437b-9aa7-727f3f846e00",
   132  									"uuid": "ServiceId-5238472e-62fa-437b-9aa7-727f3f846e00",
   133  									"crn": "crn:v1:staging:public:iam-identity::a/2bff70b5d2cc4b400814eca0bb730daa::serviceid:ServiceId-5238472e-62fa-437b-9aa7-727f3f846e00",
   134  									"version": "1-448d54be8b259443ff6169c7a2f83f96",
   135  									"createdAt": "2017-10-25T07:31+0000",
   136  									"modifiedAt": "2017-10-25T07:31+0000"
   137  								},
   138  								"entity": {
   139  									"boundTo": "crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::",
   140  									"name": "cli-test-dup",
   141  									"uniqueInstanceCrns": []
   142  								}
   143  							}]}`),
   144  					),
   145  				)
   146  			})
   147  			It("should return three service IDs", func() {
   148  				ids, err := newTestServiceIDRepo(server.URL()).List("abc")
   149  
   150  				Expect(err).ShouldNot(HaveOccurred())
   151  				Expect(ids).Should(HaveLen(3))
   152  				id := ids[0]
   153  				Expect(id.Name).Should(Equal("cli-test"))
   154  				Expect(id.Description).Should(Equal("service id to cli-test service"))
   155  				Expect(id.BoundTo).Should(Equal("crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::"))
   156  
   157  				id = ids[1]
   158  				Expect(id.Name).Should(Equal("cli-test-dup"))
   159  				Expect(id.Description).Should(Equal(""))
   160  				Expect(id.BoundTo).Should(Equal("crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::"))
   161  
   162  				id = ids[2]
   163  				Expect(id.Name).Should(Equal("cli-test-dup"))
   164  				Expect(id.Description).Should(Equal(""))
   165  				Expect(id.BoundTo).Should(Equal("crn:v1:staging:public:::a/2bff70b5d2cc4b400814eca0bb730daa:::"))
   166  			})
   167  		})
   168  
   169  		Context("When there is error", func() {
   170  			BeforeEach(func() {
   171  				server = ghttp.NewServer()
   172  				server.AppendHandlers(
   173  					ghttp.CombineHandlers(
   174  						ghttp.VerifyRequest(http.MethodGet, "/serviceids", "boundTo=abc"),
   175  						ghttp.RespondWith(http.StatusUnauthorized, `{
   176  							"errorCode": "BXNIM0308E",
   177  							"errorMessage": "No authorization header found",
   178  							"context": {
   179  							  "requestId": "2252754335",
   180  							  "requestType": "incoming.ServiceId_List",
   181  							  "startTime": "30.10.2017 06:20:29:504 UTC",
   182  							  "endTime": "30.10.2017 06:20:29:505 UTC",
   183  							  "elapsedTime": "1",
   184  							  "instanceId": "tokenservice/1",
   185  							  "host": "localhost",
   186  							  "threadId": "8d852",
   187  							  "clientIp": "114.255.160.171",
   188  							  "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36",
   189  							  "locale": "en_US"
   190  							}
   191  						}`),
   192  					),
   193  				)
   194  			})
   195  			It("should return three service IDs", func() {
   196  				ids, err := newTestServiceIDRepo(server.URL()).List("abc")
   197  
   198  				Expect(err).Should(HaveOccurred())
   199  				Expect(ids).Should(HaveLen(0))
   200  			})
   201  		})
   202  	})
   203  })
   204  
   205  func newTestServiceIDRepo(url string) ServiceIDRepository {
   206  	sess, err := session.New()
   207  	if err != nil {
   208  		log.Fatal(err)
   209  	}
   210  	conf := sess.Config.Copy()
   211  	conf.Endpoint = &url
   212  	client := client.Client{
   213  		Config:      conf,
   214  		ServiceName: bluemix.IAMService,
   215  	}
   216  	return NewServiceIDRepository(&client)
   217  }