github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/user_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/api/uaa"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("User Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeUAAClient             *v7actionfakes.FakeUAAClient
    21  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		fakeUAAClient = new(v7actionfakes.FakeUAAClient)
    26  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    27  		fakeConfig := new(v7actionfakes.FakeConfig)
    28  		actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, fakeUAAClient, nil, nil)
    29  	})
    30  
    31  	Describe("CreateUser", func() {
    32  		var (
    33  			actualUser     resources.User
    34  			actualWarnings Warnings
    35  			actualErr      error
    36  		)
    37  
    38  		JustBeforeEach(func() {
    39  			actualUser, actualWarnings, actualErr = actor.CreateUser("some-new-user", "some-password", "some-origin")
    40  		})
    41  
    42  		When("no API errors occur", func() {
    43  			var createdUser resources.User
    44  
    45  			BeforeEach(func() {
    46  				createdUser = resources.User{
    47  					GUID: "new-user-cc-guid",
    48  				}
    49  				fakeUAAClient.CreateUserReturns(
    50  					uaa.User{
    51  						ID: "new-user-uaa-id",
    52  					},
    53  					nil,
    54  				)
    55  				fakeCloudControllerClient.CreateUserReturns(
    56  					createdUser,
    57  					ccv3.Warnings{
    58  						"warning-1",
    59  						"warning-2",
    60  					},
    61  					nil,
    62  				)
    63  			})
    64  
    65  			It("creates a new user and returns all warnings", func() {
    66  				Expect(actualErr).NotTo(HaveOccurred())
    67  
    68  				Expect(actualUser).To(Equal(resources.User(createdUser)))
    69  				Expect(actualWarnings).To(ConsistOf("warning-1", "warning-2"))
    70  
    71  				Expect(fakeUAAClient.CreateUserCallCount()).To(Equal(1))
    72  				username, password, origin := fakeUAAClient.CreateUserArgsForCall(0)
    73  				Expect(username).To(Equal("some-new-user"))
    74  				Expect(password).To(Equal("some-password"))
    75  				Expect(origin).To(Equal("some-origin"))
    76  
    77  				Expect(fakeCloudControllerClient.CreateUserCallCount()).To(Equal(1))
    78  				uaaUserID := fakeCloudControllerClient.CreateUserArgsForCall(0)
    79  				Expect(uaaUserID).To(Equal("new-user-uaa-id"))
    80  			})
    81  		})
    82  
    83  		When("the UAA API returns an error", func() {
    84  			var returnedErr error
    85  
    86  			BeforeEach(func() {
    87  				returnedErr = errors.New("some UAA error")
    88  				fakeUAAClient.CreateUserReturns(
    89  					uaa.User{
    90  						ID: "new-user-uaa-id",
    91  					},
    92  					returnedErr,
    93  				)
    94  			})
    95  
    96  			It("returns the same error", func() {
    97  				Expect(actualErr).To(MatchError(returnedErr))
    98  			})
    99  		})
   100  
   101  		When("the CC API returns an error", func() {
   102  			var returnedErr error
   103  
   104  			BeforeEach(func() {
   105  				returnedErr = errors.New("CC error")
   106  				fakeUAAClient.CreateUserReturns(
   107  					uaa.User{
   108  						ID: "new-user-uaa-id",
   109  					},
   110  					nil,
   111  				)
   112  				fakeCloudControllerClient.CreateUserReturns(
   113  					resources.User{},
   114  					ccv3.Warnings{
   115  						"warning-1",
   116  						"warning-2",
   117  					},
   118  					returnedErr,
   119  				)
   120  			})
   121  
   122  			It("returns the same error and all warnings", func() {
   123  				Expect(actualErr).To(MatchError(returnedErr))
   124  				Expect(actualWarnings).To(ConsistOf("warning-1", "warning-2"))
   125  			})
   126  		})
   127  	})
   128  
   129  	Describe("GetUser", func() {
   130  		var (
   131  			actualUser resources.User
   132  			actualErr  error
   133  		)
   134  
   135  		JustBeforeEach(func() {
   136  			actualUser, actualErr = actor.GetUser("some-user", "some-origin")
   137  		})
   138  
   139  		When("when the API returns a success response", func() {
   140  			When("the API returns one user", func() {
   141  				BeforeEach(func() {
   142  
   143  					fakeUAAClient.ListUsersReturns(
   144  						[]uaa.User{
   145  							{ID: "user-id"},
   146  						},
   147  						nil,
   148  					)
   149  				})
   150  
   151  				It("returns the single user", func() {
   152  					Expect(actualErr).NotTo(HaveOccurred())
   153  					Expect(actualUser).To(Equal(resources.User{GUID: "user-id"}))
   154  
   155  					Expect(fakeUAAClient.ListUsersCallCount()).To(Equal(1))
   156  					username, origin := fakeUAAClient.ListUsersArgsForCall(0)
   157  					Expect(username).To(Equal("some-user"))
   158  					Expect(origin).To(Equal("some-origin"))
   159  				})
   160  			})
   161  
   162  			When("the API returns no user", func() {
   163  				BeforeEach(func() {
   164  					fakeUAAClient.ListUsersReturns(
   165  						[]uaa.User{},
   166  						nil,
   167  					)
   168  				})
   169  
   170  				It("returns an error indicating user was not found in UAA", func() {
   171  					Expect(actualUser).To(Equal(resources.User{}))
   172  					Expect(actualErr).To(Equal(actionerror.UserNotFoundError{
   173  						Username: "some-user",
   174  						Origin:   "some-origin",
   175  					}))
   176  					Expect(fakeUAAClient.ListUsersCallCount()).To(Equal(1))
   177  				})
   178  			})
   179  
   180  			When("the API returns multiple users", func() {
   181  				BeforeEach(func() {
   182  					fakeUAAClient.ListUsersReturns(
   183  						[]uaa.User{
   184  							{ID: "user-guid-1", Origin: "uaa"},
   185  							{ID: "user-guid-2", Origin: "ldap"},
   186  						},
   187  						nil,
   188  					)
   189  				})
   190  
   191  				It("returns an error indicating user was not found in UAA", func() {
   192  					Expect(actualUser).To(Equal(resources.User{}))
   193  					Expect(actualErr).To(Equal(actionerror.MultipleUAAUsersFoundError{
   194  						Username: "some-user",
   195  						Origins:  []string{"uaa", "ldap"},
   196  					}))
   197  					Expect(fakeUAAClient.ListUsersCallCount()).To(Equal(1))
   198  				})
   199  			})
   200  		})
   201  
   202  		When("the API returns an error", func() {
   203  			var apiError error
   204  
   205  			BeforeEach(func() {
   206  				apiError = errors.New("uaa-api-get-users-error")
   207  				fakeUAAClient.ListUsersReturns(
   208  					nil,
   209  					apiError,
   210  				)
   211  			})
   212  
   213  			It("returns error coming from the API", func() {
   214  				Expect(actualUser).To(Equal(resources.User{}))
   215  				Expect(actualErr).To(MatchError("uaa-api-get-users-error"))
   216  
   217  				Expect(fakeUAAClient.ListUsersCallCount()).To(Equal(1))
   218  			})
   219  		})
   220  	})
   221  
   222  	Describe("DeleteUser", func() {
   223  		var (
   224  			actualWarnings Warnings
   225  			actualErr      error
   226  		)
   227  
   228  		JustBeforeEach(func() {
   229  			actualWarnings, actualErr = actor.DeleteUser("some-user-guid")
   230  		})
   231  
   232  		When("the UAA API returns an error", func() {
   233  			var returnedErr error
   234  
   235  			BeforeEach(func() {
   236  				returnedErr = errors.New("some UAA error")
   237  				fakeUAAClient.DeleteUserReturns(
   238  					uaa.User{},
   239  					returnedErr,
   240  				)
   241  			})
   242  
   243  			It("returns the same error", func() {
   244  				Expect(actualErr).To(MatchError(returnedErr))
   245  			})
   246  		})
   247  
   248  		When("the CC API returns an error", func() {
   249  			var returnedErr error
   250  
   251  			BeforeEach(func() {
   252  				returnedErr = errors.New("some CC error")
   253  				fakeUAAClient.DeleteUserReturns(
   254  					uaa.User{},
   255  					nil,
   256  				)
   257  				fakeCloudControllerClient.DeleteUserReturns(
   258  					"",
   259  					ccv3.Warnings{},
   260  					returnedErr,
   261  				)
   262  			})
   263  
   264  			It("returns the same error", func() {
   265  				Expect(actualErr).To(MatchError(returnedErr))
   266  			})
   267  
   268  			When("the cloud controller found no user", func() {
   269  				BeforeEach(func() {
   270  					returnedErr = ccerror.ResourceNotFoundError{}
   271  					fakeUAAClient.DeleteUserReturns(
   272  						uaa.User{},
   273  						nil,
   274  					)
   275  					fakeCloudControllerClient.DeleteUserReturns(
   276  						"",
   277  						ccv3.Warnings{},
   278  						returnedErr,
   279  					)
   280  				})
   281  
   282  				It("does Not return the error", func() {
   283  					Expect(actualErr).To(BeNil())
   284  				})
   285  			})
   286  		})
   287  
   288  		When("the CC API returns a job", func() {
   289  			BeforeEach(func() {
   290  				fakeCloudControllerClient.DeleteUserReturns(
   291  					"some-url",
   292  					ccv3.Warnings{"warning-5", "warning-6"},
   293  					nil,
   294  				)
   295  			})
   296  
   297  			When("polling errors", func() {
   298  				var expectedErr error
   299  
   300  				BeforeEach(func() {
   301  					expectedErr = errors.New("Never expected, by anyone")
   302  					fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"warning-7", "warning-8"}, expectedErr)
   303  				})
   304  
   305  				It("returns the error", func() {
   306  					Expect(actualErr).To(Equal(expectedErr))
   307  					Expect(actualWarnings).To(ConsistOf("warning-5", "warning-6", "warning-7", "warning-8"))
   308  				})
   309  			})
   310  
   311  			When("the job is successful", func() {
   312  				BeforeEach(func() {
   313  					fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"warning-7", "warning-8"}, nil)
   314  				})
   315  
   316  				It("returns warnings and no error", func() {
   317  					Expect(actualErr).ToNot(HaveOccurred())
   318  					Expect(actualWarnings).To(ConsistOf("warning-5", "warning-6", "warning-7", "warning-8"))
   319  
   320  					Expect(fakeCloudControllerClient.DeleteUserCallCount()).To(Equal(1))
   321  					Expect(fakeCloudControllerClient.DeleteUserArgsForCall(0)).To(Equal("some-user-guid"))
   322  
   323  					Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   324  					Expect(fakeCloudControllerClient.PollJobArgsForCall(0)).To(Equal(ccv3.JobURL("some-url")))
   325  				})
   326  			})
   327  		})
   328  
   329  	})
   330  
   331  	Describe("UpdateUserPassword", func() {
   332  		var (
   333  			userGUID    = "some-user"
   334  			oldPassword = "old1"
   335  			newPassword = "new1"
   336  			actualErr   error
   337  		)
   338  
   339  		JustBeforeEach(func() {
   340  			actualErr = actor.UpdateUserPassword(userGUID, oldPassword, newPassword)
   341  		})
   342  
   343  		When("no API errors occur", func() {
   344  			BeforeEach(func() {
   345  				fakeUAAClient.UpdatePasswordReturns(nil)
   346  			})
   347  
   348  			It("does not return an error", func() {
   349  				Expect(actualErr).NotTo(HaveOccurred())
   350  
   351  				Expect(fakeUAAClient.UpdatePasswordCallCount()).To(Equal(1))
   352  				givenUserGUID, givenOldPassword, givenNewPassword := fakeUAAClient.UpdatePasswordArgsForCall(0)
   353  				Expect(givenUserGUID).To(Equal(userGUID))
   354  				Expect(givenOldPassword).To(Equal(oldPassword))
   355  				Expect(givenNewPassword).To(Equal(newPassword))
   356  			})
   357  		})
   358  
   359  		When("the UAA API returns an error", func() {
   360  			var returnedErr error
   361  
   362  			BeforeEach(func() {
   363  				returnedErr = errors.New("some UAA error")
   364  				fakeUAAClient.UpdatePasswordReturns(returnedErr)
   365  			})
   366  
   367  			It("returns the same error", func() {
   368  				Expect(actualErr).To(MatchError(returnedErr))
   369  
   370  				Expect(fakeUAAClient.UpdatePasswordCallCount()).To(Equal(1))
   371  				givenUserGUID, givenOldPassword, givenNewPassword := fakeUAAClient.UpdatePasswordArgsForCall(0)
   372  				Expect(givenUserGUID).To(Equal(userGUID))
   373  				Expect(givenOldPassword).To(Equal(oldPassword))
   374  				Expect(givenNewPassword).To(Equal(newPassword))
   375  			})
   376  		})
   377  	})
   378  
   379  	Describe("SortUsers", func() {
   380  		var (
   381  			users []resources.User
   382  		)
   383  		When("The PresentationNames are different", func() {
   384  			BeforeEach(func() {
   385  				users = []resources.User{
   386  					{PresentationName: "c", Origin: "uaa"},
   387  					{PresentationName: "a", Origin: "uaa"},
   388  					{PresentationName: "b", Origin: "ldap"},
   389  				}
   390  			})
   391  			It("sorts by PresentationName", func() {
   392  				SortUsers(users)
   393  				Expect(users[0].PresentationName).To(Equal("a"))
   394  				Expect(users[1].PresentationName).To(Equal("b"))
   395  				Expect(users[2].PresentationName).To(Equal("c"))
   396  			})
   397  		})
   398  		When("The PresentationNames are identical", func() {
   399  			BeforeEach(func() {
   400  				users = []resources.User{
   401  					{PresentationName: "a", Origin: "cc"},
   402  					{PresentationName: "a", Origin: "aa"},
   403  					{PresentationName: "a", Origin: "bb"},
   404  					{PresentationName: "a", Origin: "uaa"},
   405  					{PresentationName: "a", Origin: "zz"},
   406  					{PresentationName: "a", Origin: ""},
   407  				}
   408  			})
   409  			It("sorts by PresentationName, uaa first, clients (origin == '') last and alphabetically otherwise", func() {
   410  				SortUsers(users)
   411  				Expect(users[0].Origin).To(Equal("uaa"))
   412  				Expect(users[1].Origin).To(Equal("aa"))
   413  				Expect(users[2].Origin).To(Equal("bb"))
   414  				Expect(users[3].Origin).To(Equal("cc"))
   415  				Expect(users[4].Origin).To(Equal("zz"))
   416  				Expect(users[5].Origin).To(Equal(""))
   417  			})
   418  		})
   419  	})
   420  
   421  	Describe("GetHumanReadableOrigin", func() {
   422  		var user resources.User
   423  		When("The user has an origin", func() {
   424  			BeforeEach(func() {
   425  				user = resources.User{Origin: "some-origin"}
   426  			})
   427  			It("displays the origin", func() {
   428  				Expect(GetHumanReadableOrigin(user)).To(Equal("some-origin"))
   429  			})
   430  		})
   431  		When("The user has an empty origin", func() {
   432  			BeforeEach(func() {
   433  				user = resources.User{Origin: ""}
   434  			})
   435  			It("displays 'client'", func() {
   436  				Expect(GetHumanReadableOrigin(user)).To(Equal("client"))
   437  			})
   438  		})
   439  	})
   440  })