github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/requirements/user_test.go (about) 1 package requirements_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/models" 7 "code.cloudfoundry.org/cli/cf/requirements" 8 9 "code.cloudfoundry.org/cli/cf/api/apifakes" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("UserRequirement", func() { 16 var ( 17 userRepo *apifakes.FakeUserRepository 18 userRequirement requirements.UserRequirement 19 ) 20 21 BeforeEach(func() { 22 userRepo = new(apifakes.FakeUserRepository) 23 }) 24 25 Describe("Execute", func() { 26 Context("when wantGUID is true", func() { 27 BeforeEach(func() { 28 userRequirement = requirements.NewUserRequirement("the-username", userRepo, true) 29 }) 30 31 It("tries to find the user in CC", func() { 32 userRequirement.Execute() 33 Expect(userRepo.FindByUsernameCallCount()).To(Equal(1)) 34 Expect(userRepo.FindByUsernameArgsForCall(0)).To(Equal("the-username")) 35 }) 36 37 Context("when the call to find the user succeeds", func() { 38 var user models.UserFields 39 BeforeEach(func() { 40 user = models.UserFields{Username: "the-username", GUID: "the-guid"} 41 userRepo.FindByUsernameReturns(user, nil) 42 }) 43 44 It("stores the user that was found", func() { 45 userRequirement.Execute() 46 Expect(userRequirement.GetUser()).To(Equal(user)) 47 }) 48 49 It("does not error", func() { 50 err := userRequirement.Execute() 51 Expect(err).NotTo(HaveOccurred()) 52 }) 53 }) 54 55 Context("when the call to find the user fails", func() { 56 userError := errors.New("some error") 57 BeforeEach(func() { 58 userRepo.FindByUsernameReturns(models.UserFields{}, userError) 59 }) 60 61 It("errors", func() { 62 err := userRequirement.Execute() 63 64 Expect(err).To(HaveOccurred()) 65 Expect(err).To(Equal(userError)) 66 }) 67 }) 68 }) 69 70 Context("when wantGUID is false", func() { 71 BeforeEach(func() { 72 userRequirement = requirements.NewUserRequirement("the-username", userRepo, false) 73 }) 74 75 It("does not try to find the user in CC", func() { 76 userRequirement.Execute() 77 Expect(userRepo.FindByUsernameCallCount()).To(Equal(0)) 78 }) 79 80 It("stores a user with just Username set", func() { 81 userRequirement.Execute() 82 expectedUser := models.UserFields{Username: "the-username"} 83 Expect(userRequirement.GetUser()).To(Equal(expectedUser)) 84 }) 85 }) 86 }) 87 88 Describe("NewClientRequirement", func() { 89 var ( 90 clientRepo *apifakes.FakeClientRepository 91 ) 92 93 BeforeEach(func() { 94 clientRepo = new(apifakes.FakeClientRepository) 95 }) 96 Context("Execute", func() { 97 It("returns a user model with the client ID as its GUID and Username", func() { 98 userReq := requirements.NewClientRequirement("the-client-id", clientRepo) 99 100 userReq.Execute() 101 user := userReq.GetUser() 102 103 Expect(user.GUID).To(Equal("the-client-id")) 104 Expect(user.Username).To(Equal("the-client-id")) 105 }) 106 107 When("The client is not found by the clientRepo", func() { 108 var ( 109 clientReq requirements.UserRequirement 110 err error 111 ) 112 113 BeforeEach(func() { 114 err = errors.New("Client not found") 115 clientRepo.ClientExistsReturns(false, err) 116 }) 117 118 It("returns an error", func() { 119 clientReq = requirements.NewClientRequirement("the-client-id", clientRepo) 120 output := clientReq.Execute() 121 122 Expect(clientRepo.ClientExistsCallCount()).To(Equal(1)) 123 Expect(clientRepo.ClientExistsArgsForCall(0)).To(Equal("the-client-id")) 124 Expect(output).To(Equal(err)) 125 }) 126 }) 127 }) 128 }) 129 })