github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/iamuum/iamuumv2/dynamic_rules_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/session"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("DynamicRuleRepository", func() {
    16  	var (
    17  		server *ghttp.Server
    18  	)
    19  
    20  	Describe("List()", func() {
    21  		Context("When API error 403 returns", func() {
    22  			BeforeEach(func() {
    23  				server = ghttp.NewServer()
    24  				server.AppendHandlers(
    25  					ghttp.CombineHandlers(
    26  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups/def/rules"),
    27  						ghttp.RespondWith(http.StatusForbidden, `
    28  						{
    29  							"message": "The provided access token does not have the proper authority to access this operation."
    30  						}`),
    31  					),
    32  				)
    33  			})
    34  
    35  			It("should return API 403 error", func() {
    36  				_, err := newTestDynamicRuleRepo(server.URL()).List("def")
    37  				Expect(err).Should(HaveOccurred())
    38  				Expect(err.Error()).Should(ContainSubstring("Request failed with status code: 403"))
    39  			})
    40  		})
    41  
    42  		Context("When other JSON error returns", func() {
    43  			BeforeEach(func() {
    44  				server = ghttp.NewServer()
    45  				server.AppendHandlers(
    46  					ghttp.CombineHandlers(
    47  						ghttp.VerifyRequest(http.MethodGet, "/v2/groups/def/rules"),
    48  						ghttp.RespondWith(http.StatusBadGateway, `{
    49  							"message": "other json error"
    50  						}`),
    51  					),
    52  				)
    53  			})
    54  
    55  			It("should return server error", func() {
    56  				_, err := newTestDynamicRuleRepo(server.URL()).List("def")
    57  				Expect(err).Should(HaveOccurred())
    58  				Expect(err.Error()).Should(ContainSubstring("other json error"))
    59  			})
    60  		})
    61  
    62  	})
    63  
    64  	Describe("Create()", func() {
    65  		Context("When create one rule", func() {
    66  			BeforeEach(func() {
    67  				server = ghttp.NewServer()
    68  				server.AppendHandlers(
    69  					ghttp.CombineHandlers(
    70  						ghttp.VerifyRequest(http.MethodPost, "/v2/groups/def/rules"),
    71  						ghttp.RespondWith(http.StatusOK, `
    72  						{
    73              "id": "ClaimRule-039dbce6-d5ee-4ddf-80a3-70be45f85f3b",
    74              "name": "test rule name 3",
    75              "expiration": 24,
    76              "realm_name": "test-idp.com",
    77              "access_group_id": "AccessGroupId-eac7839a-2a15-4f1b-a08e-58df17354845",
    78              "account_id": "faf6addbf6bf476896f5e342a5bdd702",
    79              "conditions": [
    80                  {
    81                      "claim": "blueGroups",
    82                      "operator": "CONTAINS",
    83                      "value": "\"test-bluegroup-saml\""
    84                  }
    85              ],
    86              "created_at": "2020-03-09T08:46:46Z",
    87              "created_by_id": "IBMid-550003NN7D",
    88              "last_modified_at": "2020-03-09T08:46:46Z",
    89              "last_modified_by_id": "IBMid-550003NN7D"
    90          }`),
    91  					),
    92  				)
    93  			})
    94  
    95  			It("should return success", func() {
    96  				response, err := newTestDynamicRuleRepo(server.URL()).Create("def", CreateRuleRequest{
    97  					Name:       "abc",
    98  					Expiration: 24,
    99  					RealmName:  "test-idp.com",
   100  					Conditions: []Condition{
   101  						{
   102  							Claim:    "blueGroups",
   103  							Operator: "CONTAINS",
   104  							Value:    "\"test-bluegroup-saml\"",
   105  						},
   106  					},
   107  				})
   108  				Expect(err).ShouldNot(HaveOccurred())
   109  
   110  				Expect(response.Name).Should(Equal("test rule name 3"))
   111  				Expect(response.Expiration).Should(Equal(24))
   112  			})
   113  
   114  		})
   115  	})
   116  
   117  	Describe("Remove()", func() {
   118  		Context("When rule is deleted", func() {
   119  			BeforeEach(func() {
   120  				server = ghttp.NewServer()
   121  				server.AppendHandlers(
   122  					ghttp.CombineHandlers(
   123  						ghttp.VerifyRequest(http.MethodDelete, "/v2/groups/abc/rules/def"),
   124  						ghttp.RespondWith(http.StatusNoContent, ""),
   125  					),
   126  				)
   127  			})
   128  
   129  			It("should return success", func() {
   130  				err := newTestDynamicRuleRepo(server.URL()).Delete("abc", "def")
   131  
   132  				Expect(err).Should(Succeed())
   133  			})
   134  		})
   135  
   136  		Context("When group is not found", func() {
   137  			BeforeEach(func() {
   138  				server = ghttp.NewServer()
   139  				server.AppendHandlers(
   140  					ghttp.CombineHandlers(
   141  						ghttp.VerifyRequest(http.MethodDelete, "/v2/groups/abc/rules/def"),
   142  						ghttp.RespondWith(http.StatusNotFound, `{
   143  							"StatusCode": 404,
   144  							"code": "not_found",
   145  							"message": "Group abc is not found"
   146  						}`),
   147  					),
   148  				)
   149  			})
   150  
   151  			It("should return not found error", func() {
   152  				err := newTestDynamicRuleRepo(server.URL()).Delete("abc", "def")
   153  
   154  				Expect(err).Should(HaveOccurred())
   155  				Expect(err.Error()).Should(ContainSubstring("not_found"))
   156  			})
   157  		})
   158  	})
   159  
   160  })
   161  
   162  func newTestDynamicRuleRepo(url string) DynamicRuleRepository {
   163  	sess, err := session.New()
   164  	if err != nil {
   165  		log.Fatal(err)
   166  	}
   167  	conf := sess.Config.Copy()
   168  	conf.Endpoint = &url
   169  	client := client.Client{
   170  		Config:      conf,
   171  		ServiceName: bluemix.IAMUUMServicev2,
   172  	}
   173  	return NewDynamicRuleRepository(&client)
   174  }