github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/user/delete_user_test.go (about) 1 package user_test 2 3 import ( 4 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 5 . "github.com/cloudfoundry/cli/cf/commands/user" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/models" 8 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 9 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 10 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 11 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 15 . "github.com/cloudfoundry/cli/testhelpers/matchers" 16 ) 17 18 var _ = Describe("delete-user command", func() { 19 var ( 20 ui *testterm.FakeUI 21 configRepo core_config.ReadWriter 22 userRepo *testapi.FakeUserRepository 23 requirementsFactory *testreq.FakeReqFactory 24 ) 25 26 BeforeEach(func() { 27 ui = &testterm.FakeUI{Inputs: []string{"y"}} 28 userRepo = &testapi.FakeUserRepository{} 29 requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true} 30 configRepo = testconfig.NewRepositoryWithDefaults() 31 32 token, err := testconfig.EncodeAccessToken(core_config.TokenInfo{ 33 UserGuid: "admin-user-guid", 34 Username: "admin-user", 35 }) 36 Expect(err).ToNot(HaveOccurred()) 37 configRepo.SetAccessToken(token) 38 }) 39 40 runCommand := func(args ...string) bool { 41 cmd := NewDeleteUser(ui, configRepo, userRepo) 42 return testcmd.RunCommand(cmd, args, requirementsFactory) 43 } 44 45 Describe("requirements", func() { 46 It("fails when not logged in", func() { 47 requirementsFactory.LoginSuccess = false 48 49 Expect(runCommand("my-user")).To(BeFalse()) 50 }) 51 52 It("fails with usage when no arguments are given", func() { 53 runCommand() 54 Expect(ui.FailedWithUsage).To(BeTrue()) 55 }) 56 }) 57 58 Context("when the given user exists", func() { 59 BeforeEach(func() { 60 userRepo.FindByUsernameUserFields = models.UserFields{ 61 Username: "user-name", 62 Guid: "user-guid", 63 } 64 }) 65 66 It("deletes a user with the given name", func() { 67 runCommand("user-name") 68 69 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the user user-name"})) 70 71 Expect(ui.Outputs).To(ContainSubstrings( 72 []string{"Deleting user", "user-name", "admin-user"}, 73 []string{"OK"}, 74 )) 75 76 Expect(userRepo.FindByUsernameUsername).To(Equal("user-name")) 77 Expect(userRepo.DeleteUserGuid).To(Equal("user-guid")) 78 }) 79 80 It("does not delete the user when no confirmation is given", func() { 81 ui.Inputs = []string{"nope"} 82 runCommand("user") 83 84 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete"})) 85 Expect(userRepo.FindByUsernameUsername).To(Equal("")) 86 Expect(userRepo.DeleteUserGuid).To(Equal("")) 87 }) 88 89 It("deletes without confirmation when the -f flag is given", func() { 90 ui.Inputs = []string{} 91 runCommand("-f", "user-name") 92 93 Expect(ui.Outputs).To(ContainSubstrings( 94 []string{"Deleting user", "user-name"}, 95 []string{"OK"}, 96 )) 97 98 Expect(userRepo.FindByUsernameUsername).To(Equal("user-name")) 99 Expect(userRepo.DeleteUserGuid).To(Equal("user-guid")) 100 }) 101 }) 102 103 Context("when the given user does not exist", func() { 104 BeforeEach(func() { 105 userRepo.FindByUsernameNotFound = true 106 }) 107 108 It("prints a warning", func() { 109 runCommand("-f", "user-name") 110 111 Expect(ui.Outputs).To(ContainSubstrings( 112 []string{"Deleting user", "user-name"}, 113 []string{"OK"}, 114 )) 115 116 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"user-name", "does not exist"})) 117 118 Expect(userRepo.FindByUsernameUsername).To(Equal("user-name")) 119 Expect(userRepo.DeleteUserGuid).To(Equal("")) 120 }) 121 }) 122 })