github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/iamuum/iamuumv2/access_group_test.go (about)

     1  package iamuumv2
     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  	"github.com/IBM-Cloud/bluemix-go/models"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("AccessGroupRepository", func() {
    17  	var (
    18  		server *ghttp.Server
    19  	)
    20  
    21  	Describe("List()", func() {
    22  		Context("When API error 403 returns", func() {
    23  			BeforeEach(func() {
    24  				server = ghttp.NewServer()
    25  				server.AppendHandlers(
    26  					ghttp.CombineHandlers(
    27  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups"),
    28  						ghttp.RespondWith(http.StatusForbidden, `
    29  						{
    30  							"message": "The provided access token does not have the proper authority to access this operation."
    31  						}`),
    32  					),
    33  				)
    34  			})
    35  
    36  			It("should return API 403 error", func() {
    37  				_, err := newTestAccessGroupRepo(server.URL()).List("def")
    38  				Expect(err).Should(HaveOccurred())
    39  				Expect(err.Error()).Should(ContainSubstring("Request failed with status code: 403"))
    40  			})
    41  		})
    42  
    43  		Context("When other JSON error returns", func() {
    44  			BeforeEach(func() {
    45  				server = ghttp.NewServer()
    46  				server.AppendHandlers(
    47  					ghttp.CombineHandlers(
    48  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups"),
    49  						ghttp.RespondWith(http.StatusBadGateway, `{
    50  							"message": "other json error"
    51  						}`),
    52  					),
    53  				)
    54  			})
    55  
    56  			It("should return server error", func() {
    57  				_, err := newTestAccessGroupRepo(server.URL()).List("abc")
    58  				Expect(err).Should(HaveOccurred())
    59  				Expect(err.Error()).Should(ContainSubstring("other json error"))
    60  			})
    61  		})
    62  
    63  		Context("When no group returned", func() {
    64  			BeforeEach(func() {
    65  				server = ghttp.NewServer()
    66  				server.AppendHandlers(
    67  					ghttp.CombineHandlers(
    68  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups"),
    69  						ghttp.RespondWith(http.StatusOK, `
    70  						{
    71  							"limit": 50,
    72  							"offset": 0,
    73  							"total_count": 0
    74  						}`),
    75  					),
    76  				)
    77  			})
    78  
    79  			It("should return empty", func() {
    80  				groups, err := newTestAccessGroupRepo(server.URL()).List("abc")
    81  
    82  				Expect(err).ShouldNot(HaveOccurred())
    83  				Expect(groups).Should(BeEmpty())
    84  			})
    85  		})
    86  
    87  		Context("When there is one page", func() {
    88  			BeforeEach(func() {
    89  				server = ghttp.NewServer()
    90  				server.AppendHandlers(
    91  					ghttp.CombineHandlers(
    92  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups"),
    93  						ghttp.RespondWith(http.StatusOK, `
    94  						{
    95  							"limit": 50,
    96  							"offset": 0,
    97  							"total_count": 2,
    98  							"groups": [{
    99  								"description": "Editor group",
   100  								"id": "008facc4-412f-463e-bd1b-99dd7dcfa27b",
   101  								"name": "Editor"
   102  							},{
   103  								"description": "Viewer group",
   104  								"id": "048af74a-8435-4783-8ad9-8e207fa24afd",
   105  								"name": "Viewer"
   106  							}]
   107  						}`),
   108  					),
   109  				)
   110  			})
   111  
   112  			It("should return one page", func() {
   113  				groups, err := newTestAccessGroupRepo(server.URL()).List("abc")
   114  
   115  				Expect(err).ShouldNot(HaveOccurred())
   116  				Expect(groups).Should(HaveLen(2))
   117  
   118  				Expect(groups[0].ID).Should(Equal("008facc4-412f-463e-bd1b-99dd7dcfa27b"))
   119  				Expect(groups[0].Name).Should(Equal("Editor"))
   120  
   121  				Expect(groups[1].ID).Should(Equal("048af74a-8435-4783-8ad9-8e207fa24afd"))
   122  				Expect(groups[1].Name).Should(Equal("Viewer"))
   123  			})
   124  		})
   125  
   126  		Context("When there are multiple pages", func() {
   127  			BeforeEach(func() {
   128  				server = ghttp.NewServer()
   129  				server.AppendHandlers(
   130  					ghttp.CombineHandlers(
   131  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups"),
   132  						ghttp.RespondWith(http.StatusOK, `
   133  						{
   134  							"limit": 1,
   135  							"offset": 0,
   136  							"total_count": 2,
   137  							"next": {
   138  								"href": "`+server.URL()+`/v2/groups?page=1"
   139  							},
   140  							"groups": [{
   141  								"description": "Editor group",
   142  								"id": "008facc4-412f-463e-bd1b-99dd7dcfa27b",
   143  								"name": "Editor"
   144  							}]
   145  						}`),
   146  					),
   147  					ghttp.CombineHandlers(
   148  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups", "page=1"),
   149  						ghttp.RespondWith(http.StatusOK, `
   150  						{
   151  							"limit": 1,
   152  							"offset": 1,
   153  							"total_count": 2,
   154  							"groups": [{
   155  								"description": "Viewer group",
   156  								"id": "048af74a-8435-4783-8ad9-8e207fa24afd",
   157  								"name": "Viewer"
   158  							}]
   159  						}`),
   160  					),
   161  				)
   162  			})
   163  
   164  			It("should return all pages", func() {
   165  				groups, err := newTestAccessGroupRepo(server.URL()).List("abc")
   166  
   167  				Expect(err).ShouldNot(HaveOccurred())
   168  				Expect(groups).Should(HaveLen(2))
   169  
   170  				Expect(groups[0].ID).Should(Equal("008facc4-412f-463e-bd1b-99dd7dcfa27b"))
   171  				Expect(groups[0].Name).Should(Equal("Editor"))
   172  
   173  				Expect(groups[1].ID).Should(Equal("048af74a-8435-4783-8ad9-8e207fa24afd"))
   174  				Expect(groups[1].Name).Should(Equal("Viewer"))
   175  			})
   176  		})
   177  	})
   178  
   179  	Describe("Create()", func() {
   180  		Context("When create one group", func() {
   181  			BeforeEach(func() {
   182  				server = ghttp.NewServer()
   183  				server.AppendHandlers(
   184  					ghttp.CombineHandlers(
   185  						ghttp.VerifyRequest(http.MethodPost, "/v2/groups"),
   186  						ghttp.RespondWith(http.StatusOK, `
   187  						{
   188  							"description": "abc group",
   189  							"id": "008facc4-412f-463e-bd1b-99dd7dcfa27b",
   190  							"name": "abc"
   191  							
   192  						}`),
   193  					),
   194  				)
   195  			})
   196  
   197  			It("should return success", func() {
   198  				response, err := newTestAccessGroupRepo(server.URL()).Create(models.AccessGroupV2{
   199  					AccessGroup: models.AccessGroup{
   200  						Name:        "abc",
   201  						Description: "abc group",
   202  					},
   203  				}, "89999998-8880")
   204  				Expect(err).ShouldNot(HaveOccurred())
   205  
   206  				Expect(response.Name).Should(Equal("abc"))
   207  				Expect(response.Description).Should(Equal("abc group"))
   208  			})
   209  
   210  		})
   211  	})
   212  
   213  	Describe("Remove()", func() {
   214  		Context("When group is deleted", func() {
   215  			BeforeEach(func() {
   216  				server = ghttp.NewServer()
   217  				server.AppendHandlers(
   218  					ghttp.CombineHandlers(
   219  						ghttp.VerifyRequest(http.MethodDelete, "/v2/groups/abc"),
   220  						ghttp.RespondWith(http.StatusNoContent, ""),
   221  					),
   222  				)
   223  			})
   224  
   225  			It("should return success", func() {
   226  				err := newTestAccessGroupRepo(server.URL()).Delete("abc", false)
   227  
   228  				Expect(err).Should(Succeed())
   229  			})
   230  		})
   231  
   232  		Context("When group is not found", func() {
   233  			BeforeEach(func() {
   234  				server = ghttp.NewServer()
   235  				server.AppendHandlers(
   236  					ghttp.CombineHandlers(
   237  						ghttp.VerifyRequest(http.MethodDelete, "/v2/groups/abc"),
   238  						ghttp.RespondWith(http.StatusNotFound, `{
   239  							"StatusCode": 404,
   240  							"code": "not_found",
   241  							"message": "Group abc is not found"
   242  						}`),
   243  					),
   244  				)
   245  			})
   246  
   247  			It("should return not found error", func() {
   248  				err := newTestAccessGroupRepo(server.URL()).Delete("abc", false)
   249  
   250  				Expect(err).Should(HaveOccurred())
   251  				Expect(err.Error()).Should(ContainSubstring("not_found"))
   252  			})
   253  		})
   254  	})
   255  
   256  })
   257  
   258  func newTestAccessGroupRepo(url string) AccessGroupRepository {
   259  	sess, err := session.New()
   260  	if err != nil {
   261  		log.Fatal(err)
   262  	}
   263  	conf := sess.Config.Copy()
   264  	conf.Endpoint = &url
   265  	client := client.Client{
   266  		Config:      conf,
   267  		ServiceName: bluemix.IAMUUMServicev2,
   268  	}
   269  	return NewAccessGroupRepository(&client)
   270  }