github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/icd/icdv4/user_test.go (about)

     1  package icdv4
     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  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  
    12  	"github.com/onsi/gomega/ghttp"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Users", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  	Describe("Create", func() {
    24  		Context("When creation is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPost, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/users"),
    30  						ghttp.RespondWith(http.StatusCreated, `
    31                             {
    32                              "task": {
    33                                "id": "5abb6a7d11a1a5001479a0ac",
    34                                "description": "Creating user for database",
    35                                "status": "running",
    36                                "deployment_id": "59b14b19874a1c0018009482",
    37                                "progress_percent": 5,
    38                                "created_at": "2018-03-28T10:21:30Z"
    39                              }
    40                            }
    41                          `),
    42  					),
    43  				)
    44  			})
    45  
    46  			It("should return user created", func() {
    47  				user := User{
    48  					UserName: "admin1",
    49  					Password: "password",
    50  				}
    51  				params := UserReq{
    52  					User: user,
    53  				}
    54  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    55  				myTask, err := newUser(server.URL()).CreateUser(target, params)
    56  				Expect(err).NotTo(HaveOccurred())
    57  				Expect(myTask).ShouldNot(BeNil())
    58  				Expect(myTask.Id).Should(Equal("5abb6a7d11a1a5001479a0ac"))
    59  				Expect(myTask.Description).Should(Equal("Creating user for database"))
    60  				Expect(myTask.Status).Should(Equal("running"))
    61  				Expect(myTask.DeploymentId).Should(Equal("59b14b19874a1c0018009482"))
    62  				Expect(myTask.ProgressPercent).Should(Equal(5))
    63  				Expect(myTask.CreatedAt).Should(Equal("2018-03-28T10:21:30Z"))
    64  			})
    65  		})
    66  		Context("When creation is unsuccessful", func() {
    67  			BeforeEach(func() {
    68  				server = ghttp.NewServer()
    69  				server.SetAllowUnhandledRequests(true)
    70  				server.AppendHandlers(
    71  					ghttp.CombineHandlers(
    72  						ghttp.VerifyRequest(http.MethodPost, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/users"),
    73  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create User`),
    74  					),
    75  				)
    76  			})
    77  
    78  			It("should return error during User creation", func() {
    79  				user := User{
    80  					UserName: "admin1",
    81  					Password: "password",
    82  				}
    83  				params := UserReq{
    84  					User: user,
    85  				}
    86  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    87  				myTask, err := newUser(server.URL()).CreateUser(target, params)
    88  				Expect(err).To(HaveOccurred())
    89  				Expect(myTask.Id).Should(Equal(""))
    90  			})
    91  		})
    92  	})
    93  	Describe("Delete", func() {
    94  		Context("When deletion is successful", func() {
    95  			BeforeEach(func() {
    96  				server = ghttp.NewServer()
    97  				server.AppendHandlers(
    98  					ghttp.CombineHandlers(
    99  						ghttp.VerifyRequest(http.MethodDelete, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/users/admin1"),
   100  						ghttp.RespondWith(http.StatusOK, `
   101                             {
   102                                "task": {
   103                                  "id": "5abb6a7d11a1a5001479a0ae",
   104                                  "description": "Deleting user from database",
   105                                  "status": "running",
   106                                  "deployment_id": "59b14b19874a1c0018009482",
   107                                  "progress_percent": 10,
   108                                  "created_at": "2018-03-28T10:23:30Z"
   109                                }
   110                              }
   111                          `),
   112  					),
   113  				)
   114  			})
   115  
   116  			It("should return user deleted", func() {
   117  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   118  				target2 := "admin1"
   119  				myTask, err := newUser(server.URL()).DeleteUser(target1, target2)
   120  				Expect(err).NotTo(HaveOccurred())
   121  				Expect(myTask).ShouldNot(BeNil())
   122  				Expect(myTask.Id).Should(Equal("5abb6a7d11a1a5001479a0ae"))
   123  				Expect(myTask.Description).Should(Equal("Deleting user from database"))
   124  				Expect(myTask.Status).Should(Equal("running"))
   125  				Expect(myTask.DeploymentId).Should(Equal("59b14b19874a1c0018009482"))
   126  				Expect(myTask.ProgressPercent).Should(Equal(10))
   127  				Expect(myTask.CreatedAt).Should(Equal("2018-03-28T10:23:30Z"))
   128  			})
   129  		})
   130  		Context("When deletion is unsuccessful", func() {
   131  			BeforeEach(func() {
   132  				server = ghttp.NewServer()
   133  				server.SetAllowUnhandledRequests(true)
   134  				server.AppendHandlers(
   135  					ghttp.CombineHandlers(
   136  						ghttp.VerifyRequest(http.MethodDelete, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/users/admin1"),
   137  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete User`),
   138  					),
   139  				)
   140  			})
   141  
   142  			It("should return error during User deletion", func() {
   143  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   144  				target2 := "admin1"
   145  				myTask, err := newUser(server.URL()).DeleteUser(target1, target2)
   146  				Expect(err).To(HaveOccurred())
   147  				Expect(myTask.Id).Should(Equal(""))
   148  			})
   149  		})
   150  	})
   151  	Describe("Update", func() {
   152  		Context("When update is successful", func() {
   153  			BeforeEach(func() {
   154  				server = ghttp.NewServer()
   155  				server.AppendHandlers(
   156  					ghttp.CombineHandlers(
   157  						ghttp.VerifyRequest(http.MethodPatch, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/users/admin1"),
   158  						ghttp.RespondWith(http.StatusOK, `
   159                             {
   160                                  "task": {
   161                                  "id": "5abb6a7d11a1a5001479a0ad",
   162                                  "description": "Setting user password for database",
   163                                  "status": "running",
   164                                  "deployment_id": "59b14b19874a1c0018009482",
   165                                  "progress_percent": 5,
   166                                  "created_at": "2018-03-28T10:22:30Z"
   167                                  }
   168                              }
   169                          `),
   170  					),
   171  				)
   172  			})
   173  
   174  			It("should return user updated", func() {
   175  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   176  				target2 := "admin1"
   177  				user := User{
   178  					Password: "password",
   179  				}
   180  				params := UserReq{
   181  					User: user,
   182  				}
   183  				myTask, err := newUser(server.URL()).UpdateUser(target1, target2, params)
   184  				Expect(err).NotTo(HaveOccurred())
   185  				Expect(myTask).ShouldNot(BeNil())
   186  				Expect(myTask.Id).Should(Equal("5abb6a7d11a1a5001479a0ad"))
   187  				Expect(myTask.Description).Should(Equal("Setting user password for database"))
   188  				Expect(myTask.Status).Should(Equal("running"))
   189  				Expect(myTask.DeploymentId).Should(Equal("59b14b19874a1c0018009482"))
   190  				Expect(myTask.ProgressPercent).Should(Equal(5))
   191  				Expect(myTask.CreatedAt).Should(Equal("2018-03-28T10:22:30Z"))
   192  			})
   193  		})
   194  		Context("When update is unsuccessful", func() {
   195  			BeforeEach(func() {
   196  				server = ghttp.NewServer()
   197  				server.SetAllowUnhandledRequests(true)
   198  				server.AppendHandlers(
   199  					ghttp.CombineHandlers(
   200  						ghttp.VerifyRequest(http.MethodPatch, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/users/admin1"),
   201  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to update User`),
   202  					),
   203  				)
   204  			})
   205  
   206  			It("should return error during User update", func() {
   207  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   208  				target2 := "admin1"
   209  				user := User{
   210  					Password: "password",
   211  				}
   212  				params := UserReq{
   213  					User: user,
   214  				}
   215  				myTask, err := newUser(server.URL()).UpdateUser(target1, target2, params)
   216  				Expect(err).To(HaveOccurred())
   217  				Expect(myTask.Id).Should(Equal(""))
   218  			})
   219  		})
   220  	})
   221  })
   222  
   223  func newUser(url string) Users {
   224  
   225  	sess, err := session.New()
   226  	if err != nil {
   227  		log.Fatal(err)
   228  	}
   229  	conf := sess.Config.Copy()
   230  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   231  	conf.Endpoint = &url
   232  
   233  	client := client.Client{
   234  		Config:      conf,
   235  		ServiceName: bluemix.ICDService,
   236  	}
   237  	return newUsersAPI(&client)
   238  }