github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/iampap/iampapv2/roles_test.go (about)

     1  package iampapv2
     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("RoleRepository", func() {
    16  	var (
    17  		server *ghttp.Server
    18  	)
    19  
    20  	Describe("Create()", func() {
    21  		Context("When create one role", func() {
    22  			BeforeEach(func() {
    23  				server = ghttp.NewServer()
    24  				server.AppendHandlers(
    25  					ghttp.CombineHandlers(
    26  						ghttp.VerifyRequest(http.MethodPost, "/v2/roles"),
    27  						ghttp.RespondWith(http.StatusOK, `
    28  						{
    29  							"id": "12345678abcd1a2ba1b21234567890ab",
    30  							"crn": "crn:v1:bluemix:public:iam-access-management::::customRole:Example",
    31  							"name": "abc",
    32  							"display_name": "abc",
    33  							"description": "test role",
    34  							"service_name": "kms",
    35  							"account_id": "acc",
    36  							"actions": [
    37  							  "kms.secrets.readmetadata"
    38  							],
    39  							"created_at": "2018-08-30T14:09:09.907Z",
    40  							"created_by_id": "USER_ID",
    41  							"last_modified_at": "2018-08-30T14:09:09.907Z",
    42  							"last_modified_by_id": "USER_ID",
    43  							"href": "https://iam.cloud.ibm.com/v2/roles/12345678abcd1a2ba1b21234567890ab"
    44  						  }`),
    45  					),
    46  				)
    47  			})
    48  
    49  			It("should return success", func() {
    50  				response, err := newTestRoleRepo(server.URL()).Create(CreateRoleRequest{
    51  					Name:        "abc",
    52  					ServiceName: "kms",
    53  					AccountID:   "acc",
    54  					Description: "test role",
    55  					DisplayName: "abc",
    56  					Actions:     []string{"kms.secrets.readmetadata"}})
    57  				Expect(err).ShouldNot(HaveOccurred())
    58  
    59  				Expect(response.Name).Should(Equal("abc"))
    60  				Expect(response.ServiceName).Should(Equal("kms"))
    61  			})
    62  
    63  		})
    64  	})
    65  
    66  	Describe("Remove()", func() {
    67  		Context("When role is deleted", func() {
    68  			BeforeEach(func() {
    69  				server = ghttp.NewServer()
    70  				server.AppendHandlers(
    71  					ghttp.CombineHandlers(
    72  						ghttp.VerifyRequest(http.MethodDelete, "/v2/roles/abc"),
    73  						ghttp.RespondWith(http.StatusNoContent, ""),
    74  					),
    75  				)
    76  			})
    77  
    78  			It("should return success", func() {
    79  				err := newTestRoleRepo(server.URL()).Delete("abc")
    80  
    81  				Expect(err).Should(Succeed())
    82  			})
    83  		})
    84  
    85  		Context("When role is not found", func() {
    86  			BeforeEach(func() {
    87  				server = ghttp.NewServer()
    88  				server.AppendHandlers(
    89  					ghttp.CombineHandlers(
    90  						ghttp.VerifyRequest(http.MethodDelete, "/v2/roles/abc"),
    91  						ghttp.RespondWith(http.StatusNotFound, `{
    92  							"StatusCode": 404,
    93  							"code": "not_found",
    94  							"message": "role abc is not found"
    95  						}`),
    96  					),
    97  				)
    98  			})
    99  
   100  			It("should return not found error", func() {
   101  				err := newTestRoleRepo(server.URL()).Delete("abc")
   102  
   103  				Expect(err).Should(HaveOccurred())
   104  				Expect(err.Error()).Should(ContainSubstring("not_found"))
   105  			})
   106  		})
   107  	})
   108  
   109  })
   110  
   111  func newTestRoleRepo(url string) RoleRepository {
   112  	sess, err := session.New()
   113  	if err != nil {
   114  		log.Fatal(err)
   115  	}
   116  	conf := sess.Config.Copy()
   117  	conf.Endpoint = &url
   118  	client := client.Client{
   119  		Config:      conf,
   120  		ServiceName: bluemix.IAMPAPServicev2,
   121  	}
   122  	return NewRoleRepository(&client)
   123  }