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

     1  package iamuumv1
     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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/groups"),
   132  						ghttp.RespondWith(http.StatusOK, `
   133  						{
   134  							"limit": 1,
   135  							"offset": 0,
   136  							"total_count": 2,
   137  							"next": {
   138  								"href": "`+server.URL()+`/v1/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, "/v1/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, "/v1/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.AccessGroup{
   199  					Name:        "abc",
   200  					Description: "abc group",
   201  				}, "89999998-8880")
   202  				Expect(err).ShouldNot(HaveOccurred())
   203  
   204  				Expect(response.Name).Should(Equal("abc"))
   205  				Expect(response.Description).Should(Equal("abc group"))
   206  			})
   207  
   208  		})
   209  	})
   210  
   211  	Describe("Remove()", func() {
   212  		Context("When group is deleted", func() {
   213  			BeforeEach(func() {
   214  				server = ghttp.NewServer()
   215  				server.AppendHandlers(
   216  					ghttp.CombineHandlers(
   217  						ghttp.VerifyRequest(http.MethodDelete, "/v1/groups/abc"),
   218  						ghttp.RespondWith(http.StatusNoContent, ""),
   219  					),
   220  				)
   221  			})
   222  
   223  			It("should return success", func() {
   224  				err := newTestAccessGroupRepo(server.URL()).Delete("abc", false)
   225  
   226  				Expect(err).Should(Succeed())
   227  			})
   228  		})
   229  
   230  		Context("When group is not found", func() {
   231  			BeforeEach(func() {
   232  				server = ghttp.NewServer()
   233  				server.AppendHandlers(
   234  					ghttp.CombineHandlers(
   235  						ghttp.VerifyRequest(http.MethodDelete, "/v1/groups/abc"),
   236  						ghttp.RespondWith(http.StatusNotFound, `{
   237  							"StatusCode": 404,
   238  							"code": "not_found",
   239  							"message": "Group abc is not found"
   240  						}`),
   241  					),
   242  				)
   243  			})
   244  
   245  			It("should return not found error", func() {
   246  				err := newTestAccessGroupRepo(server.URL()).Delete("abc", false)
   247  
   248  				Expect(err).Should(HaveOccurred())
   249  				Expect(err.Error()).Should(ContainSubstring("not_found"))
   250  			})
   251  		})
   252  	})
   253  
   254  })
   255  
   256  func newTestAccessGroupRepo(url string) AccessGroupRepository {
   257  	sess, err := session.New()
   258  	if err != nil {
   259  		log.Fatal(err)
   260  	}
   261  	conf := sess.Config.Copy()
   262  	conf.Endpoint = &url
   263  	client := client.Client{
   264  		Config:      conf,
   265  		ServiceName: bluemix.IAMUUMService,
   266  	}
   267  	return NewAccessGroupRepository(&client)
   268  }