github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/user/delete_user_test.go (about) 1 package user_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/apifakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 12 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 13 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 17 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 18 ) 19 20 var _ = Describe("delete-user command", func() { 21 var ( 22 ui *testterm.FakeUI 23 configRepo coreconfig.Repository 24 userRepo *apifakes.FakeUserRepository 25 requirementsFactory *requirementsfakes.FakeFactory 26 deps commandregistry.Dependency 27 ) 28 29 updateCommandDependency := func(pluginCall bool) { 30 deps.UI = ui 31 deps.Config = configRepo 32 deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo) 33 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-user").SetDependency(deps, pluginCall)) 34 } 35 36 BeforeEach(func() { 37 ui = &testterm.FakeUI{Inputs: []string{"y"}} 38 userRepo = new(apifakes.FakeUserRepository) 39 requirementsFactory = new(requirementsfakes.FakeFactory) 40 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 41 configRepo = testconfig.NewRepositoryWithDefaults() 42 43 token, err := testconfig.EncodeAccessToken(coreconfig.TokenInfo{ 44 UserGUID: "admin-user-guid", 45 Username: "admin-user", 46 }) 47 Expect(err).ToNot(HaveOccurred()) 48 configRepo.SetAccessToken(token) 49 }) 50 51 runCommand := func(args ...string) bool { 52 return testcmd.RunCLICommand("delete-user", args, requirementsFactory, updateCommandDependency, false, ui) 53 } 54 55 Describe("requirements", func() { 56 It("fails when not logged in", func() { 57 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 58 59 Expect(runCommand("my-user")).To(BeFalse()) 60 }) 61 62 It("fails with usage when no arguments are given", func() { 63 runCommand() 64 Expect(ui.Outputs()).To(ContainSubstrings( 65 []string{"Incorrect Usage", "Requires an argument"}, 66 )) 67 }) 68 }) 69 70 Context("when the given user exists", func() { 71 BeforeEach(func() { 72 userRepo.FindAllByUsernameReturns([]models.UserFields{{ 73 Username: "user-name", 74 GUID: "user-guid", 75 }}, nil) 76 }) 77 78 It("deletes a user with the given name", func() { 79 runCommand("user-name") 80 81 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the user user-name"})) 82 83 Expect(ui.Outputs()).To(ContainSubstrings( 84 []string{"Deleting user", "user-name", "admin-user"}, 85 []string{"OK"}, 86 )) 87 88 Expect(userRepo.FindAllByUsernameArgsForCall(0)).To(Equal("user-name")) 89 Expect(userRepo.DeleteArgsForCall(0)).To(Equal("user-guid")) 90 }) 91 92 It("does not delete the user when no confirmation is given", func() { 93 ui.Inputs = []string{"nope"} 94 runCommand("user") 95 96 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete"})) 97 Expect(userRepo.FindAllByUsernameCallCount()).To(BeZero()) 98 Expect(userRepo.DeleteCallCount()).To(BeZero()) 99 }) 100 101 It("deletes without confirmation when the -f flag is given", func() { 102 ui.Inputs = []string{} 103 runCommand("-f", "user-name") 104 105 Expect(ui.Outputs()).To(ContainSubstrings( 106 []string{"Deleting user", "user-name"}, 107 []string{"OK"}, 108 )) 109 110 Expect(userRepo.FindAllByUsernameArgsForCall(0)).To(Equal("user-name")) 111 Expect(userRepo.DeleteArgsForCall(0)).To(Equal("user-guid")) 112 }) 113 }) 114 115 Context("when multiple users exist", func() { 116 BeforeEach(func() { 117 userRepo.FindAllByUsernameReturns([]models.UserFields{ 118 { 119 Username: "user-name1", 120 GUID: "user-guid1"}, 121 { 122 Username: "user-name2", 123 GUID: "user-guid2"}, 124 }, nil) 125 }) 126 127 It("returns a muliple users found error", func() { 128 runCommand("user-name") 129 130 Expect(ui.Outputs()).To(ContainSubstrings( 131 []string{"Error deleting user"}, 132 []string{"The user exists in multiple origins."}, 133 )) 134 }) 135 }) 136 Context("when the given user does not exist", func() { 137 BeforeEach(func() { 138 userRepo.FindAllByUsernameReturns(nil, errors.NewModelNotFoundError("User", "")) 139 }) 140 141 It("prints a warning", func() { 142 runCommand("-f", "user-name") 143 144 Expect(ui.Outputs()).To(ContainSubstrings( 145 []string{"Deleting user", "user-name"}, 146 []string{"OK"}, 147 )) 148 149 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"user-name", "does not exist"})) 150 151 Expect(userRepo.FindAllByUsernameArgsForCall(0)).To(Equal("user-name")) 152 Expect(userRepo.DeleteCallCount()).To(BeZero()) 153 }) 154 }) 155 })